vserver 1.9.3
[linux-2.6.git] / drivers / block / DAC960.c
1 /*
2
3   Linux Driver for Mylex DAC960/AcceleRAID/eXtremeRAID PCI RAID Controllers
4
5   Copyright 1998-2001 by Leonard N. Zubkoff <lnz@dandelion.com>
6
7   This program is free software; you may redistribute and/or modify it under
8   the terms of the GNU General Public License Version 2 as published by the
9   Free Software Foundation.
10
11   This program is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   for complete details.
15
16 */
17
18
19 #define DAC960_DriverVersion                    "2.5.47"
20 #define DAC960_DriverDate                       "14 November 2002"
21
22
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/miscdevice.h>
26 #include <linux/blkdev.h>
27 #include <linux/bio.h>
28 #include <linux/completion.h>
29 #include <linux/delay.h>
30 #include <linux/genhd.h>
31 #include <linux/hdreg.h>
32 #include <linux/blkpg.h>
33 #include <linux/interrupt.h>
34 #include <linux/ioport.h>
35 #include <linux/mm.h>
36 #include <linux/slab.h>
37 #include <linux/proc_fs.h>
38 #include <linux/reboot.h>
39 #include <linux/spinlock.h>
40 #include <linux/timer.h>
41 #include <linux/pci.h>
42 #include <linux/init.h>
43 #include <asm/io.h>
44 #include <asm/uaccess.h>
45 #include "DAC960.h"
46
47 #define DAC960_GAM_MINOR        252
48
49
50 static DAC960_Controller_T *DAC960_Controllers[DAC960_MaxControllers];
51 static int DAC960_ControllerCount;
52 static struct proc_dir_entry *DAC960_ProcDirectoryEntry;
53
54 static long disk_size(DAC960_Controller_T *p, int drive_nr)
55 {
56         if (p->FirmwareType == DAC960_V1_Controller) {
57                 if (drive_nr >= p->LogicalDriveCount)
58                         return 0;
59                 return p->V1.LogicalDriveInformation[drive_nr].
60                         LogicalDriveSize;
61         } else {
62                 DAC960_V2_LogicalDeviceInfo_T *i =
63                         p->V2.LogicalDeviceInformation[drive_nr];
64                 if (i == NULL)
65                         return 0;
66                 return i->ConfigurableDeviceSize;
67         }
68 }
69
70 static int DAC960_open(struct inode *inode, struct file *file)
71 {
72         struct gendisk *disk = inode->i_bdev->bd_disk;
73         DAC960_Controller_T *p = disk->queue->queuedata;
74         int drive_nr = (long)disk->private_data;
75
76         if (p->FirmwareType == DAC960_V1_Controller) {
77                 if (p->V1.LogicalDriveInformation[drive_nr].
78                     LogicalDriveState == DAC960_V1_LogicalDrive_Offline)
79                         return -ENXIO;
80         } else {
81                 DAC960_V2_LogicalDeviceInfo_T *i =
82                         p->V2.LogicalDeviceInformation[drive_nr];
83                 if (!i || i->LogicalDeviceState == DAC960_V2_LogicalDevice_Offline)
84                         return -ENXIO;
85         }
86
87         check_disk_change(inode->i_bdev);
88
89         if (!get_capacity(p->disks[drive_nr]))
90                 return -ENXIO;
91         return 0;
92 }
93
94 static int DAC960_ioctl(struct inode *inode, struct file *file,
95                         unsigned int cmd, unsigned long arg)
96 {
97         struct gendisk *disk = inode->i_bdev->bd_disk;
98         DAC960_Controller_T *p = disk->queue->queuedata;
99         int drive_nr = (long)disk->private_data;
100         struct hd_geometry g;
101         struct hd_geometry __user *loc = (struct hd_geometry __user *)arg;
102
103         if (cmd != HDIO_GETGEO || !loc)
104                 return -EINVAL;
105
106         if (p->FirmwareType == DAC960_V1_Controller) {
107                 g.heads = p->V1.GeometryTranslationHeads;
108                 g.sectors = p->V1.GeometryTranslationSectors;
109                 g.cylinders = p->V1.LogicalDriveInformation[drive_nr].
110                         LogicalDriveSize / (g.heads * g.sectors);
111         } else {
112                 DAC960_V2_LogicalDeviceInfo_T *i =
113                         p->V2.LogicalDeviceInformation[drive_nr];
114                 switch (i->DriveGeometry) {
115                 case DAC960_V2_Geometry_128_32:
116                         g.heads = 128;
117                         g.sectors = 32;
118                         break;
119                 case DAC960_V2_Geometry_255_63:
120                         g.heads = 255;
121                         g.sectors = 63;
122                         break;
123                 default:
124                         DAC960_Error("Illegal Logical Device Geometry %d\n",
125                                         p, i->DriveGeometry);
126                         return -EINVAL;
127                 }
128
129                 g.cylinders = i->ConfigurableDeviceSize / (g.heads * g.sectors);
130         }
131         
132         g.start = get_start_sect(inode->i_bdev);
133
134         return copy_to_user(loc, &g, sizeof g) ? -EFAULT : 0; 
135 }
136
137 static int DAC960_media_changed(struct gendisk *disk)
138 {
139         DAC960_Controller_T *p = disk->queue->queuedata;
140         int drive_nr = (long)disk->private_data;
141
142         if (!p->LogicalDriveInitiallyAccessible[drive_nr])
143                 return 1;
144         return 0;
145 }
146
147 static int DAC960_revalidate_disk(struct gendisk *disk)
148 {
149         DAC960_Controller_T *p = disk->queue->queuedata;
150         int unit = (long)disk->private_data;
151
152         set_capacity(disk, disk_size(p, unit));
153         return 0;
154 }
155
156 static struct block_device_operations DAC960_BlockDeviceOperations = {
157         .owner                  = THIS_MODULE,
158         .open                   = DAC960_open,
159         .ioctl                  = DAC960_ioctl,
160         .media_changed          = DAC960_media_changed,
161         .revalidate_disk        = DAC960_revalidate_disk,
162 };
163
164
165 /*
166   DAC960_AnnounceDriver announces the Driver Version and Date, Author's Name,
167   Copyright Notice, and Electronic Mail Address.
168 */
169
170 static void DAC960_AnnounceDriver(DAC960_Controller_T *Controller)
171 {
172   DAC960_Announce("***** DAC960 RAID Driver Version "
173                   DAC960_DriverVersion " of "
174                   DAC960_DriverDate " *****\n", Controller);
175   DAC960_Announce("Copyright 1998-2001 by Leonard N. Zubkoff "
176                   "<lnz@dandelion.com>\n", Controller);
177 }
178
179
180 /*
181   DAC960_Failure prints a standardized error message, and then returns false.
182 */
183
184 static boolean DAC960_Failure(DAC960_Controller_T *Controller,
185                               unsigned char *ErrorMessage)
186 {
187   DAC960_Error("While configuring DAC960 PCI RAID Controller at\n",
188                Controller);
189   if (Controller->IO_Address == 0)
190     DAC960_Error("PCI Bus %d Device %d Function %d I/O Address N/A "
191                  "PCI Address 0x%X\n", Controller,
192                  Controller->Bus, Controller->Device,
193                  Controller->Function, Controller->PCI_Address);
194   else DAC960_Error("PCI Bus %d Device %d Function %d I/O Address "
195                     "0x%X PCI Address 0x%X\n", Controller,
196                     Controller->Bus, Controller->Device,
197                     Controller->Function, Controller->IO_Address,
198                     Controller->PCI_Address);
199   DAC960_Error("%s FAILED - DETACHING\n", Controller, ErrorMessage);
200   return false;
201 }
202
203 /*
204   init_dma_loaf() and slice_dma_loaf() are helper functions for
205   aggregating the dma-mapped memory for a well-known collection of
206   data structures that are of different lengths.
207
208   These routines don't guarantee any alignment.  The caller must
209   include any space needed for alignment in the sizes of the structures
210   that are passed in.
211  */
212
213 static boolean init_dma_loaf(struct pci_dev *dev, struct dma_loaf *loaf,
214                                                                  size_t len)
215 {
216         void *cpu_addr;
217         dma_addr_t dma_handle;
218
219         cpu_addr = pci_alloc_consistent(dev, len, &dma_handle);
220         if (cpu_addr == NULL)
221                 return false;
222         
223         loaf->cpu_free = loaf->cpu_base = cpu_addr;
224         loaf->dma_free =loaf->dma_base = dma_handle;
225         loaf->length = len;
226         memset(cpu_addr, 0, len);
227         return true;
228 }
229
230 static void *slice_dma_loaf(struct dma_loaf *loaf, size_t len,
231                                         dma_addr_t *dma_handle)
232 {
233         void *cpu_end = loaf->cpu_free + len;
234         void *cpu_addr = loaf->cpu_free;
235
236         if (cpu_end > loaf->cpu_base + loaf->length)
237                 BUG();
238         *dma_handle = loaf->dma_free;
239         loaf->cpu_free = cpu_end;
240         loaf->dma_free += len;
241         return cpu_addr;
242 }
243
244 static void free_dma_loaf(struct pci_dev *dev, struct dma_loaf *loaf_handle)
245 {
246         if (loaf_handle->cpu_base != NULL)
247                 pci_free_consistent(dev, loaf_handle->length,
248                         loaf_handle->cpu_base, loaf_handle->dma_base);
249 }
250
251
252 /*
253   DAC960_CreateAuxiliaryStructures allocates and initializes the auxiliary
254   data structures for Controller.  It returns true on success and false on
255   failure.
256 */
257
258 static boolean DAC960_CreateAuxiliaryStructures(DAC960_Controller_T *Controller)
259 {
260   int CommandAllocationLength, CommandAllocationGroupSize;
261   int CommandsRemaining = 0, CommandIdentifier, CommandGroupByteCount;
262   void *AllocationPointer = NULL;
263   void *ScatterGatherCPU = NULL;
264   dma_addr_t ScatterGatherDMA;
265   struct pci_pool *ScatterGatherPool;
266   void *RequestSenseCPU = NULL;
267   dma_addr_t RequestSenseDMA;
268   struct pci_pool *RequestSensePool = NULL;
269
270   if (Controller->FirmwareType == DAC960_V1_Controller)
271     {
272       CommandAllocationLength = offsetof(DAC960_Command_T, V1.EndMarker);
273       CommandAllocationGroupSize = DAC960_V1_CommandAllocationGroupSize;
274       ScatterGatherPool = pci_pool_create("DAC960_V1_ScatterGather",
275                 Controller->PCIDevice,
276         DAC960_V1_ScatterGatherLimit * sizeof(DAC960_V1_ScatterGatherSegment_T),
277         sizeof(DAC960_V1_ScatterGatherSegment_T), 0);
278       if (ScatterGatherPool == NULL)
279             return DAC960_Failure(Controller,
280                         "AUXILIARY STRUCTURE CREATION (SG)");
281       Controller->ScatterGatherPool = ScatterGatherPool;
282     }
283   else
284     {
285       CommandAllocationLength = offsetof(DAC960_Command_T, V2.EndMarker);
286       CommandAllocationGroupSize = DAC960_V2_CommandAllocationGroupSize;
287       ScatterGatherPool = pci_pool_create("DAC960_V2_ScatterGather",
288                 Controller->PCIDevice,
289         DAC960_V2_ScatterGatherLimit * sizeof(DAC960_V2_ScatterGatherSegment_T),
290         sizeof(DAC960_V2_ScatterGatherSegment_T), 0);
291       if (ScatterGatherPool == NULL)
292             return DAC960_Failure(Controller,
293                         "AUXILIARY STRUCTURE CREATION (SG)");
294       RequestSensePool = pci_pool_create("DAC960_V2_RequestSense",
295                 Controller->PCIDevice, sizeof(DAC960_SCSI_RequestSense_T),
296                 sizeof(int), 0);
297       if (RequestSensePool == NULL) {
298             pci_pool_destroy(ScatterGatherPool);
299             return DAC960_Failure(Controller,
300                         "AUXILIARY STRUCTURE CREATION (SG)");
301       }
302       Controller->ScatterGatherPool = ScatterGatherPool;
303       Controller->V2.RequestSensePool = RequestSensePool;
304     }
305   Controller->CommandAllocationGroupSize = CommandAllocationGroupSize;
306   Controller->FreeCommands = NULL;
307   for (CommandIdentifier = 1;
308        CommandIdentifier <= Controller->DriverQueueDepth;
309        CommandIdentifier++)
310     {
311       DAC960_Command_T *Command;
312       if (--CommandsRemaining <= 0)
313         {
314           CommandsRemaining =
315                 Controller->DriverQueueDepth - CommandIdentifier + 1;
316           if (CommandsRemaining > CommandAllocationGroupSize)
317                 CommandsRemaining = CommandAllocationGroupSize;
318           CommandGroupByteCount =
319                 CommandsRemaining * CommandAllocationLength;
320           AllocationPointer = kmalloc(CommandGroupByteCount, GFP_ATOMIC);
321           if (AllocationPointer == NULL)
322                 return DAC960_Failure(Controller,
323                                         "AUXILIARY STRUCTURE CREATION");
324           memset(AllocationPointer, 0, CommandGroupByteCount);
325          }
326       Command = (DAC960_Command_T *) AllocationPointer;
327       AllocationPointer += CommandAllocationLength;
328       Command->CommandIdentifier = CommandIdentifier;
329       Command->Controller = Controller;
330       Command->Next = Controller->FreeCommands;
331       Controller->FreeCommands = Command;
332       Controller->Commands[CommandIdentifier-1] = Command;
333       ScatterGatherCPU = pci_pool_alloc(ScatterGatherPool, SLAB_ATOMIC,
334                                                         &ScatterGatherDMA);
335       if (ScatterGatherCPU == NULL)
336           return DAC960_Failure(Controller, "AUXILIARY STRUCTURE CREATION");
337
338       if (RequestSensePool != NULL) {
339           RequestSenseCPU = pci_pool_alloc(RequestSensePool, SLAB_ATOMIC,
340                                                 &RequestSenseDMA);
341           if (RequestSenseCPU == NULL) {
342                 pci_pool_free(ScatterGatherPool, ScatterGatherCPU,
343                                 ScatterGatherDMA);
344                 return DAC960_Failure(Controller,
345                                         "AUXILIARY STRUCTURE CREATION");
346           }
347         }
348      if (Controller->FirmwareType == DAC960_V1_Controller) {
349         Command->cmd_sglist = Command->V1.ScatterList;
350         Command->V1.ScatterGatherList =
351                 (DAC960_V1_ScatterGatherSegment_T *)ScatterGatherCPU;
352         Command->V1.ScatterGatherListDMA = ScatterGatherDMA;
353       } else {
354         Command->cmd_sglist = Command->V2.ScatterList;
355         Command->V2.ScatterGatherList =
356                 (DAC960_V2_ScatterGatherSegment_T *)ScatterGatherCPU;
357         Command->V2.ScatterGatherListDMA = ScatterGatherDMA;
358         Command->V2.RequestSense =
359                                 (DAC960_SCSI_RequestSense_T *)RequestSenseCPU;
360         Command->V2.RequestSenseDMA = RequestSenseDMA;
361       }
362     }
363   return true;
364 }
365
366
367 /*
368   DAC960_DestroyAuxiliaryStructures deallocates the auxiliary data
369   structures for Controller.
370 */
371
372 static void DAC960_DestroyAuxiliaryStructures(DAC960_Controller_T *Controller)
373 {
374   int i;
375   struct pci_pool *ScatterGatherPool = Controller->ScatterGatherPool;
376   struct pci_pool *RequestSensePool = NULL;
377   void *ScatterGatherCPU;
378   dma_addr_t ScatterGatherDMA;
379   void *RequestSenseCPU;
380   dma_addr_t RequestSenseDMA;
381   DAC960_Command_T *CommandGroup = NULL;
382   
383
384   if (Controller->FirmwareType == DAC960_V2_Controller)
385         RequestSensePool = Controller->V2.RequestSensePool;
386
387   Controller->FreeCommands = NULL;
388   for (i = 0; i < Controller->DriverQueueDepth; i++)
389     {
390       DAC960_Command_T *Command = Controller->Commands[i];
391
392       if (Command == NULL)
393           continue;
394
395       if (Controller->FirmwareType == DAC960_V1_Controller) {
396           ScatterGatherCPU = (void *)Command->V1.ScatterGatherList;
397           ScatterGatherDMA = Command->V1.ScatterGatherListDMA;
398           RequestSenseCPU = NULL;
399           RequestSenseDMA = (dma_addr_t)0;
400       } else {
401           ScatterGatherCPU = (void *)Command->V2.ScatterGatherList;
402           ScatterGatherDMA = Command->V2.ScatterGatherListDMA;
403           RequestSenseCPU = (void *)Command->V2.RequestSense;
404           RequestSenseDMA = Command->V2.RequestSenseDMA;
405       }
406       if (ScatterGatherCPU != NULL)
407           pci_pool_free(ScatterGatherPool, ScatterGatherCPU, ScatterGatherDMA);
408       if (RequestSenseCPU != NULL)
409           pci_pool_free(RequestSensePool, RequestSenseCPU, RequestSenseDMA);
410
411       if ((Command->CommandIdentifier
412            % Controller->CommandAllocationGroupSize) == 1) {
413            /*
414             * We can't free the group of commands until all of the
415             * request sense and scatter gather dma structures are free.
416             * Remember the beginning of the group, but don't free it
417             * until we've reached the beginning of the next group.
418             */
419            if (CommandGroup != NULL)
420                 kfree(CommandGroup);
421             CommandGroup = Command;
422       }
423       Controller->Commands[i] = NULL;
424     }
425   if (CommandGroup != NULL)
426       kfree(CommandGroup);
427
428   if (Controller->CombinedStatusBuffer != NULL)
429     {
430       kfree(Controller->CombinedStatusBuffer);
431       Controller->CombinedStatusBuffer = NULL;
432       Controller->CurrentStatusBuffer = NULL;
433     }
434
435   if (ScatterGatherPool != NULL)
436         pci_pool_destroy(ScatterGatherPool);
437   if (Controller->FirmwareType == DAC960_V1_Controller) return;
438
439   if (RequestSensePool != NULL)
440         pci_pool_destroy(RequestSensePool);
441
442   for (i = 0; i < DAC960_MaxLogicalDrives; i++)
443     if (Controller->V2.LogicalDeviceInformation[i] != NULL)
444       {
445         kfree(Controller->V2.LogicalDeviceInformation[i]);
446         Controller->V2.LogicalDeviceInformation[i] = NULL;
447       }
448
449   for (i = 0; i < DAC960_V2_MaxPhysicalDevices; i++)
450     {
451       if (Controller->V2.PhysicalDeviceInformation[i] != NULL)
452         {
453           kfree(Controller->V2.PhysicalDeviceInformation[i]);
454           Controller->V2.PhysicalDeviceInformation[i] = NULL;
455         }
456       if (Controller->V2.InquiryUnitSerialNumber[i] != NULL)
457         {
458           kfree(Controller->V2.InquiryUnitSerialNumber[i]);
459           Controller->V2.InquiryUnitSerialNumber[i] = NULL;
460         }
461     }
462 }
463
464
465 /*
466   DAC960_V1_ClearCommand clears critical fields of Command for DAC960 V1
467   Firmware Controllers.
468 */
469
470 static inline void DAC960_V1_ClearCommand(DAC960_Command_T *Command)
471 {
472   DAC960_V1_CommandMailbox_T *CommandMailbox = &Command->V1.CommandMailbox;
473   memset(CommandMailbox, 0, sizeof(DAC960_V1_CommandMailbox_T));
474   Command->V1.CommandStatus = 0;
475 }
476
477
478 /*
479   DAC960_V2_ClearCommand clears critical fields of Command for DAC960 V2
480   Firmware Controllers.
481 */
482
483 static inline void DAC960_V2_ClearCommand(DAC960_Command_T *Command)
484 {
485   DAC960_V2_CommandMailbox_T *CommandMailbox = &Command->V2.CommandMailbox;
486   memset(CommandMailbox, 0, sizeof(DAC960_V2_CommandMailbox_T));
487   Command->V2.CommandStatus = 0;
488 }
489
490
491 /*
492   DAC960_AllocateCommand allocates a Command structure from Controller's
493   free list.  During driver initialization, a special initialization command
494   has been placed on the free list to guarantee that command allocation can
495   never fail.
496 */
497
498 static inline DAC960_Command_T *DAC960_AllocateCommand(DAC960_Controller_T
499                                                        *Controller)
500 {
501   DAC960_Command_T *Command = Controller->FreeCommands;
502   if (Command == NULL) return NULL;
503   Controller->FreeCommands = Command->Next;
504   Command->Next = NULL;
505   return Command;
506 }
507
508
509 /*
510   DAC960_DeallocateCommand deallocates Command, returning it to Controller's
511   free list.
512 */
513
514 static inline void DAC960_DeallocateCommand(DAC960_Command_T *Command)
515 {
516   DAC960_Controller_T *Controller = Command->Controller;
517
518   Command->Request = NULL;
519   Command->Next = Controller->FreeCommands;
520   Controller->FreeCommands = Command;
521 }
522
523
524 /*
525   DAC960_WaitForCommand waits for a wake_up on Controller's Command Wait Queue.
526 */
527
528 static void DAC960_WaitForCommand(DAC960_Controller_T *Controller)
529 {
530   spin_unlock_irq(&Controller->queue_lock);
531   __wait_event(Controller->CommandWaitQueue, Controller->FreeCommands);
532   spin_lock_irq(&Controller->queue_lock);
533 }
534
535
536 /*
537   DAC960_BA_QueueCommand queues Command for DAC960 BA Series Controllers.
538 */
539
540 static void DAC960_BA_QueueCommand(DAC960_Command_T *Command)
541 {
542   DAC960_Controller_T *Controller = Command->Controller;
543   void __iomem *ControllerBaseAddress = Controller->BaseAddress;
544   DAC960_V2_CommandMailbox_T *CommandMailbox = &Command->V2.CommandMailbox;
545   DAC960_V2_CommandMailbox_T *NextCommandMailbox =
546     Controller->V2.NextCommandMailbox;
547   CommandMailbox->Common.CommandIdentifier = Command->CommandIdentifier;
548   DAC960_BA_WriteCommandMailbox(NextCommandMailbox, CommandMailbox);
549   if (Controller->V2.PreviousCommandMailbox1->Words[0] == 0 ||
550       Controller->V2.PreviousCommandMailbox2->Words[0] == 0)
551     DAC960_BA_MemoryMailboxNewCommand(ControllerBaseAddress);
552   Controller->V2.PreviousCommandMailbox2 =
553     Controller->V2.PreviousCommandMailbox1;
554   Controller->V2.PreviousCommandMailbox1 = NextCommandMailbox;
555   if (++NextCommandMailbox > Controller->V2.LastCommandMailbox)
556     NextCommandMailbox = Controller->V2.FirstCommandMailbox;
557   Controller->V2.NextCommandMailbox = NextCommandMailbox;
558 }
559
560
561 /*
562   DAC960_LP_QueueCommand queues Command for DAC960 LP Series Controllers.
563 */
564
565 static void DAC960_LP_QueueCommand(DAC960_Command_T *Command)
566 {
567   DAC960_Controller_T *Controller = Command->Controller;
568   void __iomem *ControllerBaseAddress = Controller->BaseAddress;
569   DAC960_V2_CommandMailbox_T *CommandMailbox = &Command->V2.CommandMailbox;
570   DAC960_V2_CommandMailbox_T *NextCommandMailbox =
571     Controller->V2.NextCommandMailbox;
572   CommandMailbox->Common.CommandIdentifier = Command->CommandIdentifier;
573   DAC960_LP_WriteCommandMailbox(NextCommandMailbox, CommandMailbox);
574   if (Controller->V2.PreviousCommandMailbox1->Words[0] == 0 ||
575       Controller->V2.PreviousCommandMailbox2->Words[0] == 0)
576     DAC960_LP_MemoryMailboxNewCommand(ControllerBaseAddress);
577   Controller->V2.PreviousCommandMailbox2 =
578     Controller->V2.PreviousCommandMailbox1;
579   Controller->V2.PreviousCommandMailbox1 = NextCommandMailbox;
580   if (++NextCommandMailbox > Controller->V2.LastCommandMailbox)
581     NextCommandMailbox = Controller->V2.FirstCommandMailbox;
582   Controller->V2.NextCommandMailbox = NextCommandMailbox;
583 }
584
585
586 /*
587   DAC960_LA_QueueCommandDualMode queues Command for DAC960 LA Series
588   Controllers with Dual Mode Firmware.
589 */
590
591 static void DAC960_LA_QueueCommandDualMode(DAC960_Command_T *Command)
592 {
593   DAC960_Controller_T *Controller = Command->Controller;
594   void __iomem *ControllerBaseAddress = Controller->BaseAddress;
595   DAC960_V1_CommandMailbox_T *CommandMailbox = &Command->V1.CommandMailbox;
596   DAC960_V1_CommandMailbox_T *NextCommandMailbox =
597     Controller->V1.NextCommandMailbox;
598   CommandMailbox->Common.CommandIdentifier = Command->CommandIdentifier;
599   DAC960_LA_WriteCommandMailbox(NextCommandMailbox, CommandMailbox);
600   if (Controller->V1.PreviousCommandMailbox1->Words[0] == 0 ||
601       Controller->V1.PreviousCommandMailbox2->Words[0] == 0)
602     DAC960_LA_MemoryMailboxNewCommand(ControllerBaseAddress);
603   Controller->V1.PreviousCommandMailbox2 =
604     Controller->V1.PreviousCommandMailbox1;
605   Controller->V1.PreviousCommandMailbox1 = NextCommandMailbox;
606   if (++NextCommandMailbox > Controller->V1.LastCommandMailbox)
607     NextCommandMailbox = Controller->V1.FirstCommandMailbox;
608   Controller->V1.NextCommandMailbox = NextCommandMailbox;
609 }
610
611
612 /*
613   DAC960_LA_QueueCommandSingleMode queues Command for DAC960 LA Series
614   Controllers with Single Mode Firmware.
615 */
616
617 static void DAC960_LA_QueueCommandSingleMode(DAC960_Command_T *Command)
618 {
619   DAC960_Controller_T *Controller = Command->Controller;
620   void __iomem *ControllerBaseAddress = Controller->BaseAddress;
621   DAC960_V1_CommandMailbox_T *CommandMailbox = &Command->V1.CommandMailbox;
622   DAC960_V1_CommandMailbox_T *NextCommandMailbox =
623     Controller->V1.NextCommandMailbox;
624   CommandMailbox->Common.CommandIdentifier = Command->CommandIdentifier;
625   DAC960_LA_WriteCommandMailbox(NextCommandMailbox, CommandMailbox);
626   if (Controller->V1.PreviousCommandMailbox1->Words[0] == 0 ||
627       Controller->V1.PreviousCommandMailbox2->Words[0] == 0)
628     DAC960_LA_HardwareMailboxNewCommand(ControllerBaseAddress);
629   Controller->V1.PreviousCommandMailbox2 =
630     Controller->V1.PreviousCommandMailbox1;
631   Controller->V1.PreviousCommandMailbox1 = NextCommandMailbox;
632   if (++NextCommandMailbox > Controller->V1.LastCommandMailbox)
633     NextCommandMailbox = Controller->V1.FirstCommandMailbox;
634   Controller->V1.NextCommandMailbox = NextCommandMailbox;
635 }
636
637
638 /*
639   DAC960_PG_QueueCommandDualMode queues Command for DAC960 PG Series
640   Controllers with Dual Mode Firmware.
641 */
642
643 static void DAC960_PG_QueueCommandDualMode(DAC960_Command_T *Command)
644 {
645   DAC960_Controller_T *Controller = Command->Controller;
646   void __iomem *ControllerBaseAddress = Controller->BaseAddress;
647   DAC960_V1_CommandMailbox_T *CommandMailbox = &Command->V1.CommandMailbox;
648   DAC960_V1_CommandMailbox_T *NextCommandMailbox =
649     Controller->V1.NextCommandMailbox;
650   CommandMailbox->Common.CommandIdentifier = Command->CommandIdentifier;
651   DAC960_PG_WriteCommandMailbox(NextCommandMailbox, CommandMailbox);
652   if (Controller->V1.PreviousCommandMailbox1->Words[0] == 0 ||
653       Controller->V1.PreviousCommandMailbox2->Words[0] == 0)
654     DAC960_PG_MemoryMailboxNewCommand(ControllerBaseAddress);
655   Controller->V1.PreviousCommandMailbox2 =
656     Controller->V1.PreviousCommandMailbox1;
657   Controller->V1.PreviousCommandMailbox1 = NextCommandMailbox;
658   if (++NextCommandMailbox > Controller->V1.LastCommandMailbox)
659     NextCommandMailbox = Controller->V1.FirstCommandMailbox;
660   Controller->V1.NextCommandMailbox = NextCommandMailbox;
661 }
662
663
664 /*
665   DAC960_PG_QueueCommandSingleMode queues Command for DAC960 PG Series
666   Controllers with Single Mode Firmware.
667 */
668
669 static void DAC960_PG_QueueCommandSingleMode(DAC960_Command_T *Command)
670 {
671   DAC960_Controller_T *Controller = Command->Controller;
672   void __iomem *ControllerBaseAddress = Controller->BaseAddress;
673   DAC960_V1_CommandMailbox_T *CommandMailbox = &Command->V1.CommandMailbox;
674   DAC960_V1_CommandMailbox_T *NextCommandMailbox =
675     Controller->V1.NextCommandMailbox;
676   CommandMailbox->Common.CommandIdentifier = Command->CommandIdentifier;
677   DAC960_PG_WriteCommandMailbox(NextCommandMailbox, CommandMailbox);
678   if (Controller->V1.PreviousCommandMailbox1->Words[0] == 0 ||
679       Controller->V1.PreviousCommandMailbox2->Words[0] == 0)
680     DAC960_PG_HardwareMailboxNewCommand(ControllerBaseAddress);
681   Controller->V1.PreviousCommandMailbox2 =
682     Controller->V1.PreviousCommandMailbox1;
683   Controller->V1.PreviousCommandMailbox1 = NextCommandMailbox;
684   if (++NextCommandMailbox > Controller->V1.LastCommandMailbox)
685     NextCommandMailbox = Controller->V1.FirstCommandMailbox;
686   Controller->V1.NextCommandMailbox = NextCommandMailbox;
687 }
688
689
690 /*
691   DAC960_PD_QueueCommand queues Command for DAC960 PD Series Controllers.
692 */
693
694 static void DAC960_PD_QueueCommand(DAC960_Command_T *Command)
695 {
696   DAC960_Controller_T *Controller = Command->Controller;
697   void __iomem *ControllerBaseAddress = Controller->BaseAddress;
698   DAC960_V1_CommandMailbox_T *CommandMailbox = &Command->V1.CommandMailbox;
699   CommandMailbox->Common.CommandIdentifier = Command->CommandIdentifier;
700   while (DAC960_PD_MailboxFullP(ControllerBaseAddress))
701     udelay(1);
702   DAC960_PD_WriteCommandMailbox(ControllerBaseAddress, CommandMailbox);
703   DAC960_PD_NewCommand(ControllerBaseAddress);
704 }
705
706
707 /*
708   DAC960_P_QueueCommand queues Command for DAC960 P Series Controllers.
709 */
710
711 static void DAC960_P_QueueCommand(DAC960_Command_T *Command)
712 {
713   DAC960_Controller_T *Controller = Command->Controller;
714   void __iomem *ControllerBaseAddress = Controller->BaseAddress;
715   DAC960_V1_CommandMailbox_T *CommandMailbox = &Command->V1.CommandMailbox;
716   CommandMailbox->Common.CommandIdentifier = Command->CommandIdentifier;
717   switch (CommandMailbox->Common.CommandOpcode)
718     {
719     case DAC960_V1_Enquiry:
720       CommandMailbox->Common.CommandOpcode = DAC960_V1_Enquiry_Old;
721       break;
722     case DAC960_V1_GetDeviceState:
723       CommandMailbox->Common.CommandOpcode = DAC960_V1_GetDeviceState_Old;
724       break;
725     case DAC960_V1_Read:
726       CommandMailbox->Common.CommandOpcode = DAC960_V1_Read_Old;
727       DAC960_PD_To_P_TranslateReadWriteCommand(CommandMailbox);
728       break;
729     case DAC960_V1_Write:
730       CommandMailbox->Common.CommandOpcode = DAC960_V1_Write_Old;
731       DAC960_PD_To_P_TranslateReadWriteCommand(CommandMailbox);
732       break;
733     case DAC960_V1_ReadWithScatterGather:
734       CommandMailbox->Common.CommandOpcode =
735         DAC960_V1_ReadWithScatterGather_Old;
736       DAC960_PD_To_P_TranslateReadWriteCommand(CommandMailbox);
737       break;
738     case DAC960_V1_WriteWithScatterGather:
739       CommandMailbox->Common.CommandOpcode =
740         DAC960_V1_WriteWithScatterGather_Old;
741       DAC960_PD_To_P_TranslateReadWriteCommand(CommandMailbox);
742       break;
743     default:
744       break;
745     }
746   while (DAC960_PD_MailboxFullP(ControllerBaseAddress))
747     udelay(1);
748   DAC960_PD_WriteCommandMailbox(ControllerBaseAddress, CommandMailbox);
749   DAC960_PD_NewCommand(ControllerBaseAddress);
750 }
751
752
753 /*
754   DAC960_ExecuteCommand executes Command and waits for completion.
755 */
756
757 static void DAC960_ExecuteCommand(DAC960_Command_T *Command)
758 {
759   DAC960_Controller_T *Controller = Command->Controller;
760   DECLARE_COMPLETION(Completion);
761   unsigned long flags;
762   Command->Completion = &Completion;
763
764   spin_lock_irqsave(&Controller->queue_lock, flags);
765   DAC960_QueueCommand(Command);
766   spin_unlock_irqrestore(&Controller->queue_lock, flags);
767  
768   if (in_interrupt())
769           return;
770   wait_for_completion(&Completion);
771 }
772
773
774 /*
775   DAC960_V1_ExecuteType3 executes a DAC960 V1 Firmware Controller Type 3
776   Command and waits for completion.  It returns true on success and false
777   on failure.
778 */
779
780 static boolean DAC960_V1_ExecuteType3(DAC960_Controller_T *Controller,
781                                       DAC960_V1_CommandOpcode_T CommandOpcode,
782                                       dma_addr_t DataDMA)
783 {
784   DAC960_Command_T *Command = DAC960_AllocateCommand(Controller);
785   DAC960_V1_CommandMailbox_T *CommandMailbox = &Command->V1.CommandMailbox;
786   DAC960_V1_CommandStatus_T CommandStatus;
787   DAC960_V1_ClearCommand(Command);
788   Command->CommandType = DAC960_ImmediateCommand;
789   CommandMailbox->Type3.CommandOpcode = CommandOpcode;
790   CommandMailbox->Type3.BusAddress = DataDMA;
791   DAC960_ExecuteCommand(Command);
792   CommandStatus = Command->V1.CommandStatus;
793   DAC960_DeallocateCommand(Command);
794   return (CommandStatus == DAC960_V1_NormalCompletion);
795 }
796
797
798 /*
799   DAC960_V1_ExecuteTypeB executes a DAC960 V1 Firmware Controller Type 3B
800   Command and waits for completion.  It returns true on success and false
801   on failure.
802 */
803
804 static boolean DAC960_V1_ExecuteType3B(DAC960_Controller_T *Controller,
805                                        DAC960_V1_CommandOpcode_T CommandOpcode,
806                                        unsigned char CommandOpcode2,
807                                        dma_addr_t DataDMA)
808 {
809   DAC960_Command_T *Command = DAC960_AllocateCommand(Controller);
810   DAC960_V1_CommandMailbox_T *CommandMailbox = &Command->V1.CommandMailbox;
811   DAC960_V1_CommandStatus_T CommandStatus;
812   DAC960_V1_ClearCommand(Command);
813   Command->CommandType = DAC960_ImmediateCommand;
814   CommandMailbox->Type3B.CommandOpcode = CommandOpcode;
815   CommandMailbox->Type3B.CommandOpcode2 = CommandOpcode2;
816   CommandMailbox->Type3B.BusAddress = DataDMA;
817   DAC960_ExecuteCommand(Command);
818   CommandStatus = Command->V1.CommandStatus;
819   DAC960_DeallocateCommand(Command);
820   return (CommandStatus == DAC960_V1_NormalCompletion);
821 }
822
823
824 /*
825   DAC960_V1_ExecuteType3D executes a DAC960 V1 Firmware Controller Type 3D
826   Command and waits for completion.  It returns true on success and false
827   on failure.
828 */
829
830 static boolean DAC960_V1_ExecuteType3D(DAC960_Controller_T *Controller,
831                                        DAC960_V1_CommandOpcode_T CommandOpcode,
832                                        unsigned char Channel,
833                                        unsigned char TargetID,
834                                        dma_addr_t DataDMA)
835 {
836   DAC960_Command_T *Command = DAC960_AllocateCommand(Controller);
837   DAC960_V1_CommandMailbox_T *CommandMailbox = &Command->V1.CommandMailbox;
838   DAC960_V1_CommandStatus_T CommandStatus;
839   DAC960_V1_ClearCommand(Command);
840   Command->CommandType = DAC960_ImmediateCommand;
841   CommandMailbox->Type3D.CommandOpcode = CommandOpcode;
842   CommandMailbox->Type3D.Channel = Channel;
843   CommandMailbox->Type3D.TargetID = TargetID;
844   CommandMailbox->Type3D.BusAddress = DataDMA;
845   DAC960_ExecuteCommand(Command);
846   CommandStatus = Command->V1.CommandStatus;
847   DAC960_DeallocateCommand(Command);
848   return (CommandStatus == DAC960_V1_NormalCompletion);
849 }
850
851
852 /*
853   DAC960_V2_GeneralInfo executes a DAC960 V2 Firmware General Information
854   Reading IOCTL Command and waits for completion.  It returns true on success
855   and false on failure.
856
857   Return data in The controller's HealthStatusBuffer, which is dma-able memory
858 */
859
860 static boolean DAC960_V2_GeneralInfo(DAC960_Controller_T *Controller)
861 {
862   DAC960_Command_T *Command = DAC960_AllocateCommand(Controller);
863   DAC960_V2_CommandMailbox_T *CommandMailbox = &Command->V2.CommandMailbox;
864   DAC960_V2_CommandStatus_T CommandStatus;
865   DAC960_V2_ClearCommand(Command);
866   Command->CommandType = DAC960_ImmediateCommand;
867   CommandMailbox->Common.CommandOpcode = DAC960_V2_IOCTL;
868   CommandMailbox->Common.CommandControlBits
869                         .DataTransferControllerToHost = true;
870   CommandMailbox->Common.CommandControlBits
871                         .NoAutoRequestSense = true;
872   CommandMailbox->Common.DataTransferSize = sizeof(DAC960_V2_HealthStatusBuffer_T);
873   CommandMailbox->Common.IOCTL_Opcode = DAC960_V2_GetHealthStatus;
874   CommandMailbox->Common.DataTransferMemoryAddress
875                         .ScatterGatherSegments[0]
876                         .SegmentDataPointer =
877     Controller->V2.HealthStatusBufferDMA;
878   CommandMailbox->Common.DataTransferMemoryAddress
879                         .ScatterGatherSegments[0]
880                         .SegmentByteCount =
881     CommandMailbox->Common.DataTransferSize;
882   DAC960_ExecuteCommand(Command);
883   CommandStatus = Command->V2.CommandStatus;
884   DAC960_DeallocateCommand(Command);
885   return (CommandStatus == DAC960_V2_NormalCompletion);
886 }
887
888
889 /*
890   DAC960_V2_ControllerInfo executes a DAC960 V2 Firmware Controller
891   Information Reading IOCTL Command and waits for completion.  It returns
892   true on success and false on failure.
893
894   Data is returned in the controller's V2.NewControllerInformation dma-able
895   memory buffer.
896 */
897
898 static boolean DAC960_V2_NewControllerInfo(DAC960_Controller_T *Controller)
899 {
900   DAC960_Command_T *Command = DAC960_AllocateCommand(Controller);
901   DAC960_V2_CommandMailbox_T *CommandMailbox = &Command->V2.CommandMailbox;
902   DAC960_V2_CommandStatus_T CommandStatus;
903   DAC960_V2_ClearCommand(Command);
904   Command->CommandType = DAC960_ImmediateCommand;
905   CommandMailbox->ControllerInfo.CommandOpcode = DAC960_V2_IOCTL;
906   CommandMailbox->ControllerInfo.CommandControlBits
907                                 .DataTransferControllerToHost = true;
908   CommandMailbox->ControllerInfo.CommandControlBits
909                                 .NoAutoRequestSense = true;
910   CommandMailbox->ControllerInfo.DataTransferSize = sizeof(DAC960_V2_ControllerInfo_T);
911   CommandMailbox->ControllerInfo.ControllerNumber = 0;
912   CommandMailbox->ControllerInfo.IOCTL_Opcode = DAC960_V2_GetControllerInfo;
913   CommandMailbox->ControllerInfo.DataTransferMemoryAddress
914                                 .ScatterGatherSegments[0]
915                                 .SegmentDataPointer =
916         Controller->V2.NewControllerInformationDMA;
917   CommandMailbox->ControllerInfo.DataTransferMemoryAddress
918                                 .ScatterGatherSegments[0]
919                                 .SegmentByteCount =
920     CommandMailbox->ControllerInfo.DataTransferSize;
921   DAC960_ExecuteCommand(Command);
922   CommandStatus = Command->V2.CommandStatus;
923   DAC960_DeallocateCommand(Command);
924   return (CommandStatus == DAC960_V2_NormalCompletion);
925 }
926
927
928 /*
929   DAC960_V2_LogicalDeviceInfo executes a DAC960 V2 Firmware Controller Logical
930   Device Information Reading IOCTL Command and waits for completion.  It
931   returns true on success and false on failure.
932
933   Data is returned in the controller's V2.NewLogicalDeviceInformation
934 */
935
936 static boolean DAC960_V2_NewLogicalDeviceInfo(DAC960_Controller_T *Controller,
937                                            unsigned short LogicalDeviceNumber)
938 {
939   DAC960_Command_T *Command = DAC960_AllocateCommand(Controller);
940   DAC960_V2_CommandMailbox_T *CommandMailbox = &Command->V2.CommandMailbox;
941   DAC960_V2_CommandStatus_T CommandStatus;
942
943   DAC960_V2_ClearCommand(Command);
944   Command->CommandType = DAC960_ImmediateCommand;
945   CommandMailbox->LogicalDeviceInfo.CommandOpcode =
946                                 DAC960_V2_IOCTL;
947   CommandMailbox->LogicalDeviceInfo.CommandControlBits
948                                    .DataTransferControllerToHost = true;
949   CommandMailbox->LogicalDeviceInfo.CommandControlBits
950                                    .NoAutoRequestSense = true;
951   CommandMailbox->LogicalDeviceInfo.DataTransferSize = 
952                                 sizeof(DAC960_V2_LogicalDeviceInfo_T);
953   CommandMailbox->LogicalDeviceInfo.LogicalDevice.LogicalDeviceNumber =
954     LogicalDeviceNumber;
955   CommandMailbox->LogicalDeviceInfo.IOCTL_Opcode = DAC960_V2_GetLogicalDeviceInfoValid;
956   CommandMailbox->LogicalDeviceInfo.DataTransferMemoryAddress
957                                    .ScatterGatherSegments[0]
958                                    .SegmentDataPointer =
959         Controller->V2.NewLogicalDeviceInformationDMA;
960   CommandMailbox->LogicalDeviceInfo.DataTransferMemoryAddress
961                                    .ScatterGatherSegments[0]
962                                    .SegmentByteCount =
963     CommandMailbox->LogicalDeviceInfo.DataTransferSize;
964   DAC960_ExecuteCommand(Command);
965   CommandStatus = Command->V2.CommandStatus;
966   DAC960_DeallocateCommand(Command);
967   return (CommandStatus == DAC960_V2_NormalCompletion);
968 }
969
970
971 /*
972   DAC960_V2_PhysicalDeviceInfo executes a DAC960 V2 Firmware Controller "Read
973   Physical Device Information" IOCTL Command and waits for completion.  It
974   returns true on success and false on failure.
975
976   The Channel, TargetID, LogicalUnit arguments should be 0 the first time
977   this function is called for a given controller.  This will return data
978   for the "first" device on that controller.  The returned data includes a
979   Channel, TargetID, LogicalUnit that can be passed in to this routine to
980   get data for the NEXT device on that controller.
981
982   Data is stored in the controller's V2.NewPhysicalDeviceInfo dma-able
983   memory buffer.
984
985 */
986
987 static boolean DAC960_V2_NewPhysicalDeviceInfo(DAC960_Controller_T *Controller,
988                                             unsigned char Channel,
989                                             unsigned char TargetID,
990                                             unsigned char LogicalUnit)
991 {
992   DAC960_Command_T *Command = DAC960_AllocateCommand(Controller);
993   DAC960_V2_CommandMailbox_T *CommandMailbox = &Command->V2.CommandMailbox;
994   DAC960_V2_CommandStatus_T CommandStatus;
995
996   DAC960_V2_ClearCommand(Command);
997   Command->CommandType = DAC960_ImmediateCommand;
998   CommandMailbox->PhysicalDeviceInfo.CommandOpcode = DAC960_V2_IOCTL;
999   CommandMailbox->PhysicalDeviceInfo.CommandControlBits
1000                                     .DataTransferControllerToHost = true;
1001   CommandMailbox->PhysicalDeviceInfo.CommandControlBits
1002                                     .NoAutoRequestSense = true;
1003   CommandMailbox->PhysicalDeviceInfo.DataTransferSize =
1004                                 sizeof(DAC960_V2_PhysicalDeviceInfo_T);
1005   CommandMailbox->PhysicalDeviceInfo.PhysicalDevice.LogicalUnit = LogicalUnit;
1006   CommandMailbox->PhysicalDeviceInfo.PhysicalDevice.TargetID = TargetID;
1007   CommandMailbox->PhysicalDeviceInfo.PhysicalDevice.Channel = Channel;
1008   CommandMailbox->PhysicalDeviceInfo.IOCTL_Opcode =
1009                                         DAC960_V2_GetPhysicalDeviceInfoValid;
1010   CommandMailbox->PhysicalDeviceInfo.DataTransferMemoryAddress
1011                                     .ScatterGatherSegments[0]
1012                                     .SegmentDataPointer =
1013                                         Controller->V2.NewPhysicalDeviceInformationDMA;
1014   CommandMailbox->PhysicalDeviceInfo.DataTransferMemoryAddress
1015                                     .ScatterGatherSegments[0]
1016                                     .SegmentByteCount =
1017     CommandMailbox->PhysicalDeviceInfo.DataTransferSize;
1018   DAC960_ExecuteCommand(Command);
1019   CommandStatus = Command->V2.CommandStatus;
1020   DAC960_DeallocateCommand(Command);
1021   return (CommandStatus == DAC960_V2_NormalCompletion);
1022 }
1023
1024
1025 static void DAC960_V2_ConstructNewUnitSerialNumber(
1026         DAC960_Controller_T *Controller,
1027         DAC960_V2_CommandMailbox_T *CommandMailbox, int Channel, int TargetID,
1028         int LogicalUnit)
1029 {
1030       CommandMailbox->SCSI_10.CommandOpcode = DAC960_V2_SCSI_10_Passthru;
1031       CommandMailbox->SCSI_10.CommandControlBits
1032                              .DataTransferControllerToHost = true;
1033       CommandMailbox->SCSI_10.CommandControlBits
1034                              .NoAutoRequestSense = true;
1035       CommandMailbox->SCSI_10.DataTransferSize =
1036         sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T);
1037       CommandMailbox->SCSI_10.PhysicalDevice.LogicalUnit = LogicalUnit;
1038       CommandMailbox->SCSI_10.PhysicalDevice.TargetID = TargetID;
1039       CommandMailbox->SCSI_10.PhysicalDevice.Channel = Channel;
1040       CommandMailbox->SCSI_10.CDBLength = 6;
1041       CommandMailbox->SCSI_10.SCSI_CDB[0] = 0x12; /* INQUIRY */
1042       CommandMailbox->SCSI_10.SCSI_CDB[1] = 1; /* EVPD = 1 */
1043       CommandMailbox->SCSI_10.SCSI_CDB[2] = 0x80; /* Page Code */
1044       CommandMailbox->SCSI_10.SCSI_CDB[3] = 0; /* Reserved */
1045       CommandMailbox->SCSI_10.SCSI_CDB[4] =
1046         sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T);
1047       CommandMailbox->SCSI_10.SCSI_CDB[5] = 0; /* Control */
1048       CommandMailbox->SCSI_10.DataTransferMemoryAddress
1049                              .ScatterGatherSegments[0]
1050                              .SegmentDataPointer =
1051                 Controller->V2.NewInquiryUnitSerialNumberDMA;
1052       CommandMailbox->SCSI_10.DataTransferMemoryAddress
1053                              .ScatterGatherSegments[0]
1054                              .SegmentByteCount =
1055                 CommandMailbox->SCSI_10.DataTransferSize;
1056 }
1057
1058
1059 /*
1060   DAC960_V2_NewUnitSerialNumber executes an SCSI pass-through
1061   Inquiry command to a SCSI device identified by Channel number,
1062   Target id, Logical Unit Number.  This function Waits for completion
1063   of the command.
1064
1065   The return data includes Unit Serial Number information for the
1066   specified device.
1067
1068   Data is stored in the controller's V2.NewPhysicalDeviceInfo dma-able
1069   memory buffer.
1070 */
1071
1072 static boolean DAC960_V2_NewInquiryUnitSerialNumber(DAC960_Controller_T *Controller,
1073                         int Channel, int TargetID, int LogicalUnit)
1074 {
1075       DAC960_Command_T *Command;
1076       DAC960_V2_CommandMailbox_T *CommandMailbox;
1077       DAC960_V2_CommandStatus_T CommandStatus;
1078
1079       Command = DAC960_AllocateCommand(Controller);
1080       CommandMailbox = &Command->V2.CommandMailbox;
1081       DAC960_V2_ClearCommand(Command);
1082       Command->CommandType = DAC960_ImmediateCommand;
1083
1084       DAC960_V2_ConstructNewUnitSerialNumber(Controller, CommandMailbox,
1085                         Channel, TargetID, LogicalUnit);
1086
1087       DAC960_ExecuteCommand(Command);
1088       CommandStatus = Command->V2.CommandStatus;
1089       DAC960_DeallocateCommand(Command);
1090       return (CommandStatus == DAC960_V2_NormalCompletion);
1091 }
1092
1093
1094 /*
1095   DAC960_V2_DeviceOperation executes a DAC960 V2 Firmware Controller Device
1096   Operation IOCTL Command and waits for completion.  It returns true on
1097   success and false on failure.
1098 */
1099
1100 static boolean DAC960_V2_DeviceOperation(DAC960_Controller_T *Controller,
1101                                          DAC960_V2_IOCTL_Opcode_T IOCTL_Opcode,
1102                                          DAC960_V2_OperationDevice_T
1103                                            OperationDevice)
1104 {
1105   DAC960_Command_T *Command = DAC960_AllocateCommand(Controller);
1106   DAC960_V2_CommandMailbox_T *CommandMailbox = &Command->V2.CommandMailbox;
1107   DAC960_V2_CommandStatus_T CommandStatus;
1108   DAC960_V2_ClearCommand(Command);
1109   Command->CommandType = DAC960_ImmediateCommand;
1110   CommandMailbox->DeviceOperation.CommandOpcode = DAC960_V2_IOCTL;
1111   CommandMailbox->DeviceOperation.CommandControlBits
1112                                  .DataTransferControllerToHost = true;
1113   CommandMailbox->DeviceOperation.CommandControlBits
1114                                  .NoAutoRequestSense = true;
1115   CommandMailbox->DeviceOperation.IOCTL_Opcode = IOCTL_Opcode;
1116   CommandMailbox->DeviceOperation.OperationDevice = OperationDevice;
1117   DAC960_ExecuteCommand(Command);
1118   CommandStatus = Command->V2.CommandStatus;
1119   DAC960_DeallocateCommand(Command);
1120   return (CommandStatus == DAC960_V2_NormalCompletion);
1121 }
1122
1123
1124 /*
1125   DAC960_V1_EnableMemoryMailboxInterface enables the Memory Mailbox Interface
1126   for DAC960 V1 Firmware Controllers.
1127
1128   PD and P controller types have no memory mailbox, but still need the
1129   other dma mapped memory.
1130 */
1131
1132 static boolean DAC960_V1_EnableMemoryMailboxInterface(DAC960_Controller_T
1133                                                       *Controller)
1134 {
1135   void __iomem *ControllerBaseAddress = Controller->BaseAddress;
1136   DAC960_HardwareType_T hw_type = Controller->HardwareType;
1137   struct pci_dev *PCI_Device = Controller->PCIDevice;
1138   struct dma_loaf *DmaPages = &Controller->DmaPages;
1139   size_t DmaPagesSize;
1140   size_t CommandMailboxesSize;
1141   size_t StatusMailboxesSize;
1142
1143   DAC960_V1_CommandMailbox_T *CommandMailboxesMemory;
1144   dma_addr_t CommandMailboxesMemoryDMA;
1145
1146   DAC960_V1_StatusMailbox_T *StatusMailboxesMemory;
1147   dma_addr_t StatusMailboxesMemoryDMA;
1148
1149   DAC960_V1_CommandMailbox_T CommandMailbox;
1150   DAC960_V1_CommandStatus_T CommandStatus;
1151   int TimeoutCounter;
1152   int i;
1153
1154   
1155   if (pci_set_dma_mask(Controller->PCIDevice, DAC690_V1_PciDmaMask))
1156         return DAC960_Failure(Controller, "DMA mask out of range");
1157   Controller->BounceBufferLimit = DAC690_V1_PciDmaMask;
1158
1159   if ((hw_type == DAC960_PD_Controller) || (hw_type == DAC960_P_Controller)) {
1160     CommandMailboxesSize =  0;
1161     StatusMailboxesSize = 0;
1162   } else {
1163     CommandMailboxesSize =  DAC960_V1_CommandMailboxCount * sizeof(DAC960_V1_CommandMailbox_T);
1164     StatusMailboxesSize = DAC960_V1_StatusMailboxCount * sizeof(DAC960_V1_StatusMailbox_T);
1165   }
1166   DmaPagesSize = CommandMailboxesSize + StatusMailboxesSize + 
1167         sizeof(DAC960_V1_DCDB_T) + sizeof(DAC960_V1_Enquiry_T) +
1168         sizeof(DAC960_V1_ErrorTable_T) + sizeof(DAC960_V1_EventLogEntry_T) +
1169         sizeof(DAC960_V1_RebuildProgress_T) +
1170         sizeof(DAC960_V1_LogicalDriveInformationArray_T) +
1171         sizeof(DAC960_V1_BackgroundInitializationStatus_T) +
1172         sizeof(DAC960_V1_DeviceState_T) + sizeof(DAC960_SCSI_Inquiry_T) +
1173         sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T);
1174
1175   if (!init_dma_loaf(PCI_Device, DmaPages, DmaPagesSize))
1176         return false;
1177
1178
1179   if ((hw_type == DAC960_PD_Controller) || (hw_type == DAC960_P_Controller)) 
1180         goto skip_mailboxes;
1181
1182   CommandMailboxesMemory = slice_dma_loaf(DmaPages,
1183                 CommandMailboxesSize, &CommandMailboxesMemoryDMA);
1184   
1185   /* These are the base addresses for the command memory mailbox array */
1186   Controller->V1.FirstCommandMailbox = CommandMailboxesMemory;
1187   Controller->V1.FirstCommandMailboxDMA = CommandMailboxesMemoryDMA;
1188
1189   CommandMailboxesMemory += DAC960_V1_CommandMailboxCount - 1;
1190   Controller->V1.LastCommandMailbox = CommandMailboxesMemory;
1191   Controller->V1.NextCommandMailbox = Controller->V1.FirstCommandMailbox;
1192   Controller->V1.PreviousCommandMailbox1 = Controller->V1.LastCommandMailbox;
1193   Controller->V1.PreviousCommandMailbox2 =
1194                                         Controller->V1.LastCommandMailbox - 1;
1195
1196   /* These are the base addresses for the status memory mailbox array */
1197   StatusMailboxesMemory = slice_dma_loaf(DmaPages,
1198                 StatusMailboxesSize, &StatusMailboxesMemoryDMA);
1199
1200   Controller->V1.FirstStatusMailbox = StatusMailboxesMemory;
1201   Controller->V1.FirstStatusMailboxDMA = StatusMailboxesMemoryDMA;
1202   StatusMailboxesMemory += DAC960_V1_StatusMailboxCount - 1;
1203   Controller->V1.LastStatusMailbox = StatusMailboxesMemory;
1204   Controller->V1.NextStatusMailbox = Controller->V1.FirstStatusMailbox;
1205
1206 skip_mailboxes:
1207   Controller->V1.MonitoringDCDB = slice_dma_loaf(DmaPages,
1208                 sizeof(DAC960_V1_DCDB_T),
1209                 &Controller->V1.MonitoringDCDB_DMA);
1210
1211   Controller->V1.NewEnquiry = slice_dma_loaf(DmaPages,
1212                 sizeof(DAC960_V1_Enquiry_T),
1213                 &Controller->V1.NewEnquiryDMA);
1214
1215   Controller->V1.NewErrorTable = slice_dma_loaf(DmaPages,
1216                 sizeof(DAC960_V1_ErrorTable_T),
1217                 &Controller->V1.NewErrorTableDMA);
1218
1219   Controller->V1.EventLogEntry = slice_dma_loaf(DmaPages,
1220                 sizeof(DAC960_V1_EventLogEntry_T),
1221                 &Controller->V1.EventLogEntryDMA);
1222
1223   Controller->V1.RebuildProgress = slice_dma_loaf(DmaPages,
1224                 sizeof(DAC960_V1_RebuildProgress_T),
1225                 &Controller->V1.RebuildProgressDMA);
1226
1227   Controller->V1.NewLogicalDriveInformation = slice_dma_loaf(DmaPages,
1228                 sizeof(DAC960_V1_LogicalDriveInformationArray_T),
1229                 &Controller->V1.NewLogicalDriveInformationDMA);
1230
1231   Controller->V1.BackgroundInitializationStatus = slice_dma_loaf(DmaPages,
1232                 sizeof(DAC960_V1_BackgroundInitializationStatus_T),
1233                 &Controller->V1.BackgroundInitializationStatusDMA);
1234
1235   Controller->V1.NewDeviceState = slice_dma_loaf(DmaPages,
1236                 sizeof(DAC960_V1_DeviceState_T),
1237                 &Controller->V1.NewDeviceStateDMA);
1238
1239   Controller->V1.NewInquiryStandardData = slice_dma_loaf(DmaPages,
1240                 sizeof(DAC960_SCSI_Inquiry_T),
1241                 &Controller->V1.NewInquiryStandardDataDMA);
1242
1243   Controller->V1.NewInquiryUnitSerialNumber = slice_dma_loaf(DmaPages,
1244                 sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T),
1245                 &Controller->V1.NewInquiryUnitSerialNumberDMA);
1246
1247   if ((hw_type == DAC960_PD_Controller) || (hw_type == DAC960_P_Controller))
1248         return true;
1249  
1250   /* Enable the Memory Mailbox Interface. */
1251   Controller->V1.DualModeMemoryMailboxInterface = true;
1252   CommandMailbox.TypeX.CommandOpcode = 0x2B;
1253   CommandMailbox.TypeX.CommandIdentifier = 0;
1254   CommandMailbox.TypeX.CommandOpcode2 = 0x14;
1255   CommandMailbox.TypeX.CommandMailboxesBusAddress =
1256                                 Controller->V1.FirstCommandMailboxDMA;
1257   CommandMailbox.TypeX.StatusMailboxesBusAddress =
1258                                 Controller->V1.FirstStatusMailboxDMA;
1259 #define TIMEOUT_COUNT 1000000
1260
1261   for (i = 0; i < 2; i++)
1262     switch (Controller->HardwareType)
1263       {
1264       case DAC960_LA_Controller:
1265         TimeoutCounter = TIMEOUT_COUNT;
1266         while (--TimeoutCounter >= 0)
1267           {
1268             if (!DAC960_LA_HardwareMailboxFullP(ControllerBaseAddress))
1269               break;
1270             udelay(10);
1271           }
1272         if (TimeoutCounter < 0) return false;
1273         DAC960_LA_WriteHardwareMailbox(ControllerBaseAddress, &CommandMailbox);
1274         DAC960_LA_HardwareMailboxNewCommand(ControllerBaseAddress);
1275         TimeoutCounter = TIMEOUT_COUNT;
1276         while (--TimeoutCounter >= 0)
1277           {
1278             if (DAC960_LA_HardwareMailboxStatusAvailableP(
1279                   ControllerBaseAddress))
1280               break;
1281             udelay(10);
1282           }
1283         if (TimeoutCounter < 0) return false;
1284         CommandStatus = DAC960_LA_ReadStatusRegister(ControllerBaseAddress);
1285         DAC960_LA_AcknowledgeHardwareMailboxInterrupt(ControllerBaseAddress);
1286         DAC960_LA_AcknowledgeHardwareMailboxStatus(ControllerBaseAddress);
1287         if (CommandStatus == DAC960_V1_NormalCompletion) return true;
1288         Controller->V1.DualModeMemoryMailboxInterface = false;
1289         CommandMailbox.TypeX.CommandOpcode2 = 0x10;
1290         break;
1291       case DAC960_PG_Controller:
1292         TimeoutCounter = TIMEOUT_COUNT;
1293         while (--TimeoutCounter >= 0)
1294           {
1295             if (!DAC960_PG_HardwareMailboxFullP(ControllerBaseAddress))
1296               break;
1297             udelay(10);
1298           }
1299         if (TimeoutCounter < 0) return false;
1300         DAC960_PG_WriteHardwareMailbox(ControllerBaseAddress, &CommandMailbox);
1301         DAC960_PG_HardwareMailboxNewCommand(ControllerBaseAddress);
1302
1303         TimeoutCounter = TIMEOUT_COUNT;
1304         while (--TimeoutCounter >= 0)
1305           {
1306             if (DAC960_PG_HardwareMailboxStatusAvailableP(
1307                   ControllerBaseAddress))
1308               break;
1309             udelay(10);
1310           }
1311         if (TimeoutCounter < 0) return false;
1312         CommandStatus = DAC960_PG_ReadStatusRegister(ControllerBaseAddress);
1313         DAC960_PG_AcknowledgeHardwareMailboxInterrupt(ControllerBaseAddress);
1314         DAC960_PG_AcknowledgeHardwareMailboxStatus(ControllerBaseAddress);
1315         if (CommandStatus == DAC960_V1_NormalCompletion) return true;
1316         Controller->V1.DualModeMemoryMailboxInterface = false;
1317         CommandMailbox.TypeX.CommandOpcode2 = 0x10;
1318         break;
1319       default:
1320         DAC960_Failure(Controller, "Unknown Controller Type\n");
1321         break;
1322       }
1323   return false;
1324 }
1325
1326
1327 /*
1328   DAC960_V2_EnableMemoryMailboxInterface enables the Memory Mailbox Interface
1329   for DAC960 V2 Firmware Controllers.
1330
1331   Aggregate the space needed for the controller's memory mailbox and
1332   the other data structures that will be targets of dma transfers with
1333   the controller.  Allocate a dma-mapped region of memory to hold these
1334   structures.  Then, save CPU pointers and dma_addr_t values to reference
1335   the structures that are contained in that region.
1336 */
1337
1338 static boolean DAC960_V2_EnableMemoryMailboxInterface(DAC960_Controller_T
1339                                                       *Controller)
1340 {
1341   void __iomem *ControllerBaseAddress = Controller->BaseAddress;
1342   struct pci_dev *PCI_Device = Controller->PCIDevice;
1343   struct dma_loaf *DmaPages = &Controller->DmaPages;
1344   size_t DmaPagesSize;
1345   size_t CommandMailboxesSize;
1346   size_t StatusMailboxesSize;
1347
1348   DAC960_V2_CommandMailbox_T *CommandMailboxesMemory;
1349   dma_addr_t CommandMailboxesMemoryDMA;
1350
1351   DAC960_V2_StatusMailbox_T *StatusMailboxesMemory;
1352   dma_addr_t StatusMailboxesMemoryDMA;
1353
1354   DAC960_V2_CommandMailbox_T *CommandMailbox;
1355   dma_addr_t    CommandMailboxDMA;
1356   DAC960_V2_CommandStatus_T CommandStatus;
1357
1358   if (pci_set_dma_mask(Controller->PCIDevice, DAC690_V2_PciDmaMask))
1359         return DAC960_Failure(Controller, "DMA mask out of range");
1360   Controller->BounceBufferLimit = DAC690_V2_PciDmaMask;
1361
1362   /* This is a temporary dma mapping, used only in the scope of this function */
1363   CommandMailbox =
1364           (DAC960_V2_CommandMailbox_T *)pci_alloc_consistent( PCI_Device,
1365                 sizeof(DAC960_V2_CommandMailbox_T), &CommandMailboxDMA);
1366   if (CommandMailbox == NULL)
1367           return false;
1368
1369   CommandMailboxesSize = DAC960_V2_CommandMailboxCount * sizeof(DAC960_V2_CommandMailbox_T);
1370   StatusMailboxesSize = DAC960_V2_StatusMailboxCount * sizeof(DAC960_V2_StatusMailbox_T);
1371   DmaPagesSize =
1372     CommandMailboxesSize + StatusMailboxesSize +
1373     sizeof(DAC960_V2_HealthStatusBuffer_T) +
1374     sizeof(DAC960_V2_ControllerInfo_T) +
1375     sizeof(DAC960_V2_LogicalDeviceInfo_T) +
1376     sizeof(DAC960_V2_PhysicalDeviceInfo_T) +
1377     sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T) +
1378     sizeof(DAC960_V2_Event_T) +
1379     sizeof(DAC960_V2_PhysicalToLogicalDevice_T);
1380
1381   if (!init_dma_loaf(PCI_Device, DmaPages, DmaPagesSize)) {
1382         pci_free_consistent(PCI_Device, sizeof(DAC960_V2_CommandMailbox_T),
1383                                         CommandMailbox, CommandMailboxDMA);
1384         return false;
1385   }
1386
1387   CommandMailboxesMemory = slice_dma_loaf(DmaPages,
1388                 CommandMailboxesSize, &CommandMailboxesMemoryDMA);
1389
1390   /* These are the base addresses for the command memory mailbox array */
1391   Controller->V2.FirstCommandMailbox = CommandMailboxesMemory;
1392   Controller->V2.FirstCommandMailboxDMA = CommandMailboxesMemoryDMA;
1393
1394   CommandMailboxesMemory += DAC960_V2_CommandMailboxCount - 1;
1395   Controller->V2.LastCommandMailbox = CommandMailboxesMemory;
1396   Controller->V2.NextCommandMailbox = Controller->V2.FirstCommandMailbox;
1397   Controller->V2.PreviousCommandMailbox1 = Controller->V2.LastCommandMailbox;
1398   Controller->V2.PreviousCommandMailbox2 =
1399                                         Controller->V2.LastCommandMailbox - 1;
1400
1401   /* These are the base addresses for the status memory mailbox array */
1402   StatusMailboxesMemory = slice_dma_loaf(DmaPages,
1403                 StatusMailboxesSize, &StatusMailboxesMemoryDMA);
1404
1405   Controller->V2.FirstStatusMailbox = StatusMailboxesMemory;
1406   Controller->V2.FirstStatusMailboxDMA = StatusMailboxesMemoryDMA;
1407   StatusMailboxesMemory += DAC960_V2_StatusMailboxCount - 1;
1408   Controller->V2.LastStatusMailbox = StatusMailboxesMemory;
1409   Controller->V2.NextStatusMailbox = Controller->V2.FirstStatusMailbox;
1410
1411   Controller->V2.HealthStatusBuffer = slice_dma_loaf(DmaPages,
1412                 sizeof(DAC960_V2_HealthStatusBuffer_T),
1413                 &Controller->V2.HealthStatusBufferDMA);
1414
1415   Controller->V2.NewControllerInformation = slice_dma_loaf(DmaPages,
1416                 sizeof(DAC960_V2_ControllerInfo_T), 
1417                 &Controller->V2.NewControllerInformationDMA);
1418
1419   Controller->V2.NewLogicalDeviceInformation =  slice_dma_loaf(DmaPages,
1420                 sizeof(DAC960_V2_LogicalDeviceInfo_T),
1421                 &Controller->V2.NewLogicalDeviceInformationDMA);
1422
1423   Controller->V2.NewPhysicalDeviceInformation = slice_dma_loaf(DmaPages,
1424                 sizeof(DAC960_V2_PhysicalDeviceInfo_T),
1425                 &Controller->V2.NewPhysicalDeviceInformationDMA);
1426
1427   Controller->V2.NewInquiryUnitSerialNumber = slice_dma_loaf(DmaPages,
1428                 sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T),
1429                 &Controller->V2.NewInquiryUnitSerialNumberDMA);
1430
1431   Controller->V2.Event = slice_dma_loaf(DmaPages,
1432                 sizeof(DAC960_V2_Event_T),
1433                 &Controller->V2.EventDMA);
1434
1435   Controller->V2.PhysicalToLogicalDevice = slice_dma_loaf(DmaPages,
1436                 sizeof(DAC960_V2_PhysicalToLogicalDevice_T),
1437                 &Controller->V2.PhysicalToLogicalDeviceDMA);
1438
1439   /*
1440     Enable the Memory Mailbox Interface.
1441     
1442     I don't know why we can't just use one of the memory mailboxes
1443     we just allocated to do this, instead of using this temporary one.
1444     Try this change later.
1445   */
1446   memset(CommandMailbox, 0, sizeof(DAC960_V2_CommandMailbox_T));
1447   CommandMailbox->SetMemoryMailbox.CommandIdentifier = 1;
1448   CommandMailbox->SetMemoryMailbox.CommandOpcode = DAC960_V2_IOCTL;
1449   CommandMailbox->SetMemoryMailbox.CommandControlBits.NoAutoRequestSense = true;
1450   CommandMailbox->SetMemoryMailbox.FirstCommandMailboxSizeKB =
1451     (DAC960_V2_CommandMailboxCount * sizeof(DAC960_V2_CommandMailbox_T)) >> 10;
1452   CommandMailbox->SetMemoryMailbox.FirstStatusMailboxSizeKB =
1453     (DAC960_V2_StatusMailboxCount * sizeof(DAC960_V2_StatusMailbox_T)) >> 10;
1454   CommandMailbox->SetMemoryMailbox.SecondCommandMailboxSizeKB = 0;
1455   CommandMailbox->SetMemoryMailbox.SecondStatusMailboxSizeKB = 0;
1456   CommandMailbox->SetMemoryMailbox.RequestSenseSize = 0;
1457   CommandMailbox->SetMemoryMailbox.IOCTL_Opcode = DAC960_V2_SetMemoryMailbox;
1458   CommandMailbox->SetMemoryMailbox.HealthStatusBufferSizeKB = 1;
1459   CommandMailbox->SetMemoryMailbox.HealthStatusBufferBusAddress =
1460                                         Controller->V2.HealthStatusBufferDMA;
1461   CommandMailbox->SetMemoryMailbox.FirstCommandMailboxBusAddress =
1462                                         Controller->V2.FirstCommandMailboxDMA;
1463   CommandMailbox->SetMemoryMailbox.FirstStatusMailboxBusAddress =
1464                                         Controller->V2.FirstStatusMailboxDMA;
1465   switch (Controller->HardwareType)
1466     {
1467     case DAC960_BA_Controller:
1468       while (DAC960_BA_HardwareMailboxFullP(ControllerBaseAddress))
1469         udelay(1);
1470       DAC960_BA_WriteHardwareMailbox(ControllerBaseAddress, CommandMailboxDMA);
1471       DAC960_BA_HardwareMailboxNewCommand(ControllerBaseAddress);
1472       while (!DAC960_BA_HardwareMailboxStatusAvailableP(ControllerBaseAddress))
1473         udelay(1);
1474       CommandStatus = DAC960_BA_ReadCommandStatus(ControllerBaseAddress);
1475       DAC960_BA_AcknowledgeHardwareMailboxInterrupt(ControllerBaseAddress);
1476       DAC960_BA_AcknowledgeHardwareMailboxStatus(ControllerBaseAddress);
1477       break;
1478     case DAC960_LP_Controller:
1479       while (DAC960_LP_HardwareMailboxFullP(ControllerBaseAddress))
1480         udelay(1);
1481       DAC960_LP_WriteHardwareMailbox(ControllerBaseAddress, CommandMailboxDMA);
1482       DAC960_LP_HardwareMailboxNewCommand(ControllerBaseAddress);
1483       while (!DAC960_LP_HardwareMailboxStatusAvailableP(ControllerBaseAddress))
1484         udelay(1);
1485       CommandStatus = DAC960_LP_ReadCommandStatus(ControllerBaseAddress);
1486       DAC960_LP_AcknowledgeHardwareMailboxInterrupt(ControllerBaseAddress);
1487       DAC960_LP_AcknowledgeHardwareMailboxStatus(ControllerBaseAddress);
1488       break;
1489     default:
1490       DAC960_Failure(Controller, "Unknown Controller Type\n");
1491       CommandStatus = DAC960_V2_AbormalCompletion;
1492       break;
1493     }
1494   pci_free_consistent(PCI_Device, sizeof(DAC960_V2_CommandMailbox_T),
1495                                         CommandMailbox, CommandMailboxDMA);
1496   return (CommandStatus == DAC960_V2_NormalCompletion);
1497 }
1498
1499
1500 /*
1501   DAC960_V1_ReadControllerConfiguration reads the Configuration Information
1502   from DAC960 V1 Firmware Controllers and initializes the Controller structure.
1503 */
1504
1505 static boolean DAC960_V1_ReadControllerConfiguration(DAC960_Controller_T
1506                                                      *Controller)
1507 {
1508   DAC960_V1_Enquiry2_T *Enquiry2;
1509   dma_addr_t Enquiry2DMA;
1510   DAC960_V1_Config2_T *Config2;
1511   dma_addr_t Config2DMA;
1512   int LogicalDriveNumber, Channel, TargetID;
1513   struct dma_loaf local_dma;
1514
1515   if (!init_dma_loaf(Controller->PCIDevice, &local_dma,
1516                 sizeof(DAC960_V1_Enquiry2_T) + sizeof(DAC960_V1_Config2_T)))
1517         return DAC960_Failure(Controller, "LOGICAL DEVICE ALLOCATION");
1518
1519   Enquiry2 = slice_dma_loaf(&local_dma, sizeof(DAC960_V1_Enquiry2_T), &Enquiry2DMA);
1520   Config2 = slice_dma_loaf(&local_dma, sizeof(DAC960_V1_Config2_T), &Config2DMA);
1521
1522   if (!DAC960_V1_ExecuteType3(Controller, DAC960_V1_Enquiry,
1523                               Controller->V1.NewEnquiryDMA)) {
1524     free_dma_loaf(Controller->PCIDevice, &local_dma);
1525     return DAC960_Failure(Controller, "ENQUIRY");
1526   }
1527   memcpy(&Controller->V1.Enquiry, Controller->V1.NewEnquiry,
1528                                                 sizeof(DAC960_V1_Enquiry_T));
1529
1530   if (!DAC960_V1_ExecuteType3(Controller, DAC960_V1_Enquiry2, Enquiry2DMA)) {
1531     free_dma_loaf(Controller->PCIDevice, &local_dma);
1532     return DAC960_Failure(Controller, "ENQUIRY2");
1533   }
1534
1535   if (!DAC960_V1_ExecuteType3(Controller, DAC960_V1_ReadConfig2, Config2DMA)) {
1536     free_dma_loaf(Controller->PCIDevice, &local_dma);
1537     return DAC960_Failure(Controller, "READ CONFIG2");
1538   }
1539
1540   if (!DAC960_V1_ExecuteType3(Controller, DAC960_V1_GetLogicalDriveInformation,
1541                               Controller->V1.NewLogicalDriveInformationDMA)) {
1542     free_dma_loaf(Controller->PCIDevice, &local_dma);
1543     return DAC960_Failure(Controller, "GET LOGICAL DRIVE INFORMATION");
1544   }
1545   memcpy(&Controller->V1.LogicalDriveInformation,
1546                 Controller->V1.NewLogicalDriveInformation,
1547                 sizeof(DAC960_V1_LogicalDriveInformationArray_T));
1548
1549   for (Channel = 0; Channel < Enquiry2->ActualChannels; Channel++)
1550     for (TargetID = 0; TargetID < Enquiry2->MaxTargets; TargetID++) {
1551       if (!DAC960_V1_ExecuteType3D(Controller, DAC960_V1_GetDeviceState,
1552                                    Channel, TargetID,
1553                                    Controller->V1.NewDeviceStateDMA)) {
1554                 free_dma_loaf(Controller->PCIDevice, &local_dma);
1555                 return DAC960_Failure(Controller, "GET DEVICE STATE");
1556         }
1557         memcpy(&Controller->V1.DeviceState[Channel][TargetID],
1558                 Controller->V1.NewDeviceState, sizeof(DAC960_V1_DeviceState_T));
1559      }
1560   /*
1561     Initialize the Controller Model Name and Full Model Name fields.
1562   */
1563   switch (Enquiry2->HardwareID.SubModel)
1564     {
1565     case DAC960_V1_P_PD_PU:
1566       if (Enquiry2->SCSICapability.BusSpeed == DAC960_V1_Ultra)
1567         strcpy(Controller->ModelName, "DAC960PU");
1568       else strcpy(Controller->ModelName, "DAC960PD");
1569       break;
1570     case DAC960_V1_PL:
1571       strcpy(Controller->ModelName, "DAC960PL");
1572       break;
1573     case DAC960_V1_PG:
1574       strcpy(Controller->ModelName, "DAC960PG");
1575       break;
1576     case DAC960_V1_PJ:
1577       strcpy(Controller->ModelName, "DAC960PJ");
1578       break;
1579     case DAC960_V1_PR:
1580       strcpy(Controller->ModelName, "DAC960PR");
1581       break;
1582     case DAC960_V1_PT:
1583       strcpy(Controller->ModelName, "DAC960PT");
1584       break;
1585     case DAC960_V1_PTL0:
1586       strcpy(Controller->ModelName, "DAC960PTL0");
1587       break;
1588     case DAC960_V1_PRL:
1589       strcpy(Controller->ModelName, "DAC960PRL");
1590       break;
1591     case DAC960_V1_PTL1:
1592       strcpy(Controller->ModelName, "DAC960PTL1");
1593       break;
1594     case DAC960_V1_1164P:
1595       strcpy(Controller->ModelName, "DAC1164P");
1596       break;
1597     default:
1598       free_dma_loaf(Controller->PCIDevice, &local_dma);
1599       return DAC960_Failure(Controller, "MODEL VERIFICATION");
1600     }
1601   strcpy(Controller->FullModelName, "Mylex ");
1602   strcat(Controller->FullModelName, Controller->ModelName);
1603   /*
1604     Initialize the Controller Firmware Version field and verify that it
1605     is a supported firmware version.  The supported firmware versions are:
1606
1607     DAC1164P                5.06 and above
1608     DAC960PTL/PRL/PJ/PG     4.06 and above
1609     DAC960PU/PD/PL          3.51 and above
1610     DAC960PU/PD/PL/P        2.73 and above
1611   */
1612 #if defined(CONFIG_ALPHA)
1613   /*
1614     DEC Alpha machines were often equipped with DAC960 cards that were
1615     OEMed from Mylex, and had their own custom firmware. Version 2.70,
1616     the last custom FW revision to be released by DEC for these older
1617     controllers, appears to work quite well with this driver.
1618
1619     Cards tested successfully were several versions each of the PD and
1620     PU, called by DEC the KZPSC and KZPAC, respectively, and having
1621     the Manufacturer Numbers (from Mylex), usually on a sticker on the
1622     back of the board, of:
1623
1624     KZPSC:  D040347 (1-channel) or D040348 (2-channel) or D040349 (3-channel)
1625     KZPAC:  D040395 (1-channel) or D040396 (2-channel) or D040397 (3-channel)
1626   */
1627 # define FIRMWARE_27X   "2.70"
1628 #else
1629 # define FIRMWARE_27X   "2.73"
1630 #endif
1631
1632   if (Enquiry2->FirmwareID.MajorVersion == 0)
1633     {
1634       Enquiry2->FirmwareID.MajorVersion =
1635         Controller->V1.Enquiry.MajorFirmwareVersion;
1636       Enquiry2->FirmwareID.MinorVersion =
1637         Controller->V1.Enquiry.MinorFirmwareVersion;
1638       Enquiry2->FirmwareID.FirmwareType = '0';
1639       Enquiry2->FirmwareID.TurnID = 0;
1640     }
1641   sprintf(Controller->FirmwareVersion, "%d.%02d-%c-%02d",
1642           Enquiry2->FirmwareID.MajorVersion, Enquiry2->FirmwareID.MinorVersion,
1643           Enquiry2->FirmwareID.FirmwareType, Enquiry2->FirmwareID.TurnID);
1644   if (!((Controller->FirmwareVersion[0] == '5' &&
1645          strcmp(Controller->FirmwareVersion, "5.06") >= 0) ||
1646         (Controller->FirmwareVersion[0] == '4' &&
1647          strcmp(Controller->FirmwareVersion, "4.06") >= 0) ||
1648         (Controller->FirmwareVersion[0] == '3' &&
1649          strcmp(Controller->FirmwareVersion, "3.51") >= 0) ||
1650         (Controller->FirmwareVersion[0] == '2' &&
1651          strcmp(Controller->FirmwareVersion, FIRMWARE_27X) >= 0)))
1652     {
1653       DAC960_Failure(Controller, "FIRMWARE VERSION VERIFICATION");
1654       DAC960_Error("Firmware Version = '%s'\n", Controller,
1655                    Controller->FirmwareVersion);
1656       free_dma_loaf(Controller->PCIDevice, &local_dma);
1657       return false;
1658     }
1659   /*
1660     Initialize the Controller Channels, Targets, Memory Size, and SAF-TE
1661     Enclosure Management Enabled fields.
1662   */
1663   Controller->Channels = Enquiry2->ActualChannels;
1664   Controller->Targets = Enquiry2->MaxTargets;
1665   Controller->MemorySize = Enquiry2->MemorySize >> 20;
1666   Controller->V1.SAFTE_EnclosureManagementEnabled =
1667     (Enquiry2->FaultManagementType == DAC960_V1_SAFTE);
1668   /*
1669     Initialize the Controller Queue Depth, Driver Queue Depth, Logical Drive
1670     Count, Maximum Blocks per Command, Controller Scatter/Gather Limit, and
1671     Driver Scatter/Gather Limit.  The Driver Queue Depth must be at most one
1672     less than the Controller Queue Depth to allow for an automatic drive
1673     rebuild operation.
1674   */
1675   Controller->ControllerQueueDepth = Controller->V1.Enquiry.MaxCommands;
1676   Controller->DriverQueueDepth = Controller->ControllerQueueDepth - 1;
1677   if (Controller->DriverQueueDepth > DAC960_MaxDriverQueueDepth)
1678     Controller->DriverQueueDepth = DAC960_MaxDriverQueueDepth;
1679   Controller->LogicalDriveCount =
1680     Controller->V1.Enquiry.NumberOfLogicalDrives;
1681   Controller->MaxBlocksPerCommand = Enquiry2->MaxBlocksPerCommand;
1682   Controller->ControllerScatterGatherLimit = Enquiry2->MaxScatterGatherEntries;
1683   Controller->DriverScatterGatherLimit =
1684     Controller->ControllerScatterGatherLimit;
1685   if (Controller->DriverScatterGatherLimit > DAC960_V1_ScatterGatherLimit)
1686     Controller->DriverScatterGatherLimit = DAC960_V1_ScatterGatherLimit;
1687   /*
1688     Initialize the Stripe Size, Segment Size, and Geometry Translation.
1689   */
1690   Controller->V1.StripeSize = Config2->BlocksPerStripe * Config2->BlockFactor
1691                               >> (10 - DAC960_BlockSizeBits);
1692   Controller->V1.SegmentSize = Config2->BlocksPerCacheLine * Config2->BlockFactor
1693                                >> (10 - DAC960_BlockSizeBits);
1694   switch (Config2->DriveGeometry)
1695     {
1696     case DAC960_V1_Geometry_128_32:
1697       Controller->V1.GeometryTranslationHeads = 128;
1698       Controller->V1.GeometryTranslationSectors = 32;
1699       break;
1700     case DAC960_V1_Geometry_255_63:
1701       Controller->V1.GeometryTranslationHeads = 255;
1702       Controller->V1.GeometryTranslationSectors = 63;
1703       break;
1704     default:
1705       free_dma_loaf(Controller->PCIDevice, &local_dma);
1706       return DAC960_Failure(Controller, "CONFIG2 DRIVE GEOMETRY");
1707     }
1708   /*
1709     Initialize the Background Initialization Status.
1710   */
1711   if ((Controller->FirmwareVersion[0] == '4' &&
1712       strcmp(Controller->FirmwareVersion, "4.08") >= 0) ||
1713       (Controller->FirmwareVersion[0] == '5' &&
1714        strcmp(Controller->FirmwareVersion, "5.08") >= 0))
1715     {
1716       Controller->V1.BackgroundInitializationStatusSupported = true;
1717       DAC960_V1_ExecuteType3B(Controller,
1718                               DAC960_V1_BackgroundInitializationControl, 0x20,
1719                               Controller->
1720                                V1.BackgroundInitializationStatusDMA);
1721       memcpy(&Controller->V1.LastBackgroundInitializationStatus,
1722                 Controller->V1.BackgroundInitializationStatus,
1723                 sizeof(DAC960_V1_BackgroundInitializationStatus_T));
1724     }
1725   /*
1726     Initialize the Logical Drive Initially Accessible flag.
1727   */
1728   for (LogicalDriveNumber = 0;
1729        LogicalDriveNumber < Controller->LogicalDriveCount;
1730        LogicalDriveNumber++)
1731     if (Controller->V1.LogicalDriveInformation
1732                        [LogicalDriveNumber].LogicalDriveState !=
1733         DAC960_V1_LogicalDrive_Offline)
1734       Controller->LogicalDriveInitiallyAccessible[LogicalDriveNumber] = true;
1735   Controller->V1.LastRebuildStatus = DAC960_V1_NoRebuildOrCheckInProgress;
1736   free_dma_loaf(Controller->PCIDevice, &local_dma);
1737   return true;
1738 }
1739
1740
1741 /*
1742   DAC960_V2_ReadControllerConfiguration reads the Configuration Information
1743   from DAC960 V2 Firmware Controllers and initializes the Controller structure.
1744 */
1745
1746 static boolean DAC960_V2_ReadControllerConfiguration(DAC960_Controller_T
1747                                                      *Controller)
1748 {
1749   DAC960_V2_ControllerInfo_T *ControllerInfo =
1750                 &Controller->V2.ControllerInformation;
1751   unsigned short LogicalDeviceNumber = 0;
1752   int ModelNameLength;
1753
1754   /* Get data into dma-able area, then copy into permanant location */
1755   if (!DAC960_V2_NewControllerInfo(Controller))
1756     return DAC960_Failure(Controller, "GET CONTROLLER INFO");
1757   memcpy(ControllerInfo, Controller->V2.NewControllerInformation,
1758                         sizeof(DAC960_V2_ControllerInfo_T));
1759          
1760   
1761   if (!DAC960_V2_GeneralInfo(Controller))
1762     return DAC960_Failure(Controller, "GET HEALTH STATUS");
1763
1764   /*
1765     Initialize the Controller Model Name and Full Model Name fields.
1766   */
1767   ModelNameLength = sizeof(ControllerInfo->ControllerName);
1768   if (ModelNameLength > sizeof(Controller->ModelName)-1)
1769     ModelNameLength = sizeof(Controller->ModelName)-1;
1770   memcpy(Controller->ModelName, ControllerInfo->ControllerName,
1771          ModelNameLength);
1772   ModelNameLength--;
1773   while (Controller->ModelName[ModelNameLength] == ' ' ||
1774          Controller->ModelName[ModelNameLength] == '\0')
1775     ModelNameLength--;
1776   Controller->ModelName[++ModelNameLength] = '\0';
1777   strcpy(Controller->FullModelName, "Mylex ");
1778   strcat(Controller->FullModelName, Controller->ModelName);
1779   /*
1780     Initialize the Controller Firmware Version field.
1781   */
1782   sprintf(Controller->FirmwareVersion, "%d.%02d-%02d",
1783           ControllerInfo->FirmwareMajorVersion,
1784           ControllerInfo->FirmwareMinorVersion,
1785           ControllerInfo->FirmwareTurnNumber);
1786   if (ControllerInfo->FirmwareMajorVersion == 6 &&
1787       ControllerInfo->FirmwareMinorVersion == 0 &&
1788       ControllerInfo->FirmwareTurnNumber < 1)
1789     {
1790       DAC960_Info("FIRMWARE VERSION %s DOES NOT PROVIDE THE CONTROLLER\n",
1791                   Controller, Controller->FirmwareVersion);
1792       DAC960_Info("STATUS MONITORING FUNCTIONALITY NEEDED BY THIS DRIVER.\n",
1793                   Controller);
1794       DAC960_Info("PLEASE UPGRADE TO VERSION 6.00-01 OR ABOVE.\n",
1795                   Controller);
1796     }
1797   /*
1798     Initialize the Controller Channels, Targets, and Memory Size.
1799   */
1800   Controller->Channels = ControllerInfo->NumberOfPhysicalChannelsPresent;
1801   Controller->Targets =
1802     ControllerInfo->MaximumTargetsPerChannel
1803                     [ControllerInfo->NumberOfPhysicalChannelsPresent-1];
1804   Controller->MemorySize = ControllerInfo->MemorySizeMB;
1805   /*
1806     Initialize the Controller Queue Depth, Driver Queue Depth, Logical Drive
1807     Count, Maximum Blocks per Command, Controller Scatter/Gather Limit, and
1808     Driver Scatter/Gather Limit.  The Driver Queue Depth must be at most one
1809     less than the Controller Queue Depth to allow for an automatic drive
1810     rebuild operation.
1811   */
1812   Controller->ControllerQueueDepth = ControllerInfo->MaximumParallelCommands;
1813   Controller->DriverQueueDepth = Controller->ControllerQueueDepth - 1;
1814   if (Controller->DriverQueueDepth > DAC960_MaxDriverQueueDepth)
1815     Controller->DriverQueueDepth = DAC960_MaxDriverQueueDepth;
1816   Controller->LogicalDriveCount = ControllerInfo->LogicalDevicesPresent;
1817   Controller->MaxBlocksPerCommand =
1818     ControllerInfo->MaximumDataTransferSizeInBlocks;
1819   Controller->ControllerScatterGatherLimit =
1820     ControllerInfo->MaximumScatterGatherEntries;
1821   Controller->DriverScatterGatherLimit =
1822     Controller->ControllerScatterGatherLimit;
1823   if (Controller->DriverScatterGatherLimit > DAC960_V2_ScatterGatherLimit)
1824     Controller->DriverScatterGatherLimit = DAC960_V2_ScatterGatherLimit;
1825   /*
1826     Initialize the Logical Device Information.
1827   */
1828   while (true)
1829     {
1830       DAC960_V2_LogicalDeviceInfo_T *NewLogicalDeviceInfo =
1831         Controller->V2.NewLogicalDeviceInformation;
1832       DAC960_V2_LogicalDeviceInfo_T *LogicalDeviceInfo;
1833       DAC960_V2_PhysicalDevice_T PhysicalDevice;
1834
1835       if (!DAC960_V2_NewLogicalDeviceInfo(Controller, LogicalDeviceNumber))
1836         break;
1837       LogicalDeviceNumber = NewLogicalDeviceInfo->LogicalDeviceNumber;
1838       if (LogicalDeviceNumber >= DAC960_MaxLogicalDrives) {
1839         DAC960_Error("DAC960: Logical Drive Number %d not supported\n",
1840                        Controller, LogicalDeviceNumber);
1841                 break;
1842       }
1843       if (NewLogicalDeviceInfo->DeviceBlockSizeInBytes != DAC960_BlockSize) {
1844         DAC960_Error("DAC960: Logical Drive Block Size %d not supported\n",
1845               Controller, NewLogicalDeviceInfo->DeviceBlockSizeInBytes);
1846         LogicalDeviceNumber++;
1847         continue;
1848       }
1849       PhysicalDevice.Controller = 0;
1850       PhysicalDevice.Channel = NewLogicalDeviceInfo->Channel;
1851       PhysicalDevice.TargetID = NewLogicalDeviceInfo->TargetID;
1852       PhysicalDevice.LogicalUnit = NewLogicalDeviceInfo->LogicalUnit;
1853       Controller->V2.LogicalDriveToVirtualDevice[LogicalDeviceNumber] =
1854         PhysicalDevice;
1855       if (NewLogicalDeviceInfo->LogicalDeviceState !=
1856           DAC960_V2_LogicalDevice_Offline)
1857         Controller->LogicalDriveInitiallyAccessible[LogicalDeviceNumber] = true;
1858       LogicalDeviceInfo = (DAC960_V2_LogicalDeviceInfo_T *)
1859         kmalloc(sizeof(DAC960_V2_LogicalDeviceInfo_T), GFP_ATOMIC);
1860       if (LogicalDeviceInfo == NULL)
1861         return DAC960_Failure(Controller, "LOGICAL DEVICE ALLOCATION");
1862       Controller->V2.LogicalDeviceInformation[LogicalDeviceNumber] =
1863         LogicalDeviceInfo;
1864       memcpy(LogicalDeviceInfo, NewLogicalDeviceInfo,
1865              sizeof(DAC960_V2_LogicalDeviceInfo_T));
1866       LogicalDeviceNumber++;
1867     }
1868   return true;
1869 }
1870
1871
1872 /*
1873   DAC960_ReportControllerConfiguration reports the Configuration Information
1874   for Controller.
1875 */
1876
1877 static boolean DAC960_ReportControllerConfiguration(DAC960_Controller_T
1878                                                     *Controller)
1879 {
1880   DAC960_Info("Configuring Mylex %s PCI RAID Controller\n",
1881               Controller, Controller->ModelName);
1882   DAC960_Info("  Firmware Version: %s, Channels: %d, Memory Size: %dMB\n",
1883               Controller, Controller->FirmwareVersion,
1884               Controller->Channels, Controller->MemorySize);
1885   DAC960_Info("  PCI Bus: %d, Device: %d, Function: %d, I/O Address: ",
1886               Controller, Controller->Bus,
1887               Controller->Device, Controller->Function);
1888   if (Controller->IO_Address == 0)
1889     DAC960_Info("Unassigned\n", Controller);
1890   else DAC960_Info("0x%X\n", Controller, Controller->IO_Address);
1891   DAC960_Info("  PCI Address: 0x%X mapped at 0x%lX, IRQ Channel: %d\n",
1892               Controller, Controller->PCI_Address,
1893               (unsigned long) Controller->BaseAddress,
1894               Controller->IRQ_Channel);
1895   DAC960_Info("  Controller Queue Depth: %d, "
1896               "Maximum Blocks per Command: %d\n",
1897               Controller, Controller->ControllerQueueDepth,
1898               Controller->MaxBlocksPerCommand);
1899   DAC960_Info("  Driver Queue Depth: %d, "
1900               "Scatter/Gather Limit: %d of %d Segments\n",
1901               Controller, Controller->DriverQueueDepth,
1902               Controller->DriverScatterGatherLimit,
1903               Controller->ControllerScatterGatherLimit);
1904   if (Controller->FirmwareType == DAC960_V1_Controller)
1905     {
1906       DAC960_Info("  Stripe Size: %dKB, Segment Size: %dKB, "
1907                   "BIOS Geometry: %d/%d\n", Controller,
1908                   Controller->V1.StripeSize,
1909                   Controller->V1.SegmentSize,
1910                   Controller->V1.GeometryTranslationHeads,
1911                   Controller->V1.GeometryTranslationSectors);
1912       if (Controller->V1.SAFTE_EnclosureManagementEnabled)
1913         DAC960_Info("  SAF-TE Enclosure Management Enabled\n", Controller);
1914     }
1915   return true;
1916 }
1917
1918
1919 /*
1920   DAC960_V1_ReadDeviceConfiguration reads the Device Configuration Information
1921   for DAC960 V1 Firmware Controllers by requesting the SCSI Inquiry and SCSI
1922   Inquiry Unit Serial Number information for each device connected to
1923   Controller.
1924 */
1925
1926 static boolean DAC960_V1_ReadDeviceConfiguration(DAC960_Controller_T
1927                                                  *Controller)
1928 {
1929   struct dma_loaf local_dma;
1930
1931   dma_addr_t DCDBs_dma[DAC960_V1_MaxChannels];
1932   DAC960_V1_DCDB_T *DCDBs_cpu[DAC960_V1_MaxChannels];
1933
1934   dma_addr_t SCSI_Inquiry_dma[DAC960_V1_MaxChannels];
1935   DAC960_SCSI_Inquiry_T *SCSI_Inquiry_cpu[DAC960_V1_MaxChannels];
1936
1937   dma_addr_t SCSI_NewInquiryUnitSerialNumberDMA[DAC960_V1_MaxChannels];
1938   DAC960_SCSI_Inquiry_UnitSerialNumber_T *SCSI_NewInquiryUnitSerialNumberCPU[DAC960_V1_MaxChannels];
1939
1940   struct completion Completions[DAC960_V1_MaxChannels];
1941   unsigned long flags;
1942   int Channel, TargetID;
1943
1944   if (!init_dma_loaf(Controller->PCIDevice, &local_dma, 
1945                 DAC960_V1_MaxChannels*(sizeof(DAC960_V1_DCDB_T) +
1946                         sizeof(DAC960_SCSI_Inquiry_T) +
1947                         sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T))))
1948      return DAC960_Failure(Controller,
1949                         "DMA ALLOCATION FAILED IN ReadDeviceConfiguration"); 
1950    
1951   for (Channel = 0; Channel < Controller->Channels; Channel++) {
1952         DCDBs_cpu[Channel] = slice_dma_loaf(&local_dma,
1953                         sizeof(DAC960_V1_DCDB_T), DCDBs_dma + Channel);
1954         SCSI_Inquiry_cpu[Channel] = slice_dma_loaf(&local_dma,
1955                         sizeof(DAC960_SCSI_Inquiry_T),
1956                         SCSI_Inquiry_dma + Channel);
1957         SCSI_NewInquiryUnitSerialNumberCPU[Channel] = slice_dma_loaf(&local_dma,
1958                         sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T),
1959                         SCSI_NewInquiryUnitSerialNumberDMA + Channel);
1960   }
1961                 
1962   for (TargetID = 0; TargetID < Controller->Targets; TargetID++)
1963     {
1964       /*
1965        * For each channel, submit a probe for a device on that channel.
1966        * The timeout interval for a device that is present is 10 seconds.
1967        * With this approach, the timeout periods can elapse in parallel
1968        * on each channel.
1969        */
1970       for (Channel = 0; Channel < Controller->Channels; Channel++)
1971         {
1972           dma_addr_t NewInquiryStandardDataDMA = SCSI_Inquiry_dma[Channel];
1973           DAC960_V1_DCDB_T *DCDB = DCDBs_cpu[Channel];
1974           dma_addr_t DCDB_dma = DCDBs_dma[Channel];
1975           DAC960_Command_T *Command = Controller->Commands[Channel];
1976           struct completion *Completion = &Completions[Channel];
1977
1978           init_completion(Completion);
1979           DAC960_V1_ClearCommand(Command);
1980           Command->CommandType = DAC960_ImmediateCommand;
1981           Command->Completion = Completion;
1982           Command->V1.CommandMailbox.Type3.CommandOpcode = DAC960_V1_DCDB;
1983           Command->V1.CommandMailbox.Type3.BusAddress = DCDB_dma;
1984           DCDB->Channel = Channel;
1985           DCDB->TargetID = TargetID;
1986           DCDB->Direction = DAC960_V1_DCDB_DataTransferDeviceToSystem;
1987           DCDB->EarlyStatus = false;
1988           DCDB->Timeout = DAC960_V1_DCDB_Timeout_10_seconds;
1989           DCDB->NoAutomaticRequestSense = false;
1990           DCDB->DisconnectPermitted = true;
1991           DCDB->TransferLength = sizeof(DAC960_SCSI_Inquiry_T);
1992           DCDB->BusAddress = NewInquiryStandardDataDMA;
1993           DCDB->CDBLength = 6;
1994           DCDB->TransferLengthHigh4 = 0;
1995           DCDB->SenseLength = sizeof(DCDB->SenseData);
1996           DCDB->CDB[0] = 0x12; /* INQUIRY */
1997           DCDB->CDB[1] = 0; /* EVPD = 0 */
1998           DCDB->CDB[2] = 0; /* Page Code */
1999           DCDB->CDB[3] = 0; /* Reserved */
2000           DCDB->CDB[4] = sizeof(DAC960_SCSI_Inquiry_T);
2001           DCDB->CDB[5] = 0; /* Control */
2002
2003           spin_lock_irqsave(&Controller->queue_lock, flags);
2004           DAC960_QueueCommand(Command);
2005           spin_unlock_irqrestore(&Controller->queue_lock, flags);
2006         }
2007       /*
2008        * Wait for the problems submitted in the previous loop
2009        * to complete.  On the probes that are successful, 
2010        * get the serial number of the device that was found.
2011        */
2012       for (Channel = 0; Channel < Controller->Channels; Channel++)
2013         {
2014           DAC960_SCSI_Inquiry_T *InquiryStandardData =
2015             &Controller->V1.InquiryStandardData[Channel][TargetID];
2016           DAC960_SCSI_Inquiry_T *NewInquiryStandardData = SCSI_Inquiry_cpu[Channel];
2017           dma_addr_t NewInquiryUnitSerialNumberDMA =
2018                         SCSI_NewInquiryUnitSerialNumberDMA[Channel];
2019           DAC960_SCSI_Inquiry_UnitSerialNumber_T *NewInquiryUnitSerialNumber =
2020                         SCSI_NewInquiryUnitSerialNumberCPU[Channel];
2021           DAC960_SCSI_Inquiry_UnitSerialNumber_T *InquiryUnitSerialNumber =
2022             &Controller->V1.InquiryUnitSerialNumber[Channel][TargetID];
2023           DAC960_Command_T *Command = Controller->Commands[Channel];
2024           DAC960_V1_DCDB_T *DCDB = DCDBs_cpu[Channel];
2025           struct completion *Completion = &Completions[Channel];
2026
2027           wait_for_completion(Completion);
2028
2029           if (Command->V1.CommandStatus != DAC960_V1_NormalCompletion) {
2030             memset(InquiryStandardData, 0, sizeof(DAC960_SCSI_Inquiry_T));
2031             InquiryStandardData->PeripheralDeviceType = 0x1F;
2032             continue;
2033           } else
2034             memcpy(InquiryStandardData, NewInquiryStandardData, sizeof(DAC960_SCSI_Inquiry_T));
2035         
2036           /* Preserve Channel and TargetID values from the previous loop */
2037           Command->Completion = Completion;
2038           DCDB->TransferLength = sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T);
2039           DCDB->BusAddress = NewInquiryUnitSerialNumberDMA;
2040           DCDB->SenseLength = sizeof(DCDB->SenseData);
2041           DCDB->CDB[0] = 0x12; /* INQUIRY */
2042           DCDB->CDB[1] = 1; /* EVPD = 1 */
2043           DCDB->CDB[2] = 0x80; /* Page Code */
2044           DCDB->CDB[3] = 0; /* Reserved */
2045           DCDB->CDB[4] = sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T);
2046           DCDB->CDB[5] = 0; /* Control */
2047
2048           spin_lock_irqsave(&Controller->queue_lock, flags);
2049           DAC960_QueueCommand(Command);
2050           spin_unlock_irqrestore(&Controller->queue_lock, flags);
2051           wait_for_completion(Completion);
2052
2053           if (Command->V1.CommandStatus != DAC960_V1_NormalCompletion) {
2054                 memset(InquiryUnitSerialNumber, 0,
2055                         sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T));
2056                 InquiryUnitSerialNumber->PeripheralDeviceType = 0x1F;
2057           } else
2058                 memcpy(InquiryUnitSerialNumber, NewInquiryUnitSerialNumber,
2059                         sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T));
2060         }
2061     }
2062     free_dma_loaf(Controller->PCIDevice, &local_dma);
2063   return true;
2064 }
2065
2066
2067 /*
2068   DAC960_V2_ReadDeviceConfiguration reads the Device Configuration Information
2069   for DAC960 V2 Firmware Controllers by requesting the Physical Device
2070   Information and SCSI Inquiry Unit Serial Number information for each
2071   device connected to Controller.
2072 */
2073
2074 static boolean DAC960_V2_ReadDeviceConfiguration(DAC960_Controller_T
2075                                                  *Controller)
2076 {
2077   unsigned char Channel = 0, TargetID = 0, LogicalUnit = 0;
2078   unsigned short PhysicalDeviceIndex = 0;
2079
2080   while (true)
2081     {
2082       DAC960_V2_PhysicalDeviceInfo_T *NewPhysicalDeviceInfo =
2083                 Controller->V2.NewPhysicalDeviceInformation;
2084       DAC960_V2_PhysicalDeviceInfo_T *PhysicalDeviceInfo;
2085       DAC960_SCSI_Inquiry_UnitSerialNumber_T *NewInquiryUnitSerialNumber =
2086                 Controller->V2.NewInquiryUnitSerialNumber;
2087       DAC960_SCSI_Inquiry_UnitSerialNumber_T *InquiryUnitSerialNumber;
2088
2089       if (!DAC960_V2_NewPhysicalDeviceInfo(Controller, Channel, TargetID, LogicalUnit))
2090           break;
2091
2092       PhysicalDeviceInfo = (DAC960_V2_PhysicalDeviceInfo_T *)
2093                 kmalloc(sizeof(DAC960_V2_PhysicalDeviceInfo_T), GFP_ATOMIC);
2094       if (PhysicalDeviceInfo == NULL)
2095                 return DAC960_Failure(Controller, "PHYSICAL DEVICE ALLOCATION");
2096       Controller->V2.PhysicalDeviceInformation[PhysicalDeviceIndex] =
2097                 PhysicalDeviceInfo;
2098       memcpy(PhysicalDeviceInfo, NewPhysicalDeviceInfo,
2099                 sizeof(DAC960_V2_PhysicalDeviceInfo_T));
2100
2101       InquiryUnitSerialNumber = (DAC960_SCSI_Inquiry_UnitSerialNumber_T *)
2102         kmalloc(sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T), GFP_ATOMIC);
2103       if (InquiryUnitSerialNumber == NULL) {
2104         kfree(PhysicalDeviceInfo);
2105         return DAC960_Failure(Controller, "SERIAL NUMBER ALLOCATION");
2106       }
2107       Controller->V2.InquiryUnitSerialNumber[PhysicalDeviceIndex] =
2108                 InquiryUnitSerialNumber;
2109
2110       Channel = NewPhysicalDeviceInfo->Channel;
2111       TargetID = NewPhysicalDeviceInfo->TargetID;
2112       LogicalUnit = NewPhysicalDeviceInfo->LogicalUnit;
2113
2114       /*
2115          Some devices do NOT have Unit Serial Numbers.
2116          This command fails for them.  But, we still want to
2117          remember those devices are there.  Construct a
2118          UnitSerialNumber structure for the failure case.
2119       */
2120       if (!DAC960_V2_NewInquiryUnitSerialNumber(Controller, Channel, TargetID, LogicalUnit)) {
2121         memset(InquiryUnitSerialNumber, 0,
2122              sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T));
2123         InquiryUnitSerialNumber->PeripheralDeviceType = 0x1F;
2124       } else
2125         memcpy(InquiryUnitSerialNumber, NewInquiryUnitSerialNumber,
2126                 sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T));
2127
2128       PhysicalDeviceIndex++;
2129       LogicalUnit++;
2130     }
2131   return true;
2132 }
2133
2134
2135 /*
2136   DAC960_SanitizeInquiryData sanitizes the Vendor, Model, Revision, and
2137   Product Serial Number fields of the Inquiry Standard Data and Inquiry
2138   Unit Serial Number structures.
2139 */
2140
2141 static void DAC960_SanitizeInquiryData(DAC960_SCSI_Inquiry_T
2142                                          *InquiryStandardData,
2143                                        DAC960_SCSI_Inquiry_UnitSerialNumber_T
2144                                          *InquiryUnitSerialNumber,
2145                                        unsigned char *Vendor,
2146                                        unsigned char *Model,
2147                                        unsigned char *Revision,
2148                                        unsigned char *SerialNumber)
2149 {
2150   int SerialNumberLength, i;
2151   if (InquiryStandardData->PeripheralDeviceType == 0x1F) return;
2152   for (i = 0; i < sizeof(InquiryStandardData->VendorIdentification); i++)
2153     {
2154       unsigned char VendorCharacter =
2155         InquiryStandardData->VendorIdentification[i];
2156       Vendor[i] = (VendorCharacter >= ' ' && VendorCharacter <= '~'
2157                    ? VendorCharacter : ' ');
2158     }
2159   Vendor[sizeof(InquiryStandardData->VendorIdentification)] = '\0';
2160   for (i = 0; i < sizeof(InquiryStandardData->ProductIdentification); i++)
2161     {
2162       unsigned char ModelCharacter =
2163         InquiryStandardData->ProductIdentification[i];
2164       Model[i] = (ModelCharacter >= ' ' && ModelCharacter <= '~'
2165                   ? ModelCharacter : ' ');
2166     }
2167   Model[sizeof(InquiryStandardData->ProductIdentification)] = '\0';
2168   for (i = 0; i < sizeof(InquiryStandardData->ProductRevisionLevel); i++)
2169     {
2170       unsigned char RevisionCharacter =
2171         InquiryStandardData->ProductRevisionLevel[i];
2172       Revision[i] = (RevisionCharacter >= ' ' && RevisionCharacter <= '~'
2173                      ? RevisionCharacter : ' ');
2174     }
2175   Revision[sizeof(InquiryStandardData->ProductRevisionLevel)] = '\0';
2176   if (InquiryUnitSerialNumber->PeripheralDeviceType == 0x1F) return;
2177   SerialNumberLength = InquiryUnitSerialNumber->PageLength;
2178   if (SerialNumberLength >
2179       sizeof(InquiryUnitSerialNumber->ProductSerialNumber))
2180     SerialNumberLength = sizeof(InquiryUnitSerialNumber->ProductSerialNumber);
2181   for (i = 0; i < SerialNumberLength; i++)
2182     {
2183       unsigned char SerialNumberCharacter =
2184         InquiryUnitSerialNumber->ProductSerialNumber[i];
2185       SerialNumber[i] =
2186         (SerialNumberCharacter >= ' ' && SerialNumberCharacter <= '~'
2187          ? SerialNumberCharacter : ' ');
2188     }
2189   SerialNumber[SerialNumberLength] = '\0';
2190 }
2191
2192
2193 /*
2194   DAC960_V1_ReportDeviceConfiguration reports the Device Configuration
2195   Information for DAC960 V1 Firmware Controllers.
2196 */
2197
2198 static boolean DAC960_V1_ReportDeviceConfiguration(DAC960_Controller_T
2199                                                    *Controller)
2200 {
2201   int LogicalDriveNumber, Channel, TargetID;
2202   DAC960_Info("  Physical Devices:\n", Controller);
2203   for (Channel = 0; Channel < Controller->Channels; Channel++)
2204     for (TargetID = 0; TargetID < Controller->Targets; TargetID++)
2205       {
2206         DAC960_SCSI_Inquiry_T *InquiryStandardData =
2207           &Controller->V1.InquiryStandardData[Channel][TargetID];
2208         DAC960_SCSI_Inquiry_UnitSerialNumber_T *InquiryUnitSerialNumber =
2209           &Controller->V1.InquiryUnitSerialNumber[Channel][TargetID];
2210         DAC960_V1_DeviceState_T *DeviceState =
2211           &Controller->V1.DeviceState[Channel][TargetID];
2212         DAC960_V1_ErrorTableEntry_T *ErrorEntry =
2213           &Controller->V1.ErrorTable.ErrorTableEntries[Channel][TargetID];
2214         char Vendor[1+sizeof(InquiryStandardData->VendorIdentification)];
2215         char Model[1+sizeof(InquiryStandardData->ProductIdentification)];
2216         char Revision[1+sizeof(InquiryStandardData->ProductRevisionLevel)];
2217         char SerialNumber[1+sizeof(InquiryUnitSerialNumber
2218                                    ->ProductSerialNumber)];
2219         if (InquiryStandardData->PeripheralDeviceType == 0x1F) continue;
2220         DAC960_SanitizeInquiryData(InquiryStandardData, InquiryUnitSerialNumber,
2221                                    Vendor, Model, Revision, SerialNumber);
2222         DAC960_Info("    %d:%d%s Vendor: %s  Model: %s  Revision: %s\n",
2223                     Controller, Channel, TargetID, (TargetID < 10 ? " " : ""),
2224                     Vendor, Model, Revision);
2225         if (InquiryUnitSerialNumber->PeripheralDeviceType != 0x1F)
2226           DAC960_Info("         Serial Number: %s\n", Controller, SerialNumber);
2227         if (DeviceState->Present &&
2228             DeviceState->DeviceType == DAC960_V1_DiskType)
2229           {
2230             if (Controller->V1.DeviceResetCount[Channel][TargetID] > 0)
2231               DAC960_Info("         Disk Status: %s, %u blocks, %d resets\n",
2232                           Controller,
2233                           (DeviceState->DeviceState == DAC960_V1_Device_Dead
2234                            ? "Dead"
2235                            : DeviceState->DeviceState
2236                              == DAC960_V1_Device_WriteOnly
2237                              ? "Write-Only"
2238                              : DeviceState->DeviceState
2239                                == DAC960_V1_Device_Online
2240                                ? "Online" : "Standby"),
2241                           DeviceState->DiskSize,
2242                           Controller->V1.DeviceResetCount[Channel][TargetID]);
2243             else
2244               DAC960_Info("         Disk Status: %s, %u blocks\n", Controller,
2245                           (DeviceState->DeviceState == DAC960_V1_Device_Dead
2246                            ? "Dead"
2247                            : DeviceState->DeviceState
2248                              == DAC960_V1_Device_WriteOnly
2249                              ? "Write-Only"
2250                              : DeviceState->DeviceState
2251                                == DAC960_V1_Device_Online
2252                                ? "Online" : "Standby"),
2253                           DeviceState->DiskSize);
2254           }
2255         if (ErrorEntry->ParityErrorCount > 0 ||
2256             ErrorEntry->SoftErrorCount > 0 ||
2257             ErrorEntry->HardErrorCount > 0 ||
2258             ErrorEntry->MiscErrorCount > 0)
2259           DAC960_Info("         Errors - Parity: %d, Soft: %d, "
2260                       "Hard: %d, Misc: %d\n", Controller,
2261                       ErrorEntry->ParityErrorCount,
2262                       ErrorEntry->SoftErrorCount,
2263                       ErrorEntry->HardErrorCount,
2264                       ErrorEntry->MiscErrorCount);
2265       }
2266   DAC960_Info("  Logical Drives:\n", Controller);
2267   for (LogicalDriveNumber = 0;
2268        LogicalDriveNumber < Controller->LogicalDriveCount;
2269        LogicalDriveNumber++)
2270     {
2271       DAC960_V1_LogicalDriveInformation_T *LogicalDriveInformation =
2272         &Controller->V1.LogicalDriveInformation[LogicalDriveNumber];
2273       DAC960_Info("    /dev/rd/c%dd%d: RAID-%d, %s, %u blocks, %s\n",
2274                   Controller, Controller->ControllerNumber, LogicalDriveNumber,
2275                   LogicalDriveInformation->RAIDLevel,
2276                   (LogicalDriveInformation->LogicalDriveState
2277                    == DAC960_V1_LogicalDrive_Online
2278                    ? "Online"
2279                    : LogicalDriveInformation->LogicalDriveState
2280                      == DAC960_V1_LogicalDrive_Critical
2281                      ? "Critical" : "Offline"),
2282                   LogicalDriveInformation->LogicalDriveSize,
2283                   (LogicalDriveInformation->WriteBack
2284                    ? "Write Back" : "Write Thru"));
2285     }
2286   return true;
2287 }
2288
2289
2290 /*
2291   DAC960_V2_ReportDeviceConfiguration reports the Device Configuration
2292   Information for DAC960 V2 Firmware Controllers.
2293 */
2294
2295 static boolean DAC960_V2_ReportDeviceConfiguration(DAC960_Controller_T
2296                                                    *Controller)
2297 {
2298   int PhysicalDeviceIndex, LogicalDriveNumber;
2299   DAC960_Info("  Physical Devices:\n", Controller);
2300   for (PhysicalDeviceIndex = 0;
2301        PhysicalDeviceIndex < DAC960_V2_MaxPhysicalDevices;
2302        PhysicalDeviceIndex++)
2303     {
2304       DAC960_V2_PhysicalDeviceInfo_T *PhysicalDeviceInfo =
2305         Controller->V2.PhysicalDeviceInformation[PhysicalDeviceIndex];
2306       DAC960_SCSI_Inquiry_T *InquiryStandardData =
2307         (DAC960_SCSI_Inquiry_T *) &PhysicalDeviceInfo->SCSI_InquiryData;
2308       DAC960_SCSI_Inquiry_UnitSerialNumber_T *InquiryUnitSerialNumber =
2309         Controller->V2.InquiryUnitSerialNumber[PhysicalDeviceIndex];
2310       char Vendor[1+sizeof(InquiryStandardData->VendorIdentification)];
2311       char Model[1+sizeof(InquiryStandardData->ProductIdentification)];
2312       char Revision[1+sizeof(InquiryStandardData->ProductRevisionLevel)];
2313       char SerialNumber[1+sizeof(InquiryUnitSerialNumber->ProductSerialNumber)];
2314       if (PhysicalDeviceInfo == NULL) break;
2315       DAC960_SanitizeInquiryData(InquiryStandardData, InquiryUnitSerialNumber,
2316                                  Vendor, Model, Revision, SerialNumber);
2317       DAC960_Info("    %d:%d%s Vendor: %s  Model: %s  Revision: %s\n",
2318                   Controller,
2319                   PhysicalDeviceInfo->Channel,
2320                   PhysicalDeviceInfo->TargetID,
2321                   (PhysicalDeviceInfo->TargetID < 10 ? " " : ""),
2322                   Vendor, Model, Revision);
2323       if (PhysicalDeviceInfo->NegotiatedSynchronousMegaTransfers == 0)
2324         DAC960_Info("         %sAsynchronous\n", Controller,
2325                     (PhysicalDeviceInfo->NegotiatedDataWidthBits == 16
2326                      ? "Wide " :""));
2327       else
2328         DAC960_Info("         %sSynchronous at %d MB/sec\n", Controller,
2329                     (PhysicalDeviceInfo->NegotiatedDataWidthBits == 16
2330                      ? "Wide " :""),
2331                     (PhysicalDeviceInfo->NegotiatedSynchronousMegaTransfers
2332                      * PhysicalDeviceInfo->NegotiatedDataWidthBits/8));
2333       if (InquiryUnitSerialNumber->PeripheralDeviceType != 0x1F)
2334         DAC960_Info("         Serial Number: %s\n", Controller, SerialNumber);
2335       if (PhysicalDeviceInfo->PhysicalDeviceState ==
2336           DAC960_V2_Device_Unconfigured)
2337         continue;
2338       DAC960_Info("         Disk Status: %s, %u blocks\n", Controller,
2339                   (PhysicalDeviceInfo->PhysicalDeviceState
2340                    == DAC960_V2_Device_Online
2341                    ? "Online"
2342                    : PhysicalDeviceInfo->PhysicalDeviceState
2343                      == DAC960_V2_Device_Rebuild
2344                      ? "Rebuild"
2345                      : PhysicalDeviceInfo->PhysicalDeviceState
2346                        == DAC960_V2_Device_Missing
2347                        ? "Missing"
2348                        : PhysicalDeviceInfo->PhysicalDeviceState
2349                          == DAC960_V2_Device_Critical
2350                          ? "Critical"
2351                          : PhysicalDeviceInfo->PhysicalDeviceState
2352                            == DAC960_V2_Device_Dead
2353                            ? "Dead"
2354                            : PhysicalDeviceInfo->PhysicalDeviceState
2355                              == DAC960_V2_Device_SuspectedDead
2356                              ? "Suspected-Dead"
2357                              : PhysicalDeviceInfo->PhysicalDeviceState
2358                                == DAC960_V2_Device_CommandedOffline
2359                                ? "Commanded-Offline"
2360                                : PhysicalDeviceInfo->PhysicalDeviceState
2361                                  == DAC960_V2_Device_Standby
2362                                  ? "Standby" : "Unknown"),
2363                   PhysicalDeviceInfo->ConfigurableDeviceSize);
2364       if (PhysicalDeviceInfo->ParityErrors == 0 &&
2365           PhysicalDeviceInfo->SoftErrors == 0 &&
2366           PhysicalDeviceInfo->HardErrors == 0 &&
2367           PhysicalDeviceInfo->MiscellaneousErrors == 0 &&
2368           PhysicalDeviceInfo->CommandTimeouts == 0 &&
2369           PhysicalDeviceInfo->Retries == 0 &&
2370           PhysicalDeviceInfo->Aborts == 0 &&
2371           PhysicalDeviceInfo->PredictedFailuresDetected == 0)
2372         continue;
2373       DAC960_Info("         Errors - Parity: %d, Soft: %d, "
2374                   "Hard: %d, Misc: %d\n", Controller,
2375                   PhysicalDeviceInfo->ParityErrors,
2376                   PhysicalDeviceInfo->SoftErrors,
2377                   PhysicalDeviceInfo->HardErrors,
2378                   PhysicalDeviceInfo->MiscellaneousErrors);
2379       DAC960_Info("                  Timeouts: %d, Retries: %d, "
2380                   "Aborts: %d, Predicted: %d\n", Controller,
2381                   PhysicalDeviceInfo->CommandTimeouts,
2382                   PhysicalDeviceInfo->Retries,
2383                   PhysicalDeviceInfo->Aborts,
2384                   PhysicalDeviceInfo->PredictedFailuresDetected);
2385     }
2386   DAC960_Info("  Logical Drives:\n", Controller);
2387   for (LogicalDriveNumber = 0;
2388        LogicalDriveNumber < DAC960_MaxLogicalDrives;
2389        LogicalDriveNumber++)
2390     {
2391       DAC960_V2_LogicalDeviceInfo_T *LogicalDeviceInfo =
2392         Controller->V2.LogicalDeviceInformation[LogicalDriveNumber];
2393       unsigned char *ReadCacheStatus[] = { "Read Cache Disabled",
2394                                            "Read Cache Enabled",
2395                                            "Read Ahead Enabled",
2396                                            "Intelligent Read Ahead Enabled",
2397                                            "-", "-", "-", "-" };
2398       unsigned char *WriteCacheStatus[] = { "Write Cache Disabled",
2399                                             "Logical Device Read Only",
2400                                             "Write Cache Enabled",
2401                                             "Intelligent Write Cache Enabled",
2402                                             "-", "-", "-", "-" };
2403       unsigned char *GeometryTranslation;
2404       if (LogicalDeviceInfo == NULL) continue;
2405       switch (LogicalDeviceInfo->DriveGeometry)
2406         {
2407         case DAC960_V2_Geometry_128_32:
2408           GeometryTranslation = "128/32";
2409           break;
2410         case DAC960_V2_Geometry_255_63:
2411           GeometryTranslation = "255/63";
2412           break;
2413         default:
2414           GeometryTranslation = "Invalid";
2415           DAC960_Error("Illegal Logical Device Geometry %d\n",
2416                        Controller, LogicalDeviceInfo->DriveGeometry);
2417           break;
2418         }
2419       DAC960_Info("    /dev/rd/c%dd%d: RAID-%d, %s, %u blocks\n",
2420                   Controller, Controller->ControllerNumber, LogicalDriveNumber,
2421                   LogicalDeviceInfo->RAIDLevel,
2422                   (LogicalDeviceInfo->LogicalDeviceState
2423                    == DAC960_V2_LogicalDevice_Online
2424                    ? "Online"
2425                    : LogicalDeviceInfo->LogicalDeviceState
2426                      == DAC960_V2_LogicalDevice_Critical
2427                      ? "Critical" : "Offline"),
2428                   LogicalDeviceInfo->ConfigurableDeviceSize);
2429       DAC960_Info("                  Logical Device %s, BIOS Geometry: %s\n",
2430                   Controller,
2431                   (LogicalDeviceInfo->LogicalDeviceControl
2432                                      .LogicalDeviceInitialized
2433                    ? "Initialized" : "Uninitialized"),
2434                   GeometryTranslation);
2435       if (LogicalDeviceInfo->StripeSize == 0)
2436         {
2437           if (LogicalDeviceInfo->CacheLineSize == 0)
2438             DAC960_Info("                  Stripe Size: N/A, "
2439                         "Segment Size: N/A\n", Controller);
2440           else
2441             DAC960_Info("                  Stripe Size: N/A, "
2442                         "Segment Size: %dKB\n", Controller,
2443                         1 << (LogicalDeviceInfo->CacheLineSize - 2));
2444         }
2445       else
2446         {
2447           if (LogicalDeviceInfo->CacheLineSize == 0)
2448             DAC960_Info("                  Stripe Size: %dKB, "
2449                         "Segment Size: N/A\n", Controller,
2450                         1 << (LogicalDeviceInfo->StripeSize - 2));
2451           else
2452             DAC960_Info("                  Stripe Size: %dKB, "
2453                         "Segment Size: %dKB\n", Controller,
2454                         1 << (LogicalDeviceInfo->StripeSize - 2),
2455                         1 << (LogicalDeviceInfo->CacheLineSize - 2));
2456         }
2457       DAC960_Info("                  %s, %s\n", Controller,
2458                   ReadCacheStatus[
2459                     LogicalDeviceInfo->LogicalDeviceControl.ReadCache],
2460                   WriteCacheStatus[
2461                     LogicalDeviceInfo->LogicalDeviceControl.WriteCache]);
2462       if (LogicalDeviceInfo->SoftErrors > 0 ||
2463           LogicalDeviceInfo->CommandsFailed > 0 ||
2464           LogicalDeviceInfo->DeferredWriteErrors)
2465         DAC960_Info("                  Errors - Soft: %d, Failed: %d, "
2466                     "Deferred Write: %d\n", Controller,
2467                     LogicalDeviceInfo->SoftErrors,
2468                     LogicalDeviceInfo->CommandsFailed,
2469                     LogicalDeviceInfo->DeferredWriteErrors);
2470
2471     }
2472   return true;
2473 }
2474
2475 /*
2476   DAC960_RegisterBlockDevice registers the Block Device structures
2477   associated with Controller.
2478 */
2479
2480 static boolean DAC960_RegisterBlockDevice(DAC960_Controller_T *Controller)
2481 {
2482   int MajorNumber = DAC960_MAJOR + Controller->ControllerNumber;
2483   int n;
2484
2485   /*
2486     Register the Block Device Major Number for this DAC960 Controller.
2487   */
2488   if (register_blkdev(MajorNumber, "dac960") < 0)
2489       return false;
2490
2491   for (n = 0; n < DAC960_MaxLogicalDrives; n++) {
2492         struct gendisk *disk = Controller->disks[n];
2493         struct request_queue *RequestQueue;
2494
2495         /* for now, let all request queues share controller's lock */
2496         RequestQueue = blk_init_queue(DAC960_RequestFunction,&Controller->queue_lock);
2497         if (!RequestQueue) {
2498                 printk("DAC960: failure to allocate request queue\n");
2499                 continue;
2500         }
2501         Controller->RequestQueue[n] = RequestQueue;
2502         blk_queue_bounce_limit(RequestQueue, Controller->BounceBufferLimit);
2503         RequestQueue->queuedata = Controller;
2504         blk_queue_max_hw_segments(RequestQueue, Controller->DriverScatterGatherLimit);
2505         blk_queue_max_phys_segments(RequestQueue, Controller->DriverScatterGatherLimit);
2506         blk_queue_max_sectors(RequestQueue, Controller->MaxBlocksPerCommand);
2507         disk->queue = RequestQueue;
2508         sprintf(disk->disk_name, "rd/c%dd%d", Controller->ControllerNumber, n);
2509         sprintf(disk->devfs_name, "rd/host%d/target%d", Controller->ControllerNumber, n);
2510         disk->major = MajorNumber;
2511         disk->first_minor = n << DAC960_MaxPartitionsBits;
2512         disk->fops = &DAC960_BlockDeviceOperations;
2513    }
2514   /*
2515     Indicate the Block Device Registration completed successfully,
2516   */
2517   return true;
2518 }
2519
2520
2521 /*
2522   DAC960_UnregisterBlockDevice unregisters the Block Device structures
2523   associated with Controller.
2524 */
2525
2526 static void DAC960_UnregisterBlockDevice(DAC960_Controller_T *Controller)
2527 {
2528   int MajorNumber = DAC960_MAJOR + Controller->ControllerNumber;
2529   int disk;
2530
2531   /* does order matter when deleting gendisk and cleanup in request queue? */
2532   for (disk = 0; disk < DAC960_MaxLogicalDrives; disk++) {
2533         del_gendisk(Controller->disks[disk]);
2534         blk_cleanup_queue(Controller->RequestQueue[disk]);
2535         Controller->RequestQueue[disk] = NULL;
2536   }
2537
2538   /*
2539     Unregister the Block Device Major Number for this DAC960 Controller.
2540   */
2541   unregister_blkdev(MajorNumber, "dac960");
2542 }
2543
2544 /*
2545   DAC960_ComputeGenericDiskInfo computes the values for the Generic Disk
2546   Information Partition Sector Counts and Block Sizes.
2547 */
2548
2549 static void DAC960_ComputeGenericDiskInfo(DAC960_Controller_T *Controller)
2550 {
2551         int disk;
2552         for (disk = 0; disk < DAC960_MaxLogicalDrives; disk++)
2553                 set_capacity(Controller->disks[disk], disk_size(Controller, disk));
2554 }
2555
2556 /*
2557   DAC960_ReportErrorStatus reports Controller BIOS Messages passed through
2558   the Error Status Register when the driver performs the BIOS handshaking.
2559   It returns true for fatal errors and false otherwise.
2560 */
2561
2562 static boolean DAC960_ReportErrorStatus(DAC960_Controller_T *Controller,
2563                                         unsigned char ErrorStatus,
2564                                         unsigned char Parameter0,
2565                                         unsigned char Parameter1)
2566 {
2567   switch (ErrorStatus)
2568     {
2569     case 0x00:
2570       DAC960_Notice("Physical Device %d:%d Not Responding\n",
2571                     Controller, Parameter1, Parameter0);
2572       break;
2573     case 0x08:
2574       if (Controller->DriveSpinUpMessageDisplayed) break;
2575       DAC960_Notice("Spinning Up Drives\n", Controller);
2576       Controller->DriveSpinUpMessageDisplayed = true;
2577       break;
2578     case 0x30:
2579       DAC960_Notice("Configuration Checksum Error\n", Controller);
2580       break;
2581     case 0x60:
2582       DAC960_Notice("Mirror Race Recovery Failed\n", Controller);
2583       break;
2584     case 0x70:
2585       DAC960_Notice("Mirror Race Recovery In Progress\n", Controller);
2586       break;
2587     case 0x90:
2588       DAC960_Notice("Physical Device %d:%d COD Mismatch\n",
2589                     Controller, Parameter1, Parameter0);
2590       break;
2591     case 0xA0:
2592       DAC960_Notice("Logical Drive Installation Aborted\n", Controller);
2593       break;
2594     case 0xB0:
2595       DAC960_Notice("Mirror Race On A Critical Logical Drive\n", Controller);
2596       break;
2597     case 0xD0:
2598       DAC960_Notice("New Controller Configuration Found\n", Controller);
2599       break;
2600     case 0xF0:
2601       DAC960_Error("Fatal Memory Parity Error for Controller at\n", Controller);
2602       return true;
2603     default:
2604       DAC960_Error("Unknown Initialization Error %02X for Controller at\n",
2605                    Controller, ErrorStatus);
2606       return true;
2607     }
2608   return false;
2609 }
2610
2611
2612 /*
2613  * DAC960_DetectCleanup releases the resources that were allocated
2614  * during DAC960_DetectController().  DAC960_DetectController can
2615  * has several internal failure points, so not ALL resources may 
2616  * have been allocated.  It's important to free only
2617  * resources that HAVE been allocated.  The code below always
2618  * tests that the resource has been allocated before attempting to
2619  * free it.
2620  */
2621 static void DAC960_DetectCleanup(DAC960_Controller_T *Controller)
2622 {
2623   int i;
2624
2625   /* Free the memory mailbox, status, and related structures */
2626   free_dma_loaf(Controller->PCIDevice, &Controller->DmaPages);
2627   if (Controller->MemoryMappedAddress) {
2628         switch(Controller->HardwareType)
2629         {
2630                 case DAC960_BA_Controller:
2631                         DAC960_BA_DisableInterrupts(Controller->BaseAddress);
2632                         break;
2633                 case DAC960_LP_Controller:
2634                         DAC960_LP_DisableInterrupts(Controller->BaseAddress);
2635                         break;
2636                 case DAC960_LA_Controller:
2637                         DAC960_LA_DisableInterrupts(Controller->BaseAddress);
2638                         break;
2639                 case DAC960_PG_Controller:
2640                         DAC960_PG_DisableInterrupts(Controller->BaseAddress);
2641                         break;
2642                 case DAC960_PD_Controller:
2643                         DAC960_PD_DisableInterrupts(Controller->BaseAddress);
2644                         break;
2645                 case DAC960_P_Controller:
2646                         DAC960_PD_DisableInterrupts(Controller->BaseAddress);
2647                         break;
2648         }
2649         iounmap(Controller->MemoryMappedAddress);
2650   }
2651   if (Controller->IRQ_Channel)
2652         free_irq(Controller->IRQ_Channel, Controller);
2653   if (Controller->IO_Address)
2654         release_region(Controller->IO_Address, 0x80);
2655   pci_disable_device(Controller->PCIDevice);
2656   for (i = 0; (i < DAC960_MaxLogicalDrives) && Controller->disks[i]; i++)
2657        put_disk(Controller->disks[i]);
2658   DAC960_Controllers[Controller->ControllerNumber] = NULL;
2659   kfree(Controller);
2660 }
2661
2662
2663 /*
2664   DAC960_DetectController detects Mylex DAC960/AcceleRAID/eXtremeRAID
2665   PCI RAID Controllers by interrogating the PCI Configuration Space for
2666   Controller Type.
2667 */
2668
2669 static DAC960_Controller_T * 
2670 DAC960_DetectController(struct pci_dev *PCI_Device,
2671                         const struct pci_device_id *entry)
2672 {
2673   struct DAC960_privdata *privdata =
2674                 (struct DAC960_privdata *)entry->driver_data;
2675   irqreturn_t (*InterruptHandler)(int, void *, struct pt_regs *) =
2676                 privdata->InterruptHandler;
2677   unsigned int MemoryWindowSize = privdata->MemoryWindowSize;
2678   DAC960_Controller_T *Controller = NULL;
2679   unsigned char DeviceFunction = PCI_Device->devfn;
2680   unsigned char ErrorStatus, Parameter0, Parameter1;
2681   unsigned int IRQ_Channel = PCI_Device->irq;
2682   void __iomem *BaseAddress;
2683   int i;
2684
2685   Controller = (DAC960_Controller_T *)
2686         kmalloc(sizeof(DAC960_Controller_T), GFP_ATOMIC);
2687   if (Controller == NULL) {
2688         DAC960_Error("Unable to allocate Controller structure for "
2689                        "Controller at\n", NULL);
2690         return NULL;
2691   }
2692   memset(Controller, 0, sizeof(DAC960_Controller_T));
2693   Controller->ControllerNumber = DAC960_ControllerCount;
2694   DAC960_Controllers[DAC960_ControllerCount++] = Controller;
2695   Controller->Bus = PCI_Device->bus->number;
2696   Controller->FirmwareType = privdata->FirmwareType;
2697   Controller->HardwareType = privdata->HardwareType;
2698   Controller->Device = DeviceFunction >> 3;
2699   Controller->Function = DeviceFunction & 0x7;
2700   Controller->PCIDevice = PCI_Device;
2701   strcpy(Controller->FullModelName, "DAC960");
2702
2703   if (pci_enable_device(PCI_Device))  {
2704         kfree(Controller);
2705         goto Failure;
2706   }
2707
2708   switch (Controller->HardwareType)
2709   {
2710         case DAC960_BA_Controller:
2711           Controller->PCI_Address = pci_resource_start(PCI_Device, 0);
2712           break;
2713         case DAC960_LP_Controller:
2714           Controller->PCI_Address = pci_resource_start(PCI_Device, 0);
2715           break;
2716         case DAC960_LA_Controller:
2717           Controller->PCI_Address = pci_resource_start(PCI_Device, 0);
2718           break;
2719         case DAC960_PG_Controller:
2720           Controller->PCI_Address = pci_resource_start(PCI_Device, 0);
2721           break;
2722         case DAC960_PD_Controller:
2723           Controller->IO_Address = pci_resource_start(PCI_Device, 0);
2724           Controller->PCI_Address = pci_resource_start(PCI_Device, 1);
2725           break;
2726         case DAC960_P_Controller:
2727           Controller->IO_Address = pci_resource_start(PCI_Device, 0);
2728           Controller->PCI_Address = pci_resource_start(PCI_Device, 1);
2729           break;
2730   }
2731
2732   pci_set_drvdata(PCI_Device, (void *)((long)Controller->ControllerNumber));
2733   for (i = 0; i < DAC960_MaxLogicalDrives; i++) {
2734         Controller->disks[i] = alloc_disk(1<<DAC960_MaxPartitionsBits);
2735         if (!Controller->disks[i])
2736                 goto Failure;
2737         Controller->disks[i]->private_data = (void *)((long)i);
2738   }
2739   init_waitqueue_head(&Controller->CommandWaitQueue);
2740   init_waitqueue_head(&Controller->HealthStatusWaitQueue);
2741   Controller->queue_lock = SPIN_LOCK_UNLOCKED; 
2742   DAC960_AnnounceDriver(Controller);
2743   /*
2744     Map the Controller Register Window.
2745   */
2746  if (MemoryWindowSize < PAGE_SIZE)
2747         MemoryWindowSize = PAGE_SIZE;
2748   Controller->MemoryMappedAddress =
2749         ioremap_nocache(Controller->PCI_Address & PAGE_MASK, MemoryWindowSize);
2750   Controller->BaseAddress =
2751         Controller->MemoryMappedAddress + (Controller->PCI_Address & ~PAGE_MASK);
2752   if (Controller->MemoryMappedAddress == NULL)
2753   {
2754           DAC960_Error("Unable to map Controller Register Window for "
2755                        "Controller at\n", Controller);
2756           goto Failure;
2757   }
2758   BaseAddress = Controller->BaseAddress;
2759   switch (Controller->HardwareType)
2760   {
2761         case DAC960_BA_Controller:
2762           DAC960_BA_DisableInterrupts(BaseAddress);
2763           DAC960_BA_AcknowledgeHardwareMailboxStatus(BaseAddress);
2764           udelay(1000);
2765           while (DAC960_BA_InitializationInProgressP(BaseAddress))
2766             {
2767               if (DAC960_BA_ReadErrorStatus(BaseAddress, &ErrorStatus,
2768                                             &Parameter0, &Parameter1) &&
2769                   DAC960_ReportErrorStatus(Controller, ErrorStatus,
2770                                            Parameter0, Parameter1))
2771                 goto Failure;
2772               udelay(10);
2773             }
2774           if (!DAC960_V2_EnableMemoryMailboxInterface(Controller))
2775             {
2776               DAC960_Error("Unable to Enable Memory Mailbox Interface "
2777                            "for Controller at\n", Controller);
2778               goto Failure;
2779             }
2780           DAC960_BA_EnableInterrupts(BaseAddress);
2781           Controller->QueueCommand = DAC960_BA_QueueCommand;
2782           Controller->ReadControllerConfiguration =
2783             DAC960_V2_ReadControllerConfiguration;
2784           Controller->ReadDeviceConfiguration =
2785             DAC960_V2_ReadDeviceConfiguration;
2786           Controller->ReportDeviceConfiguration =
2787             DAC960_V2_ReportDeviceConfiguration;
2788           Controller->QueueReadWriteCommand =
2789             DAC960_V2_QueueReadWriteCommand;
2790           break;
2791         case DAC960_LP_Controller:
2792           DAC960_LP_DisableInterrupts(BaseAddress);
2793           DAC960_LP_AcknowledgeHardwareMailboxStatus(BaseAddress);
2794           udelay(1000);
2795           while (DAC960_LP_InitializationInProgressP(BaseAddress))
2796             {
2797               if (DAC960_LP_ReadErrorStatus(BaseAddress, &ErrorStatus,
2798                                             &Parameter0, &Parameter1) &&
2799                   DAC960_ReportErrorStatus(Controller, ErrorStatus,
2800                                            Parameter0, Parameter1))
2801                 goto Failure;
2802               udelay(10);
2803             }
2804           if (!DAC960_V2_EnableMemoryMailboxInterface(Controller))
2805             {
2806               DAC960_Error("Unable to Enable Memory Mailbox Interface "
2807                            "for Controller at\n", Controller);
2808               goto Failure;
2809             }
2810           DAC960_LP_EnableInterrupts(BaseAddress);
2811           Controller->QueueCommand = DAC960_LP_QueueCommand;
2812           Controller->ReadControllerConfiguration =
2813             DAC960_V2_ReadControllerConfiguration;
2814           Controller->ReadDeviceConfiguration =
2815             DAC960_V2_ReadDeviceConfiguration;
2816           Controller->ReportDeviceConfiguration =
2817             DAC960_V2_ReportDeviceConfiguration;
2818           Controller->QueueReadWriteCommand =
2819             DAC960_V2_QueueReadWriteCommand;
2820           break;
2821         case DAC960_LA_Controller:
2822           DAC960_LA_DisableInterrupts(BaseAddress);
2823           DAC960_LA_AcknowledgeHardwareMailboxStatus(BaseAddress);
2824           udelay(1000);
2825           while (DAC960_LA_InitializationInProgressP(BaseAddress))
2826             {
2827               if (DAC960_LA_ReadErrorStatus(BaseAddress, &ErrorStatus,
2828                                             &Parameter0, &Parameter1) &&
2829                   DAC960_ReportErrorStatus(Controller, ErrorStatus,
2830                                            Parameter0, Parameter1))
2831                 goto Failure;
2832               udelay(10);
2833             }
2834           if (!DAC960_V1_EnableMemoryMailboxInterface(Controller))
2835             {
2836               DAC960_Error("Unable to Enable Memory Mailbox Interface "
2837                            "for Controller at\n", Controller);
2838               goto Failure;
2839             }
2840           DAC960_LA_EnableInterrupts(BaseAddress);
2841           if (Controller->V1.DualModeMemoryMailboxInterface)
2842             Controller->QueueCommand = DAC960_LA_QueueCommandDualMode;
2843           else Controller->QueueCommand = DAC960_LA_QueueCommandSingleMode;
2844           Controller->ReadControllerConfiguration =
2845             DAC960_V1_ReadControllerConfiguration;
2846           Controller->ReadDeviceConfiguration =
2847             DAC960_V1_ReadDeviceConfiguration;
2848           Controller->ReportDeviceConfiguration =
2849             DAC960_V1_ReportDeviceConfiguration;
2850           Controller->QueueReadWriteCommand =
2851             DAC960_V1_QueueReadWriteCommand;
2852           break;
2853         case DAC960_PG_Controller:
2854           DAC960_PG_DisableInterrupts(BaseAddress);
2855           DAC960_PG_AcknowledgeHardwareMailboxStatus(BaseAddress);
2856           udelay(1000);
2857           while (DAC960_PG_InitializationInProgressP(BaseAddress))
2858             {
2859               if (DAC960_PG_ReadErrorStatus(BaseAddress, &ErrorStatus,
2860                                             &Parameter0, &Parameter1) &&
2861                   DAC960_ReportErrorStatus(Controller, ErrorStatus,
2862                                            Parameter0, Parameter1))
2863                 goto Failure;
2864               udelay(10);
2865             }
2866           if (!DAC960_V1_EnableMemoryMailboxInterface(Controller))
2867             {
2868               DAC960_Error("Unable to Enable Memory Mailbox Interface "
2869                            "for Controller at\n", Controller);
2870               goto Failure;
2871             }
2872           DAC960_PG_EnableInterrupts(BaseAddress);
2873           if (Controller->V1.DualModeMemoryMailboxInterface)
2874             Controller->QueueCommand = DAC960_PG_QueueCommandDualMode;
2875           else Controller->QueueCommand = DAC960_PG_QueueCommandSingleMode;
2876           Controller->ReadControllerConfiguration =
2877             DAC960_V1_ReadControllerConfiguration;
2878           Controller->ReadDeviceConfiguration =
2879             DAC960_V1_ReadDeviceConfiguration;
2880           Controller->ReportDeviceConfiguration =
2881             DAC960_V1_ReportDeviceConfiguration;
2882           Controller->QueueReadWriteCommand =
2883             DAC960_V1_QueueReadWriteCommand;
2884           break;
2885         case DAC960_PD_Controller:
2886           if (!request_region(Controller->IO_Address, 0x80,
2887                               Controller->FullModelName)) {
2888                 DAC960_Error("IO port 0x%d busy for Controller at\n",
2889                              Controller, Controller->IO_Address);
2890                 goto Failure;
2891           }
2892           DAC960_PD_DisableInterrupts(BaseAddress);
2893           DAC960_PD_AcknowledgeStatus(BaseAddress);
2894           udelay(1000);
2895           while (DAC960_PD_InitializationInProgressP(BaseAddress))
2896             {
2897               if (DAC960_PD_ReadErrorStatus(BaseAddress, &ErrorStatus,
2898                                             &Parameter0, &Parameter1) &&
2899                   DAC960_ReportErrorStatus(Controller, ErrorStatus,
2900                                            Parameter0, Parameter1))
2901                 goto Failure;
2902               udelay(10);
2903             }
2904           if (!DAC960_V1_EnableMemoryMailboxInterface(Controller))
2905             {
2906               DAC960_Error("Unable to allocate DMA mapped memory "
2907                            "for Controller at\n", Controller);
2908               goto Failure;
2909             }
2910           DAC960_PD_EnableInterrupts(BaseAddress);
2911           Controller->QueueCommand = DAC960_PD_QueueCommand;
2912           Controller->ReadControllerConfiguration =
2913             DAC960_V1_ReadControllerConfiguration;
2914           Controller->ReadDeviceConfiguration =
2915             DAC960_V1_ReadDeviceConfiguration;
2916           Controller->ReportDeviceConfiguration =
2917             DAC960_V1_ReportDeviceConfiguration;
2918           Controller->QueueReadWriteCommand =
2919             DAC960_V1_QueueReadWriteCommand;
2920           break;
2921         case DAC960_P_Controller:
2922           if (!request_region(Controller->IO_Address, 0x80,
2923                               Controller->FullModelName)){
2924                 DAC960_Error("IO port 0x%d busy for Controller at\n",
2925                              Controller, Controller->IO_Address);
2926                 goto Failure;
2927           }
2928           DAC960_PD_DisableInterrupts(BaseAddress);
2929           DAC960_PD_AcknowledgeStatus(BaseAddress);
2930           udelay(1000);
2931           while (DAC960_PD_InitializationInProgressP(BaseAddress))
2932             {
2933               if (DAC960_PD_ReadErrorStatus(BaseAddress, &ErrorStatus,
2934                                             &Parameter0, &Parameter1) &&
2935                   DAC960_ReportErrorStatus(Controller, ErrorStatus,
2936                                            Parameter0, Parameter1))
2937                 goto Failure;
2938               udelay(10);
2939             }
2940           if (!DAC960_V1_EnableMemoryMailboxInterface(Controller))
2941             {
2942               DAC960_Error("Unable to allocate DMA mapped memory"
2943                            "for Controller at\n", Controller);
2944               goto Failure;
2945             }
2946           DAC960_PD_EnableInterrupts(BaseAddress);
2947           Controller->QueueCommand = DAC960_P_QueueCommand;
2948           Controller->ReadControllerConfiguration =
2949             DAC960_V1_ReadControllerConfiguration;
2950           Controller->ReadDeviceConfiguration =
2951             DAC960_V1_ReadDeviceConfiguration;
2952           Controller->ReportDeviceConfiguration =
2953             DAC960_V1_ReportDeviceConfiguration;
2954           Controller->QueueReadWriteCommand =
2955             DAC960_V1_QueueReadWriteCommand;
2956           break;
2957   }
2958   /*
2959      Acquire shared access to the IRQ Channel.
2960   */
2961   if (request_irq(IRQ_Channel, InterruptHandler, SA_SHIRQ,
2962                       Controller->FullModelName, Controller) < 0)
2963   {
2964         DAC960_Error("Unable to acquire IRQ Channel %d for Controller at\n",
2965                        Controller, Controller->IRQ_Channel);
2966         goto Failure;
2967   }
2968   Controller->IRQ_Channel = IRQ_Channel;
2969   Controller->InitialCommand.CommandIdentifier = 1;
2970   Controller->InitialCommand.Controller = Controller;
2971   Controller->Commands[0] = &Controller->InitialCommand;
2972   Controller->FreeCommands = &Controller->InitialCommand;
2973   return Controller;
2974       
2975 Failure:
2976   if (Controller->IO_Address == 0)
2977         DAC960_Error("PCI Bus %d Device %d Function %d I/O Address N/A "
2978                      "PCI Address 0x%X\n", Controller,
2979                      Controller->Bus, Controller->Device,
2980                      Controller->Function, Controller->PCI_Address);
2981   else
2982         DAC960_Error("PCI Bus %d Device %d Function %d I/O Address "
2983                         "0x%X PCI Address 0x%X\n", Controller,
2984                         Controller->Bus, Controller->Device,
2985                         Controller->Function, Controller->IO_Address,
2986                         Controller->PCI_Address);
2987   DAC960_DetectCleanup(Controller);
2988   DAC960_ControllerCount--;
2989   return NULL;
2990 }
2991
2992 /*
2993   DAC960_InitializeController initializes Controller.
2994 */
2995
2996 static boolean 
2997 DAC960_InitializeController(DAC960_Controller_T *Controller)
2998 {
2999   if (DAC960_ReadControllerConfiguration(Controller) &&
3000       DAC960_ReportControllerConfiguration(Controller) &&
3001       DAC960_CreateAuxiliaryStructures(Controller) &&
3002       DAC960_ReadDeviceConfiguration(Controller) &&
3003       DAC960_ReportDeviceConfiguration(Controller) &&
3004       DAC960_RegisterBlockDevice(Controller))
3005     {
3006       /*
3007         Initialize the Monitoring Timer.
3008       */
3009       init_timer(&Controller->MonitoringTimer);
3010       Controller->MonitoringTimer.expires =
3011         jiffies + DAC960_MonitoringTimerInterval;
3012       Controller->MonitoringTimer.data = (unsigned long) Controller;
3013       Controller->MonitoringTimer.function = DAC960_MonitoringTimerFunction;
3014       add_timer(&Controller->MonitoringTimer);
3015       Controller->ControllerInitialized = true;
3016       return true;
3017     }
3018   return false;
3019 }
3020
3021
3022 /*
3023   DAC960_FinalizeController finalizes Controller.
3024 */
3025
3026 static void DAC960_FinalizeController(DAC960_Controller_T *Controller)
3027 {
3028   if (Controller->ControllerInitialized)
3029     {
3030       unsigned long flags;
3031
3032       /*
3033        * Acquiring and releasing lock here eliminates
3034        * a very low probability race.
3035        *
3036        * The code below allocates controller command structures
3037        * from the free list without holding the controller lock.
3038        * This is safe assuming there is no other activity on
3039        * the controller at the time.
3040        * 
3041        * But, there might be a monitoring command still
3042        * in progress.  Setting the Shutdown flag while holding
3043        * the lock ensures that there is no monitoring command
3044        * in the interrupt handler currently, and any monitoring
3045        * commands that complete from this time on will NOT return
3046        * their command structure to the free list.
3047        */
3048
3049       spin_lock_irqsave(&Controller->queue_lock, flags);
3050       Controller->ShutdownMonitoringTimer = 1;
3051       spin_unlock_irqrestore(&Controller->queue_lock, flags);
3052
3053       del_timer_sync(&Controller->MonitoringTimer);
3054       if (Controller->FirmwareType == DAC960_V1_Controller)
3055         {
3056           DAC960_Notice("Flushing Cache...", Controller);
3057           DAC960_V1_ExecuteType3(Controller, DAC960_V1_Flush, 0);
3058           DAC960_Notice("done\n", Controller);
3059
3060           if (Controller->HardwareType == DAC960_PD_Controller)
3061               release_region(Controller->IO_Address, 0x80);
3062         }
3063       else
3064         {
3065           DAC960_Notice("Flushing Cache...", Controller);
3066           DAC960_V2_DeviceOperation(Controller, DAC960_V2_PauseDevice,
3067                                     DAC960_V2_RAID_Controller);
3068           DAC960_Notice("done\n", Controller);
3069         }
3070     }
3071   DAC960_UnregisterBlockDevice(Controller);
3072   DAC960_DestroyAuxiliaryStructures(Controller);
3073   DAC960_DestroyProcEntries(Controller);
3074   DAC960_DetectCleanup(Controller);
3075 }
3076
3077
3078 /*
3079   DAC960_Probe verifies controller's existence and
3080   initializes the DAC960 Driver for that controller.
3081 */
3082
3083 static int 
3084 DAC960_Probe(struct pci_dev *dev, const struct pci_device_id *entry)
3085 {
3086   int disk;
3087   DAC960_Controller_T *Controller;
3088
3089   if (DAC960_ControllerCount == DAC960_MaxControllers)
3090   {
3091         DAC960_Error("More than %d DAC960 Controllers detected - "
3092                        "ignoring from Controller at\n",
3093                        NULL, DAC960_MaxControllers);
3094         return -ENODEV;
3095   }
3096
3097   Controller = DAC960_DetectController(dev, entry);
3098   if (!Controller)
3099         return -ENODEV;
3100
3101   if (!DAC960_InitializeController(Controller)) {
3102         DAC960_FinalizeController(Controller);
3103         return -ENODEV;
3104   }
3105
3106   for (disk = 0; disk < DAC960_MaxLogicalDrives; disk++) {
3107         set_capacity(Controller->disks[disk], disk_size(Controller, disk));
3108         add_disk(Controller->disks[disk]);
3109   }
3110   DAC960_CreateProcEntries(Controller);
3111   return 0;
3112 }
3113
3114
3115 /*
3116   DAC960_Finalize finalizes the DAC960 Driver.
3117 */
3118
3119 static void DAC960_Remove(struct pci_dev *PCI_Device)
3120 {
3121   int Controller_Number = (long)pci_get_drvdata(PCI_Device);
3122   DAC960_Controller_T *Controller = DAC960_Controllers[Controller_Number];
3123   if (Controller != NULL)
3124       DAC960_FinalizeController(Controller);
3125 }
3126
3127
3128 /*
3129   DAC960_V1_QueueReadWriteCommand prepares and queues a Read/Write Command for
3130   DAC960 V1 Firmware Controllers.
3131 */
3132
3133 static void DAC960_V1_QueueReadWriteCommand(DAC960_Command_T *Command)
3134 {
3135   DAC960_Controller_T *Controller = Command->Controller;
3136   DAC960_V1_CommandMailbox_T *CommandMailbox = &Command->V1.CommandMailbox;
3137   DAC960_V1_ScatterGatherSegment_T *ScatterGatherList =
3138                                         Command->V1.ScatterGatherList;
3139   struct scatterlist *ScatterList = Command->V1.ScatterList;
3140
3141   DAC960_V1_ClearCommand(Command);
3142
3143   if (Command->SegmentCount == 1)
3144     {
3145       if (Command->DmaDirection == PCI_DMA_FROMDEVICE)
3146         CommandMailbox->Type5.CommandOpcode = DAC960_V1_Read;
3147       else 
3148         CommandMailbox->Type5.CommandOpcode = DAC960_V1_Write;
3149
3150       CommandMailbox->Type5.LD.TransferLength = Command->BlockCount;
3151       CommandMailbox->Type5.LD.LogicalDriveNumber = Command->LogicalDriveNumber;
3152       CommandMailbox->Type5.LogicalBlockAddress = Command->BlockNumber;
3153       CommandMailbox->Type5.BusAddress =
3154                         (DAC960_BusAddress32_T)sg_dma_address(ScatterList);     
3155     }
3156   else
3157     {
3158       int i;
3159
3160       if (Command->DmaDirection == PCI_DMA_FROMDEVICE)
3161         CommandMailbox->Type5.CommandOpcode = DAC960_V1_ReadWithScatterGather;
3162       else
3163         CommandMailbox->Type5.CommandOpcode = DAC960_V1_WriteWithScatterGather;
3164
3165       CommandMailbox->Type5.LD.TransferLength = Command->BlockCount;
3166       CommandMailbox->Type5.LD.LogicalDriveNumber = Command->LogicalDriveNumber;
3167       CommandMailbox->Type5.LogicalBlockAddress = Command->BlockNumber;
3168       CommandMailbox->Type5.BusAddress = Command->V1.ScatterGatherListDMA;
3169
3170       CommandMailbox->Type5.ScatterGatherCount = Command->SegmentCount;
3171
3172       for (i = 0; i < Command->SegmentCount; i++, ScatterList++, ScatterGatherList++) {
3173                 ScatterGatherList->SegmentDataPointer =
3174                         (DAC960_BusAddress32_T)sg_dma_address(ScatterList);
3175                 ScatterGatherList->SegmentByteCount =
3176                         (DAC960_ByteCount32_T)sg_dma_len(ScatterList);
3177       }
3178     }
3179   DAC960_QueueCommand(Command);
3180 }
3181
3182
3183 /*
3184   DAC960_V2_QueueReadWriteCommand prepares and queues a Read/Write Command for
3185   DAC960 V2 Firmware Controllers.
3186 */
3187
3188 static void DAC960_V2_QueueReadWriteCommand(DAC960_Command_T *Command)
3189 {
3190   DAC960_Controller_T *Controller = Command->Controller;
3191   DAC960_V2_CommandMailbox_T *CommandMailbox = &Command->V2.CommandMailbox;
3192   struct scatterlist *ScatterList = Command->V2.ScatterList;
3193
3194   DAC960_V2_ClearCommand(Command);
3195
3196   CommandMailbox->SCSI_10.CommandOpcode = DAC960_V2_SCSI_10;
3197   CommandMailbox->SCSI_10.CommandControlBits.DataTransferControllerToHost =
3198     (Command->DmaDirection == PCI_DMA_FROMDEVICE);
3199   CommandMailbox->SCSI_10.DataTransferSize =
3200     Command->BlockCount << DAC960_BlockSizeBits;
3201   CommandMailbox->SCSI_10.RequestSenseBusAddress = Command->V2.RequestSenseDMA;
3202   CommandMailbox->SCSI_10.PhysicalDevice =
3203     Controller->V2.LogicalDriveToVirtualDevice[Command->LogicalDriveNumber];
3204   CommandMailbox->SCSI_10.RequestSenseSize = sizeof(DAC960_SCSI_RequestSense_T);
3205   CommandMailbox->SCSI_10.CDBLength = 10;
3206   CommandMailbox->SCSI_10.SCSI_CDB[0] =
3207     (Command->DmaDirection == PCI_DMA_FROMDEVICE ? 0x28 : 0x2A);
3208   CommandMailbox->SCSI_10.SCSI_CDB[2] = Command->BlockNumber >> 24;
3209   CommandMailbox->SCSI_10.SCSI_CDB[3] = Command->BlockNumber >> 16;
3210   CommandMailbox->SCSI_10.SCSI_CDB[4] = Command->BlockNumber >> 8;
3211   CommandMailbox->SCSI_10.SCSI_CDB[5] = Command->BlockNumber;
3212   CommandMailbox->SCSI_10.SCSI_CDB[7] = Command->BlockCount >> 8;
3213   CommandMailbox->SCSI_10.SCSI_CDB[8] = Command->BlockCount;
3214
3215   if (Command->SegmentCount == 1)
3216     {
3217       CommandMailbox->SCSI_10.DataTransferMemoryAddress
3218                              .ScatterGatherSegments[0]
3219                              .SegmentDataPointer =
3220         (DAC960_BusAddress64_T)sg_dma_address(ScatterList);
3221       CommandMailbox->SCSI_10.DataTransferMemoryAddress
3222                              .ScatterGatherSegments[0]
3223                              .SegmentByteCount =
3224         CommandMailbox->SCSI_10.DataTransferSize;
3225     }
3226   else
3227     {
3228       DAC960_V2_ScatterGatherSegment_T *ScatterGatherList;
3229       int i;
3230
3231       if (Command->SegmentCount > 2)
3232         {
3233           ScatterGatherList = Command->V2.ScatterGatherList;
3234           CommandMailbox->SCSI_10.CommandControlBits
3235                          .AdditionalScatterGatherListMemory = true;
3236           CommandMailbox->SCSI_10.DataTransferMemoryAddress
3237                 .ExtendedScatterGather.ScatterGatherList0Length = Command->SegmentCount;
3238           CommandMailbox->SCSI_10.DataTransferMemoryAddress
3239                          .ExtendedScatterGather.ScatterGatherList0Address =
3240             Command->V2.ScatterGatherListDMA;
3241         }
3242       else
3243         ScatterGatherList = CommandMailbox->SCSI_10.DataTransferMemoryAddress
3244                                  .ScatterGatherSegments;
3245
3246       for (i = 0; i < Command->SegmentCount; i++, ScatterList++, ScatterGatherList++) {
3247                 ScatterGatherList->SegmentDataPointer =
3248                         (DAC960_BusAddress64_T)sg_dma_address(ScatterList);
3249                 ScatterGatherList->SegmentByteCount =
3250                         (DAC960_ByteCount64_T)sg_dma_len(ScatterList);
3251       }
3252     }
3253   DAC960_QueueCommand(Command);
3254 }
3255
3256
3257 static int DAC960_process_queue(DAC960_Controller_T *Controller, struct request_queue *req_q)
3258 {
3259         struct request *Request;
3260         DAC960_Command_T *Command;
3261
3262    while(1) {
3263         Request = elv_next_request(req_q);
3264         if (!Request)
3265                 return 1;
3266
3267         Command = DAC960_AllocateCommand(Controller);
3268         if (Command == NULL)
3269                 return 0;
3270
3271         if (rq_data_dir(Request) == READ) {
3272                 Command->DmaDirection = PCI_DMA_FROMDEVICE;
3273                 Command->CommandType = DAC960_ReadCommand;
3274         } else {
3275                 Command->DmaDirection = PCI_DMA_TODEVICE;
3276                 Command->CommandType = DAC960_WriteCommand;
3277         }
3278         Command->Completion = Request->waiting;
3279         Command->LogicalDriveNumber = (long)Request->rq_disk->private_data;
3280         Command->BlockNumber = Request->sector;
3281         Command->BlockCount = Request->nr_sectors;
3282         Command->Request = Request;
3283         blkdev_dequeue_request(Request);
3284         Command->SegmentCount = blk_rq_map_sg(req_q,
3285                   Command->Request, Command->cmd_sglist);
3286         /* pci_map_sg MAY change the value of SegCount */
3287         Command->SegmentCount = pci_map_sg(Controller->PCIDevice, Command->cmd_sglist,
3288                  Command->SegmentCount, Command->DmaDirection);
3289
3290         DAC960_QueueReadWriteCommand(Command);
3291   }
3292 }
3293
3294 /*
3295   DAC960_ProcessRequest attempts to remove one I/O Request from Controller's
3296   I/O Request Queue and queues it to the Controller.  WaitForCommand is true if
3297   this function should wait for a Command to become available if necessary.
3298   This function returns true if an I/O Request was queued and false otherwise.
3299 */
3300 static void DAC960_ProcessRequest(DAC960_Controller_T *controller)
3301 {
3302         int i;
3303
3304         if (!controller->ControllerInitialized)
3305                 return;
3306
3307         /* Do this better later! */
3308         for (i = controller->req_q_index; i < DAC960_MaxLogicalDrives; i++) {
3309                 struct request_queue *req_q = controller->RequestQueue[i];
3310
3311                 if (req_q == NULL)
3312                         continue;
3313
3314                 if (!DAC960_process_queue(controller, req_q)) {
3315                         controller->req_q_index = i;
3316                         return;
3317                 }
3318         }
3319
3320         if (controller->req_q_index == 0)
3321                 return;
3322
3323         for (i = 0; i < controller->req_q_index; i++) {
3324                 struct request_queue *req_q = controller->RequestQueue[i];
3325
3326                 if (req_q == NULL)
3327                         continue;
3328
3329                 if (!DAC960_process_queue(controller, req_q)) {
3330                         controller->req_q_index = i;
3331                         return;
3332                 }
3333         }
3334 }
3335
3336
3337 /*
3338   DAC960_queue_partial_rw extracts one bio from the request already
3339   associated with argument command, and construct a new command block to retry I/O
3340   only on that bio.  Queue that command to the controller.
3341
3342   This function re-uses a previously-allocated Command,
3343         there is no failure mode from trying to allocate a command.
3344 */
3345
3346 static void DAC960_queue_partial_rw(DAC960_Command_T *Command)
3347 {
3348   DAC960_Controller_T *Controller = Command->Controller;
3349   struct request *Request = Command->Request;
3350   struct request_queue *req_q = Controller->RequestQueue[Command->LogicalDriveNumber];
3351
3352   if (Command->DmaDirection == PCI_DMA_FROMDEVICE)
3353     Command->CommandType = DAC960_ReadRetryCommand;
3354   else
3355     Command->CommandType = DAC960_WriteRetryCommand;
3356
3357   /*
3358    * We could be more efficient with these mapping requests
3359    * and map only the portions that we need.  But since this
3360    * code should almost never be called, just go with a
3361    * simple coding.
3362    */
3363   (void)blk_rq_map_sg(req_q, Command->Request, Command->cmd_sglist);
3364
3365   (void)pci_map_sg(Controller->PCIDevice, Command->cmd_sglist, 1, Command->DmaDirection);
3366   /*
3367    * Resubmitting the request sector at a time is really tedious.
3368    * But, this should almost never happen.  So, we're willing to pay
3369    * this price so that in the end, as much of the transfer is completed
3370    * successfully as possible.
3371    */
3372   Command->SegmentCount = 1;
3373   Command->BlockNumber = Request->sector;
3374   Command->BlockCount = 1;
3375   DAC960_QueueReadWriteCommand(Command);
3376   return;
3377 }
3378
3379 /*
3380   DAC960_RequestFunction is the I/O Request Function for DAC960 Controllers.
3381 */
3382
3383 static void DAC960_RequestFunction(struct request_queue *RequestQueue)
3384 {
3385         DAC960_ProcessRequest(RequestQueue->queuedata);
3386 }
3387
3388 /*
3389   DAC960_ProcessCompletedBuffer performs completion processing for an
3390   individual Buffer.
3391 */
3392
3393 static inline boolean DAC960_ProcessCompletedRequest(DAC960_Command_T *Command,
3394                                                  boolean SuccessfulIO)
3395 {
3396         struct request *Request = Command->Request;
3397         int UpToDate;
3398
3399         UpToDate = 0;
3400         if (SuccessfulIO)
3401                 UpToDate = 1;
3402
3403         pci_unmap_sg(Command->Controller->PCIDevice, Command->cmd_sglist,
3404                 Command->SegmentCount, Command->DmaDirection);
3405
3406          if (!end_that_request_first(Request, UpToDate, Command->BlockCount)) {
3407
3408                 end_that_request_last(Request);
3409
3410                 if (Command->Completion) {
3411                         complete(Command->Completion);
3412                         Command->Completion = NULL;
3413                 }
3414                 return true;
3415         }
3416         return false;
3417 }
3418
3419 /*
3420   DAC960_V1_ReadWriteError prints an appropriate error message for Command
3421   when an error occurs on a Read or Write operation.
3422 */
3423
3424 static void DAC960_V1_ReadWriteError(DAC960_Command_T *Command)
3425 {
3426   DAC960_Controller_T *Controller = Command->Controller;
3427   unsigned char *CommandName = "UNKNOWN";
3428   switch (Command->CommandType)
3429     {
3430     case DAC960_ReadCommand:
3431     case DAC960_ReadRetryCommand:
3432       CommandName = "READ";
3433       break;
3434     case DAC960_WriteCommand:
3435     case DAC960_WriteRetryCommand:
3436       CommandName = "WRITE";
3437       break;
3438     case DAC960_MonitoringCommand:
3439     case DAC960_ImmediateCommand:
3440     case DAC960_QueuedCommand:
3441       break;
3442     }
3443   switch (Command->V1.CommandStatus)
3444     {
3445     case DAC960_V1_IrrecoverableDataError:
3446       DAC960_Error("Irrecoverable Data Error on %s:\n",
3447                    Controller, CommandName);
3448       break;
3449     case DAC960_V1_LogicalDriveNonexistentOrOffline:
3450       DAC960_Error("Logical Drive Nonexistent or Offline on %s:\n",
3451                    Controller, CommandName);
3452       break;
3453     case DAC960_V1_AccessBeyondEndOfLogicalDrive:
3454       DAC960_Error("Attempt to Access Beyond End of Logical Drive "
3455                    "on %s:\n", Controller, CommandName);
3456       break;
3457     case DAC960_V1_BadDataEncountered:
3458       DAC960_Error("Bad Data Encountered on %s:\n", Controller, CommandName);
3459       break;
3460     default:
3461       DAC960_Error("Unexpected Error Status %04X on %s:\n",
3462                    Controller, Command->V1.CommandStatus, CommandName);
3463       break;
3464     }
3465   DAC960_Error("  /dev/rd/c%dd%d:   absolute blocks %u..%u\n",
3466                Controller, Controller->ControllerNumber,
3467                Command->LogicalDriveNumber, Command->BlockNumber,
3468                Command->BlockNumber + Command->BlockCount - 1);
3469 }
3470
3471
3472 /*
3473   DAC960_V1_ProcessCompletedCommand performs completion processing for Command
3474   for DAC960 V1 Firmware Controllers.
3475 */
3476
3477 static void DAC960_V1_ProcessCompletedCommand(DAC960_Command_T *Command)
3478 {
3479   DAC960_Controller_T *Controller = Command->Controller;
3480   DAC960_CommandType_T CommandType = Command->CommandType;
3481   DAC960_V1_CommandOpcode_T CommandOpcode =
3482     Command->V1.CommandMailbox.Common.CommandOpcode;
3483   DAC960_V1_CommandStatus_T CommandStatus = Command->V1.CommandStatus;
3484
3485   if (CommandType == DAC960_ReadCommand ||
3486       CommandType == DAC960_WriteCommand)
3487     {
3488
3489 #ifdef FORCE_RETRY_DEBUG
3490       CommandStatus = DAC960_V1_IrrecoverableDataError;
3491 #endif
3492
3493       if (CommandStatus == DAC960_V1_NormalCompletion) {
3494
3495                 if (!DAC960_ProcessCompletedRequest(Command, true))
3496                         BUG();
3497
3498       } else if (CommandStatus == DAC960_V1_IrrecoverableDataError ||
3499                 CommandStatus == DAC960_V1_BadDataEncountered)
3500         {
3501           /*
3502            * break the command down into pieces and resubmit each
3503            * piece, hoping that some of them will succeed.
3504            */
3505            DAC960_queue_partial_rw(Command);
3506            return;
3507         }
3508       else
3509         {
3510           if (CommandStatus != DAC960_V1_LogicalDriveNonexistentOrOffline)
3511             DAC960_V1_ReadWriteError(Command);
3512
3513          if (!DAC960_ProcessCompletedRequest(Command, false))
3514                 BUG();
3515         }
3516     }
3517   else if (CommandType == DAC960_ReadRetryCommand ||
3518            CommandType == DAC960_WriteRetryCommand)
3519     {
3520       boolean normal_completion;
3521 #ifdef FORCE_RETRY_FAILURE_DEBUG
3522       static int retry_count = 1;
3523 #endif
3524       /*
3525         Perform completion processing for the portion that was
3526         retried, and submit the next portion, if any.
3527       */
3528       normal_completion = true;
3529       if (CommandStatus != DAC960_V1_NormalCompletion) {
3530         normal_completion = false;
3531         if (CommandStatus != DAC960_V1_LogicalDriveNonexistentOrOffline)
3532             DAC960_V1_ReadWriteError(Command);
3533       }
3534
3535 #ifdef FORCE_RETRY_FAILURE_DEBUG
3536       if (!(++retry_count % 10000)) {
3537               printk("V1 error retry failure test\n");
3538               normal_completion = false;
3539               DAC960_V1_ReadWriteError(Command);
3540       }
3541 #endif
3542
3543       if (!DAC960_ProcessCompletedRequest(Command, normal_completion)) {
3544         DAC960_queue_partial_rw(Command);
3545         return;
3546       }
3547     }
3548
3549   else if (CommandType == DAC960_MonitoringCommand)
3550     {
3551       if (Controller->ShutdownMonitoringTimer)
3552               return;
3553       if (CommandOpcode == DAC960_V1_Enquiry)
3554         {
3555           DAC960_V1_Enquiry_T *OldEnquiry = &Controller->V1.Enquiry;
3556           DAC960_V1_Enquiry_T *NewEnquiry = Controller->V1.NewEnquiry;
3557           unsigned int OldCriticalLogicalDriveCount =
3558             OldEnquiry->CriticalLogicalDriveCount;
3559           unsigned int NewCriticalLogicalDriveCount =
3560             NewEnquiry->CriticalLogicalDriveCount;
3561           if (NewEnquiry->NumberOfLogicalDrives > Controller->LogicalDriveCount)
3562             {
3563               int LogicalDriveNumber = Controller->LogicalDriveCount - 1;
3564               while (++LogicalDriveNumber < NewEnquiry->NumberOfLogicalDrives)
3565                 DAC960_Critical("Logical Drive %d (/dev/rd/c%dd%d) "
3566                                 "Now Exists\n", Controller,
3567                                 LogicalDriveNumber,
3568                                 Controller->ControllerNumber,
3569                                 LogicalDriveNumber);
3570               Controller->LogicalDriveCount = NewEnquiry->NumberOfLogicalDrives;
3571               DAC960_ComputeGenericDiskInfo(Controller);
3572             }
3573           if (NewEnquiry->NumberOfLogicalDrives < Controller->LogicalDriveCount)
3574             {
3575               int LogicalDriveNumber = NewEnquiry->NumberOfLogicalDrives - 1;
3576               while (++LogicalDriveNumber < Controller->LogicalDriveCount)
3577                 DAC960_Critical("Logical Drive %d (/dev/rd/c%dd%d) "
3578                                 "No Longer Exists\n", Controller,
3579                                 LogicalDriveNumber,
3580                                 Controller->ControllerNumber,
3581                                 LogicalDriveNumber);
3582               Controller->LogicalDriveCount = NewEnquiry->NumberOfLogicalDrives;
3583               DAC960_ComputeGenericDiskInfo(Controller);
3584             }
3585           if (NewEnquiry->StatusFlags.DeferredWriteError !=
3586               OldEnquiry->StatusFlags.DeferredWriteError)
3587             DAC960_Critical("Deferred Write Error Flag is now %s\n", Controller,
3588                             (NewEnquiry->StatusFlags.DeferredWriteError
3589                              ? "TRUE" : "FALSE"));
3590           if ((NewCriticalLogicalDriveCount > 0 ||
3591                NewCriticalLogicalDriveCount != OldCriticalLogicalDriveCount) ||
3592               (NewEnquiry->OfflineLogicalDriveCount > 0 ||
3593                NewEnquiry->OfflineLogicalDriveCount !=
3594                OldEnquiry->OfflineLogicalDriveCount) ||
3595               (NewEnquiry->DeadDriveCount > 0 ||
3596                NewEnquiry->DeadDriveCount !=
3597                OldEnquiry->DeadDriveCount) ||
3598               (NewEnquiry->EventLogSequenceNumber !=
3599                OldEnquiry->EventLogSequenceNumber) ||
3600               Controller->MonitoringTimerCount == 0 ||
3601               (jiffies - Controller->SecondaryMonitoringTime
3602                >= DAC960_SecondaryMonitoringInterval))
3603             {
3604               Controller->V1.NeedLogicalDriveInformation = true;
3605               Controller->V1.NewEventLogSequenceNumber =
3606                 NewEnquiry->EventLogSequenceNumber;
3607               Controller->V1.NeedErrorTableInformation = true;
3608               Controller->V1.NeedDeviceStateInformation = true;
3609               Controller->V1.StartDeviceStateScan = true;
3610               Controller->V1.NeedBackgroundInitializationStatus =
3611                 Controller->V1.BackgroundInitializationStatusSupported;
3612               Controller->SecondaryMonitoringTime = jiffies;
3613             }
3614           if (NewEnquiry->RebuildFlag == DAC960_V1_StandbyRebuildInProgress ||
3615               NewEnquiry->RebuildFlag
3616               == DAC960_V1_BackgroundRebuildInProgress ||
3617               OldEnquiry->RebuildFlag == DAC960_V1_StandbyRebuildInProgress ||
3618               OldEnquiry->RebuildFlag == DAC960_V1_BackgroundRebuildInProgress)
3619             {
3620               Controller->V1.NeedRebuildProgress = true;
3621               Controller->V1.RebuildProgressFirst =
3622                 (NewEnquiry->CriticalLogicalDriveCount <
3623                  OldEnquiry->CriticalLogicalDriveCount);
3624             }
3625           if (OldEnquiry->RebuildFlag == DAC960_V1_BackgroundCheckInProgress)
3626             switch (NewEnquiry->RebuildFlag)
3627               {
3628               case DAC960_V1_NoStandbyRebuildOrCheckInProgress:
3629                 DAC960_Progress("Consistency Check Completed Successfully\n",
3630                                 Controller);
3631                 break;
3632               case DAC960_V1_StandbyRebuildInProgress:
3633               case DAC960_V1_BackgroundRebuildInProgress:
3634                 break;
3635               case DAC960_V1_BackgroundCheckInProgress:
3636                 Controller->V1.NeedConsistencyCheckProgress = true;
3637                 break;
3638               case DAC960_V1_StandbyRebuildCompletedWithError:
3639                 DAC960_Progress("Consistency Check Completed with Error\n",
3640                                 Controller);
3641                 break;
3642               case DAC960_V1_BackgroundRebuildOrCheckFailed_DriveFailed:
3643                 DAC960_Progress("Consistency Check Failed - "
3644                                 "Physical Device Failed\n", Controller);
3645                 break;
3646               case DAC960_V1_BackgroundRebuildOrCheckFailed_LogicalDriveFailed:
3647                 DAC960_Progress("Consistency Check Failed - "
3648                                 "Logical Drive Failed\n", Controller);
3649                 break;
3650               case DAC960_V1_BackgroundRebuildOrCheckFailed_OtherCauses:
3651                 DAC960_Progress("Consistency Check Failed - Other Causes\n",
3652                                 Controller);
3653                 break;
3654               case DAC960_V1_BackgroundRebuildOrCheckSuccessfullyTerminated:
3655                 DAC960_Progress("Consistency Check Successfully Terminated\n",
3656                                 Controller);
3657                 break;
3658               }
3659           else if (NewEnquiry->RebuildFlag
3660                    == DAC960_V1_BackgroundCheckInProgress)
3661             Controller->V1.NeedConsistencyCheckProgress = true;
3662           Controller->MonitoringAlertMode =
3663             (NewEnquiry->CriticalLogicalDriveCount > 0 ||
3664              NewEnquiry->OfflineLogicalDriveCount > 0 ||
3665              NewEnquiry->DeadDriveCount > 0);
3666           if (NewEnquiry->RebuildFlag > DAC960_V1_BackgroundCheckInProgress)
3667             {
3668               Controller->V1.PendingRebuildFlag = NewEnquiry->RebuildFlag;
3669               Controller->V1.RebuildFlagPending = true;
3670             }
3671           memcpy(&Controller->V1.Enquiry, &Controller->V1.NewEnquiry,
3672                  sizeof(DAC960_V1_Enquiry_T));
3673         }
3674       else if (CommandOpcode == DAC960_V1_PerformEventLogOperation)
3675         {
3676           static char
3677             *DAC960_EventMessages[] =
3678                { "killed because write recovery failed",
3679                  "killed because of SCSI bus reset failure",
3680                  "killed because of double check condition",
3681                  "killed because it was removed",
3682                  "killed because of gross error on SCSI chip",
3683                  "killed because of bad tag returned from drive",
3684                  "killed because of timeout on SCSI command",
3685                  "killed because of reset SCSI command issued from system",
3686                  "killed because busy or parity error count exceeded limit",
3687                  "killed because of 'kill drive' command from system",
3688                  "killed because of selection timeout",
3689                  "killed due to SCSI phase sequence error",
3690                  "killed due to unknown status" };
3691           DAC960_V1_EventLogEntry_T *EventLogEntry =
3692                 Controller->V1.EventLogEntry;
3693           if (EventLogEntry->SequenceNumber ==
3694               Controller->V1.OldEventLogSequenceNumber)
3695             {
3696               unsigned char SenseKey = EventLogEntry->SenseKey;
3697               unsigned char AdditionalSenseCode =
3698                 EventLogEntry->AdditionalSenseCode;
3699               unsigned char AdditionalSenseCodeQualifier =
3700                 EventLogEntry->AdditionalSenseCodeQualifier;
3701               if (SenseKey == DAC960_SenseKey_VendorSpecific &&
3702                   AdditionalSenseCode == 0x80 &&
3703                   AdditionalSenseCodeQualifier <
3704                   sizeof(DAC960_EventMessages) / sizeof(char *))
3705                 DAC960_Critical("Physical Device %d:%d %s\n", Controller,
3706                                 EventLogEntry->Channel,
3707                                 EventLogEntry->TargetID,
3708                                 DAC960_EventMessages[
3709                                   AdditionalSenseCodeQualifier]);
3710               else if (SenseKey == DAC960_SenseKey_UnitAttention &&
3711                        AdditionalSenseCode == 0x29)
3712                 {
3713                   if (Controller->MonitoringTimerCount > 0)
3714                     Controller->V1.DeviceResetCount[EventLogEntry->Channel]
3715                                                    [EventLogEntry->TargetID]++;
3716                 }
3717               else if (!(SenseKey == DAC960_SenseKey_NoSense ||
3718                          (SenseKey == DAC960_SenseKey_NotReady &&
3719                           AdditionalSenseCode == 0x04 &&
3720                           (AdditionalSenseCodeQualifier == 0x01 ||
3721                            AdditionalSenseCodeQualifier == 0x02))))
3722                 {
3723                   DAC960_Critical("Physical Device %d:%d Error Log: "
3724                                   "Sense Key = %X, ASC = %02X, ASCQ = %02X\n",
3725                                   Controller,
3726                                   EventLogEntry->Channel,
3727                                   EventLogEntry->TargetID,
3728                                   SenseKey,
3729                                   AdditionalSenseCode,
3730                                   AdditionalSenseCodeQualifier);
3731                   DAC960_Critical("Physical Device %d:%d Error Log: "
3732                                   "Information = %02X%02X%02X%02X "
3733                                   "%02X%02X%02X%02X\n",
3734                                   Controller,
3735                                   EventLogEntry->Channel,
3736                                   EventLogEntry->TargetID,
3737                                   EventLogEntry->Information[0],
3738                                   EventLogEntry->Information[1],
3739                                   EventLogEntry->Information[2],
3740                                   EventLogEntry->Information[3],
3741                                   EventLogEntry->CommandSpecificInformation[0],
3742                                   EventLogEntry->CommandSpecificInformation[1],
3743                                   EventLogEntry->CommandSpecificInformation[2],
3744                                   EventLogEntry->CommandSpecificInformation[3]);
3745                 }
3746             }
3747           Controller->V1.OldEventLogSequenceNumber++;
3748         }
3749       else if (CommandOpcode == DAC960_V1_GetErrorTable)
3750         {
3751           DAC960_V1_ErrorTable_T *OldErrorTable = &Controller->V1.ErrorTable;
3752           DAC960_V1_ErrorTable_T *NewErrorTable = Controller->V1.NewErrorTable;
3753           int Channel, TargetID;
3754           for (Channel = 0; Channel < Controller->Channels; Channel++)
3755             for (TargetID = 0; TargetID < Controller->Targets; TargetID++)
3756               {
3757                 DAC960_V1_ErrorTableEntry_T *NewErrorEntry =
3758                   &NewErrorTable->ErrorTableEntries[Channel][TargetID];
3759                 DAC960_V1_ErrorTableEntry_T *OldErrorEntry =
3760                   &OldErrorTable->ErrorTableEntries[Channel][TargetID];
3761                 if ((NewErrorEntry->ParityErrorCount !=
3762                      OldErrorEntry->ParityErrorCount) ||
3763                     (NewErrorEntry->SoftErrorCount !=
3764                      OldErrorEntry->SoftErrorCount) ||
3765                     (NewErrorEntry->HardErrorCount !=
3766                      OldErrorEntry->HardErrorCount) ||
3767                     (NewErrorEntry->MiscErrorCount !=
3768                      OldErrorEntry->MiscErrorCount))
3769                   DAC960_Critical("Physical Device %d:%d Errors: "
3770                                   "Parity = %d, Soft = %d, "
3771                                   "Hard = %d, Misc = %d\n",
3772                                   Controller, Channel, TargetID,
3773                                   NewErrorEntry->ParityErrorCount,
3774                                   NewErrorEntry->SoftErrorCount,
3775                                   NewErrorEntry->HardErrorCount,
3776                                   NewErrorEntry->MiscErrorCount);
3777               }
3778           memcpy(&Controller->V1.ErrorTable, Controller->V1.NewErrorTable,
3779                  sizeof(DAC960_V1_ErrorTable_T));
3780         }
3781       else if (CommandOpcode == DAC960_V1_GetDeviceState)
3782         {
3783           DAC960_V1_DeviceState_T *OldDeviceState =
3784             &Controller->V1.DeviceState[Controller->V1.DeviceStateChannel]
3785                                        [Controller->V1.DeviceStateTargetID];
3786           DAC960_V1_DeviceState_T *NewDeviceState =
3787             Controller->V1.NewDeviceState;
3788           if (NewDeviceState->DeviceState != OldDeviceState->DeviceState)
3789             DAC960_Critical("Physical Device %d:%d is now %s\n", Controller,
3790                             Controller->V1.DeviceStateChannel,
3791                             Controller->V1.DeviceStateTargetID,
3792                             (NewDeviceState->DeviceState
3793                              == DAC960_V1_Device_Dead
3794                              ? "DEAD"
3795                              : NewDeviceState->DeviceState
3796                                == DAC960_V1_Device_WriteOnly
3797                                ? "WRITE-ONLY"
3798                                : NewDeviceState->DeviceState
3799                                  == DAC960_V1_Device_Online
3800                                  ? "ONLINE" : "STANDBY"));
3801           if (OldDeviceState->DeviceState == DAC960_V1_Device_Dead &&
3802               NewDeviceState->DeviceState != DAC960_V1_Device_Dead)
3803             {
3804               Controller->V1.NeedDeviceInquiryInformation = true;
3805               Controller->V1.NeedDeviceSerialNumberInformation = true;
3806               Controller->V1.DeviceResetCount
3807                              [Controller->V1.DeviceStateChannel]
3808                              [Controller->V1.DeviceStateTargetID] = 0;
3809             }
3810           memcpy(OldDeviceState, NewDeviceState,
3811                  sizeof(DAC960_V1_DeviceState_T));
3812         }
3813       else if (CommandOpcode == DAC960_V1_GetLogicalDriveInformation)
3814         {
3815           int LogicalDriveNumber;
3816           for (LogicalDriveNumber = 0;
3817                LogicalDriveNumber < Controller->LogicalDriveCount;
3818                LogicalDriveNumber++)
3819             {
3820               DAC960_V1_LogicalDriveInformation_T *OldLogicalDriveInformation =
3821                 &Controller->V1.LogicalDriveInformation[LogicalDriveNumber];
3822               DAC960_V1_LogicalDriveInformation_T *NewLogicalDriveInformation =
3823                 &(*Controller->V1.NewLogicalDriveInformation)[LogicalDriveNumber];
3824               if (NewLogicalDriveInformation->LogicalDriveState !=
3825                   OldLogicalDriveInformation->LogicalDriveState)
3826                 DAC960_Critical("Logical Drive %d (/dev/rd/c%dd%d) "
3827                                 "is now %s\n", Controller,
3828                                 LogicalDriveNumber,
3829                                 Controller->ControllerNumber,
3830                                 LogicalDriveNumber,
3831                                 (NewLogicalDriveInformation->LogicalDriveState
3832                                  == DAC960_V1_LogicalDrive_Online
3833                                  ? "ONLINE"
3834                                  : NewLogicalDriveInformation->LogicalDriveState
3835                                    == DAC960_V1_LogicalDrive_Critical
3836                                    ? "CRITICAL" : "OFFLINE"));
3837               if (NewLogicalDriveInformation->WriteBack !=
3838                   OldLogicalDriveInformation->WriteBack)
3839                 DAC960_Critical("Logical Drive %d (/dev/rd/c%dd%d) "
3840                                 "is now %s\n", Controller,
3841                                 LogicalDriveNumber,
3842                                 Controller->ControllerNumber,
3843                                 LogicalDriveNumber,
3844                                 (NewLogicalDriveInformation->WriteBack
3845                                  ? "WRITE BACK" : "WRITE THRU"));
3846             }
3847           memcpy(&Controller->V1.LogicalDriveInformation,
3848                  Controller->V1.NewLogicalDriveInformation,
3849                  sizeof(DAC960_V1_LogicalDriveInformationArray_T));
3850         }
3851       else if (CommandOpcode == DAC960_V1_GetRebuildProgress)
3852         {
3853           unsigned int LogicalDriveNumber =
3854             Controller->V1.RebuildProgress->LogicalDriveNumber;
3855           unsigned int LogicalDriveSize =
3856             Controller->V1.RebuildProgress->LogicalDriveSize;
3857           unsigned int BlocksCompleted =
3858             LogicalDriveSize - Controller->V1.RebuildProgress->RemainingBlocks;
3859           if (CommandStatus == DAC960_V1_NoRebuildOrCheckInProgress &&
3860               Controller->V1.LastRebuildStatus == DAC960_V1_NormalCompletion)
3861             CommandStatus = DAC960_V1_RebuildSuccessful;
3862           switch (CommandStatus)
3863             {
3864             case DAC960_V1_NormalCompletion:
3865               Controller->EphemeralProgressMessage = true;
3866               DAC960_Progress("Rebuild in Progress: "
3867                               "Logical Drive %d (/dev/rd/c%dd%d) "
3868                               "%d%% completed\n",
3869                               Controller, LogicalDriveNumber,
3870                               Controller->ControllerNumber,
3871                               LogicalDriveNumber,
3872                               (100 * (BlocksCompleted >> 7))
3873                               / (LogicalDriveSize >> 7));
3874               Controller->EphemeralProgressMessage = false;
3875               break;
3876             case DAC960_V1_RebuildFailed_LogicalDriveFailure:
3877               DAC960_Progress("Rebuild Failed due to "
3878                               "Logical Drive Failure\n", Controller);
3879               break;
3880             case DAC960_V1_RebuildFailed_BadBlocksOnOther:
3881               DAC960_Progress("Rebuild Failed due to "
3882                               "Bad Blocks on Other Drives\n", Controller);
3883               break;
3884             case DAC960_V1_RebuildFailed_NewDriveFailed:
3885               DAC960_Progress("Rebuild Failed due to "
3886                               "Failure of Drive Being Rebuilt\n", Controller);
3887               break;
3888             case DAC960_V1_NoRebuildOrCheckInProgress:
3889               break;
3890             case DAC960_V1_RebuildSuccessful:
3891               DAC960_Progress("Rebuild Completed Successfully\n", Controller);
3892               break;
3893             case DAC960_V1_RebuildSuccessfullyTerminated:
3894               DAC960_Progress("Rebuild Successfully Terminated\n", Controller);
3895               break;
3896             }
3897           Controller->V1.LastRebuildStatus = CommandStatus;
3898           if (CommandType != DAC960_MonitoringCommand &&
3899               Controller->V1.RebuildStatusPending)
3900             {
3901               Command->V1.CommandStatus = Controller->V1.PendingRebuildStatus;
3902               Controller->V1.RebuildStatusPending = false;
3903             }
3904           else if (CommandType == DAC960_MonitoringCommand &&
3905                    CommandStatus != DAC960_V1_NormalCompletion &&
3906                    CommandStatus != DAC960_V1_NoRebuildOrCheckInProgress)
3907             {
3908               Controller->V1.PendingRebuildStatus = CommandStatus;
3909               Controller->V1.RebuildStatusPending = true;
3910             }
3911         }
3912       else if (CommandOpcode == DAC960_V1_RebuildStat)
3913         {
3914           unsigned int LogicalDriveNumber =
3915             Controller->V1.RebuildProgress->LogicalDriveNumber;
3916           unsigned int LogicalDriveSize =
3917             Controller->V1.RebuildProgress->LogicalDriveSize;
3918           unsigned int BlocksCompleted =
3919             LogicalDriveSize - Controller->V1.RebuildProgress->RemainingBlocks;
3920           if (CommandStatus == DAC960_V1_NormalCompletion)
3921             {
3922               Controller->EphemeralProgressMessage = true;
3923               DAC960_Progress("Consistency Check in Progress: "
3924                               "Logical Drive %d (/dev/rd/c%dd%d) "
3925                               "%d%% completed\n",
3926                               Controller, LogicalDriveNumber,
3927                               Controller->ControllerNumber,
3928                               LogicalDriveNumber,
3929                               (100 * (BlocksCompleted >> 7))
3930                               / (LogicalDriveSize >> 7));
3931               Controller->EphemeralProgressMessage = false;
3932             }
3933         }
3934       else if (CommandOpcode == DAC960_V1_BackgroundInitializationControl)
3935         {
3936           unsigned int LogicalDriveNumber =
3937             Controller->V1.BackgroundInitializationStatus->LogicalDriveNumber;
3938           unsigned int LogicalDriveSize =
3939             Controller->V1.BackgroundInitializationStatus->LogicalDriveSize;
3940           unsigned int BlocksCompleted =
3941             Controller->V1.BackgroundInitializationStatus->BlocksCompleted;
3942           switch (CommandStatus)
3943             {
3944             case DAC960_V1_NormalCompletion:
3945               switch (Controller->V1.BackgroundInitializationStatus->Status)
3946                 {
3947                 case DAC960_V1_BackgroundInitializationInvalid:
3948                   break;
3949                 case DAC960_V1_BackgroundInitializationStarted:
3950                   DAC960_Progress("Background Initialization Started\n",
3951                                   Controller);
3952                   break;
3953                 case DAC960_V1_BackgroundInitializationInProgress:
3954                   if (BlocksCompleted ==
3955                       Controller->V1.LastBackgroundInitializationStatus.
3956                                 BlocksCompleted &&
3957                       LogicalDriveNumber ==
3958                       Controller->V1.LastBackgroundInitializationStatus.
3959                                 LogicalDriveNumber)
3960                     break;
3961                   Controller->EphemeralProgressMessage = true;
3962                   DAC960_Progress("Background Initialization in Progress: "
3963                                   "Logical Drive %d (/dev/rd/c%dd%d) "
3964                                   "%d%% completed\n",
3965                                   Controller, LogicalDriveNumber,
3966                                   Controller->ControllerNumber,
3967                                   LogicalDriveNumber,
3968                                   (100 * (BlocksCompleted >> 7))
3969                                   / (LogicalDriveSize >> 7));
3970                   Controller->EphemeralProgressMessage = false;
3971                   break;
3972                 case DAC960_V1_BackgroundInitializationSuspended:
3973                   DAC960_Progress("Background Initialization Suspended\n",
3974                                   Controller);
3975                   break;
3976                 case DAC960_V1_BackgroundInitializationCancelled:
3977                   DAC960_Progress("Background Initialization Cancelled\n",
3978                                   Controller);
3979                   break;
3980                 }
3981               memcpy(&Controller->V1.LastBackgroundInitializationStatus,
3982                      Controller->V1.BackgroundInitializationStatus,
3983                      sizeof(DAC960_V1_BackgroundInitializationStatus_T));
3984               break;
3985             case DAC960_V1_BackgroundInitSuccessful:
3986               if (Controller->V1.BackgroundInitializationStatus->Status ==
3987                   DAC960_V1_BackgroundInitializationInProgress)
3988                 DAC960_Progress("Background Initialization "
3989                                 "Completed Successfully\n", Controller);
3990               Controller->V1.BackgroundInitializationStatus->Status =
3991                 DAC960_V1_BackgroundInitializationInvalid;
3992               break;
3993             case DAC960_V1_BackgroundInitAborted:
3994               if (Controller->V1.BackgroundInitializationStatus->Status ==
3995                   DAC960_V1_BackgroundInitializationInProgress)
3996                 DAC960_Progress("Background Initialization Aborted\n",
3997                                 Controller);
3998               Controller->V1.BackgroundInitializationStatus->Status =
3999                 DAC960_V1_BackgroundInitializationInvalid;
4000               break;
4001             case DAC960_V1_NoBackgroundInitInProgress:
4002               break;
4003             }
4004         } 
4005       else if (CommandOpcode == DAC960_V1_DCDB)
4006         {
4007            /*
4008              This is a bit ugly.
4009
4010              The InquiryStandardData and 
4011              the InquiryUntitSerialNumber information
4012              retrieval operations BOTH use the DAC960_V1_DCDB
4013              commands.  the test above can't distinguish between
4014              these two cases.
4015
4016              Instead, we rely on the order of code later in this
4017              function to ensure that DeviceInquiryInformation commands
4018              are submitted before DeviceSerialNumber commands.
4019            */
4020            if (Controller->V1.NeedDeviceInquiryInformation)
4021              {
4022                 DAC960_SCSI_Inquiry_T *InquiryStandardData =
4023                         &Controller->V1.InquiryStandardData
4024                                 [Controller->V1.DeviceStateChannel]
4025                                 [Controller->V1.DeviceStateTargetID];
4026                 if (CommandStatus != DAC960_V1_NormalCompletion)
4027                    {
4028                         memset(InquiryStandardData, 0,
4029                                 sizeof(DAC960_SCSI_Inquiry_T));
4030                         InquiryStandardData->PeripheralDeviceType = 0x1F;
4031                     }
4032                  else
4033                         memcpy(InquiryStandardData, 
4034                                 Controller->V1.NewInquiryStandardData,
4035                                 sizeof(DAC960_SCSI_Inquiry_T));
4036                  Controller->V1.NeedDeviceInquiryInformation = false;
4037               }
4038            else if (Controller->V1.NeedDeviceSerialNumberInformation) 
4039               {
4040                 DAC960_SCSI_Inquiry_UnitSerialNumber_T *InquiryUnitSerialNumber =
4041                   &Controller->V1.InquiryUnitSerialNumber
4042                                 [Controller->V1.DeviceStateChannel]
4043                                 [Controller->V1.DeviceStateTargetID];
4044                  if (CommandStatus != DAC960_V1_NormalCompletion)
4045                    {
4046                         memset(InquiryUnitSerialNumber, 0,
4047                                 sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T));
4048                         InquiryUnitSerialNumber->PeripheralDeviceType = 0x1F;
4049                     }
4050                   else
4051                         memcpy(InquiryUnitSerialNumber, 
4052                                 Controller->V1.NewInquiryUnitSerialNumber,
4053                                 sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T));
4054               Controller->V1.NeedDeviceSerialNumberInformation = false;
4055              }
4056         }
4057       /*
4058         Begin submitting new monitoring commands.
4059        */
4060       if (Controller->V1.NewEventLogSequenceNumber
4061           - Controller->V1.OldEventLogSequenceNumber > 0)
4062         {
4063           Command->V1.CommandMailbox.Type3E.CommandOpcode =
4064             DAC960_V1_PerformEventLogOperation;
4065           Command->V1.CommandMailbox.Type3E.OperationType =
4066             DAC960_V1_GetEventLogEntry;
4067           Command->V1.CommandMailbox.Type3E.OperationQualifier = 1;
4068           Command->V1.CommandMailbox.Type3E.SequenceNumber =
4069             Controller->V1.OldEventLogSequenceNumber;
4070           Command->V1.CommandMailbox.Type3E.BusAddress =
4071                 Controller->V1.EventLogEntryDMA;
4072           DAC960_QueueCommand(Command);
4073           return;
4074         }
4075       if (Controller->V1.NeedErrorTableInformation)
4076         {
4077           Controller->V1.NeedErrorTableInformation = false;
4078           Command->V1.CommandMailbox.Type3.CommandOpcode =
4079             DAC960_V1_GetErrorTable;
4080           Command->V1.CommandMailbox.Type3.BusAddress =
4081                 Controller->V1.NewErrorTableDMA;
4082           DAC960_QueueCommand(Command);
4083           return;
4084         }
4085       if (Controller->V1.NeedRebuildProgress &&
4086           Controller->V1.RebuildProgressFirst)
4087         {
4088           Controller->V1.NeedRebuildProgress = false;
4089           Command->V1.CommandMailbox.Type3.CommandOpcode =
4090             DAC960_V1_GetRebuildProgress;
4091           Command->V1.CommandMailbox.Type3.BusAddress =
4092             Controller->V1.RebuildProgressDMA;
4093           DAC960_QueueCommand(Command);
4094           return;
4095         }
4096       if (Controller->V1.NeedDeviceStateInformation)
4097         {
4098           if (Controller->V1.NeedDeviceInquiryInformation)
4099             {
4100               DAC960_V1_DCDB_T *DCDB = Controller->V1.MonitoringDCDB;
4101               dma_addr_t DCDB_DMA = Controller->V1.MonitoringDCDB_DMA;
4102
4103               dma_addr_t NewInquiryStandardDataDMA =
4104                 Controller->V1.NewInquiryStandardDataDMA;
4105
4106               Command->V1.CommandMailbox.Type3.CommandOpcode = DAC960_V1_DCDB;
4107               Command->V1.CommandMailbox.Type3.BusAddress = DCDB_DMA;
4108               DCDB->Channel = Controller->V1.DeviceStateChannel;
4109               DCDB->TargetID = Controller->V1.DeviceStateTargetID;
4110               DCDB->Direction = DAC960_V1_DCDB_DataTransferDeviceToSystem;
4111               DCDB->EarlyStatus = false;
4112               DCDB->Timeout = DAC960_V1_DCDB_Timeout_10_seconds;
4113               DCDB->NoAutomaticRequestSense = false;
4114               DCDB->DisconnectPermitted = true;
4115               DCDB->TransferLength = sizeof(DAC960_SCSI_Inquiry_T);
4116               DCDB->BusAddress = NewInquiryStandardDataDMA;
4117               DCDB->CDBLength = 6;
4118               DCDB->TransferLengthHigh4 = 0;
4119               DCDB->SenseLength = sizeof(DCDB->SenseData);
4120               DCDB->CDB[0] = 0x12; /* INQUIRY */
4121               DCDB->CDB[1] = 0; /* EVPD = 0 */
4122               DCDB->CDB[2] = 0; /* Page Code */
4123               DCDB->CDB[3] = 0; /* Reserved */
4124               DCDB->CDB[4] = sizeof(DAC960_SCSI_Inquiry_T);
4125               DCDB->CDB[5] = 0; /* Control */
4126               DAC960_QueueCommand(Command);
4127               return;
4128             }
4129           if (Controller->V1.NeedDeviceSerialNumberInformation)
4130             {
4131               DAC960_V1_DCDB_T *DCDB = Controller->V1.MonitoringDCDB;
4132               dma_addr_t DCDB_DMA = Controller->V1.MonitoringDCDB_DMA;
4133               dma_addr_t NewInquiryUnitSerialNumberDMA = 
4134                         Controller->V1.NewInquiryUnitSerialNumberDMA;
4135
4136               Command->V1.CommandMailbox.Type3.CommandOpcode = DAC960_V1_DCDB;
4137               Command->V1.CommandMailbox.Type3.BusAddress = DCDB_DMA;
4138               DCDB->Channel = Controller->V1.DeviceStateChannel;
4139               DCDB->TargetID = Controller->V1.DeviceStateTargetID;
4140               DCDB->Direction = DAC960_V1_DCDB_DataTransferDeviceToSystem;
4141               DCDB->EarlyStatus = false;
4142               DCDB->Timeout = DAC960_V1_DCDB_Timeout_10_seconds;
4143               DCDB->NoAutomaticRequestSense = false;
4144               DCDB->DisconnectPermitted = true;
4145               DCDB->TransferLength =
4146                 sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T);
4147               DCDB->BusAddress = NewInquiryUnitSerialNumberDMA;
4148               DCDB->CDBLength = 6;
4149               DCDB->TransferLengthHigh4 = 0;
4150               DCDB->SenseLength = sizeof(DCDB->SenseData);
4151               DCDB->CDB[0] = 0x12; /* INQUIRY */
4152               DCDB->CDB[1] = 1; /* EVPD = 1 */
4153               DCDB->CDB[2] = 0x80; /* Page Code */
4154               DCDB->CDB[3] = 0; /* Reserved */
4155               DCDB->CDB[4] = sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T);
4156               DCDB->CDB[5] = 0; /* Control */
4157               DAC960_QueueCommand(Command);
4158               return;
4159             }
4160           if (Controller->V1.StartDeviceStateScan)
4161             {
4162               Controller->V1.DeviceStateChannel = 0;
4163               Controller->V1.DeviceStateTargetID = 0;
4164               Controller->V1.StartDeviceStateScan = false;
4165             }
4166           else if (++Controller->V1.DeviceStateTargetID == Controller->Targets)
4167             {
4168               Controller->V1.DeviceStateChannel++;
4169               Controller->V1.DeviceStateTargetID = 0;
4170             }
4171           if (Controller->V1.DeviceStateChannel < Controller->Channels)
4172             {
4173               Controller->V1.NewDeviceState->DeviceState =
4174                 DAC960_V1_Device_Dead;
4175               Command->V1.CommandMailbox.Type3D.CommandOpcode =
4176                 DAC960_V1_GetDeviceState;
4177               Command->V1.CommandMailbox.Type3D.Channel =
4178                 Controller->V1.DeviceStateChannel;
4179               Command->V1.CommandMailbox.Type3D.TargetID =
4180                 Controller->V1.DeviceStateTargetID;
4181               Command->V1.CommandMailbox.Type3D.BusAddress =
4182                 Controller->V1.NewDeviceStateDMA;
4183               DAC960_QueueCommand(Command);
4184               return;
4185             }
4186           Controller->V1.NeedDeviceStateInformation = false;
4187         }
4188       if (Controller->V1.NeedLogicalDriveInformation)
4189         {
4190           Controller->V1.NeedLogicalDriveInformation = false;
4191           Command->V1.CommandMailbox.Type3.CommandOpcode =
4192             DAC960_V1_GetLogicalDriveInformation;
4193           Command->V1.CommandMailbox.Type3.BusAddress =
4194             Controller->V1.NewLogicalDriveInformationDMA;
4195           DAC960_QueueCommand(Command);
4196           return;
4197         }
4198       if (Controller->V1.NeedRebuildProgress)
4199         {
4200           Controller->V1.NeedRebuildProgress = false;
4201           Command->V1.CommandMailbox.Type3.CommandOpcode =
4202             DAC960_V1_GetRebuildProgress;
4203           Command->V1.CommandMailbox.Type3.BusAddress =
4204                 Controller->V1.RebuildProgressDMA;
4205           DAC960_QueueCommand(Command);
4206           return;
4207         }
4208       if (Controller->V1.NeedConsistencyCheckProgress)
4209         {
4210           Controller->V1.NeedConsistencyCheckProgress = false;
4211           Command->V1.CommandMailbox.Type3.CommandOpcode =
4212             DAC960_V1_RebuildStat;
4213           Command->V1.CommandMailbox.Type3.BusAddress =
4214             Controller->V1.RebuildProgressDMA;
4215           DAC960_QueueCommand(Command);
4216           return;
4217         }
4218       if (Controller->V1.NeedBackgroundInitializationStatus)
4219         {
4220           Controller->V1.NeedBackgroundInitializationStatus = false;
4221           Command->V1.CommandMailbox.Type3B.CommandOpcode =
4222             DAC960_V1_BackgroundInitializationControl;
4223           Command->V1.CommandMailbox.Type3B.CommandOpcode2 = 0x20;
4224           Command->V1.CommandMailbox.Type3B.BusAddress =
4225             Controller->V1.BackgroundInitializationStatusDMA;
4226           DAC960_QueueCommand(Command);
4227           return;
4228         }
4229       Controller->MonitoringTimerCount++;
4230       Controller->MonitoringTimer.expires =
4231         jiffies + DAC960_MonitoringTimerInterval;
4232         add_timer(&Controller->MonitoringTimer);
4233     }
4234   if (CommandType == DAC960_ImmediateCommand)
4235     {
4236       complete(Command->Completion);
4237       Command->Completion = NULL;
4238       return;
4239     }
4240   if (CommandType == DAC960_QueuedCommand)
4241     {
4242       DAC960_V1_KernelCommand_T *KernelCommand = Command->V1.KernelCommand;
4243       KernelCommand->CommandStatus = Command->V1.CommandStatus;
4244       Command->V1.KernelCommand = NULL;
4245       if (CommandOpcode == DAC960_V1_DCDB)
4246         Controller->V1.DirectCommandActive[KernelCommand->DCDB->Channel]
4247                                           [KernelCommand->DCDB->TargetID] =
4248           false;
4249       DAC960_DeallocateCommand(Command);
4250       KernelCommand->CompletionFunction(KernelCommand);
4251       return;
4252     }
4253   /*
4254     Queue a Status Monitoring Command to the Controller using the just
4255     completed Command if one was deferred previously due to lack of a
4256     free Command when the Monitoring Timer Function was called.
4257   */
4258   if (Controller->MonitoringCommandDeferred)
4259     {
4260       Controller->MonitoringCommandDeferred = false;
4261       DAC960_V1_QueueMonitoringCommand(Command);
4262       return;
4263     }
4264   /*
4265     Deallocate the Command.
4266   */
4267   DAC960_DeallocateCommand(Command);
4268   /*
4269     Wake up any processes waiting on a free Command.
4270   */
4271   wake_up(&Controller->CommandWaitQueue);
4272 }
4273
4274
4275 /*
4276   DAC960_V2_ReadWriteError prints an appropriate error message for Command
4277   when an error occurs on a Read or Write operation.
4278 */
4279
4280 static void DAC960_V2_ReadWriteError(DAC960_Command_T *Command)
4281 {
4282   DAC960_Controller_T *Controller = Command->Controller;
4283   unsigned char *SenseErrors[] = { "NO SENSE", "RECOVERED ERROR",
4284                                    "NOT READY", "MEDIUM ERROR",
4285                                    "HARDWARE ERROR", "ILLEGAL REQUEST",
4286                                    "UNIT ATTENTION", "DATA PROTECT",
4287                                    "BLANK CHECK", "VENDOR-SPECIFIC",
4288                                    "COPY ABORTED", "ABORTED COMMAND",
4289                                    "EQUAL", "VOLUME OVERFLOW",
4290                                    "MISCOMPARE", "RESERVED" };
4291   unsigned char *CommandName = "UNKNOWN";
4292   switch (Command->CommandType)
4293     {
4294     case DAC960_ReadCommand:
4295     case DAC960_ReadRetryCommand:
4296       CommandName = "READ";
4297       break;
4298     case DAC960_WriteCommand:
4299     case DAC960_WriteRetryCommand:
4300       CommandName = "WRITE";
4301       break;
4302     case DAC960_MonitoringCommand:
4303     case DAC960_ImmediateCommand:
4304     case DAC960_QueuedCommand:
4305       break;
4306     }
4307   DAC960_Error("Error Condition %s on %s:\n", Controller,
4308                SenseErrors[Command->V2.RequestSense->SenseKey], CommandName);
4309   DAC960_Error("  /dev/rd/c%dd%d:   absolute blocks %u..%u\n",
4310                Controller, Controller->ControllerNumber,
4311                Command->LogicalDriveNumber, Command->BlockNumber,
4312                Command->BlockNumber + Command->BlockCount - 1);
4313 }
4314
4315
4316 /*
4317   DAC960_V2_ReportEvent prints an appropriate message when a Controller Event
4318   occurs.
4319 */
4320
4321 static void DAC960_V2_ReportEvent(DAC960_Controller_T *Controller,
4322                                   DAC960_V2_Event_T *Event)
4323 {
4324   DAC960_SCSI_RequestSense_T *RequestSense =
4325     (DAC960_SCSI_RequestSense_T *) &Event->RequestSenseData;
4326   unsigned char MessageBuffer[DAC960_LineBufferSize];
4327   static struct { int EventCode; unsigned char *EventMessage; } EventList[] =
4328     { /* Physical Device Events (0x0000 - 0x007F) */
4329       { 0x0001, "P Online" },
4330       { 0x0002, "P Standby" },
4331       { 0x0005, "P Automatic Rebuild Started" },
4332       { 0x0006, "P Manual Rebuild Started" },
4333       { 0x0007, "P Rebuild Completed" },
4334       { 0x0008, "P Rebuild Cancelled" },
4335       { 0x0009, "P Rebuild Failed for Unknown Reasons" },
4336       { 0x000A, "P Rebuild Failed due to New Physical Device" },
4337       { 0x000B, "P Rebuild Failed due to Logical Drive Failure" },
4338       { 0x000C, "S Offline" },
4339       { 0x000D, "P Found" },
4340       { 0x000E, "P Removed" },
4341       { 0x000F, "P Unconfigured" },
4342       { 0x0010, "P Expand Capacity Started" },
4343       { 0x0011, "P Expand Capacity Completed" },
4344       { 0x0012, "P Expand Capacity Failed" },
4345       { 0x0013, "P Command Timed Out" },
4346       { 0x0014, "P Command Aborted" },
4347       { 0x0015, "P Command Retried" },
4348       { 0x0016, "P Parity Error" },
4349       { 0x0017, "P Soft Error" },
4350       { 0x0018, "P Miscellaneous Error" },
4351       { 0x0019, "P Reset" },
4352       { 0x001A, "P Active Spare Found" },
4353       { 0x001B, "P Warm Spare Found" },
4354       { 0x001C, "S Sense Data Received" },
4355       { 0x001D, "P Initialization Started" },
4356       { 0x001E, "P Initialization Completed" },
4357       { 0x001F, "P Initialization Failed" },
4358       { 0x0020, "P Initialization Cancelled" },
4359       { 0x0021, "P Failed because Write Recovery Failed" },
4360       { 0x0022, "P Failed because SCSI Bus Reset Failed" },
4361       { 0x0023, "P Failed because of Double Check Condition" },
4362       { 0x0024, "P Failed because Device Cannot Be Accessed" },
4363       { 0x0025, "P Failed because of Gross Error on SCSI Processor" },
4364       { 0x0026, "P Failed because of Bad Tag from Device" },
4365       { 0x0027, "P Failed because of Command Timeout" },
4366       { 0x0028, "P Failed because of System Reset" },
4367       { 0x0029, "P Failed because of Busy Status or Parity Error" },
4368       { 0x002A, "P Failed because Host Set Device to Failed State" },
4369       { 0x002B, "P Failed because of Selection Timeout" },
4370       { 0x002C, "P Failed because of SCSI Bus Phase Error" },
4371       { 0x002D, "P Failed because Device Returned Unknown Status" },
4372       { 0x002E, "P Failed because Device Not Ready" },
4373       { 0x002F, "P Failed because Device Not Found at Startup" },
4374       { 0x0030, "P Failed because COD Write Operation Failed" },
4375       { 0x0031, "P Failed because BDT Write Operation Failed" },
4376       { 0x0039, "P Missing at Startup" },
4377       { 0x003A, "P Start Rebuild Failed due to Physical Drive Too Small" },
4378       { 0x003C, "P Temporarily Offline Device Automatically Made Online" },
4379       { 0x003D, "P Standby Rebuild Started" },
4380       /* Logical Device Events (0x0080 - 0x00FF) */
4381       { 0x0080, "M Consistency Check Started" },
4382       { 0x0081, "M Consistency Check Completed" },
4383       { 0x0082, "M Consistency Check Cancelled" },
4384       { 0x0083, "M Consistency Check Completed With Errors" },
4385       { 0x0084, "M Consistency Check Failed due to Logical Drive Failure" },
4386       { 0x0085, "M Consistency Check Failed due to Physical Device Failure" },
4387       { 0x0086, "L Offline" },
4388       { 0x0087, "L Critical" },
4389       { 0x0088, "L Online" },
4390       { 0x0089, "M Automatic Rebuild Started" },
4391       { 0x008A, "M Manual Rebuild Started" },
4392       { 0x008B, "M Rebuild Completed" },
4393       { 0x008C, "M Rebuild Cancelled" },
4394       { 0x008D, "M Rebuild Failed for Unknown Reasons" },
4395       { 0x008E, "M Rebuild Failed due to New Physical Device" },
4396       { 0x008F, "M Rebuild Failed due to Logical Drive Failure" },
4397       { 0x0090, "M Initialization Started" },
4398       { 0x0091, "M Initialization Completed" },
4399       { 0x0092, "M Initialization Cancelled" },
4400       { 0x0093, "M Initialization Failed" },
4401       { 0x0094, "L Found" },
4402       { 0x0095, "L Deleted" },
4403       { 0x0096, "M Expand Capacity Started" },
4404       { 0x0097, "M Expand Capacity Completed" },
4405       { 0x0098, "M Expand Capacity Failed" },
4406       { 0x0099, "L Bad Block Found" },
4407       { 0x009A, "L Size Changed" },
4408       { 0x009B, "L Type Changed" },
4409       { 0x009C, "L Bad Data Block Found" },
4410       { 0x009E, "L Read of Data Block in BDT" },
4411       { 0x009F, "L Write Back Data for Disk Block Lost" },
4412       { 0x00A0, "L Temporarily Offline RAID-5/3 Drive Made Online" },
4413       { 0x00A1, "L Temporarily Offline RAID-6/1/0/7 Drive Made Online" },
4414       { 0x00A2, "L Standby Rebuild Started" },
4415       /* Fault Management Events (0x0100 - 0x017F) */
4416       { 0x0140, "E Fan %d Failed" },
4417       { 0x0141, "E Fan %d OK" },
4418       { 0x0142, "E Fan %d Not Present" },
4419       { 0x0143, "E Power Supply %d Failed" },
4420       { 0x0144, "E Power Supply %d OK" },
4421       { 0x0145, "E Power Supply %d Not Present" },
4422       { 0x0146, "E Temperature Sensor %d Temperature Exceeds Safe Limit" },
4423       { 0x0147, "E Temperature Sensor %d Temperature Exceeds Working Limit" },
4424       { 0x0148, "E Temperature Sensor %d Temperature Normal" },
4425       { 0x0149, "E Temperature Sensor %d Not Present" },
4426       { 0x014A, "E Enclosure Management Unit %d Access Critical" },
4427       { 0x014B, "E Enclosure Management Unit %d Access OK" },
4428       { 0x014C, "E Enclosure Management Unit %d Access Offline" },
4429       /* Controller Events (0x0180 - 0x01FF) */
4430       { 0x0181, "C Cache Write Back Error" },
4431       { 0x0188, "C Battery Backup Unit Found" },
4432       { 0x0189, "C Battery Backup Unit Charge Level Low" },
4433       { 0x018A, "C Battery Backup Unit Charge Level OK" },
4434       { 0x0193, "C Installation Aborted" },
4435       { 0x0195, "C Battery Backup Unit Physically Removed" },
4436       { 0x0196, "C Memory Error During Warm Boot" },
4437       { 0x019E, "C Memory Soft ECC Error Corrected" },
4438       { 0x019F, "C Memory Hard ECC Error Corrected" },
4439       { 0x01A2, "C Battery Backup Unit Failed" },
4440       { 0x01AB, "C Mirror Race Recovery Failed" },
4441       { 0x01AC, "C Mirror Race on Critical Drive" },
4442       /* Controller Internal Processor Events */
4443       { 0x0380, "C Internal Controller Hung" },
4444       { 0x0381, "C Internal Controller Firmware Breakpoint" },
4445       { 0x0390, "C Internal Controller i960 Processor Specific Error" },
4446       { 0x03A0, "C Internal Controller StrongARM Processor Specific Error" },
4447       { 0, "" } };
4448   int EventListIndex = 0, EventCode;
4449   unsigned char EventType, *EventMessage;
4450   if (Event->EventCode == 0x1C &&
4451       RequestSense->SenseKey == DAC960_SenseKey_VendorSpecific &&
4452       (RequestSense->AdditionalSenseCode == 0x80 ||
4453        RequestSense->AdditionalSenseCode == 0x81))
4454     Event->EventCode = ((RequestSense->AdditionalSenseCode - 0x80) << 8) |
4455                        RequestSense->AdditionalSenseCodeQualifier;
4456   while (true)
4457     {
4458       EventCode = EventList[EventListIndex].EventCode;
4459       if (EventCode == Event->EventCode || EventCode == 0) break;
4460       EventListIndex++;
4461     }
4462   EventType = EventList[EventListIndex].EventMessage[0];
4463   EventMessage = &EventList[EventListIndex].EventMessage[2];
4464   if (EventCode == 0)
4465     {
4466       DAC960_Critical("Unknown Controller Event Code %04X\n",
4467                       Controller, Event->EventCode);
4468       return;
4469     }
4470   switch (EventType)
4471     {
4472     case 'P':
4473       DAC960_Critical("Physical Device %d:%d %s\n", Controller,
4474                       Event->Channel, Event->TargetID, EventMessage);
4475       break;
4476     case 'L':
4477       DAC960_Critical("Logical Drive %d (/dev/rd/c%dd%d) %s\n", Controller,
4478                       Event->LogicalUnit, Controller->ControllerNumber,
4479                       Event->LogicalUnit, EventMessage);
4480       break;
4481     case 'M':
4482       DAC960_Progress("Logical Drive %d (/dev/rd/c%dd%d) %s\n", Controller,
4483                       Event->LogicalUnit, Controller->ControllerNumber,
4484                       Event->LogicalUnit, EventMessage);
4485       break;
4486     case 'S':
4487       if (RequestSense->SenseKey == DAC960_SenseKey_NoSense ||
4488           (RequestSense->SenseKey == DAC960_SenseKey_NotReady &&
4489            RequestSense->AdditionalSenseCode == 0x04 &&
4490            (RequestSense->AdditionalSenseCodeQualifier == 0x01 ||
4491             RequestSense->AdditionalSenseCodeQualifier == 0x02)))
4492         break;
4493       DAC960_Critical("Physical Device %d:%d %s\n", Controller,
4494                       Event->Channel, Event->TargetID, EventMessage);
4495       DAC960_Critical("Physical Device %d:%d Request Sense: "
4496                       "Sense Key = %X, ASC = %02X, ASCQ = %02X\n",
4497                       Controller,
4498                       Event->Channel,
4499                       Event->TargetID,
4500                       RequestSense->SenseKey,
4501                       RequestSense->AdditionalSenseCode,
4502                       RequestSense->AdditionalSenseCodeQualifier);
4503       DAC960_Critical("Physical Device %d:%d Request Sense: "
4504                       "Information = %02X%02X%02X%02X "
4505                       "%02X%02X%02X%02X\n",
4506                       Controller,
4507                       Event->Channel,
4508                       Event->TargetID,
4509                       RequestSense->Information[0],
4510                       RequestSense->Information[1],
4511                       RequestSense->Information[2],
4512                       RequestSense->Information[3],
4513                       RequestSense->CommandSpecificInformation[0],
4514                       RequestSense->CommandSpecificInformation[1],
4515                       RequestSense->CommandSpecificInformation[2],
4516                       RequestSense->CommandSpecificInformation[3]);
4517       break;
4518     case 'E':
4519       if (Controller->SuppressEnclosureMessages) break;
4520       sprintf(MessageBuffer, EventMessage, Event->LogicalUnit);
4521       DAC960_Critical("Enclosure %d %s\n", Controller,
4522                       Event->TargetID, MessageBuffer);
4523       break;
4524     case 'C':
4525       DAC960_Critical("Controller %s\n", Controller, EventMessage);
4526       break;
4527     default:
4528       DAC960_Critical("Unknown Controller Event Code %04X\n",
4529                       Controller, Event->EventCode);
4530       break;
4531     }
4532 }
4533
4534
4535 /*
4536   DAC960_V2_ReportProgress prints an appropriate progress message for
4537   Logical Device Long Operations.
4538 */
4539
4540 static void DAC960_V2_ReportProgress(DAC960_Controller_T *Controller,
4541                                      unsigned char *MessageString,
4542                                      unsigned int LogicalDeviceNumber,
4543                                      unsigned long BlocksCompleted,
4544                                      unsigned long LogicalDeviceSize)
4545 {
4546   Controller->EphemeralProgressMessage = true;
4547   DAC960_Progress("%s in Progress: Logical Drive %d (/dev/rd/c%dd%d) "
4548                   "%d%% completed\n", Controller,
4549                   MessageString,
4550                   LogicalDeviceNumber,
4551                   Controller->ControllerNumber,
4552                   LogicalDeviceNumber,
4553                   (100 * (BlocksCompleted >> 7)) / (LogicalDeviceSize >> 7));
4554   Controller->EphemeralProgressMessage = false;
4555 }
4556
4557
4558 /*
4559   DAC960_V2_ProcessCompletedCommand performs completion processing for Command
4560   for DAC960 V2 Firmware Controllers.
4561 */
4562
4563 static void DAC960_V2_ProcessCompletedCommand(DAC960_Command_T *Command)
4564 {
4565   DAC960_Controller_T *Controller = Command->Controller;
4566   DAC960_CommandType_T CommandType = Command->CommandType;
4567   DAC960_V2_CommandMailbox_T *CommandMailbox = &Command->V2.CommandMailbox;
4568   DAC960_V2_IOCTL_Opcode_T CommandOpcode = CommandMailbox->Common.IOCTL_Opcode;
4569   DAC960_V2_CommandStatus_T CommandStatus = Command->V2.CommandStatus;
4570
4571   if (CommandType == DAC960_ReadCommand ||
4572       CommandType == DAC960_WriteCommand)
4573     {
4574
4575 #ifdef FORCE_RETRY_DEBUG
4576       CommandStatus = DAC960_V2_AbormalCompletion;
4577 #endif
4578       Command->V2.RequestSense->SenseKey = DAC960_SenseKey_MediumError;
4579
4580       if (CommandStatus == DAC960_V2_NormalCompletion) {
4581
4582                 if (!DAC960_ProcessCompletedRequest(Command, true))
4583                         BUG();
4584
4585       } else if (Command->V2.RequestSense->SenseKey == DAC960_SenseKey_MediumError)
4586         {
4587           /*
4588            * break the command down into pieces and resubmit each
4589            * piece, hoping that some of them will succeed.
4590            */
4591            DAC960_queue_partial_rw(Command);
4592            return;
4593         }
4594       else
4595         {
4596           if (Command->V2.RequestSense->SenseKey != DAC960_SenseKey_NotReady)
4597             DAC960_V2_ReadWriteError(Command);
4598           /*
4599             Perform completion processing for all buffers in this I/O Request.
4600           */
4601           (void)DAC960_ProcessCompletedRequest(Command, false);
4602         }
4603     }
4604   else if (CommandType == DAC960_ReadRetryCommand ||
4605            CommandType == DAC960_WriteRetryCommand)
4606     {
4607       boolean normal_completion;
4608
4609 #ifdef FORCE_RETRY_FAILURE_DEBUG
4610       static int retry_count = 1;
4611 #endif
4612       /*
4613         Perform completion processing for the portion that was
4614         retried, and submit the next portion, if any.
4615       */
4616       normal_completion = true;
4617       if (CommandStatus != DAC960_V2_NormalCompletion) {
4618         normal_completion = false;
4619         if (Command->V2.RequestSense->SenseKey != DAC960_SenseKey_NotReady)
4620             DAC960_V2_ReadWriteError(Command);
4621       }
4622
4623 #ifdef FORCE_RETRY_FAILURE_DEBUG
4624       if (!(++retry_count % 10000)) {
4625               printk("V2 error retry failure test\n");
4626               normal_completion = false;
4627               DAC960_V2_ReadWriteError(Command);
4628       }
4629 #endif
4630
4631       if (!DAC960_ProcessCompletedRequest(Command, normal_completion)) {
4632                 DAC960_queue_partial_rw(Command);
4633                 return;
4634       }
4635     }
4636   else if (CommandType == DAC960_MonitoringCommand)
4637     {
4638       if (Controller->ShutdownMonitoringTimer)
4639               return;
4640       if (CommandOpcode == DAC960_V2_GetControllerInfo)
4641         {
4642           DAC960_V2_ControllerInfo_T *NewControllerInfo =
4643             Controller->V2.NewControllerInformation;
4644           DAC960_V2_ControllerInfo_T *ControllerInfo =
4645             &Controller->V2.ControllerInformation;
4646           Controller->LogicalDriveCount =
4647             NewControllerInfo->LogicalDevicesPresent;
4648           Controller->V2.NeedLogicalDeviceInformation = true;
4649           Controller->V2.NeedPhysicalDeviceInformation = true;
4650           Controller->V2.StartLogicalDeviceInformationScan = true;
4651           Controller->V2.StartPhysicalDeviceInformationScan = true;
4652           Controller->MonitoringAlertMode =
4653             (NewControllerInfo->LogicalDevicesCritical > 0 ||
4654              NewControllerInfo->LogicalDevicesOffline > 0 ||
4655              NewControllerInfo->PhysicalDisksCritical > 0 ||
4656              NewControllerInfo->PhysicalDisksOffline > 0);
4657           memcpy(ControllerInfo, NewControllerInfo,
4658                  sizeof(DAC960_V2_ControllerInfo_T));
4659         }
4660       else if (CommandOpcode == DAC960_V2_GetEvent)
4661         {
4662           if (CommandStatus == DAC960_V2_NormalCompletion) {
4663             DAC960_V2_ReportEvent(Controller, Controller->V2.Event);
4664           }
4665           Controller->V2.NextEventSequenceNumber++;
4666         }
4667       else if (CommandOpcode == DAC960_V2_GetPhysicalDeviceInfoValid &&
4668                CommandStatus == DAC960_V2_NormalCompletion)
4669         {
4670           DAC960_V2_PhysicalDeviceInfo_T *NewPhysicalDeviceInfo =
4671             Controller->V2.NewPhysicalDeviceInformation;
4672           unsigned int PhysicalDeviceIndex = Controller->V2.PhysicalDeviceIndex;
4673           DAC960_V2_PhysicalDeviceInfo_T *PhysicalDeviceInfo =
4674             Controller->V2.PhysicalDeviceInformation[PhysicalDeviceIndex];
4675           DAC960_SCSI_Inquiry_UnitSerialNumber_T *InquiryUnitSerialNumber =
4676             Controller->V2.InquiryUnitSerialNumber[PhysicalDeviceIndex];
4677           unsigned int DeviceIndex;
4678           while (PhysicalDeviceInfo != NULL &&
4679                  (NewPhysicalDeviceInfo->Channel >
4680                   PhysicalDeviceInfo->Channel ||
4681                   (NewPhysicalDeviceInfo->Channel ==
4682                    PhysicalDeviceInfo->Channel &&
4683                    (NewPhysicalDeviceInfo->TargetID >
4684                     PhysicalDeviceInfo->TargetID ||
4685                    (NewPhysicalDeviceInfo->TargetID ==
4686                     PhysicalDeviceInfo->TargetID &&
4687                     NewPhysicalDeviceInfo->LogicalUnit >
4688                     PhysicalDeviceInfo->LogicalUnit)))))
4689             {
4690               DAC960_Critical("Physical Device %d:%d No Longer Exists\n",
4691                               Controller,
4692                               PhysicalDeviceInfo->Channel,
4693                               PhysicalDeviceInfo->TargetID);
4694               Controller->V2.PhysicalDeviceInformation
4695                              [PhysicalDeviceIndex] = NULL;
4696               Controller->V2.InquiryUnitSerialNumber
4697                              [PhysicalDeviceIndex] = NULL;
4698               kfree(PhysicalDeviceInfo);
4699               kfree(InquiryUnitSerialNumber);
4700               for (DeviceIndex = PhysicalDeviceIndex;
4701                    DeviceIndex < DAC960_V2_MaxPhysicalDevices - 1;
4702                    DeviceIndex++)
4703                 {
4704                   Controller->V2.PhysicalDeviceInformation[DeviceIndex] =
4705                     Controller->V2.PhysicalDeviceInformation[DeviceIndex+1];
4706                   Controller->V2.InquiryUnitSerialNumber[DeviceIndex] =
4707                     Controller->V2.InquiryUnitSerialNumber[DeviceIndex+1];
4708                 }
4709               Controller->V2.PhysicalDeviceInformation
4710                              [DAC960_V2_MaxPhysicalDevices-1] = NULL;
4711               Controller->V2.InquiryUnitSerialNumber
4712                              [DAC960_V2_MaxPhysicalDevices-1] = NULL;
4713               PhysicalDeviceInfo =
4714                 Controller->V2.PhysicalDeviceInformation[PhysicalDeviceIndex];
4715               InquiryUnitSerialNumber =
4716                 Controller->V2.InquiryUnitSerialNumber[PhysicalDeviceIndex];
4717             }
4718           if (PhysicalDeviceInfo == NULL ||
4719               (NewPhysicalDeviceInfo->Channel !=
4720                PhysicalDeviceInfo->Channel) ||
4721               (NewPhysicalDeviceInfo->TargetID !=
4722                PhysicalDeviceInfo->TargetID) ||
4723               (NewPhysicalDeviceInfo->LogicalUnit !=
4724                PhysicalDeviceInfo->LogicalUnit))
4725             {
4726               PhysicalDeviceInfo = (DAC960_V2_PhysicalDeviceInfo_T *)
4727                 kmalloc(sizeof(DAC960_V2_PhysicalDeviceInfo_T), GFP_ATOMIC);
4728               InquiryUnitSerialNumber =
4729                 (DAC960_SCSI_Inquiry_UnitSerialNumber_T *)
4730                   kmalloc(sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T),
4731                           GFP_ATOMIC);
4732               if (InquiryUnitSerialNumber == NULL &&
4733                   PhysicalDeviceInfo != NULL)
4734                 {
4735                   kfree(PhysicalDeviceInfo);
4736                   PhysicalDeviceInfo = NULL;
4737                 }
4738               DAC960_Critical("Physical Device %d:%d Now Exists%s\n",
4739                               Controller,
4740                               NewPhysicalDeviceInfo->Channel,
4741                               NewPhysicalDeviceInfo->TargetID,
4742                               (PhysicalDeviceInfo != NULL
4743                                ? "" : " - Allocation Failed"));
4744               if (PhysicalDeviceInfo != NULL)
4745                 {
4746                   memset(PhysicalDeviceInfo, 0,
4747                          sizeof(DAC960_V2_PhysicalDeviceInfo_T));
4748                   PhysicalDeviceInfo->PhysicalDeviceState =
4749                     DAC960_V2_Device_InvalidState;
4750                   memset(InquiryUnitSerialNumber, 0,
4751                          sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T));
4752                   InquiryUnitSerialNumber->PeripheralDeviceType = 0x1F;
4753                   for (DeviceIndex = DAC960_V2_MaxPhysicalDevices - 1;
4754                        DeviceIndex > PhysicalDeviceIndex;
4755                        DeviceIndex--)
4756                     {
4757                       Controller->V2.PhysicalDeviceInformation[DeviceIndex] =
4758                         Controller->V2.PhysicalDeviceInformation[DeviceIndex-1];
4759                       Controller->V2.InquiryUnitSerialNumber[DeviceIndex] =
4760                         Controller->V2.InquiryUnitSerialNumber[DeviceIndex-1];
4761                     }
4762                   Controller->V2.PhysicalDeviceInformation
4763                                  [PhysicalDeviceIndex] =
4764                     PhysicalDeviceInfo;
4765                   Controller->V2.InquiryUnitSerialNumber
4766                                  [PhysicalDeviceIndex] =
4767                     InquiryUnitSerialNumber;
4768                   Controller->V2.NeedDeviceSerialNumberInformation = true;
4769                 }
4770             }
4771           if (PhysicalDeviceInfo != NULL)
4772             {
4773               if (NewPhysicalDeviceInfo->PhysicalDeviceState !=
4774                   PhysicalDeviceInfo->PhysicalDeviceState)
4775                 DAC960_Critical(
4776                   "Physical Device %d:%d is now %s\n", Controller,
4777                   NewPhysicalDeviceInfo->Channel,
4778                   NewPhysicalDeviceInfo->TargetID,
4779                   (NewPhysicalDeviceInfo->PhysicalDeviceState
4780                    == DAC960_V2_Device_Online
4781                    ? "ONLINE"
4782                    : NewPhysicalDeviceInfo->PhysicalDeviceState
4783                      == DAC960_V2_Device_Rebuild
4784                      ? "REBUILD"
4785                      : NewPhysicalDeviceInfo->PhysicalDeviceState
4786                        == DAC960_V2_Device_Missing
4787                        ? "MISSING"
4788                        : NewPhysicalDeviceInfo->PhysicalDeviceState
4789                          == DAC960_V2_Device_Critical
4790                          ? "CRITICAL"
4791                          : NewPhysicalDeviceInfo->PhysicalDeviceState
4792                            == DAC960_V2_Device_Dead
4793                            ? "DEAD"
4794                            : NewPhysicalDeviceInfo->PhysicalDeviceState
4795                              == DAC960_V2_Device_SuspectedDead
4796                              ? "SUSPECTED-DEAD"
4797                              : NewPhysicalDeviceInfo->PhysicalDeviceState
4798                                == DAC960_V2_Device_CommandedOffline
4799                                ? "COMMANDED-OFFLINE"
4800                                : NewPhysicalDeviceInfo->PhysicalDeviceState
4801                                  == DAC960_V2_Device_Standby
4802                                  ? "STANDBY" : "UNKNOWN"));
4803               if ((NewPhysicalDeviceInfo->ParityErrors !=
4804                    PhysicalDeviceInfo->ParityErrors) ||
4805                   (NewPhysicalDeviceInfo->SoftErrors !=
4806                    PhysicalDeviceInfo->SoftErrors) ||
4807                   (NewPhysicalDeviceInfo->HardErrors !=
4808                    PhysicalDeviceInfo->HardErrors) ||
4809                   (NewPhysicalDeviceInfo->MiscellaneousErrors !=
4810                    PhysicalDeviceInfo->MiscellaneousErrors) ||
4811                   (NewPhysicalDeviceInfo->CommandTimeouts !=
4812                    PhysicalDeviceInfo->CommandTimeouts) ||
4813                   (NewPhysicalDeviceInfo->Retries !=
4814                    PhysicalDeviceInfo->Retries) ||
4815                   (NewPhysicalDeviceInfo->Aborts !=
4816                    PhysicalDeviceInfo->Aborts) ||
4817                   (NewPhysicalDeviceInfo->PredictedFailuresDetected !=
4818                    PhysicalDeviceInfo->PredictedFailuresDetected))
4819                 {
4820                   DAC960_Critical("Physical Device %d:%d Errors: "
4821                                   "Parity = %d, Soft = %d, "
4822                                   "Hard = %d, Misc = %d\n",
4823                                   Controller,
4824                                   NewPhysicalDeviceInfo->Channel,
4825                                   NewPhysicalDeviceInfo->TargetID,
4826                                   NewPhysicalDeviceInfo->ParityErrors,
4827                                   NewPhysicalDeviceInfo->SoftErrors,
4828                                   NewPhysicalDeviceInfo->HardErrors,
4829                                   NewPhysicalDeviceInfo->MiscellaneousErrors);
4830                   DAC960_Critical("Physical Device %d:%d Errors: "
4831                                   "Timeouts = %d, Retries = %d, "
4832                                   "Aborts = %d, Predicted = %d\n",
4833                                   Controller,
4834                                   NewPhysicalDeviceInfo->Channel,
4835                                   NewPhysicalDeviceInfo->TargetID,
4836                                   NewPhysicalDeviceInfo->CommandTimeouts,
4837                                   NewPhysicalDeviceInfo->Retries,
4838                                   NewPhysicalDeviceInfo->Aborts,
4839                                   NewPhysicalDeviceInfo
4840                                   ->PredictedFailuresDetected);
4841                 }
4842               if ((PhysicalDeviceInfo->PhysicalDeviceState
4843                    == DAC960_V2_Device_Dead ||
4844                    PhysicalDeviceInfo->PhysicalDeviceState
4845                    == DAC960_V2_Device_InvalidState) &&
4846                   NewPhysicalDeviceInfo->PhysicalDeviceState
4847                   != DAC960_V2_Device_Dead)
4848                 Controller->V2.NeedDeviceSerialNumberInformation = true;
4849               memcpy(PhysicalDeviceInfo, NewPhysicalDeviceInfo,
4850                      sizeof(DAC960_V2_PhysicalDeviceInfo_T));
4851             }
4852           NewPhysicalDeviceInfo->LogicalUnit++;
4853           Controller->V2.PhysicalDeviceIndex++;
4854         }
4855       else if (CommandOpcode == DAC960_V2_GetPhysicalDeviceInfoValid)
4856         {
4857           unsigned int DeviceIndex;
4858           for (DeviceIndex = Controller->V2.PhysicalDeviceIndex;
4859                DeviceIndex < DAC960_V2_MaxPhysicalDevices;
4860                DeviceIndex++)
4861             {
4862               DAC960_V2_PhysicalDeviceInfo_T *PhysicalDeviceInfo =
4863                 Controller->V2.PhysicalDeviceInformation[DeviceIndex];
4864               DAC960_SCSI_Inquiry_UnitSerialNumber_T *InquiryUnitSerialNumber =
4865                 Controller->V2.InquiryUnitSerialNumber[DeviceIndex];
4866               if (PhysicalDeviceInfo == NULL) break;
4867               DAC960_Critical("Physical Device %d:%d No Longer Exists\n",
4868                               Controller,
4869                               PhysicalDeviceInfo->Channel,
4870                               PhysicalDeviceInfo->TargetID);
4871               Controller->V2.PhysicalDeviceInformation[DeviceIndex] = NULL;
4872               Controller->V2.InquiryUnitSerialNumber[DeviceIndex] = NULL;
4873               kfree(PhysicalDeviceInfo);
4874               kfree(InquiryUnitSerialNumber);
4875             }
4876           Controller->V2.NeedPhysicalDeviceInformation = false;
4877         }
4878       else if (CommandOpcode == DAC960_V2_GetLogicalDeviceInfoValid &&
4879                CommandStatus == DAC960_V2_NormalCompletion)
4880         {
4881           DAC960_V2_LogicalDeviceInfo_T *NewLogicalDeviceInfo =
4882             Controller->V2.NewLogicalDeviceInformation;
4883           unsigned short LogicalDeviceNumber =
4884             NewLogicalDeviceInfo->LogicalDeviceNumber;
4885           DAC960_V2_LogicalDeviceInfo_T *LogicalDeviceInfo =
4886             Controller->V2.LogicalDeviceInformation[LogicalDeviceNumber];
4887           if (LogicalDeviceInfo == NULL)
4888             {
4889               DAC960_V2_PhysicalDevice_T PhysicalDevice;
4890               PhysicalDevice.Controller = 0;
4891               PhysicalDevice.Channel = NewLogicalDeviceInfo->Channel;
4892               PhysicalDevice.TargetID = NewLogicalDeviceInfo->TargetID;
4893               PhysicalDevice.LogicalUnit = NewLogicalDeviceInfo->LogicalUnit;
4894               Controller->V2.LogicalDriveToVirtualDevice[LogicalDeviceNumber] =
4895                 PhysicalDevice;
4896               LogicalDeviceInfo = (DAC960_V2_LogicalDeviceInfo_T *)
4897                 kmalloc(sizeof(DAC960_V2_LogicalDeviceInfo_T), GFP_ATOMIC);
4898               Controller->V2.LogicalDeviceInformation[LogicalDeviceNumber] =
4899                 LogicalDeviceInfo;
4900               DAC960_Critical("Logical Drive %d (/dev/rd/c%dd%d) "
4901                               "Now Exists%s\n", Controller,
4902                               LogicalDeviceNumber,
4903                               Controller->ControllerNumber,
4904                               LogicalDeviceNumber,
4905                               (LogicalDeviceInfo != NULL
4906                                ? "" : " - Allocation Failed"));
4907               if (LogicalDeviceInfo != NULL)
4908                 {
4909                   memset(LogicalDeviceInfo, 0,
4910                          sizeof(DAC960_V2_LogicalDeviceInfo_T));
4911                   DAC960_ComputeGenericDiskInfo(Controller);
4912                 }
4913             }
4914           if (LogicalDeviceInfo != NULL)
4915             {
4916               unsigned long LogicalDeviceSize =
4917                 NewLogicalDeviceInfo->ConfigurableDeviceSize;
4918               if (NewLogicalDeviceInfo->LogicalDeviceState !=
4919                   LogicalDeviceInfo->LogicalDeviceState)
4920                 DAC960_Critical("Logical Drive %d (/dev/rd/c%dd%d) "
4921                                 "is now %s\n", Controller,
4922                                 LogicalDeviceNumber,
4923                                 Controller->ControllerNumber,
4924                                 LogicalDeviceNumber,
4925                                 (NewLogicalDeviceInfo->LogicalDeviceState
4926                                  == DAC960_V2_LogicalDevice_Online
4927                                  ? "ONLINE"
4928                                  : NewLogicalDeviceInfo->LogicalDeviceState
4929                                    == DAC960_V2_LogicalDevice_Critical
4930                                    ? "CRITICAL" : "OFFLINE"));
4931               if ((NewLogicalDeviceInfo->SoftErrors !=
4932                    LogicalDeviceInfo->SoftErrors) ||
4933                   (NewLogicalDeviceInfo->CommandsFailed !=
4934                    LogicalDeviceInfo->CommandsFailed) ||
4935                   (NewLogicalDeviceInfo->DeferredWriteErrors !=
4936                    LogicalDeviceInfo->DeferredWriteErrors))
4937                 DAC960_Critical("Logical Drive %d (/dev/rd/c%dd%d) Errors: "
4938                                 "Soft = %d, Failed = %d, Deferred Write = %d\n",
4939                                 Controller, LogicalDeviceNumber,
4940                                 Controller->ControllerNumber,
4941                                 LogicalDeviceNumber,
4942                                 NewLogicalDeviceInfo->SoftErrors,
4943                                 NewLogicalDeviceInfo->CommandsFailed,
4944                                 NewLogicalDeviceInfo->DeferredWriteErrors);
4945               if (NewLogicalDeviceInfo->ConsistencyCheckInProgress)
4946                 DAC960_V2_ReportProgress(Controller,
4947                                          "Consistency Check",
4948                                          LogicalDeviceNumber,
4949                                          NewLogicalDeviceInfo
4950                                          ->ConsistencyCheckBlockNumber,
4951                                          LogicalDeviceSize);
4952               else if (NewLogicalDeviceInfo->RebuildInProgress)
4953                 DAC960_V2_ReportProgress(Controller,
4954                                          "Rebuild",
4955                                          LogicalDeviceNumber,
4956                                          NewLogicalDeviceInfo
4957                                          ->RebuildBlockNumber,
4958                                          LogicalDeviceSize);
4959               else if (NewLogicalDeviceInfo->BackgroundInitializationInProgress)
4960                 DAC960_V2_ReportProgress(Controller,
4961                                          "Background Initialization",
4962                                          LogicalDeviceNumber,
4963                                          NewLogicalDeviceInfo
4964                                          ->BackgroundInitializationBlockNumber,
4965                                          LogicalDeviceSize);
4966               else if (NewLogicalDeviceInfo->ForegroundInitializationInProgress)
4967                 DAC960_V2_ReportProgress(Controller,
4968                                          "Foreground Initialization",
4969                                          LogicalDeviceNumber,
4970                                          NewLogicalDeviceInfo
4971                                          ->ForegroundInitializationBlockNumber,
4972                                          LogicalDeviceSize);
4973               else if (NewLogicalDeviceInfo->DataMigrationInProgress)
4974                 DAC960_V2_ReportProgress(Controller,
4975                                          "Data Migration",
4976                                          LogicalDeviceNumber,
4977                                          NewLogicalDeviceInfo
4978                                          ->DataMigrationBlockNumber,
4979                                          LogicalDeviceSize);
4980               else if (NewLogicalDeviceInfo->PatrolOperationInProgress)
4981                 DAC960_V2_ReportProgress(Controller,
4982                                          "Patrol Operation",
4983                                          LogicalDeviceNumber,
4984                                          NewLogicalDeviceInfo
4985                                          ->PatrolOperationBlockNumber,
4986                                          LogicalDeviceSize);
4987               if (LogicalDeviceInfo->BackgroundInitializationInProgress &&
4988                   !NewLogicalDeviceInfo->BackgroundInitializationInProgress)
4989                 DAC960_Progress("Logical Drive %d (/dev/rd/c%dd%d) "
4990                                 "Background Initialization %s\n",
4991                                 Controller,
4992                                 LogicalDeviceNumber,
4993                                 Controller->ControllerNumber,
4994                                 LogicalDeviceNumber,
4995                                 (NewLogicalDeviceInfo->LogicalDeviceControl
4996                                                       .LogicalDeviceInitialized
4997                                  ? "Completed" : "Failed"));
4998               memcpy(LogicalDeviceInfo, NewLogicalDeviceInfo,
4999                      sizeof(DAC960_V2_LogicalDeviceInfo_T));
5000             }
5001           Controller->V2.LogicalDriveFoundDuringScan
5002                          [LogicalDeviceNumber] = true;
5003           NewLogicalDeviceInfo->LogicalDeviceNumber++;
5004         }
5005       else if (CommandOpcode == DAC960_V2_GetLogicalDeviceInfoValid)
5006         {
5007           int LogicalDriveNumber;
5008           for (LogicalDriveNumber = 0;
5009                LogicalDriveNumber < DAC960_MaxLogicalDrives;
5010                LogicalDriveNumber++)
5011             {
5012               DAC960_V2_LogicalDeviceInfo_T *LogicalDeviceInfo =
5013                 Controller->V2.LogicalDeviceInformation[LogicalDriveNumber];
5014               if (LogicalDeviceInfo == NULL ||
5015                   Controller->V2.LogicalDriveFoundDuringScan
5016                                  [LogicalDriveNumber])
5017                 continue;
5018               DAC960_Critical("Logical Drive %d (/dev/rd/c%dd%d) "
5019                               "No Longer Exists\n", Controller,
5020                               LogicalDriveNumber,
5021                               Controller->ControllerNumber,
5022                               LogicalDriveNumber);
5023               Controller->V2.LogicalDeviceInformation
5024                              [LogicalDriveNumber] = NULL;
5025               kfree(LogicalDeviceInfo);
5026               Controller->LogicalDriveInitiallyAccessible
5027                           [LogicalDriveNumber] = false;
5028               DAC960_ComputeGenericDiskInfo(Controller);
5029             }
5030           Controller->V2.NeedLogicalDeviceInformation = false;
5031         }
5032       else if (CommandOpcode == DAC960_V2_SCSI_10_Passthru)
5033         {
5034             DAC960_SCSI_Inquiry_UnitSerialNumber_T *InquiryUnitSerialNumber =
5035                 Controller->V2.InquiryUnitSerialNumber[Controller->V2.PhysicalDeviceIndex - 1];
5036
5037             if (CommandStatus != DAC960_V2_NormalCompletion) {
5038                 memset(InquiryUnitSerialNumber,
5039                         0, sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T));
5040                 InquiryUnitSerialNumber->PeripheralDeviceType = 0x1F;
5041             } else
5042                 memcpy(InquiryUnitSerialNumber,
5043                         Controller->V2.NewInquiryUnitSerialNumber,
5044                         sizeof(DAC960_SCSI_Inquiry_UnitSerialNumber_T));
5045
5046              Controller->V2.NeedDeviceSerialNumberInformation = false;
5047         }
5048
5049       if (Controller->V2.HealthStatusBuffer->NextEventSequenceNumber
5050           - Controller->V2.NextEventSequenceNumber > 0)
5051         {
5052           CommandMailbox->GetEvent.CommandOpcode = DAC960_V2_IOCTL;
5053           CommandMailbox->GetEvent.DataTransferSize = sizeof(DAC960_V2_Event_T);
5054           CommandMailbox->GetEvent.EventSequenceNumberHigh16 =
5055             Controller->V2.NextEventSequenceNumber >> 16;
5056           CommandMailbox->GetEvent.ControllerNumber = 0;
5057           CommandMailbox->GetEvent.IOCTL_Opcode =
5058             DAC960_V2_GetEvent;
5059           CommandMailbox->GetEvent.EventSequenceNumberLow16 =
5060             Controller->V2.NextEventSequenceNumber & 0xFFFF;
5061           CommandMailbox->GetEvent.DataTransferMemoryAddress
5062                                   .ScatterGatherSegments[0]
5063                                   .SegmentDataPointer =
5064             Controller->V2.EventDMA;
5065           CommandMailbox->GetEvent.DataTransferMemoryAddress
5066                                   .ScatterGatherSegments[0]
5067                                   .SegmentByteCount =
5068             CommandMailbox->GetEvent.DataTransferSize;
5069           DAC960_QueueCommand(Command);
5070           return;
5071         }
5072       if (Controller->V2.NeedPhysicalDeviceInformation)
5073         {
5074           if (Controller->V2.NeedDeviceSerialNumberInformation)
5075             {
5076               DAC960_SCSI_Inquiry_UnitSerialNumber_T *InquiryUnitSerialNumber =
5077                 Controller->V2.NewInquiryUnitSerialNumber;
5078               InquiryUnitSerialNumber->PeripheralDeviceType = 0x1F;
5079
5080               DAC960_V2_ConstructNewUnitSerialNumber(Controller, CommandMailbox,
5081                         Controller->V2.NewPhysicalDeviceInformation->Channel,
5082                         Controller->V2.NewPhysicalDeviceInformation->TargetID,
5083                 Controller->V2.NewPhysicalDeviceInformation->LogicalUnit - 1);
5084
5085
5086               DAC960_QueueCommand(Command);
5087               return;
5088             }
5089           if (Controller->V2.StartPhysicalDeviceInformationScan)
5090             {
5091               Controller->V2.PhysicalDeviceIndex = 0;
5092               Controller->V2.NewPhysicalDeviceInformation->Channel = 0;
5093               Controller->V2.NewPhysicalDeviceInformation->TargetID = 0;
5094               Controller->V2.NewPhysicalDeviceInformation->LogicalUnit = 0;
5095               Controller->V2.StartPhysicalDeviceInformationScan = false;
5096             }
5097           CommandMailbox->PhysicalDeviceInfo.CommandOpcode = DAC960_V2_IOCTL;
5098           CommandMailbox->PhysicalDeviceInfo.DataTransferSize =
5099             sizeof(DAC960_V2_PhysicalDeviceInfo_T);
5100           CommandMailbox->PhysicalDeviceInfo.PhysicalDevice.LogicalUnit =
5101             Controller->V2.NewPhysicalDeviceInformation->LogicalUnit;
5102           CommandMailbox->PhysicalDeviceInfo.PhysicalDevice.TargetID =
5103             Controller->V2.NewPhysicalDeviceInformation->TargetID;
5104           CommandMailbox->PhysicalDeviceInfo.PhysicalDevice.Channel =
5105             Controller->V2.NewPhysicalDeviceInformation->Channel;
5106           CommandMailbox->PhysicalDeviceInfo.IOCTL_Opcode =
5107             DAC960_V2_GetPhysicalDeviceInfoValid;
5108           CommandMailbox->PhysicalDeviceInfo.DataTransferMemoryAddress
5109                                             .ScatterGatherSegments[0]
5110                                             .SegmentDataPointer =
5111             Controller->V2.NewPhysicalDeviceInformationDMA;
5112           CommandMailbox->PhysicalDeviceInfo.DataTransferMemoryAddress
5113                                             .ScatterGatherSegments[0]
5114                                             .SegmentByteCount =
5115             CommandMailbox->PhysicalDeviceInfo.DataTransferSize;
5116           DAC960_QueueCommand(Command);
5117           return;
5118         }
5119       if (Controller->V2.NeedLogicalDeviceInformation)
5120         {
5121           if (Controller->V2.StartLogicalDeviceInformationScan)
5122             {
5123               int LogicalDriveNumber;
5124               for (LogicalDriveNumber = 0;
5125                    LogicalDriveNumber < DAC960_MaxLogicalDrives;
5126                    LogicalDriveNumber++)
5127                 Controller->V2.LogicalDriveFoundDuringScan
5128                                [LogicalDriveNumber] = false;
5129               Controller->V2.NewLogicalDeviceInformation->LogicalDeviceNumber = 0;
5130               Controller->V2.StartLogicalDeviceInformationScan = false;
5131             }
5132           CommandMailbox->LogicalDeviceInfo.CommandOpcode = DAC960_V2_IOCTL;
5133           CommandMailbox->LogicalDeviceInfo.DataTransferSize =
5134             sizeof(DAC960_V2_LogicalDeviceInfo_T);
5135           CommandMailbox->LogicalDeviceInfo.LogicalDevice.LogicalDeviceNumber =
5136             Controller->V2.NewLogicalDeviceInformation->LogicalDeviceNumber;
5137           CommandMailbox->LogicalDeviceInfo.IOCTL_Opcode =
5138             DAC960_V2_GetLogicalDeviceInfoValid;
5139           CommandMailbox->LogicalDeviceInfo.DataTransferMemoryAddress
5140                                            .ScatterGatherSegments[0]
5141                                            .SegmentDataPointer =
5142             Controller->V2.NewLogicalDeviceInformationDMA;
5143           CommandMailbox->LogicalDeviceInfo.DataTransferMemoryAddress
5144                                            .ScatterGatherSegments[0]
5145                                            .SegmentByteCount =
5146             CommandMailbox->LogicalDeviceInfo.DataTransferSize;
5147           DAC960_QueueCommand(Command);
5148           return;
5149         }
5150       Controller->MonitoringTimerCount++;
5151       Controller->MonitoringTimer.expires =
5152         jiffies + DAC960_HealthStatusMonitoringInterval;
5153         add_timer(&Controller->MonitoringTimer);
5154     }
5155   if (CommandType == DAC960_ImmediateCommand)
5156     {
5157       complete(Command->Completion);
5158       Command->Completion = NULL;
5159       return;
5160     }
5161   if (CommandType == DAC960_QueuedCommand)
5162     {
5163       DAC960_V2_KernelCommand_T *KernelCommand = Command->V2.KernelCommand;
5164       KernelCommand->CommandStatus = CommandStatus;
5165       KernelCommand->RequestSenseLength = Command->V2.RequestSenseLength;
5166       KernelCommand->DataTransferLength = Command->V2.DataTransferResidue;
5167       Command->V2.KernelCommand = NULL;
5168       DAC960_DeallocateCommand(Command);
5169       KernelCommand->CompletionFunction(KernelCommand);
5170       return;
5171     }
5172   /*
5173     Queue a Status Monitoring Command to the Controller using the just
5174     completed Command if one was deferred previously due to lack of a
5175     free Command when the Monitoring Timer Function was called.
5176   */
5177   if (Controller->MonitoringCommandDeferred)
5178     {
5179       Controller->MonitoringCommandDeferred = false;
5180       DAC960_V2_QueueMonitoringCommand(Command);
5181       return;
5182     }
5183   /*
5184     Deallocate the Command.
5185   */
5186   DAC960_DeallocateCommand(Command);
5187   /*
5188     Wake up any processes waiting on a free Command.
5189   */
5190   wake_up(&Controller->CommandWaitQueue);
5191 }
5192
5193
5194 /*
5195   DAC960_BA_InterruptHandler handles hardware interrupts from DAC960 BA Series
5196   Controllers.
5197 */
5198
5199 static irqreturn_t DAC960_BA_InterruptHandler(int IRQ_Channel,
5200                                        void *DeviceIdentifier,
5201                                        struct pt_regs *InterruptRegisters)
5202 {
5203   DAC960_Controller_T *Controller = (DAC960_Controller_T *) DeviceIdentifier;
5204   void __iomem *ControllerBaseAddress = Controller->BaseAddress;
5205   DAC960_V2_StatusMailbox_T *NextStatusMailbox;
5206   unsigned long flags;
5207
5208   spin_lock_irqsave(&Controller->queue_lock, flags);
5209   DAC960_BA_AcknowledgeInterrupt(ControllerBaseAddress);
5210   NextStatusMailbox = Controller->V2.NextStatusMailbox;
5211   while (NextStatusMailbox->Fields.CommandIdentifier > 0)
5212     {
5213       DAC960_V2_CommandIdentifier_T CommandIdentifier =
5214         NextStatusMailbox->Fields.CommandIdentifier;
5215       DAC960_Command_T *Command = Controller->Commands[CommandIdentifier-1];
5216       Command->V2.CommandStatus = NextStatusMailbox->Fields.CommandStatus;
5217       Command->V2.RequestSenseLength =
5218         NextStatusMailbox->Fields.RequestSenseLength;
5219       Command->V2.DataTransferResidue =
5220         NextStatusMailbox->Fields.DataTransferResidue;
5221       NextStatusMailbox->Words[0] = 0;
5222       if (++NextStatusMailbox > Controller->V2.LastStatusMailbox)
5223         NextStatusMailbox = Controller->V2.FirstStatusMailbox;
5224       DAC960_V2_ProcessCompletedCommand(Command);
5225     }
5226   Controller->V2.NextStatusMailbox = NextStatusMailbox;
5227   /*
5228     Attempt to remove additional I/O Requests from the Controller's
5229     I/O Request Queue and queue them to the Controller.
5230   */
5231   DAC960_ProcessRequest(Controller);
5232   spin_unlock_irqrestore(&Controller->queue_lock, flags);
5233   return IRQ_HANDLED;
5234 }
5235
5236
5237 /*
5238   DAC960_LP_InterruptHandler handles hardware interrupts from DAC960 LP Series
5239   Controllers.
5240 */
5241
5242 static irqreturn_t DAC960_LP_InterruptHandler(int IRQ_Channel,
5243                                        void *DeviceIdentifier,
5244                                        struct pt_regs *InterruptRegisters)
5245 {
5246   DAC960_Controller_T *Controller = (DAC960_Controller_T *) DeviceIdentifier;
5247   void __iomem *ControllerBaseAddress = Controller->BaseAddress;
5248   DAC960_V2_StatusMailbox_T *NextStatusMailbox;
5249   unsigned long flags;
5250
5251   spin_lock_irqsave(&Controller->queue_lock, flags);
5252   DAC960_LP_AcknowledgeInterrupt(ControllerBaseAddress);
5253   NextStatusMailbox = Controller->V2.NextStatusMailbox;
5254   while (NextStatusMailbox->Fields.CommandIdentifier > 0)
5255     {
5256       DAC960_V2_CommandIdentifier_T CommandIdentifier =
5257         NextStatusMailbox->Fields.CommandIdentifier;
5258       DAC960_Command_T *Command = Controller->Commands[CommandIdentifier-1];
5259       Command->V2.CommandStatus = NextStatusMailbox->Fields.CommandStatus;
5260       Command->V2.RequestSenseLength =
5261         NextStatusMailbox->Fields.RequestSenseLength;
5262       Command->V2.DataTransferResidue =
5263         NextStatusMailbox->Fields.DataTransferResidue;
5264       NextStatusMailbox->Words[0] = 0;
5265       if (++NextStatusMailbox > Controller->V2.LastStatusMailbox)
5266         NextStatusMailbox = Controller->V2.FirstStatusMailbox;
5267       DAC960_V2_ProcessCompletedCommand(Command);
5268     }
5269   Controller->V2.NextStatusMailbox = NextStatusMailbox;
5270   /*
5271     Attempt to remove additional I/O Requests from the Controller's
5272     I/O Request Queue and queue them to the Controller.
5273   */
5274   DAC960_ProcessRequest(Controller);
5275   spin_unlock_irqrestore(&Controller->queue_lock, flags);
5276   return IRQ_HANDLED;
5277 }
5278
5279
5280 /*
5281   DAC960_LA_InterruptHandler handles hardware interrupts from DAC960 LA Series
5282   Controllers.
5283 */
5284
5285 static irqreturn_t DAC960_LA_InterruptHandler(int IRQ_Channel,
5286                                        void *DeviceIdentifier,
5287                                        struct pt_regs *InterruptRegisters)
5288 {
5289   DAC960_Controller_T *Controller = (DAC960_Controller_T *) DeviceIdentifier;
5290   void __iomem *ControllerBaseAddress = Controller->BaseAddress;
5291   DAC960_V1_StatusMailbox_T *NextStatusMailbox;
5292   unsigned long flags;
5293
5294   spin_lock_irqsave(&Controller->queue_lock, flags);
5295   DAC960_LA_AcknowledgeInterrupt(ControllerBaseAddress);
5296   NextStatusMailbox = Controller->V1.NextStatusMailbox;
5297   while (NextStatusMailbox->Fields.Valid)
5298     {
5299       DAC960_V1_CommandIdentifier_T CommandIdentifier =
5300         NextStatusMailbox->Fields.CommandIdentifier;
5301       DAC960_Command_T *Command = Controller->Commands[CommandIdentifier-1];
5302       Command->V1.CommandStatus = NextStatusMailbox->Fields.CommandStatus;
5303       NextStatusMailbox->Word = 0;
5304       if (++NextStatusMailbox > Controller->V1.LastStatusMailbox)
5305         NextStatusMailbox = Controller->V1.FirstStatusMailbox;
5306       DAC960_V1_ProcessCompletedCommand(Command);
5307     }
5308   Controller->V1.NextStatusMailbox = NextStatusMailbox;
5309   /*
5310     Attempt to remove additional I/O Requests from the Controller's
5311     I/O Request Queue and queue them to the Controller.
5312   */
5313   DAC960_ProcessRequest(Controller);
5314   spin_unlock_irqrestore(&Controller->queue_lock, flags);
5315   return IRQ_HANDLED;
5316 }
5317
5318
5319 /*
5320   DAC960_PG_InterruptHandler handles hardware interrupts from DAC960 PG Series
5321   Controllers.
5322 */
5323
5324 static irqreturn_t DAC960_PG_InterruptHandler(int IRQ_Channel,
5325                                        void *DeviceIdentifier,
5326                                        struct pt_regs *InterruptRegisters)
5327 {
5328   DAC960_Controller_T *Controller = (DAC960_Controller_T *) DeviceIdentifier;
5329   void __iomem *ControllerBaseAddress = Controller->BaseAddress;
5330   DAC960_V1_StatusMailbox_T *NextStatusMailbox;
5331   unsigned long flags;
5332
5333   spin_lock_irqsave(&Controller->queue_lock, flags);
5334   DAC960_PG_AcknowledgeInterrupt(ControllerBaseAddress);
5335   NextStatusMailbox = Controller->V1.NextStatusMailbox;
5336   while (NextStatusMailbox->Fields.Valid)
5337     {
5338       DAC960_V1_CommandIdentifier_T CommandIdentifier =
5339         NextStatusMailbox->Fields.CommandIdentifier;
5340       DAC960_Command_T *Command = Controller->Commands[CommandIdentifier-1];
5341       Command->V1.CommandStatus = NextStatusMailbox->Fields.CommandStatus;
5342       NextStatusMailbox->Word = 0;
5343       if (++NextStatusMailbox > Controller->V1.LastStatusMailbox)
5344         NextStatusMailbox = Controller->V1.FirstStatusMailbox;
5345       DAC960_V1_ProcessCompletedCommand(Command);
5346     }
5347   Controller->V1.NextStatusMailbox = NextStatusMailbox;
5348   /*
5349     Attempt to remove additional I/O Requests from the Controller's
5350     I/O Request Queue and queue them to the Controller.
5351   */
5352   DAC960_ProcessRequest(Controller);
5353   spin_unlock_irqrestore(&Controller->queue_lock, flags);
5354   return IRQ_HANDLED;
5355 }
5356
5357
5358 /*
5359   DAC960_PD_InterruptHandler handles hardware interrupts from DAC960 PD Series
5360   Controllers.
5361 */
5362
5363 static irqreturn_t DAC960_PD_InterruptHandler(int IRQ_Channel,
5364                                        void *DeviceIdentifier,
5365                                        struct pt_regs *InterruptRegisters)
5366 {
5367   DAC960_Controller_T *Controller = (DAC960_Controller_T *) DeviceIdentifier;
5368   void __iomem *ControllerBaseAddress = Controller->BaseAddress;
5369   unsigned long flags;
5370
5371   spin_lock_irqsave(&Controller->queue_lock, flags);
5372   while (DAC960_PD_StatusAvailableP(ControllerBaseAddress))
5373     {
5374       DAC960_V1_CommandIdentifier_T CommandIdentifier =
5375         DAC960_PD_ReadStatusCommandIdentifier(ControllerBaseAddress);
5376       DAC960_Command_T *Command = Controller->Commands[CommandIdentifier-1];
5377       Command->V1.CommandStatus =
5378         DAC960_PD_ReadStatusRegister(ControllerBaseAddress);
5379       DAC960_PD_AcknowledgeInterrupt(ControllerBaseAddress);
5380       DAC960_PD_AcknowledgeStatus(ControllerBaseAddress);
5381       DAC960_V1_ProcessCompletedCommand(Command);
5382     }
5383   /*
5384     Attempt to remove additional I/O Requests from the Controller's
5385     I/O Request Queue and queue them to the Controller.
5386   */
5387   DAC960_ProcessRequest(Controller);
5388   spin_unlock_irqrestore(&Controller->queue_lock, flags);
5389   return IRQ_HANDLED;
5390 }
5391
5392
5393 /*
5394   DAC960_P_InterruptHandler handles hardware interrupts from DAC960 P Series
5395   Controllers.
5396
5397   Translations of DAC960_V1_Enquiry and DAC960_V1_GetDeviceState rely
5398   on the data having been placed into DAC960_Controller_T, rather than
5399   an arbitrary buffer.
5400 */
5401
5402 static irqreturn_t DAC960_P_InterruptHandler(int IRQ_Channel,
5403                                       void *DeviceIdentifier,
5404                                       struct pt_regs *InterruptRegisters)
5405 {
5406   DAC960_Controller_T *Controller = (DAC960_Controller_T *) DeviceIdentifier;
5407   void __iomem *ControllerBaseAddress = Controller->BaseAddress;
5408   unsigned long flags;
5409
5410   spin_lock_irqsave(&Controller->queue_lock, flags);
5411   while (DAC960_PD_StatusAvailableP(ControllerBaseAddress))
5412     {
5413       DAC960_V1_CommandIdentifier_T CommandIdentifier =
5414         DAC960_PD_ReadStatusCommandIdentifier(ControllerBaseAddress);
5415       DAC960_Command_T *Command = Controller->Commands[CommandIdentifier-1];
5416       DAC960_V1_CommandMailbox_T *CommandMailbox = &Command->V1.CommandMailbox;
5417       DAC960_V1_CommandOpcode_T CommandOpcode =
5418         CommandMailbox->Common.CommandOpcode;
5419       Command->V1.CommandStatus =
5420         DAC960_PD_ReadStatusRegister(ControllerBaseAddress);
5421       DAC960_PD_AcknowledgeInterrupt(ControllerBaseAddress);
5422       DAC960_PD_AcknowledgeStatus(ControllerBaseAddress);
5423       switch (CommandOpcode)
5424         {
5425         case DAC960_V1_Enquiry_Old:
5426           Command->V1.CommandMailbox.Common.CommandOpcode = DAC960_V1_Enquiry;
5427           DAC960_P_To_PD_TranslateEnquiry(Controller->V1.NewEnquiry);
5428           break;
5429         case DAC960_V1_GetDeviceState_Old:
5430           Command->V1.CommandMailbox.Common.CommandOpcode =
5431                                                 DAC960_V1_GetDeviceState;
5432           DAC960_P_To_PD_TranslateDeviceState(Controller->V1.NewDeviceState);
5433           break;
5434         case DAC960_V1_Read_Old:
5435           Command->V1.CommandMailbox.Common.CommandOpcode = DAC960_V1_Read;
5436           DAC960_P_To_PD_TranslateReadWriteCommand(CommandMailbox);
5437           break;
5438         case DAC960_V1_Write_Old:
5439           Command->V1.CommandMailbox.Common.CommandOpcode = DAC960_V1_Write;
5440           DAC960_P_To_PD_TranslateReadWriteCommand(CommandMailbox);
5441           break;
5442         case DAC960_V1_ReadWithScatterGather_Old:
5443           Command->V1.CommandMailbox.Common.CommandOpcode =
5444             DAC960_V1_ReadWithScatterGather;
5445           DAC960_P_To_PD_TranslateReadWriteCommand(CommandMailbox);
5446           break;
5447         case DAC960_V1_WriteWithScatterGather_Old:
5448           Command->V1.CommandMailbox.Common.CommandOpcode =
5449             DAC960_V1_WriteWithScatterGather;
5450           DAC960_P_To_PD_TranslateReadWriteCommand(CommandMailbox);
5451           break;
5452         default:
5453           break;
5454         }
5455       DAC960_V1_ProcessCompletedCommand(Command);
5456     }
5457   /*
5458     Attempt to remove additional I/O Requests from the Controller's
5459     I/O Request Queue and queue them to the Controller.
5460   */
5461   DAC960_ProcessRequest(Controller);
5462   spin_unlock_irqrestore(&Controller->queue_lock, flags);
5463   return IRQ_HANDLED;
5464 }
5465
5466
5467 /*
5468   DAC960_V1_QueueMonitoringCommand queues a Monitoring Command to DAC960 V1
5469   Firmware Controllers.
5470 */
5471
5472 static void DAC960_V1_QueueMonitoringCommand(DAC960_Command_T *Command)
5473 {
5474   DAC960_Controller_T *Controller = Command->Controller;
5475   DAC960_V1_CommandMailbox_T *CommandMailbox = &Command->V1.CommandMailbox;
5476   DAC960_V1_ClearCommand(Command);
5477   Command->CommandType = DAC960_MonitoringCommand;
5478   CommandMailbox->Type3.CommandOpcode = DAC960_V1_Enquiry;
5479   CommandMailbox->Type3.BusAddress = Controller->V1.NewEnquiryDMA;
5480   DAC960_QueueCommand(Command);
5481 }
5482
5483
5484 /*
5485   DAC960_V2_QueueMonitoringCommand queues a Monitoring Command to DAC960 V2
5486   Firmware Controllers.
5487 */
5488
5489 static void DAC960_V2_QueueMonitoringCommand(DAC960_Command_T *Command)
5490 {
5491   DAC960_Controller_T *Controller = Command->Controller;
5492   DAC960_V2_CommandMailbox_T *CommandMailbox = &Command->V2.CommandMailbox;
5493   DAC960_V2_ClearCommand(Command);
5494   Command->CommandType = DAC960_MonitoringCommand;
5495   CommandMailbox->ControllerInfo.CommandOpcode = DAC960_V2_IOCTL;
5496   CommandMailbox->ControllerInfo.CommandControlBits
5497                                 .DataTransferControllerToHost = true;
5498   CommandMailbox->ControllerInfo.CommandControlBits
5499                                 .NoAutoRequestSense = true;
5500   CommandMailbox->ControllerInfo.DataTransferSize =
5501     sizeof(DAC960_V2_ControllerInfo_T);
5502   CommandMailbox->ControllerInfo.ControllerNumber = 0;
5503   CommandMailbox->ControllerInfo.IOCTL_Opcode = DAC960_V2_GetControllerInfo;
5504   CommandMailbox->ControllerInfo.DataTransferMemoryAddress
5505                                 .ScatterGatherSegments[0]
5506                                 .SegmentDataPointer =
5507     Controller->V2.NewControllerInformationDMA;
5508   CommandMailbox->ControllerInfo.DataTransferMemoryAddress
5509                                 .ScatterGatherSegments[0]
5510                                 .SegmentByteCount =
5511     CommandMailbox->ControllerInfo.DataTransferSize;
5512   DAC960_QueueCommand(Command);
5513 }
5514
5515
5516 /*
5517   DAC960_MonitoringTimerFunction is the timer function for monitoring
5518   the status of DAC960 Controllers.
5519 */
5520
5521 static void DAC960_MonitoringTimerFunction(unsigned long TimerData)
5522 {
5523   DAC960_Controller_T *Controller = (DAC960_Controller_T *) TimerData;
5524   DAC960_Command_T *Command;
5525   unsigned long flags;
5526
5527   if (Controller->FirmwareType == DAC960_V1_Controller)
5528     {
5529       spin_lock_irqsave(&Controller->queue_lock, flags);
5530       /*
5531         Queue a Status Monitoring Command to Controller.
5532       */
5533       Command = DAC960_AllocateCommand(Controller);
5534       if (Command != NULL)
5535         DAC960_V1_QueueMonitoringCommand(Command);
5536       else Controller->MonitoringCommandDeferred = true;
5537       spin_unlock_irqrestore(&Controller->queue_lock, flags);
5538     }
5539   else
5540     {
5541       DAC960_V2_ControllerInfo_T *ControllerInfo =
5542         &Controller->V2.ControllerInformation;
5543       unsigned int StatusChangeCounter =
5544         Controller->V2.HealthStatusBuffer->StatusChangeCounter;
5545       boolean ForceMonitoringCommand = false;
5546       if (jiffies - Controller->SecondaryMonitoringTime
5547           > DAC960_SecondaryMonitoringInterval)
5548         {
5549           int LogicalDriveNumber;
5550           for (LogicalDriveNumber = 0;
5551                LogicalDriveNumber < DAC960_MaxLogicalDrives;
5552                LogicalDriveNumber++)
5553             {
5554               DAC960_V2_LogicalDeviceInfo_T *LogicalDeviceInfo =
5555                 Controller->V2.LogicalDeviceInformation[LogicalDriveNumber];
5556               if (LogicalDeviceInfo == NULL) continue;
5557               if (!LogicalDeviceInfo->LogicalDeviceControl
5558                                      .LogicalDeviceInitialized)
5559                 {
5560                   ForceMonitoringCommand = true;
5561                   break;
5562                 }
5563             }
5564           Controller->SecondaryMonitoringTime = jiffies;
5565         }
5566       if (StatusChangeCounter == Controller->V2.StatusChangeCounter &&
5567           Controller->V2.HealthStatusBuffer->NextEventSequenceNumber
5568           == Controller->V2.NextEventSequenceNumber &&
5569           (ControllerInfo->BackgroundInitializationsActive +
5570            ControllerInfo->LogicalDeviceInitializationsActive +
5571            ControllerInfo->PhysicalDeviceInitializationsActive +
5572            ControllerInfo->ConsistencyChecksActive +
5573            ControllerInfo->RebuildsActive +
5574            ControllerInfo->OnlineExpansionsActive == 0 ||
5575            jiffies - Controller->PrimaryMonitoringTime
5576            < DAC960_MonitoringTimerInterval) &&
5577           !ForceMonitoringCommand)
5578         {
5579           Controller->MonitoringTimer.expires =
5580             jiffies + DAC960_HealthStatusMonitoringInterval;
5581             add_timer(&Controller->MonitoringTimer);
5582           return;
5583         }
5584       Controller->V2.StatusChangeCounter = StatusChangeCounter;
5585       Controller->PrimaryMonitoringTime = jiffies;
5586
5587       spin_lock_irqsave(&Controller->queue_lock, flags);
5588       /*
5589         Queue a Status Monitoring Command to Controller.
5590       */
5591       Command = DAC960_AllocateCommand(Controller);
5592       if (Command != NULL)
5593         DAC960_V2_QueueMonitoringCommand(Command);
5594       else Controller->MonitoringCommandDeferred = true;
5595       spin_unlock_irqrestore(&Controller->queue_lock, flags);
5596       /*
5597         Wake up any processes waiting on a Health Status Buffer change.
5598       */
5599       wake_up(&Controller->HealthStatusWaitQueue);
5600     }
5601 }
5602
5603 /*
5604   DAC960_CheckStatusBuffer verifies that there is room to hold ByteCount
5605   additional bytes in the Combined Status Buffer and grows the buffer if
5606   necessary.  It returns true if there is enough room and false otherwise.
5607 */
5608
5609 static boolean DAC960_CheckStatusBuffer(DAC960_Controller_T *Controller,
5610                                         unsigned int ByteCount)
5611 {
5612   unsigned char *NewStatusBuffer;
5613   if (Controller->InitialStatusLength + 1 +
5614       Controller->CurrentStatusLength + ByteCount + 1 <=
5615       Controller->CombinedStatusBufferLength)
5616     return true;
5617   if (Controller->CombinedStatusBufferLength == 0)
5618     {
5619       unsigned int NewStatusBufferLength = DAC960_InitialStatusBufferSize;
5620       while (NewStatusBufferLength < ByteCount)
5621         NewStatusBufferLength *= 2;
5622       Controller->CombinedStatusBuffer =
5623         (unsigned char *) kmalloc(NewStatusBufferLength, GFP_ATOMIC);
5624       if (Controller->CombinedStatusBuffer == NULL) return false;
5625       Controller->CombinedStatusBufferLength = NewStatusBufferLength;
5626       return true;
5627     }
5628   NewStatusBuffer = (unsigned char *)
5629     kmalloc(2 * Controller->CombinedStatusBufferLength, GFP_ATOMIC);
5630   if (NewStatusBuffer == NULL)
5631     {
5632       DAC960_Warning("Unable to expand Combined Status Buffer - Truncating\n",
5633                      Controller);
5634       return false;
5635     }
5636   memcpy(NewStatusBuffer, Controller->CombinedStatusBuffer,
5637          Controller->CombinedStatusBufferLength);
5638   kfree(Controller->CombinedStatusBuffer);
5639   Controller->CombinedStatusBuffer = NewStatusBuffer;
5640   Controller->CombinedStatusBufferLength *= 2;
5641   Controller->CurrentStatusBuffer =
5642     &NewStatusBuffer[Controller->InitialStatusLength + 1];
5643   return true;
5644 }
5645
5646
5647 /*
5648   DAC960_Message prints Driver Messages.
5649 */
5650
5651 static void DAC960_Message(DAC960_MessageLevel_T MessageLevel,
5652                            unsigned char *Format,
5653                            DAC960_Controller_T *Controller,
5654                            ...)
5655 {
5656   static unsigned char Buffer[DAC960_LineBufferSize];
5657   static boolean BeginningOfLine = true;
5658   va_list Arguments;
5659   int Length = 0;
5660   va_start(Arguments, Controller);
5661   Length = vsprintf(Buffer, Format, Arguments);
5662   va_end(Arguments);
5663   if (Controller == NULL)
5664     printk("%sDAC960#%d: %s", DAC960_MessageLevelMap[MessageLevel],
5665            DAC960_ControllerCount, Buffer);
5666   else if (MessageLevel == DAC960_AnnounceLevel ||
5667            MessageLevel == DAC960_InfoLevel)
5668     {
5669       if (!Controller->ControllerInitialized)
5670         {
5671           if (DAC960_CheckStatusBuffer(Controller, Length))
5672             {
5673               strcpy(&Controller->CombinedStatusBuffer
5674                                   [Controller->InitialStatusLength],
5675                      Buffer);
5676               Controller->InitialStatusLength += Length;
5677               Controller->CurrentStatusBuffer =
5678                 &Controller->CombinedStatusBuffer
5679                              [Controller->InitialStatusLength + 1];
5680             }
5681           if (MessageLevel == DAC960_AnnounceLevel)
5682             {
5683               static int AnnouncementLines = 0;
5684               if (++AnnouncementLines <= 2)
5685                 printk("%sDAC960: %s", DAC960_MessageLevelMap[MessageLevel],
5686                        Buffer);
5687             }
5688           else
5689             {
5690               if (BeginningOfLine)
5691                 {
5692                   if (Buffer[0] != '\n' || Length > 1)
5693                     printk("%sDAC960#%d: %s",
5694                            DAC960_MessageLevelMap[MessageLevel],
5695                            Controller->ControllerNumber, Buffer);
5696                 }
5697               else printk("%s", Buffer);
5698             }
5699         }
5700       else if (DAC960_CheckStatusBuffer(Controller, Length))
5701         {
5702           strcpy(&Controller->CurrentStatusBuffer[
5703                     Controller->CurrentStatusLength], Buffer);
5704           Controller->CurrentStatusLength += Length;
5705         }
5706     }
5707   else if (MessageLevel == DAC960_ProgressLevel)
5708     {
5709       strcpy(Controller->ProgressBuffer, Buffer);
5710       Controller->ProgressBufferLength = Length;
5711       if (Controller->EphemeralProgressMessage)
5712         {
5713           if (jiffies - Controller->LastProgressReportTime
5714               >= DAC960_ProgressReportingInterval)
5715             {
5716               printk("%sDAC960#%d: %s", DAC960_MessageLevelMap[MessageLevel],
5717                      Controller->ControllerNumber, Buffer);
5718               Controller->LastProgressReportTime = jiffies;
5719             }
5720         }
5721       else printk("%sDAC960#%d: %s", DAC960_MessageLevelMap[MessageLevel],
5722                   Controller->ControllerNumber, Buffer);
5723     }
5724   else if (MessageLevel == DAC960_UserCriticalLevel)
5725     {
5726       strcpy(&Controller->UserStatusBuffer[Controller->UserStatusLength],
5727              Buffer);
5728       Controller->UserStatusLength += Length;
5729       if (Buffer[0] != '\n' || Length > 1)
5730         printk("%sDAC960#%d: %s", DAC960_MessageLevelMap[MessageLevel],
5731                Controller->ControllerNumber, Buffer);
5732     }
5733   else
5734     {
5735       if (BeginningOfLine)
5736         printk("%sDAC960#%d: %s", DAC960_MessageLevelMap[MessageLevel],
5737                Controller->ControllerNumber, Buffer);
5738       else printk("%s", Buffer);
5739     }
5740   BeginningOfLine = (Buffer[Length-1] == '\n');
5741 }
5742
5743
5744 /*
5745   DAC960_ParsePhysicalDevice parses spaces followed by a Physical Device
5746   Channel:TargetID specification from a User Command string.  It updates
5747   Channel and TargetID and returns true on success and false on failure.
5748 */
5749
5750 static boolean DAC960_ParsePhysicalDevice(DAC960_Controller_T *Controller,
5751                                           char *UserCommandString,
5752                                           unsigned char *Channel,
5753                                           unsigned char *TargetID)
5754 {
5755   char *NewUserCommandString = UserCommandString;
5756   unsigned long XChannel, XTargetID;
5757   while (*UserCommandString == ' ') UserCommandString++;
5758   if (UserCommandString == NewUserCommandString)
5759     return false;
5760   XChannel = simple_strtoul(UserCommandString, &NewUserCommandString, 10);
5761   if (NewUserCommandString == UserCommandString ||
5762       *NewUserCommandString != ':' ||
5763       XChannel >= Controller->Channels)
5764     return false;
5765   UserCommandString = ++NewUserCommandString;
5766   XTargetID = simple_strtoul(UserCommandString, &NewUserCommandString, 10);
5767   if (NewUserCommandString == UserCommandString ||
5768       *NewUserCommandString != '\0' ||
5769       XTargetID >= Controller->Targets)
5770     return false;
5771   *Channel = XChannel;
5772   *TargetID = XTargetID;
5773   return true;
5774 }
5775
5776
5777 /*
5778   DAC960_ParseLogicalDrive parses spaces followed by a Logical Drive Number
5779   specification from a User Command string.  It updates LogicalDriveNumber and
5780   returns true on success and false on failure.
5781 */
5782
5783 static boolean DAC960_ParseLogicalDrive(DAC960_Controller_T *Controller,
5784                                         char *UserCommandString,
5785                                         unsigned char *LogicalDriveNumber)
5786 {
5787   char *NewUserCommandString = UserCommandString;
5788   unsigned long XLogicalDriveNumber;
5789   while (*UserCommandString == ' ') UserCommandString++;
5790   if (UserCommandString == NewUserCommandString)
5791     return false;
5792   XLogicalDriveNumber =
5793     simple_strtoul(UserCommandString, &NewUserCommandString, 10);
5794   if (NewUserCommandString == UserCommandString ||
5795       *NewUserCommandString != '\0' ||
5796       XLogicalDriveNumber > DAC960_MaxLogicalDrives - 1)
5797     return false;
5798   *LogicalDriveNumber = XLogicalDriveNumber;
5799   return true;
5800 }
5801
5802
5803 /*
5804   DAC960_V1_SetDeviceState sets the Device State for a Physical Device for
5805   DAC960 V1 Firmware Controllers.
5806 */
5807
5808 static void DAC960_V1_SetDeviceState(DAC960_Controller_T *Controller,
5809                                      DAC960_Command_T *Command,
5810                                      unsigned char Channel,
5811                                      unsigned char TargetID,
5812                                      DAC960_V1_PhysicalDeviceState_T
5813                                        DeviceState,
5814                                      const unsigned char *DeviceStateString)
5815 {
5816   DAC960_V1_CommandMailbox_T *CommandMailbox = &Command->V1.CommandMailbox;
5817   CommandMailbox->Type3D.CommandOpcode = DAC960_V1_StartDevice;
5818   CommandMailbox->Type3D.Channel = Channel;
5819   CommandMailbox->Type3D.TargetID = TargetID;
5820   CommandMailbox->Type3D.DeviceState = DeviceState;
5821   CommandMailbox->Type3D.Modifier = 0;
5822   DAC960_ExecuteCommand(Command);
5823   switch (Command->V1.CommandStatus)
5824     {
5825     case DAC960_V1_NormalCompletion:
5826       DAC960_UserCritical("%s of Physical Device %d:%d Succeeded\n", Controller,
5827                           DeviceStateString, Channel, TargetID);
5828       break;
5829     case DAC960_V1_UnableToStartDevice:
5830       DAC960_UserCritical("%s of Physical Device %d:%d Failed - "
5831                           "Unable to Start Device\n", Controller,
5832                           DeviceStateString, Channel, TargetID);
5833       break;
5834     case DAC960_V1_NoDeviceAtAddress:
5835       DAC960_UserCritical("%s of Physical Device %d:%d Failed - "
5836                           "No Device at Address\n", Controller,
5837                           DeviceStateString, Channel, TargetID);
5838       break;
5839     case DAC960_V1_InvalidChannelOrTargetOrModifier:
5840       DAC960_UserCritical("%s of Physical Device %d:%d Failed - "
5841                           "Invalid Channel or Target or Modifier\n",
5842                           Controller, DeviceStateString, Channel, TargetID);
5843       break;
5844     case DAC960_V1_ChannelBusy:
5845       DAC960_UserCritical("%s of Physical Device %d:%d Failed - "
5846                           "Channel Busy\n", Controller,
5847                           DeviceStateString, Channel, TargetID);
5848       break;
5849     default:
5850       DAC960_UserCritical("%s of Physical Device %d:%d Failed - "
5851                           "Unexpected Status %04X\n", Controller,
5852                           DeviceStateString, Channel, TargetID,
5853                           Command->V1.CommandStatus);
5854       break;
5855     }
5856 }
5857
5858
5859 /*
5860   DAC960_V1_ExecuteUserCommand executes a User Command for DAC960 V1 Firmware
5861   Controllers.
5862 */
5863
5864 static boolean DAC960_V1_ExecuteUserCommand(DAC960_Controller_T *Controller,
5865                                             unsigned char *UserCommand)
5866 {
5867   DAC960_Command_T *Command;
5868   DAC960_V1_CommandMailbox_T *CommandMailbox;
5869   unsigned long flags;
5870   unsigned char Channel, TargetID, LogicalDriveNumber;
5871
5872   spin_lock_irqsave(&Controller->queue_lock, flags);
5873   while ((Command = DAC960_AllocateCommand(Controller)) == NULL)
5874     DAC960_WaitForCommand(Controller);
5875   spin_unlock_irqrestore(&Controller->queue_lock, flags);
5876   Controller->UserStatusLength = 0;
5877   DAC960_V1_ClearCommand(Command);
5878   Command->CommandType = DAC960_ImmediateCommand;
5879   CommandMailbox = &Command->V1.CommandMailbox;
5880   if (strcmp(UserCommand, "flush-cache") == 0)
5881     {
5882       CommandMailbox->Type3.CommandOpcode = DAC960_V1_Flush;
5883       DAC960_ExecuteCommand(Command);
5884       DAC960_UserCritical("Cache Flush Completed\n", Controller);
5885     }
5886   else if (strncmp(UserCommand, "kill", 4) == 0 &&
5887            DAC960_ParsePhysicalDevice(Controller, &UserCommand[4],
5888                                       &Channel, &TargetID))
5889     {
5890       DAC960_V1_DeviceState_T *DeviceState =
5891         &Controller->V1.DeviceState[Channel][TargetID];
5892       if (DeviceState->Present &&
5893           DeviceState->DeviceType == DAC960_V1_DiskType &&
5894           DeviceState->DeviceState != DAC960_V1_Device_Dead)
5895         DAC960_V1_SetDeviceState(Controller, Command, Channel, TargetID,
5896                                  DAC960_V1_Device_Dead, "Kill");
5897       else DAC960_UserCritical("Kill of Physical Device %d:%d Illegal\n",
5898                                Controller, Channel, TargetID);
5899     }
5900   else if (strncmp(UserCommand, "make-online", 11) == 0 &&
5901            DAC960_ParsePhysicalDevice(Controller, &UserCommand[11],
5902                                       &Channel, &TargetID))
5903     {
5904       DAC960_V1_DeviceState_T *DeviceState =
5905         &Controller->V1.DeviceState[Channel][TargetID];
5906       if (DeviceState->Present &&
5907           DeviceState->DeviceType == DAC960_V1_DiskType &&
5908           DeviceState->DeviceState == DAC960_V1_Device_Dead)
5909         DAC960_V1_SetDeviceState(Controller, Command, Channel, TargetID,
5910                                  DAC960_V1_Device_Online, "Make Online");
5911       else DAC960_UserCritical("Make Online of Physical Device %d:%d Illegal\n",
5912                                Controller, Channel, TargetID);
5913
5914     }
5915   else if (strncmp(UserCommand, "make-standby", 12) == 0 &&
5916            DAC960_ParsePhysicalDevice(Controller, &UserCommand[12],
5917                                       &Channel, &TargetID))
5918     {
5919       DAC960_V1_DeviceState_T *DeviceState =
5920         &Controller->V1.DeviceState[Channel][TargetID];
5921       if (DeviceState->Present &&
5922           DeviceState->DeviceType == DAC960_V1_DiskType &&
5923           DeviceState->DeviceState == DAC960_V1_Device_Dead)
5924         DAC960_V1_SetDeviceState(Controller, Command, Channel, TargetID,
5925                                  DAC960_V1_Device_Standby, "Make Standby");
5926       else DAC960_UserCritical("Make Standby of Physical "
5927                                "Device %d:%d Illegal\n",
5928                                Controller, Channel, TargetID);
5929     }
5930   else if (strncmp(UserCommand, "rebuild", 7) == 0 &&
5931            DAC960_ParsePhysicalDevice(Controller, &UserCommand[7],
5932                                       &Channel, &TargetID))
5933     {
5934       CommandMailbox->Type3D.CommandOpcode = DAC960_V1_RebuildAsync;
5935       CommandMailbox->Type3D.Channel = Channel;
5936       CommandMailbox->Type3D.TargetID = TargetID;
5937       DAC960_ExecuteCommand(Command);
5938       switch (Command->V1.CommandStatus)
5939         {
5940         case DAC960_V1_NormalCompletion:
5941           DAC960_UserCritical("Rebuild of Physical Device %d:%d Initiated\n",
5942                               Controller, Channel, TargetID);
5943           break;
5944         case DAC960_V1_AttemptToRebuildOnlineDrive:
5945           DAC960_UserCritical("Rebuild of Physical Device %d:%d Failed - "
5946                               "Attempt to Rebuild Online or "
5947                               "Unresponsive Drive\n",
5948                               Controller, Channel, TargetID);
5949           break;
5950         case DAC960_V1_NewDiskFailedDuringRebuild:
5951           DAC960_UserCritical("Rebuild of Physical Device %d:%d Failed - "
5952                               "New Disk Failed During Rebuild\n",
5953                               Controller, Channel, TargetID);
5954           break;
5955         case DAC960_V1_InvalidDeviceAddress:
5956           DAC960_UserCritical("Rebuild of Physical Device %d:%d Failed - "
5957                               "Invalid Device Address\n",
5958                               Controller, Channel, TargetID);
5959           break;
5960         case DAC960_V1_RebuildOrCheckAlreadyInProgress:
5961           DAC960_UserCritical("Rebuild of Physical Device %d:%d Failed - "
5962                               "Rebuild or Consistency Check Already "
5963                               "in Progress\n", Controller, Channel, TargetID);
5964           break;
5965         default:
5966           DAC960_UserCritical("Rebuild of Physical Device %d:%d Failed - "
5967                               "Unexpected Status %04X\n", Controller,
5968                               Channel, TargetID, Command->V1.CommandStatus);
5969           break;
5970         }
5971     }
5972   else if (strncmp(UserCommand, "check-consistency", 17) == 0 &&
5973            DAC960_ParseLogicalDrive(Controller, &UserCommand[17],
5974                                     &LogicalDriveNumber))
5975     {
5976       CommandMailbox->Type3C.CommandOpcode = DAC960_V1_CheckConsistencyAsync;
5977       CommandMailbox->Type3C.LogicalDriveNumber = LogicalDriveNumber;
5978       CommandMailbox->Type3C.AutoRestore = true;
5979       DAC960_ExecuteCommand(Command);
5980       switch (Command->V1.CommandStatus)
5981         {
5982         case DAC960_V1_NormalCompletion:
5983           DAC960_UserCritical("Consistency Check of Logical Drive %d "
5984                               "(/dev/rd/c%dd%d) Initiated\n",
5985                               Controller, LogicalDriveNumber,
5986                               Controller->ControllerNumber,
5987                               LogicalDriveNumber);
5988           break;
5989         case DAC960_V1_DependentDiskIsDead:
5990           DAC960_UserCritical("Consistency Check of Logical Drive %d "
5991                               "(/dev/rd/c%dd%d) Failed - "
5992                               "Dependent Physical Device is DEAD\n",
5993                               Controller, LogicalDriveNumber,
5994                               Controller->ControllerNumber,
5995                               LogicalDriveNumber);
5996           break;
5997         case DAC960_V1_InvalidOrNonredundantLogicalDrive:
5998           DAC960_UserCritical("Consistency Check of Logical Drive %d "
5999                               "(/dev/rd/c%dd%d) Failed - "
6000                               "Invalid or Nonredundant Logical Drive\n",
6001                               Controller, LogicalDriveNumber,
6002                               Controller->ControllerNumber,
6003                               LogicalDriveNumber);
6004           break;
6005         case DAC960_V1_RebuildOrCheckAlreadyInProgress:
6006           DAC960_UserCritical("Consistency Check of Logical Drive %d "
6007                               "(/dev/rd/c%dd%d) Failed - Rebuild or "
6008                               "Consistency Check Already in Progress\n",
6009                               Controller, LogicalDriveNumber,
6010                               Controller->ControllerNumber,
6011                               LogicalDriveNumber);
6012           break;
6013         default:
6014           DAC960_UserCritical("Consistency Check of Logical Drive %d "
6015                               "(/dev/rd/c%dd%d) Failed - "
6016                               "Unexpected Status %04X\n",
6017                               Controller, LogicalDriveNumber,
6018                               Controller->ControllerNumber,
6019                               LogicalDriveNumber, Command->V1.CommandStatus);
6020           break;
6021         }
6022     }
6023   else if (strcmp(UserCommand, "cancel-rebuild") == 0 ||
6024            strcmp(UserCommand, "cancel-consistency-check") == 0)
6025     {
6026       /*
6027         the OldRebuildRateConstant is never actually used
6028         once its value is retrieved from the controller.
6029        */
6030       unsigned char *OldRebuildRateConstant;
6031       dma_addr_t OldRebuildRateConstantDMA;
6032
6033       OldRebuildRateConstant = pci_alloc_consistent( Controller->PCIDevice,
6034                 sizeof(char), &OldRebuildRateConstantDMA);
6035       if (OldRebuildRateConstant == NULL) {
6036          DAC960_UserCritical("Cancellation of Rebuild or "
6037                              "Consistency Check Failed - "
6038                              "Out of Memory",
6039                              Controller);
6040          goto failure;
6041       }
6042       CommandMailbox->Type3R.CommandOpcode = DAC960_V1_RebuildControl;
6043       CommandMailbox->Type3R.RebuildRateConstant = 0xFF;
6044       CommandMailbox->Type3R.BusAddress = OldRebuildRateConstantDMA;
6045       DAC960_ExecuteCommand(Command);
6046       switch (Command->V1.CommandStatus)
6047         {
6048         case DAC960_V1_NormalCompletion:
6049           DAC960_UserCritical("Rebuild or Consistency Check Cancelled\n",
6050                               Controller);
6051           break;
6052         default:
6053           DAC960_UserCritical("Cancellation of Rebuild or "
6054                               "Consistency Check Failed - "
6055                               "Unexpected Status %04X\n",
6056                               Controller, Command->V1.CommandStatus);
6057           break;
6058         }
6059 failure:
6060         pci_free_consistent(Controller->PCIDevice, sizeof(char),
6061                 OldRebuildRateConstant, OldRebuildRateConstantDMA);
6062     }
6063   else DAC960_UserCritical("Illegal User Command: '%s'\n",
6064                            Controller, UserCommand);
6065
6066   spin_lock_irqsave(&Controller->queue_lock, flags);
6067   DAC960_DeallocateCommand(Command);
6068   spin_unlock_irqrestore(&Controller->queue_lock, flags);
6069   return true;
6070 }
6071
6072
6073 /*
6074   DAC960_V2_TranslatePhysicalDevice translates a Physical Device Channel and
6075   TargetID into a Logical Device.  It returns true on success and false
6076   on failure.
6077 */
6078
6079 static boolean DAC960_V2_TranslatePhysicalDevice(DAC960_Command_T *Command,
6080                                                  unsigned char Channel,
6081                                                  unsigned char TargetID,
6082                                                  unsigned short
6083                                                    *LogicalDeviceNumber)
6084 {
6085   DAC960_V2_CommandMailbox_T SavedCommandMailbox, *CommandMailbox;
6086   DAC960_Controller_T *Controller =  Command->Controller;
6087
6088   CommandMailbox = &Command->V2.CommandMailbox;
6089   memcpy(&SavedCommandMailbox, CommandMailbox,
6090          sizeof(DAC960_V2_CommandMailbox_T));
6091
6092   CommandMailbox->PhysicalDeviceInfo.CommandOpcode = DAC960_V2_IOCTL;
6093   CommandMailbox->PhysicalDeviceInfo.CommandControlBits
6094                                     .DataTransferControllerToHost = true;
6095   CommandMailbox->PhysicalDeviceInfo.CommandControlBits
6096                                     .NoAutoRequestSense = true;
6097   CommandMailbox->PhysicalDeviceInfo.DataTransferSize =
6098     sizeof(DAC960_V2_PhysicalToLogicalDevice_T);
6099   CommandMailbox->PhysicalDeviceInfo.PhysicalDevice.TargetID = TargetID;
6100   CommandMailbox->PhysicalDeviceInfo.PhysicalDevice.Channel = Channel;
6101   CommandMailbox->PhysicalDeviceInfo.IOCTL_Opcode =
6102     DAC960_V2_TranslatePhysicalToLogicalDevice;
6103   CommandMailbox->Common.DataTransferMemoryAddress
6104                         .ScatterGatherSegments[0]
6105                         .SegmentDataPointer =
6106                 Controller->V2.PhysicalToLogicalDeviceDMA;
6107   CommandMailbox->Common.DataTransferMemoryAddress
6108                         .ScatterGatherSegments[0]
6109                         .SegmentByteCount =
6110                 CommandMailbox->Common.DataTransferSize;
6111
6112   DAC960_ExecuteCommand(Command);
6113   *LogicalDeviceNumber = Controller->V2.PhysicalToLogicalDevice->LogicalDeviceNumber;
6114
6115   memcpy(CommandMailbox, &SavedCommandMailbox,
6116          sizeof(DAC960_V2_CommandMailbox_T));
6117   return (Command->V2.CommandStatus == DAC960_V2_NormalCompletion);
6118 }
6119
6120
6121 /*
6122   DAC960_V2_ExecuteUserCommand executes a User Command for DAC960 V2 Firmware
6123   Controllers.
6124 */
6125
6126 static boolean DAC960_V2_ExecuteUserCommand(DAC960_Controller_T *Controller,
6127                                             unsigned char *UserCommand)
6128 {
6129   DAC960_Command_T *Command;
6130   DAC960_V2_CommandMailbox_T *CommandMailbox;
6131   unsigned long flags;
6132   unsigned char Channel, TargetID, LogicalDriveNumber;
6133   unsigned short LogicalDeviceNumber;
6134
6135   spin_lock_irqsave(&Controller->queue_lock, flags);
6136   while ((Command = DAC960_AllocateCommand(Controller)) == NULL)
6137     DAC960_WaitForCommand(Controller);
6138   spin_unlock_irqrestore(&Controller->queue_lock, flags);
6139   Controller->UserStatusLength = 0;
6140   DAC960_V2_ClearCommand(Command);
6141   Command->CommandType = DAC960_ImmediateCommand;
6142   CommandMailbox = &Command->V2.CommandMailbox;
6143   CommandMailbox->Common.CommandOpcode = DAC960_V2_IOCTL;
6144   CommandMailbox->Common.CommandControlBits.DataTransferControllerToHost = true;
6145   CommandMailbox->Common.CommandControlBits.NoAutoRequestSense = true;
6146   if (strcmp(UserCommand, "flush-cache") == 0)
6147     {
6148       CommandMailbox->DeviceOperation.IOCTL_Opcode = DAC960_V2_PauseDevice;
6149       CommandMailbox->DeviceOperation.OperationDevice =
6150         DAC960_V2_RAID_Controller;
6151       DAC960_ExecuteCommand(Command);
6152       DAC960_UserCritical("Cache Flush Completed\n", Controller);
6153     }
6154   else if (strncmp(UserCommand, "kill", 4) == 0 &&
6155            DAC960_ParsePhysicalDevice(Controller, &UserCommand[4],
6156                                       &Channel, &TargetID) &&
6157            DAC960_V2_TranslatePhysicalDevice(Command, Channel, TargetID,
6158                                              &LogicalDeviceNumber))
6159     {
6160       CommandMailbox->SetDeviceState.LogicalDevice.LogicalDeviceNumber =
6161         LogicalDeviceNumber;
6162       CommandMailbox->SetDeviceState.IOCTL_Opcode =
6163         DAC960_V2_SetDeviceState;
6164       CommandMailbox->SetDeviceState.DeviceState.PhysicalDeviceState =
6165         DAC960_V2_Device_Dead;
6166       DAC960_ExecuteCommand(Command);
6167       DAC960_UserCritical("Kill of Physical Device %d:%d %s\n",
6168                           Controller, Channel, TargetID,
6169                           (Command->V2.CommandStatus
6170                            == DAC960_V2_NormalCompletion
6171                            ? "Succeeded" : "Failed"));
6172     }
6173   else if (strncmp(UserCommand, "make-online", 11) == 0 &&
6174            DAC960_ParsePhysicalDevice(Controller, &UserCommand[11],
6175                                       &Channel, &TargetID) &&
6176            DAC960_V2_TranslatePhysicalDevice(Command, Channel, TargetID,
6177                                              &LogicalDeviceNumber))
6178     {
6179       CommandMailbox->SetDeviceState.LogicalDevice.LogicalDeviceNumber =
6180         LogicalDeviceNumber;
6181       CommandMailbox->SetDeviceState.IOCTL_Opcode =
6182         DAC960_V2_SetDeviceState;
6183       CommandMailbox->SetDeviceState.DeviceState.PhysicalDeviceState =
6184         DAC960_V2_Device_Online;
6185       DAC960_ExecuteCommand(Command);
6186       DAC960_UserCritical("Make Online of Physical Device %d:%d %s\n",
6187                           Controller, Channel, TargetID,
6188                           (Command->V2.CommandStatus
6189                            == DAC960_V2_NormalCompletion
6190                            ? "Succeeded" : "Failed"));
6191     }
6192   else if (strncmp(UserCommand, "make-standby", 12) == 0 &&
6193            DAC960_ParsePhysicalDevice(Controller, &UserCommand[12],
6194                                       &Channel, &TargetID) &&
6195            DAC960_V2_TranslatePhysicalDevice(Command, Channel, TargetID,
6196                                              &LogicalDeviceNumber))
6197     {
6198       CommandMailbox->SetDeviceState.LogicalDevice.LogicalDeviceNumber =
6199         LogicalDeviceNumber;
6200       CommandMailbox->SetDeviceState.IOCTL_Opcode =
6201         DAC960_V2_SetDeviceState;
6202       CommandMailbox->SetDeviceState.DeviceState.PhysicalDeviceState =
6203         DAC960_V2_Device_Standby;
6204       DAC960_ExecuteCommand(Command);
6205       DAC960_UserCritical("Make Standby of Physical Device %d:%d %s\n",
6206                           Controller, Channel, TargetID,
6207                           (Command->V2.CommandStatus
6208                            == DAC960_V2_NormalCompletion
6209                            ? "Succeeded" : "Failed"));
6210     }
6211   else if (strncmp(UserCommand, "rebuild", 7) == 0 &&
6212            DAC960_ParsePhysicalDevice(Controller, &UserCommand[7],
6213                                       &Channel, &TargetID) &&
6214            DAC960_V2_TranslatePhysicalDevice(Command, Channel, TargetID,
6215                                              &LogicalDeviceNumber))
6216     {
6217       CommandMailbox->LogicalDeviceInfo.LogicalDevice.LogicalDeviceNumber =
6218         LogicalDeviceNumber;
6219       CommandMailbox->LogicalDeviceInfo.IOCTL_Opcode =
6220         DAC960_V2_RebuildDeviceStart;
6221       DAC960_ExecuteCommand(Command);
6222       DAC960_UserCritical("Rebuild of Physical Device %d:%d %s\n",
6223                           Controller, Channel, TargetID,
6224                           (Command->V2.CommandStatus
6225                            == DAC960_V2_NormalCompletion
6226                            ? "Initiated" : "Not Initiated"));
6227     }
6228   else if (strncmp(UserCommand, "cancel-rebuild", 14) == 0 &&
6229            DAC960_ParsePhysicalDevice(Controller, &UserCommand[14],
6230                                       &Channel, &TargetID) &&
6231            DAC960_V2_TranslatePhysicalDevice(Command, Channel, TargetID,
6232                                              &LogicalDeviceNumber))
6233     {
6234       CommandMailbox->LogicalDeviceInfo.LogicalDevice.LogicalDeviceNumber =
6235         LogicalDeviceNumber;
6236       CommandMailbox->LogicalDeviceInfo.IOCTL_Opcode =
6237         DAC960_V2_RebuildDeviceStop;
6238       DAC960_ExecuteCommand(Command);
6239       DAC960_UserCritical("Rebuild of Physical Device %d:%d %s\n",
6240                           Controller, Channel, TargetID,
6241                           (Command->V2.CommandStatus
6242                            == DAC960_V2_NormalCompletion
6243                            ? "Cancelled" : "Not Cancelled"));
6244     }
6245   else if (strncmp(UserCommand, "check-consistency", 17) == 0 &&
6246            DAC960_ParseLogicalDrive(Controller, &UserCommand[17],
6247                                     &LogicalDriveNumber))
6248     {
6249       CommandMailbox->ConsistencyCheck.LogicalDevice.LogicalDeviceNumber =
6250         LogicalDriveNumber;
6251       CommandMailbox->ConsistencyCheck.IOCTL_Opcode =
6252         DAC960_V2_ConsistencyCheckStart;
6253       CommandMailbox->ConsistencyCheck.RestoreConsistency = true;
6254       CommandMailbox->ConsistencyCheck.InitializedAreaOnly = false;
6255       DAC960_ExecuteCommand(Command);
6256       DAC960_UserCritical("Consistency Check of Logical Drive %d "
6257                           "(/dev/rd/c%dd%d) %s\n",
6258                           Controller, LogicalDriveNumber,
6259                           Controller->ControllerNumber,
6260                           LogicalDriveNumber,
6261                           (Command->V2.CommandStatus
6262                            == DAC960_V2_NormalCompletion
6263                            ? "Initiated" : "Not Initiated"));
6264     }
6265   else if (strncmp(UserCommand, "cancel-consistency-check", 24) == 0 &&
6266            DAC960_ParseLogicalDrive(Controller, &UserCommand[24],
6267                                     &LogicalDriveNumber))
6268     {
6269       CommandMailbox->ConsistencyCheck.LogicalDevice.LogicalDeviceNumber =
6270         LogicalDriveNumber;
6271       CommandMailbox->ConsistencyCheck.IOCTL_Opcode =
6272         DAC960_V2_ConsistencyCheckStop;
6273       DAC960_ExecuteCommand(Command);
6274       DAC960_UserCritical("Consistency Check of Logical Drive %d "
6275                           "(/dev/rd/c%dd%d) %s\n",
6276                           Controller, LogicalDriveNumber,
6277                           Controller->ControllerNumber,
6278                           LogicalDriveNumber,
6279                           (Command->V2.CommandStatus
6280                            == DAC960_V2_NormalCompletion
6281                            ? "Cancelled" : "Not Cancelled"));
6282     }
6283   else if (strcmp(UserCommand, "perform-discovery") == 0)
6284     {
6285       CommandMailbox->Common.IOCTL_Opcode = DAC960_V2_StartDiscovery;
6286       DAC960_ExecuteCommand(Command);
6287       DAC960_UserCritical("Discovery %s\n", Controller,
6288                           (Command->V2.CommandStatus
6289                            == DAC960_V2_NormalCompletion
6290                            ? "Initiated" : "Not Initiated"));
6291       if (Command->V2.CommandStatus == DAC960_V2_NormalCompletion)
6292         {
6293           CommandMailbox->ControllerInfo.CommandOpcode = DAC960_V2_IOCTL;
6294           CommandMailbox->ControllerInfo.CommandControlBits
6295                                         .DataTransferControllerToHost = true;
6296           CommandMailbox->ControllerInfo.CommandControlBits
6297                                         .NoAutoRequestSense = true;
6298           CommandMailbox->ControllerInfo.DataTransferSize =
6299             sizeof(DAC960_V2_ControllerInfo_T);
6300           CommandMailbox->ControllerInfo.ControllerNumber = 0;
6301           CommandMailbox->ControllerInfo.IOCTL_Opcode =
6302             DAC960_V2_GetControllerInfo;
6303           /*
6304            * How does this NOT race with the queued Monitoring
6305            * usage of this structure?
6306            */
6307           CommandMailbox->ControllerInfo.DataTransferMemoryAddress
6308                                         .ScatterGatherSegments[0]
6309                                         .SegmentDataPointer =
6310             Controller->V2.NewControllerInformationDMA;
6311           CommandMailbox->ControllerInfo.DataTransferMemoryAddress
6312                                         .ScatterGatherSegments[0]
6313                                         .SegmentByteCount =
6314             CommandMailbox->ControllerInfo.DataTransferSize;
6315           DAC960_ExecuteCommand(Command);
6316           while (Controller->V2.NewControllerInformation->PhysicalScanActive)
6317             {
6318               DAC960_ExecuteCommand(Command);
6319               sleep_on_timeout(&Controller->CommandWaitQueue, HZ);
6320             }
6321           DAC960_UserCritical("Discovery Completed\n", Controller);
6322         }
6323     }
6324   else if (strcmp(UserCommand, "suppress-enclosure-messages") == 0)
6325     Controller->SuppressEnclosureMessages = true;
6326   else DAC960_UserCritical("Illegal User Command: '%s'\n",
6327                            Controller, UserCommand);
6328
6329   spin_lock_irqsave(&Controller->queue_lock, flags);
6330   DAC960_DeallocateCommand(Command);
6331   spin_unlock_irqrestore(&Controller->queue_lock, flags);
6332   return true;
6333 }
6334
6335
6336 /*
6337   DAC960_ProcReadStatus implements reading /proc/rd/status.
6338 */
6339
6340 static int DAC960_ProcReadStatus(char *Page, char **Start, off_t Offset,
6341                                  int Count, int *EOF, void *Data)
6342 {
6343   unsigned char *StatusMessage = "OK\n";
6344   int ControllerNumber, BytesAvailable;
6345   for (ControllerNumber = 0;
6346        ControllerNumber < DAC960_ControllerCount;
6347        ControllerNumber++)
6348     {
6349       DAC960_Controller_T *Controller = DAC960_Controllers[ControllerNumber];
6350       if (Controller == NULL) continue;
6351       if (Controller->MonitoringAlertMode)
6352         {
6353           StatusMessage = "ALERT\n";
6354           break;
6355         }
6356     }
6357   BytesAvailable = strlen(StatusMessage) - Offset;
6358   if (Count >= BytesAvailable)
6359     {
6360       Count = BytesAvailable;
6361       *EOF = true;
6362     }
6363   if (Count <= 0) return 0;
6364   *Start = Page;
6365   memcpy(Page, &StatusMessage[Offset], Count);
6366   return Count;
6367 }
6368
6369
6370 /*
6371   DAC960_ProcReadInitialStatus implements reading /proc/rd/cN/initial_status.
6372 */
6373
6374 static int DAC960_ProcReadInitialStatus(char *Page, char **Start, off_t Offset,
6375                                         int Count, int *EOF, void *Data)
6376 {
6377   DAC960_Controller_T *Controller = (DAC960_Controller_T *) Data;
6378   int BytesAvailable = Controller->InitialStatusLength - Offset;
6379   if (Count >= BytesAvailable)
6380     {
6381       Count = BytesAvailable;
6382       *EOF = true;
6383     }
6384   if (Count <= 0) return 0;
6385   *Start = Page;
6386   memcpy(Page, &Controller->CombinedStatusBuffer[Offset], Count);
6387   return Count;
6388 }
6389
6390
6391 /*
6392   DAC960_ProcReadCurrentStatus implements reading /proc/rd/cN/current_status.
6393 */
6394
6395 static int DAC960_ProcReadCurrentStatus(char *Page, char **Start, off_t Offset,
6396                                         int Count, int *EOF, void *Data)
6397 {
6398   DAC960_Controller_T *Controller = (DAC960_Controller_T *) Data;
6399   unsigned char *StatusMessage =
6400     "No Rebuild or Consistency Check in Progress\n";
6401   int ProgressMessageLength = strlen(StatusMessage);
6402   int BytesAvailable;
6403   if (jiffies != Controller->LastCurrentStatusTime)
6404     {
6405       Controller->CurrentStatusLength = 0;
6406       DAC960_AnnounceDriver(Controller);
6407       DAC960_ReportControllerConfiguration(Controller);
6408       DAC960_ReportDeviceConfiguration(Controller);
6409       if (Controller->ProgressBufferLength > 0)
6410         ProgressMessageLength = Controller->ProgressBufferLength;
6411       if (DAC960_CheckStatusBuffer(Controller, 2 + ProgressMessageLength))
6412         {
6413           unsigned char *CurrentStatusBuffer = Controller->CurrentStatusBuffer;
6414           CurrentStatusBuffer[Controller->CurrentStatusLength++] = ' ';
6415           CurrentStatusBuffer[Controller->CurrentStatusLength++] = ' ';
6416           if (Controller->ProgressBufferLength > 0)
6417             strcpy(&CurrentStatusBuffer[Controller->CurrentStatusLength],
6418                    Controller->ProgressBuffer);
6419           else
6420             strcpy(&CurrentStatusBuffer[Controller->CurrentStatusLength],
6421                    StatusMessage);
6422           Controller->CurrentStatusLength += ProgressMessageLength;
6423         }
6424       Controller->LastCurrentStatusTime = jiffies;
6425     }
6426   BytesAvailable = Controller->CurrentStatusLength - Offset;
6427   if (Count >= BytesAvailable)
6428     {
6429       Count = BytesAvailable;
6430       *EOF = true;
6431     }
6432   if (Count <= 0) return 0;
6433   *Start = Page;
6434   memcpy(Page, &Controller->CurrentStatusBuffer[Offset], Count);
6435   return Count;
6436 }
6437
6438
6439 /*
6440   DAC960_ProcReadUserCommand implements reading /proc/rd/cN/user_command.
6441 */
6442
6443 static int DAC960_ProcReadUserCommand(char *Page, char **Start, off_t Offset,
6444                                       int Count, int *EOF, void *Data)
6445 {
6446   DAC960_Controller_T *Controller = (DAC960_Controller_T *) Data;
6447   int BytesAvailable = Controller->UserStatusLength - Offset;
6448   if (Count >= BytesAvailable)
6449     {
6450       Count = BytesAvailable;
6451       *EOF = true;
6452     }
6453   if (Count <= 0) return 0;
6454   *Start = Page;
6455   memcpy(Page, &Controller->UserStatusBuffer[Offset], Count);
6456   return Count;
6457 }
6458
6459
6460 /*
6461   DAC960_ProcWriteUserCommand implements writing /proc/rd/cN/user_command.
6462 */
6463
6464 static int DAC960_ProcWriteUserCommand(struct file *file,
6465                                        const char __user *Buffer,
6466                                        unsigned long Count, void *Data)
6467 {
6468   DAC960_Controller_T *Controller = (DAC960_Controller_T *) Data;
6469   unsigned char CommandBuffer[80];
6470   int Length;
6471   if (Count > sizeof(CommandBuffer)-1) return -EINVAL;
6472   if (copy_from_user(CommandBuffer, Buffer, Count)) return -EFAULT;
6473   CommandBuffer[Count] = '\0';
6474   Length = strlen(CommandBuffer);
6475   if (CommandBuffer[Length-1] == '\n')
6476     CommandBuffer[--Length] = '\0';
6477   if (Controller->FirmwareType == DAC960_V1_Controller)
6478     return (DAC960_V1_ExecuteUserCommand(Controller, CommandBuffer)
6479             ? Count : -EBUSY);
6480   else
6481     return (DAC960_V2_ExecuteUserCommand(Controller, CommandBuffer)
6482             ? Count : -EBUSY);
6483 }
6484
6485
6486 /*
6487   DAC960_CreateProcEntries creates the /proc/rd/... entries for the
6488   DAC960 Driver.
6489 */
6490
6491 static void DAC960_CreateProcEntries(DAC960_Controller_T *Controller)
6492 {
6493         struct proc_dir_entry *StatusProcEntry;
6494         struct proc_dir_entry *ControllerProcEntry;
6495         struct proc_dir_entry *UserCommandProcEntry;
6496
6497         if (DAC960_ProcDirectoryEntry == NULL) {
6498                 DAC960_ProcDirectoryEntry = proc_mkdir("rd", NULL);
6499                 StatusProcEntry = create_proc_read_entry("status", 0,
6500                                            DAC960_ProcDirectoryEntry,
6501                                            DAC960_ProcReadStatus, NULL);
6502         }
6503
6504       sprintf(Controller->ControllerName, "c%d", Controller->ControllerNumber);
6505       ControllerProcEntry = proc_mkdir(Controller->ControllerName,
6506                                        DAC960_ProcDirectoryEntry);
6507       create_proc_read_entry("initial_status", 0, ControllerProcEntry,
6508                              DAC960_ProcReadInitialStatus, Controller);
6509       create_proc_read_entry("current_status", 0, ControllerProcEntry,
6510                              DAC960_ProcReadCurrentStatus, Controller);
6511       UserCommandProcEntry =
6512         create_proc_read_entry("user_command", S_IWUSR | S_IRUSR,
6513                                ControllerProcEntry, DAC960_ProcReadUserCommand,
6514                                Controller);
6515       UserCommandProcEntry->write_proc = DAC960_ProcWriteUserCommand;
6516       Controller->ControllerProcEntry = ControllerProcEntry;
6517 }
6518
6519
6520 /*
6521   DAC960_DestroyProcEntries destroys the /proc/rd/... entries for the
6522   DAC960 Driver.
6523 */
6524
6525 static void DAC960_DestroyProcEntries(DAC960_Controller_T *Controller)
6526 {
6527       if (Controller->ControllerProcEntry == NULL)
6528               return;
6529       remove_proc_entry("initial_status", Controller->ControllerProcEntry);
6530       remove_proc_entry("current_status", Controller->ControllerProcEntry);
6531       remove_proc_entry("user_command", Controller->ControllerProcEntry);
6532       remove_proc_entry(Controller->ControllerName, DAC960_ProcDirectoryEntry);
6533       Controller->ControllerProcEntry = NULL;
6534 }
6535
6536 #ifdef DAC960_GAM_MINOR
6537
6538 /*
6539  * DAC960_gam_ioctl is the ioctl function for performing RAID operations.
6540 */
6541
6542 static int DAC960_gam_ioctl(struct inode *inode, struct file *file,
6543                             unsigned int Request, unsigned long Argument)
6544 {
6545   int ErrorCode = 0;
6546   if (!capable(CAP_SYS_ADMIN)) return -EACCES;
6547   switch (Request)
6548     {
6549     case DAC960_IOCTL_GET_CONTROLLER_COUNT:
6550       return DAC960_ControllerCount;
6551     case DAC960_IOCTL_GET_CONTROLLER_INFO:
6552       {
6553         DAC960_ControllerInfo_T __user *UserSpaceControllerInfo =
6554           (DAC960_ControllerInfo_T __user *) Argument;
6555         DAC960_ControllerInfo_T ControllerInfo;
6556         DAC960_Controller_T *Controller;
6557         int ControllerNumber;
6558         if (UserSpaceControllerInfo == NULL) return -EINVAL;
6559         ErrorCode = get_user(ControllerNumber,
6560                              &UserSpaceControllerInfo->ControllerNumber);
6561         if (ErrorCode != 0) return ErrorCode;
6562         if (ControllerNumber < 0 ||
6563             ControllerNumber > DAC960_ControllerCount - 1)
6564           return -ENXIO;
6565         Controller = DAC960_Controllers[ControllerNumber];
6566         if (Controller == NULL) return -ENXIO;
6567         memset(&ControllerInfo, 0, sizeof(DAC960_ControllerInfo_T));
6568         ControllerInfo.ControllerNumber = ControllerNumber;
6569         ControllerInfo.FirmwareType = Controller->FirmwareType;
6570         ControllerInfo.Channels = Controller->Channels;
6571         ControllerInfo.Targets = Controller->Targets;
6572         ControllerInfo.PCI_Bus = Controller->Bus;
6573         ControllerInfo.PCI_Device = Controller->Device;
6574         ControllerInfo.PCI_Function = Controller->Function;
6575         ControllerInfo.IRQ_Channel = Controller->IRQ_Channel;
6576         ControllerInfo.PCI_Address = Controller->PCI_Address;
6577         strcpy(ControllerInfo.ModelName, Controller->ModelName);
6578         strcpy(ControllerInfo.FirmwareVersion, Controller->FirmwareVersion);
6579         return (copy_to_user(UserSpaceControllerInfo, &ControllerInfo,
6580                              sizeof(DAC960_ControllerInfo_T)) ? -EFAULT : 0);
6581       }
6582     case DAC960_IOCTL_V1_EXECUTE_COMMAND:
6583       {
6584         DAC960_V1_UserCommand_T __user *UserSpaceUserCommand =
6585           (DAC960_V1_UserCommand_T __user *) Argument;
6586         DAC960_V1_UserCommand_T UserCommand;
6587         DAC960_Controller_T *Controller;
6588         DAC960_Command_T *Command = NULL;
6589         DAC960_V1_CommandOpcode_T CommandOpcode;
6590         DAC960_V1_CommandStatus_T CommandStatus;
6591         DAC960_V1_DCDB_T DCDB;
6592         DAC960_V1_DCDB_T *DCDB_IOBUF = NULL;
6593         dma_addr_t      DCDB_IOBUFDMA;
6594         unsigned long flags;
6595         int ControllerNumber, DataTransferLength;
6596         unsigned char *DataTransferBuffer = NULL;
6597         dma_addr_t DataTransferBufferDMA;
6598         if (UserSpaceUserCommand == NULL) return -EINVAL;
6599         if (copy_from_user(&UserCommand, UserSpaceUserCommand,
6600                                    sizeof(DAC960_V1_UserCommand_T))) {
6601                 ErrorCode = -EFAULT;
6602                 goto Failure1a;
6603         }
6604         ControllerNumber = UserCommand.ControllerNumber;
6605         if (ControllerNumber < 0 ||
6606             ControllerNumber > DAC960_ControllerCount - 1)
6607           return -ENXIO;
6608         Controller = DAC960_Controllers[ControllerNumber];
6609         if (Controller == NULL) return -ENXIO;
6610         if (Controller->FirmwareType != DAC960_V1_Controller) return -EINVAL;
6611         CommandOpcode = UserCommand.CommandMailbox.Common.CommandOpcode;
6612         DataTransferLength = UserCommand.DataTransferLength;
6613         if (CommandOpcode & 0x80) return -EINVAL;
6614         if (CommandOpcode == DAC960_V1_DCDB)
6615           {
6616             if (copy_from_user(&DCDB, UserCommand.DCDB,
6617                                sizeof(DAC960_V1_DCDB_T))) {
6618                 ErrorCode = -EFAULT;
6619                 goto Failure1a;
6620             }
6621             if (DCDB.Channel >= DAC960_V1_MaxChannels) return -EINVAL;
6622             if (!((DataTransferLength == 0 &&
6623                    DCDB.Direction
6624                    == DAC960_V1_DCDB_NoDataTransfer) ||
6625                   (DataTransferLength > 0 &&
6626                    DCDB.Direction
6627                    == DAC960_V1_DCDB_DataTransferDeviceToSystem) ||
6628                   (DataTransferLength < 0 &&
6629                    DCDB.Direction
6630                    == DAC960_V1_DCDB_DataTransferSystemToDevice)))
6631               return -EINVAL;
6632             if (((DCDB.TransferLengthHigh4 << 16) | DCDB.TransferLength)
6633                 != abs(DataTransferLength))
6634               return -EINVAL;
6635             DCDB_IOBUF = pci_alloc_consistent(Controller->PCIDevice,
6636                         sizeof(DAC960_V1_DCDB_T), &DCDB_IOBUFDMA);
6637             if (DCDB_IOBUF == NULL)
6638                         return -ENOMEM;
6639           }
6640         if (DataTransferLength > 0)
6641           {
6642             DataTransferBuffer = pci_alloc_consistent(Controller->PCIDevice,
6643                                 DataTransferLength, &DataTransferBufferDMA);
6644             if (DataTransferBuffer == NULL) {
6645                 ErrorCode = -ENOMEM;
6646                 goto Failure1;
6647             }
6648             memset(DataTransferBuffer, 0, DataTransferLength);
6649           }
6650         else if (DataTransferLength < 0)
6651           {
6652             DataTransferBuffer = pci_alloc_consistent(Controller->PCIDevice,
6653                                 -DataTransferLength, &DataTransferBufferDMA);
6654             if (DataTransferBuffer == NULL) {
6655                 ErrorCode = -ENOMEM;
6656                 goto Failure1;
6657             }
6658             if (copy_from_user(DataTransferBuffer,
6659                                UserCommand.DataTransferBuffer,
6660                                -DataTransferLength)) {
6661                 ErrorCode = -EFAULT;
6662                 goto Failure1;
6663             }
6664           }
6665         if (CommandOpcode == DAC960_V1_DCDB)
6666           {
6667             spin_lock_irqsave(&Controller->queue_lock, flags);
6668             while ((Command = DAC960_AllocateCommand(Controller)) == NULL)
6669               DAC960_WaitForCommand(Controller);
6670             while (Controller->V1.DirectCommandActive[DCDB.Channel]
6671                                                      [DCDB.TargetID])
6672               {
6673                 spin_unlock_irq(&Controller->queue_lock);
6674                 __wait_event(Controller->CommandWaitQueue,
6675                              !Controller->V1.DirectCommandActive
6676                                              [DCDB.Channel][DCDB.TargetID]);
6677                 spin_lock_irq(&Controller->queue_lock);
6678               }
6679             Controller->V1.DirectCommandActive[DCDB.Channel]
6680                                               [DCDB.TargetID] = true;
6681             spin_unlock_irqrestore(&Controller->queue_lock, flags);
6682             DAC960_V1_ClearCommand(Command);
6683             Command->CommandType = DAC960_ImmediateCommand;
6684             memcpy(&Command->V1.CommandMailbox, &UserCommand.CommandMailbox,
6685                    sizeof(DAC960_V1_CommandMailbox_T));
6686             Command->V1.CommandMailbox.Type3.BusAddress = DCDB_IOBUFDMA;
6687             DCDB.BusAddress = DataTransferBufferDMA;
6688             memcpy(DCDB_IOBUF, &DCDB, sizeof(DAC960_V1_DCDB_T));
6689           }
6690         else
6691           {
6692             spin_lock_irqsave(&Controller->queue_lock, flags);
6693             while ((Command = DAC960_AllocateCommand(Controller)) == NULL)
6694               DAC960_WaitForCommand(Controller);
6695             spin_unlock_irqrestore(&Controller->queue_lock, flags);
6696             DAC960_V1_ClearCommand(Command);
6697             Command->CommandType = DAC960_ImmediateCommand;
6698             memcpy(&Command->V1.CommandMailbox, &UserCommand.CommandMailbox,
6699                    sizeof(DAC960_V1_CommandMailbox_T));
6700             if (DataTransferBuffer != NULL)
6701               Command->V1.CommandMailbox.Type3.BusAddress =
6702                 DataTransferBufferDMA;
6703           }
6704         DAC960_ExecuteCommand(Command);
6705         CommandStatus = Command->V1.CommandStatus;
6706         spin_lock_irqsave(&Controller->queue_lock, flags);
6707         DAC960_DeallocateCommand(Command);
6708         spin_unlock_irqrestore(&Controller->queue_lock, flags);
6709         if (DataTransferLength > 0)
6710           {
6711             if (copy_to_user(UserCommand.DataTransferBuffer,
6712                              DataTransferBuffer, DataTransferLength)) {
6713                 ErrorCode = -EFAULT;
6714                 goto Failure1;
6715             }
6716           }
6717         if (CommandOpcode == DAC960_V1_DCDB)
6718           {
6719             /*
6720               I don't believe Target or Channel in the DCDB_IOBUF
6721               should be any different from the contents of DCDB.
6722              */
6723             Controller->V1.DirectCommandActive[DCDB.Channel]
6724                                               [DCDB.TargetID] = false;
6725             if (copy_to_user(UserCommand.DCDB, DCDB_IOBUF,
6726                              sizeof(DAC960_V1_DCDB_T))) {
6727                 ErrorCode = -EFAULT;
6728                 goto Failure1;
6729             }
6730           }
6731         ErrorCode = CommandStatus;
6732       Failure1:
6733         if (DataTransferBuffer != NULL)
6734           pci_free_consistent(Controller->PCIDevice, abs(DataTransferLength),
6735                         DataTransferBuffer, DataTransferBufferDMA);
6736         if (DCDB_IOBUF != NULL)
6737           pci_free_consistent(Controller->PCIDevice, sizeof(DAC960_V1_DCDB_T),
6738                         DCDB_IOBUF, DCDB_IOBUFDMA);
6739       Failure1a:
6740         return ErrorCode;
6741       }
6742     case DAC960_IOCTL_V2_EXECUTE_COMMAND:
6743       {
6744         DAC960_V2_UserCommand_T __user *UserSpaceUserCommand =
6745           (DAC960_V2_UserCommand_T __user *) Argument;
6746         DAC960_V2_UserCommand_T UserCommand;
6747         DAC960_Controller_T *Controller;
6748         DAC960_Command_T *Command = NULL;
6749         DAC960_V2_CommandMailbox_T *CommandMailbox;
6750         DAC960_V2_CommandStatus_T CommandStatus;
6751         unsigned long flags;
6752         int ControllerNumber, DataTransferLength;
6753         int DataTransferResidue, RequestSenseLength;
6754         unsigned char *DataTransferBuffer = NULL;
6755         dma_addr_t DataTransferBufferDMA;
6756         unsigned char *RequestSenseBuffer = NULL;
6757         dma_addr_t RequestSenseBufferDMA;
6758         if (UserSpaceUserCommand == NULL) return -EINVAL;
6759         if (copy_from_user(&UserCommand, UserSpaceUserCommand,
6760                            sizeof(DAC960_V2_UserCommand_T))) {
6761                 ErrorCode = -EFAULT;
6762                 goto Failure2a;
6763         }
6764         ControllerNumber = UserCommand.ControllerNumber;
6765         if (ControllerNumber < 0 ||
6766             ControllerNumber > DAC960_ControllerCount - 1)
6767           return -ENXIO;
6768         Controller = DAC960_Controllers[ControllerNumber];
6769         if (Controller == NULL) return -ENXIO;
6770         if (Controller->FirmwareType != DAC960_V2_Controller) return -EINVAL;
6771         DataTransferLength = UserCommand.DataTransferLength;
6772         if (DataTransferLength > 0)
6773           {
6774             DataTransferBuffer = pci_alloc_consistent(Controller->PCIDevice,
6775                                 DataTransferLength, &DataTransferBufferDMA);
6776             if (DataTransferBuffer == NULL) return -ENOMEM;
6777             memset(DataTransferBuffer, 0, DataTransferLength);
6778           }
6779         else if (DataTransferLength < 0)
6780           {
6781             DataTransferBuffer = pci_alloc_consistent(Controller->PCIDevice,
6782                                 -DataTransferLength, &DataTransferBufferDMA);
6783             if (DataTransferBuffer == NULL) return -ENOMEM;
6784             if (copy_from_user(DataTransferBuffer,
6785                                UserCommand.DataTransferBuffer,
6786                                -DataTransferLength)) {
6787                 ErrorCode = -EFAULT;
6788                 goto Failure2;
6789             }
6790           }
6791         RequestSenseLength = UserCommand.RequestSenseLength;
6792         if (RequestSenseLength > 0)
6793           {
6794             RequestSenseBuffer = pci_alloc_consistent(Controller->PCIDevice,
6795                         RequestSenseLength, &RequestSenseBufferDMA);
6796             if (RequestSenseBuffer == NULL)
6797               {
6798                 ErrorCode = -ENOMEM;
6799                 goto Failure2;
6800               }
6801             memset(RequestSenseBuffer, 0, RequestSenseLength);
6802           }
6803         spin_lock_irqsave(&Controller->queue_lock, flags);
6804         while ((Command = DAC960_AllocateCommand(Controller)) == NULL)
6805           DAC960_WaitForCommand(Controller);
6806         spin_unlock_irqrestore(&Controller->queue_lock, flags);
6807         DAC960_V2_ClearCommand(Command);
6808         Command->CommandType = DAC960_ImmediateCommand;
6809         CommandMailbox = &Command->V2.CommandMailbox;
6810         memcpy(CommandMailbox, &UserCommand.CommandMailbox,
6811                sizeof(DAC960_V2_CommandMailbox_T));
6812         CommandMailbox->Common.CommandControlBits
6813                               .AdditionalScatterGatherListMemory = false;
6814         CommandMailbox->Common.CommandControlBits
6815                               .NoAutoRequestSense = true;
6816         CommandMailbox->Common.DataTransferSize = 0;
6817         CommandMailbox->Common.DataTransferPageNumber = 0;
6818         memset(&CommandMailbox->Common.DataTransferMemoryAddress, 0,
6819                sizeof(DAC960_V2_DataTransferMemoryAddress_T));
6820         if (DataTransferLength != 0)
6821           {
6822             if (DataTransferLength > 0)
6823               {
6824                 CommandMailbox->Common.CommandControlBits
6825                                       .DataTransferControllerToHost = true;
6826                 CommandMailbox->Common.DataTransferSize = DataTransferLength;
6827               }
6828             else
6829               {
6830                 CommandMailbox->Common.CommandControlBits
6831                                       .DataTransferControllerToHost = false;
6832                 CommandMailbox->Common.DataTransferSize = -DataTransferLength;
6833               }
6834             CommandMailbox->Common.DataTransferMemoryAddress
6835                                   .ScatterGatherSegments[0]
6836                                   .SegmentDataPointer = DataTransferBufferDMA;
6837             CommandMailbox->Common.DataTransferMemoryAddress
6838                                   .ScatterGatherSegments[0]
6839                                   .SegmentByteCount =
6840               CommandMailbox->Common.DataTransferSize;
6841           }
6842         if (RequestSenseLength > 0)
6843           {
6844             CommandMailbox->Common.CommandControlBits
6845                                   .NoAutoRequestSense = false;
6846             CommandMailbox->Common.RequestSenseSize = RequestSenseLength;
6847             CommandMailbox->Common.RequestSenseBusAddress =
6848                                                         RequestSenseBufferDMA;
6849           }
6850         DAC960_ExecuteCommand(Command);
6851         CommandStatus = Command->V2.CommandStatus;
6852         RequestSenseLength = Command->V2.RequestSenseLength;
6853         DataTransferResidue = Command->V2.DataTransferResidue;
6854         spin_lock_irqsave(&Controller->queue_lock, flags);
6855         DAC960_DeallocateCommand(Command);
6856         spin_unlock_irqrestore(&Controller->queue_lock, flags);
6857         if (RequestSenseLength > UserCommand.RequestSenseLength)
6858           RequestSenseLength = UserCommand.RequestSenseLength;
6859         if (copy_to_user(&UserSpaceUserCommand->DataTransferLength,
6860                                  &DataTransferResidue,
6861                                  sizeof(DataTransferResidue))) {
6862                 ErrorCode = -EFAULT;
6863                 goto Failure2;
6864         }
6865         if (copy_to_user(&UserSpaceUserCommand->RequestSenseLength,
6866                          &RequestSenseLength, sizeof(RequestSenseLength))) {
6867                 ErrorCode = -EFAULT;
6868                 goto Failure2;
6869         }
6870         if (DataTransferLength > 0)
6871           {
6872             if (copy_to_user(UserCommand.DataTransferBuffer,
6873                              DataTransferBuffer, DataTransferLength)) {
6874                 ErrorCode = -EFAULT;
6875                 goto Failure2;
6876             }
6877           }
6878         if (RequestSenseLength > 0)
6879           {
6880             if (copy_to_user(UserCommand.RequestSenseBuffer,
6881                              RequestSenseBuffer, RequestSenseLength)) {
6882                 ErrorCode = -EFAULT;
6883                 goto Failure2;
6884             }
6885           }
6886         ErrorCode = CommandStatus;
6887       Failure2:
6888           pci_free_consistent(Controller->PCIDevice, abs(DataTransferLength),
6889                 DataTransferBuffer, DataTransferBufferDMA);
6890         if (RequestSenseBuffer != NULL)
6891           pci_free_consistent(Controller->PCIDevice, RequestSenseLength,
6892                 RequestSenseBuffer, RequestSenseBufferDMA);
6893       Failure2a:
6894         return ErrorCode;
6895       }
6896     case DAC960_IOCTL_V2_GET_HEALTH_STATUS:
6897       {
6898         DAC960_V2_GetHealthStatus_T __user *UserSpaceGetHealthStatus =
6899           (DAC960_V2_GetHealthStatus_T __user *) Argument;
6900         DAC960_V2_GetHealthStatus_T GetHealthStatus;
6901         DAC960_V2_HealthStatusBuffer_T HealthStatusBuffer;
6902         DAC960_Controller_T *Controller;
6903         int ControllerNumber;
6904         if (UserSpaceGetHealthStatus == NULL) return -EINVAL;
6905         if (copy_from_user(&GetHealthStatus, UserSpaceGetHealthStatus,
6906                            sizeof(DAC960_V2_GetHealthStatus_T)))
6907                 return -EFAULT;
6908         ControllerNumber = GetHealthStatus.ControllerNumber;
6909         if (ControllerNumber < 0 ||
6910             ControllerNumber > DAC960_ControllerCount - 1)
6911           return -ENXIO;
6912         Controller = DAC960_Controllers[ControllerNumber];
6913         if (Controller == NULL) return -ENXIO;
6914         if (Controller->FirmwareType != DAC960_V2_Controller) return -EINVAL;
6915         if (copy_from_user(&HealthStatusBuffer,
6916                            GetHealthStatus.HealthStatusBuffer,
6917                            sizeof(DAC960_V2_HealthStatusBuffer_T)))
6918                 return -EFAULT;
6919         while (Controller->V2.HealthStatusBuffer->StatusChangeCounter
6920                == HealthStatusBuffer.StatusChangeCounter &&
6921                Controller->V2.HealthStatusBuffer->NextEventSequenceNumber
6922                == HealthStatusBuffer.NextEventSequenceNumber)
6923           {
6924             interruptible_sleep_on_timeout(&Controller->HealthStatusWaitQueue,
6925                                            DAC960_MonitoringTimerInterval);
6926             if (signal_pending(current)) return -EINTR;
6927           }
6928         if (copy_to_user(GetHealthStatus.HealthStatusBuffer,
6929                          Controller->V2.HealthStatusBuffer,
6930                          sizeof(DAC960_V2_HealthStatusBuffer_T)))
6931                 return -EFAULT;
6932         return 0;
6933       }
6934     }
6935   return -EINVAL;
6936 }
6937
6938 static struct file_operations DAC960_gam_fops = {
6939         .owner          = THIS_MODULE,
6940         .ioctl          = DAC960_gam_ioctl
6941 };
6942
6943 static struct miscdevice DAC960_gam_dev = {
6944         DAC960_GAM_MINOR,
6945         "dac960_gam",
6946         &DAC960_gam_fops
6947 };
6948
6949 static int DAC960_gam_init(void)
6950 {
6951         int ret;
6952
6953         ret = misc_register(&DAC960_gam_dev);
6954         if (ret)
6955                 printk(KERN_ERR "DAC960_gam: can't misc_register on minor %d\n", DAC960_GAM_MINOR);
6956         return ret;
6957 }
6958
6959 static void DAC960_gam_cleanup(void)
6960 {
6961         misc_deregister(&DAC960_gam_dev);
6962 }
6963
6964 #endif /* DAC960_GAM_MINOR */
6965
6966 static struct DAC960_privdata DAC960_BA_privdata = {
6967         .HardwareType =         DAC960_BA_Controller,
6968         .FirmwareType   =       DAC960_V2_Controller,
6969         .InterruptHandler =     DAC960_BA_InterruptHandler,
6970         .MemoryWindowSize =     DAC960_BA_RegisterWindowSize,
6971 };
6972
6973 static struct DAC960_privdata DAC960_LP_privdata = {
6974         .HardwareType =         DAC960_LP_Controller,
6975         .FirmwareType   =       DAC960_LP_Controller,
6976         .InterruptHandler =     DAC960_LP_InterruptHandler,
6977         .MemoryWindowSize =     DAC960_LP_RegisterWindowSize,
6978 };
6979
6980 static struct DAC960_privdata DAC960_LA_privdata = {
6981         .HardwareType =         DAC960_LA_Controller,
6982         .FirmwareType   =       DAC960_V1_Controller,
6983         .InterruptHandler =     DAC960_LA_InterruptHandler,
6984         .MemoryWindowSize =     DAC960_LA_RegisterWindowSize,
6985 };
6986
6987 static struct DAC960_privdata DAC960_PG_privdata = {
6988         .HardwareType =         DAC960_PG_Controller,
6989         .FirmwareType   =       DAC960_V1_Controller,
6990         .InterruptHandler =     DAC960_PG_InterruptHandler,
6991         .MemoryWindowSize =     DAC960_PG_RegisterWindowSize,
6992 };
6993
6994 static struct DAC960_privdata DAC960_PD_privdata = {
6995         .HardwareType =         DAC960_PD_Controller,
6996         .FirmwareType   =       DAC960_V1_Controller,
6997         .InterruptHandler =     DAC960_PD_InterruptHandler,
6998         .MemoryWindowSize =     DAC960_PD_RegisterWindowSize,
6999 };
7000
7001 static struct DAC960_privdata DAC960_P_privdata = {
7002         .HardwareType =         DAC960_P_Controller,
7003         .FirmwareType   =       DAC960_V1_Controller,
7004         .InterruptHandler =     DAC960_P_InterruptHandler,
7005         .MemoryWindowSize =     DAC960_PD_RegisterWindowSize,
7006 };
7007
7008 static struct pci_device_id DAC960_id_table[] = {
7009         {
7010                 .vendor         = PCI_VENDOR_ID_MYLEX,
7011                 .device         = PCI_DEVICE_ID_MYLEX_DAC960_BA,
7012                 .subvendor      = PCI_ANY_ID,
7013                 .subdevice      = PCI_ANY_ID,
7014                 .driver_data    = (unsigned long) &DAC960_BA_privdata,
7015         },
7016         {
7017                 .vendor         = PCI_VENDOR_ID_MYLEX,
7018                 .device         = PCI_DEVICE_ID_MYLEX_DAC960_LP,
7019                 .subvendor      = PCI_ANY_ID,
7020                 .subdevice      = PCI_ANY_ID,
7021                 .driver_data    = (unsigned long) &DAC960_LP_privdata,
7022         },
7023         {
7024                 .vendor         = PCI_VENDOR_ID_DEC,
7025                 .device         = PCI_DEVICE_ID_DEC_21285,
7026                 .subvendor      = PCI_VENDOR_ID_MYLEX,
7027                 .subdevice      = PCI_DEVICE_ID_MYLEX_DAC960_LA,
7028                 .driver_data    = (unsigned long) &DAC960_LA_privdata,
7029         },
7030         {
7031                 .vendor         = PCI_VENDOR_ID_MYLEX,
7032                 .device         = PCI_DEVICE_ID_MYLEX_DAC960_PG,
7033                 .subvendor      = PCI_ANY_ID,
7034                 .subdevice      = PCI_ANY_ID,
7035                 .driver_data    = (unsigned long) &DAC960_PG_privdata,
7036         },
7037         {
7038                 .vendor         = PCI_VENDOR_ID_MYLEX,
7039                 .device         = PCI_DEVICE_ID_MYLEX_DAC960_PD,
7040                 .subvendor      = PCI_ANY_ID,
7041                 .subdevice      = PCI_ANY_ID,
7042                 .driver_data    = (unsigned long) &DAC960_PD_privdata,
7043         },
7044         {
7045                 .vendor         = PCI_VENDOR_ID_MYLEX,
7046                 .device         = PCI_DEVICE_ID_MYLEX_DAC960_P,
7047                 .subvendor      = PCI_ANY_ID,
7048                 .subdevice      = PCI_ANY_ID,
7049                 .driver_data    = (unsigned long) &DAC960_P_privdata,
7050         },
7051         {0, },
7052 };
7053
7054 MODULE_DEVICE_TABLE(pci, DAC960_id_table);
7055
7056 static struct pci_driver DAC960_pci_driver = {
7057         .name           = "DAC960",
7058         .id_table       = DAC960_id_table,
7059         .probe          = DAC960_Probe,
7060         .remove         = DAC960_Remove,
7061 };
7062
7063 static int DAC960_init_module(void)
7064 {
7065         int ret;
7066
7067         ret =  pci_module_init(&DAC960_pci_driver);
7068 #ifdef DAC960_GAM_MINOR
7069         if (!ret)
7070                 DAC960_gam_init();
7071 #endif
7072         return ret;
7073 }
7074
7075 static void DAC960_cleanup_module(void)
7076 {
7077         int i;
7078
7079 #ifdef DAC960_GAM_MINOR
7080         DAC960_gam_cleanup();
7081 #endif
7082
7083         for (i = 0; i < DAC960_ControllerCount; i++) {
7084                 DAC960_Controller_T *Controller = DAC960_Controllers[i];
7085                 if (Controller == NULL)
7086                         continue;
7087                 DAC960_FinalizeController(Controller);
7088         }
7089         if (DAC960_ProcDirectoryEntry != NULL) {
7090                 remove_proc_entry("rd/status", NULL);
7091                 remove_proc_entry("rd", NULL);
7092         }
7093         DAC960_ControllerCount = 0;
7094         pci_unregister_driver(&DAC960_pci_driver);
7095 }
7096
7097 module_init(DAC960_init_module);
7098 module_exit(DAC960_cleanup_module);
7099
7100 MODULE_LICENSE("GPL");