Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux...
[sfrench/cifs-2.6.git] / drivers / scsi / BusLogic.c
1
2 /*
3
4   Linux Driver for BusLogic MultiMaster and FlashPoint SCSI Host Adapters
5
6   Copyright 1995-1998 by Leonard N. Zubkoff <lnz@dandelion.com>
7
8   This program is free software; you may redistribute and/or modify it under
9   the terms of the GNU General Public License Version 2 as published by the
10   Free Software Foundation.
11
12   This program is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
14   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15   for complete details.
16
17   The author respectfully requests that any modifications to this software be
18   sent directly to him for evaluation and testing.
19
20   Special thanks to Wayne Yen, Jin-Lon Hon, and Alex Win of BusLogic, whose
21   advice has been invaluable, to David Gentzel, for writing the original Linux
22   BusLogic driver, and to Paul Gortmaker, for being such a dedicated test site.
23
24   Finally, special thanks to Mylex/BusLogic for making the FlashPoint SCCB
25   Manager available as freely redistributable source code.
26
27 */
28
29 #define BusLogic_DriverVersion          "2.1.16"
30 #define BusLogic_DriverDate             "18 July 2002"
31
32 #include <linux/module.h>
33 #include <linux/init.h>
34 #include <linux/interrupt.h>
35 #include <linux/types.h>
36 #include <linux/blkdev.h>
37 #include <linux/delay.h>
38 #include <linux/ioport.h>
39 #include <linux/mm.h>
40 #include <linux/stat.h>
41 #include <linux/pci.h>
42 #include <linux/spinlock.h>
43 #include <linux/jiffies.h>
44 #include <linux/dma-mapping.h>
45 #include <scsi/scsicam.h>
46
47 #include <asm/dma.h>
48 #include <asm/io.h>
49 #include <asm/system.h>
50
51 #include <scsi/scsi.h>
52 #include <scsi/scsi_cmnd.h>
53 #include <scsi/scsi_device.h>
54 #include <scsi/scsi_host.h>
55 #include <scsi/scsi_tcq.h>
56 #include "BusLogic.h"
57 #include "FlashPoint.c"
58
59 #ifndef FAILURE
60 #define FAILURE (-1)
61 #endif
62
63 static struct scsi_host_template Bus_Logic_template;
64
65 /*
66   BusLogic_DriverOptionsCount is a count of the number of BusLogic Driver
67   Options specifications provided via the Linux Kernel Command Line or via
68   the Loadable Kernel Module Installation Facility.
69 */
70
71 static int BusLogic_DriverOptionsCount;
72
73
74 /*
75   BusLogic_DriverOptions is an array of Driver Options structures representing
76   BusLogic Driver Options specifications provided via the Linux Kernel Command
77   Line or via the Loadable Kernel Module Installation Facility.
78 */
79
80 static struct BusLogic_DriverOptions BusLogic_DriverOptions[BusLogic_MaxHostAdapters];
81
82
83 /*
84   BusLogic can be assigned a string by insmod.
85 */
86
87 MODULE_LICENSE("GPL");
88 #ifdef MODULE
89 static char *BusLogic;
90 module_param(BusLogic, charp, 0);
91 #endif
92
93
94 /*
95   BusLogic_ProbeOptions is a set of Probe Options to be applied across
96   all BusLogic Host Adapters.
97 */
98
99 static struct BusLogic_ProbeOptions BusLogic_ProbeOptions;
100
101
102 /*
103   BusLogic_GlobalOptions is a set of Global Options to be applied across
104   all BusLogic Host Adapters.
105 */
106
107 static struct BusLogic_GlobalOptions BusLogic_GlobalOptions;
108
109 static LIST_HEAD(BusLogic_host_list);
110
111 /*
112   BusLogic_ProbeInfoCount is the number of entries in BusLogic_ProbeInfoList.
113 */
114
115 static int BusLogic_ProbeInfoCount;
116
117
118 /*
119   BusLogic_ProbeInfoList is the list of I/O Addresses and Bus Probe Information
120   to be checked for potential BusLogic Host Adapters.  It is initialized by
121   interrogating the PCI Configuration Space on PCI machines as well as from the
122   list of standard BusLogic I/O Addresses.
123 */
124
125 static struct BusLogic_ProbeInfo *BusLogic_ProbeInfoList;
126
127
128 /*
129   BusLogic_CommandFailureReason holds a string identifying the reason why a
130   call to BusLogic_Command failed.  It is only non-NULL when BusLogic_Command
131   returns a failure code.
132 */
133
134 static char *BusLogic_CommandFailureReason;
135
136 /*
137   BusLogic_AnnounceDriver announces the Driver Version and Date, Author's
138   Name, Copyright Notice, and Electronic Mail Address.
139 */
140
141 static void BusLogic_AnnounceDriver(struct BusLogic_HostAdapter *HostAdapter)
142 {
143         BusLogic_Announce("***** BusLogic SCSI Driver Version " BusLogic_DriverVersion " of " BusLogic_DriverDate " *****\n", HostAdapter);
144         BusLogic_Announce("Copyright 1995-1998 by Leonard N. Zubkoff " "<lnz@dandelion.com>\n", HostAdapter);
145 }
146
147
148 /*
149   BusLogic_DriverInfo returns the Host Adapter Name to identify this SCSI
150   Driver and Host Adapter.
151 */
152
153 static const char *BusLogic_DriverInfo(struct Scsi_Host *Host)
154 {
155         struct BusLogic_HostAdapter *HostAdapter = (struct BusLogic_HostAdapter *) Host->hostdata;
156         return HostAdapter->FullModelName;
157 }
158
159 /*
160   BusLogic_InitializeCCBs initializes a group of Command Control Blocks (CCBs)
161   for Host Adapter from the BlockSize bytes located at BlockPointer.  The newly
162   created CCBs are added to Host Adapter's free list.
163 */
164
165 static void BusLogic_InitializeCCBs(struct BusLogic_HostAdapter *HostAdapter, void *BlockPointer, int BlockSize, dma_addr_t BlockPointerHandle)
166 {
167         struct BusLogic_CCB *CCB = (struct BusLogic_CCB *) BlockPointer;
168         unsigned int offset = 0;
169         memset(BlockPointer, 0, BlockSize);
170         CCB->AllocationGroupHead = BlockPointerHandle;
171         CCB->AllocationGroupSize = BlockSize;
172         while ((BlockSize -= sizeof(struct BusLogic_CCB)) >= 0) {
173                 CCB->Status = BusLogic_CCB_Free;
174                 CCB->HostAdapter = HostAdapter;
175                 CCB->DMA_Handle = (u32) BlockPointerHandle + offset;
176                 if (BusLogic_FlashPointHostAdapterP(HostAdapter)) {
177                         CCB->CallbackFunction = BusLogic_QueueCompletedCCB;
178                         CCB->BaseAddress = HostAdapter->FlashPointInfo.BaseAddress;
179                 }
180                 CCB->Next = HostAdapter->Free_CCBs;
181                 CCB->NextAll = HostAdapter->All_CCBs;
182                 HostAdapter->Free_CCBs = CCB;
183                 HostAdapter->All_CCBs = CCB;
184                 HostAdapter->AllocatedCCBs++;
185                 CCB++;
186                 offset += sizeof(struct BusLogic_CCB);
187         }
188 }
189
190
191 /*
192   BusLogic_CreateInitialCCBs allocates the initial CCBs for Host Adapter.
193 */
194
195 static boolean __init BusLogic_CreateInitialCCBs(struct BusLogic_HostAdapter *HostAdapter)
196 {
197         int BlockSize = BusLogic_CCB_AllocationGroupSize * sizeof(struct BusLogic_CCB);
198         void *BlockPointer;
199         dma_addr_t BlockPointerHandle;
200         while (HostAdapter->AllocatedCCBs < HostAdapter->InitialCCBs) {
201                 BlockPointer = pci_alloc_consistent(HostAdapter->PCI_Device, BlockSize, &BlockPointerHandle);
202                 if (BlockPointer == NULL) {
203                         BusLogic_Error("UNABLE TO ALLOCATE CCB GROUP - DETACHING\n", HostAdapter);
204                         return false;
205                 }
206                 BusLogic_InitializeCCBs(HostAdapter, BlockPointer, BlockSize, BlockPointerHandle);
207         }
208         return true;
209 }
210
211
212 /*
213   BusLogic_DestroyCCBs deallocates the CCBs for Host Adapter.
214 */
215
216 static void BusLogic_DestroyCCBs(struct BusLogic_HostAdapter *HostAdapter)
217 {
218         struct BusLogic_CCB *NextCCB = HostAdapter->All_CCBs, *CCB, *Last_CCB = NULL;
219         HostAdapter->All_CCBs = NULL;
220         HostAdapter->Free_CCBs = NULL;
221         while ((CCB = NextCCB) != NULL) {
222                 NextCCB = CCB->NextAll;
223                 if (CCB->AllocationGroupHead) {
224                         if (Last_CCB)
225                                 pci_free_consistent(HostAdapter->PCI_Device, Last_CCB->AllocationGroupSize, Last_CCB, Last_CCB->AllocationGroupHead);
226                         Last_CCB = CCB;
227                 }
228         }
229         if (Last_CCB)
230                 pci_free_consistent(HostAdapter->PCI_Device, Last_CCB->AllocationGroupSize, Last_CCB, Last_CCB->AllocationGroupHead);
231 }
232
233
234 /*
235   BusLogic_CreateAdditionalCCBs allocates Additional CCBs for Host Adapter.  If
236   allocation fails and there are no remaining CCBs available, the Driver Queue
237   Depth is decreased to a known safe value to avoid potential deadlocks when
238   multiple host adapters share the same IRQ Channel.
239 */
240
241 static void BusLogic_CreateAdditionalCCBs(struct BusLogic_HostAdapter *HostAdapter, int AdditionalCCBs, boolean SuccessMessageP)
242 {
243         int BlockSize = BusLogic_CCB_AllocationGroupSize * sizeof(struct BusLogic_CCB);
244         int PreviouslyAllocated = HostAdapter->AllocatedCCBs;
245         void *BlockPointer;
246         dma_addr_t BlockPointerHandle;
247         if (AdditionalCCBs <= 0)
248                 return;
249         while (HostAdapter->AllocatedCCBs - PreviouslyAllocated < AdditionalCCBs) {
250                 BlockPointer = pci_alloc_consistent(HostAdapter->PCI_Device, BlockSize, &BlockPointerHandle);
251                 if (BlockPointer == NULL)
252                         break;
253                 BusLogic_InitializeCCBs(HostAdapter, BlockPointer, BlockSize, BlockPointerHandle);
254         }
255         if (HostAdapter->AllocatedCCBs > PreviouslyAllocated) {
256                 if (SuccessMessageP)
257                         BusLogic_Notice("Allocated %d additional CCBs (total now %d)\n", HostAdapter, HostAdapter->AllocatedCCBs - PreviouslyAllocated, HostAdapter->AllocatedCCBs);
258                 return;
259         }
260         BusLogic_Notice("Failed to allocate additional CCBs\n", HostAdapter);
261         if (HostAdapter->DriverQueueDepth > HostAdapter->AllocatedCCBs - HostAdapter->TargetDeviceCount) {
262                 HostAdapter->DriverQueueDepth = HostAdapter->AllocatedCCBs - HostAdapter->TargetDeviceCount;
263                 HostAdapter->SCSI_Host->can_queue = HostAdapter->DriverQueueDepth;
264         }
265 }
266
267 /*
268   BusLogic_AllocateCCB allocates a CCB from Host Adapter's free list,
269   allocating more memory from the Kernel if necessary.  The Host Adapter's
270   Lock should already have been acquired by the caller.
271 */
272
273 static struct BusLogic_CCB *BusLogic_AllocateCCB(struct BusLogic_HostAdapter
274                                                  *HostAdapter)
275 {
276         static unsigned long SerialNumber = 0;
277         struct BusLogic_CCB *CCB;
278         CCB = HostAdapter->Free_CCBs;
279         if (CCB != NULL) {
280                 CCB->SerialNumber = ++SerialNumber;
281                 HostAdapter->Free_CCBs = CCB->Next;
282                 CCB->Next = NULL;
283                 if (HostAdapter->Free_CCBs == NULL)
284                         BusLogic_CreateAdditionalCCBs(HostAdapter, HostAdapter->IncrementalCCBs, true);
285                 return CCB;
286         }
287         BusLogic_CreateAdditionalCCBs(HostAdapter, HostAdapter->IncrementalCCBs, true);
288         CCB = HostAdapter->Free_CCBs;
289         if (CCB == NULL)
290                 return NULL;
291         CCB->SerialNumber = ++SerialNumber;
292         HostAdapter->Free_CCBs = CCB->Next;
293         CCB->Next = NULL;
294         return CCB;
295 }
296
297
298 /*
299   BusLogic_DeallocateCCB deallocates a CCB, returning it to the Host Adapter's
300   free list.  The Host Adapter's Lock should already have been acquired by the
301   caller.
302 */
303
304 static void BusLogic_DeallocateCCB(struct BusLogic_CCB *CCB)
305 {
306         struct BusLogic_HostAdapter *HostAdapter = CCB->HostAdapter;
307         struct scsi_cmnd *cmd = CCB->Command;
308
309         if (cmd->use_sg != 0) {
310                 pci_unmap_sg(HostAdapter->PCI_Device,
311                                 (struct scatterlist *)cmd->request_buffer,
312                                 cmd->use_sg, cmd->sc_data_direction);
313         } else if (cmd->request_bufflen != 0) {
314                 pci_unmap_single(HostAdapter->PCI_Device, CCB->DataPointer,
315                                 CCB->DataLength, cmd->sc_data_direction);
316         }
317         pci_unmap_single(HostAdapter->PCI_Device, CCB->SenseDataPointer,
318                         CCB->SenseDataLength, PCI_DMA_FROMDEVICE);
319
320         CCB->Command = NULL;
321         CCB->Status = BusLogic_CCB_Free;
322         CCB->Next = HostAdapter->Free_CCBs;
323         HostAdapter->Free_CCBs = CCB;
324 }
325
326
327 /*
328   BusLogic_Command sends the command OperationCode to HostAdapter, optionally
329   providing ParameterLength bytes of ParameterData and receiving at most
330   ReplyLength bytes of ReplyData; any excess reply data is received but
331   discarded.
332
333   On success, this function returns the number of reply bytes read from
334   the Host Adapter (including any discarded data); on failure, it returns
335   -1 if the command was invalid, or -2 if a timeout occurred.
336
337   BusLogic_Command is called exclusively during host adapter detection and
338   initialization, so performance and latency are not critical, and exclusive
339   access to the Host Adapter hardware is assumed.  Once the host adapter and
340   driver are initialized, the only Host Adapter command that is issued is the
341   single byte Execute Mailbox Command operation code, which does not require
342   waiting for the Host Adapter Ready bit to be set in the Status Register.
343 */
344
345 static int BusLogic_Command(struct BusLogic_HostAdapter *HostAdapter, enum BusLogic_OperationCode OperationCode, void *ParameterData, int ParameterLength, void *ReplyData, int ReplyLength)
346 {
347         unsigned char *ParameterPointer = (unsigned char *) ParameterData;
348         unsigned char *ReplyPointer = (unsigned char *) ReplyData;
349         union BusLogic_StatusRegister StatusRegister;
350         union BusLogic_InterruptRegister InterruptRegister;
351         unsigned long ProcessorFlags = 0;
352         int ReplyBytes = 0, Result;
353         long TimeoutCounter;
354         /*
355            Clear out the Reply Data if provided.
356          */
357         if (ReplyLength > 0)
358                 memset(ReplyData, 0, ReplyLength);
359         /*
360            If the IRQ Channel has not yet been acquired, then interrupts must be
361            disabled while issuing host adapter commands since a Command Complete
362            interrupt could occur if the IRQ Channel was previously enabled by another
363            BusLogic Host Adapter or another driver sharing the same IRQ Channel.
364          */
365         if (!HostAdapter->IRQ_ChannelAcquired) {
366                 local_irq_save(ProcessorFlags);
367                 local_irq_disable();
368         }
369         /*
370            Wait for the Host Adapter Ready bit to be set and the Command/Parameter
371            Register Busy bit to be reset in the Status Register.
372          */
373         TimeoutCounter = 10000;
374         while (--TimeoutCounter >= 0) {
375                 StatusRegister.All = BusLogic_ReadStatusRegister(HostAdapter);
376                 if (StatusRegister.sr.HostAdapterReady && !StatusRegister.sr.CommandParameterRegisterBusy)
377                         break;
378                 udelay(100);
379         }
380         if (TimeoutCounter < 0) {
381                 BusLogic_CommandFailureReason = "Timeout waiting for Host Adapter Ready";
382                 Result = -2;
383                 goto Done;
384         }
385         /*
386            Write the OperationCode to the Command/Parameter Register.
387          */
388         HostAdapter->HostAdapterCommandCompleted = false;
389         BusLogic_WriteCommandParameterRegister(HostAdapter, OperationCode);
390         /*
391            Write any additional Parameter Bytes.
392          */
393         TimeoutCounter = 10000;
394         while (ParameterLength > 0 && --TimeoutCounter >= 0) {
395                 /*
396                    Wait 100 microseconds to give the Host Adapter enough time to determine
397                    whether the last value written to the Command/Parameter Register was
398                    valid or not.  If the Command Complete bit is set in the Interrupt
399                    Register, then the Command Invalid bit in the Status Register will be
400                    reset if the Operation Code or Parameter was valid and the command
401                    has completed, or set if the Operation Code or Parameter was invalid.
402                    If the Data In Register Ready bit is set in the Status Register, then
403                    the Operation Code was valid, and data is waiting to be read back
404                    from the Host Adapter.  Otherwise, wait for the Command/Parameter
405                    Register Busy bit in the Status Register to be reset.
406                  */
407                 udelay(100);
408                 InterruptRegister.All = BusLogic_ReadInterruptRegister(HostAdapter);
409                 StatusRegister.All = BusLogic_ReadStatusRegister(HostAdapter);
410                 if (InterruptRegister.ir.CommandComplete)
411                         break;
412                 if (HostAdapter->HostAdapterCommandCompleted)
413                         break;
414                 if (StatusRegister.sr.DataInRegisterReady)
415                         break;
416                 if (StatusRegister.sr.CommandParameterRegisterBusy)
417                         continue;
418                 BusLogic_WriteCommandParameterRegister(HostAdapter, *ParameterPointer++);
419                 ParameterLength--;
420         }
421         if (TimeoutCounter < 0) {
422                 BusLogic_CommandFailureReason = "Timeout waiting for Parameter Acceptance";
423                 Result = -2;
424                 goto Done;
425         }
426         /*
427            The Modify I/O Address command does not cause a Command Complete Interrupt.
428          */
429         if (OperationCode == BusLogic_ModifyIOAddress) {
430                 StatusRegister.All = BusLogic_ReadStatusRegister(HostAdapter);
431                 if (StatusRegister.sr.CommandInvalid) {
432                         BusLogic_CommandFailureReason = "Modify I/O Address Invalid";
433                         Result = -1;
434                         goto Done;
435                 }
436                 if (BusLogic_GlobalOptions.TraceConfiguration)
437                         BusLogic_Notice("BusLogic_Command(%02X) Status = %02X: " "(Modify I/O Address)\n", HostAdapter, OperationCode, StatusRegister.All);
438                 Result = 0;
439                 goto Done;
440         }
441         /*
442            Select an appropriate timeout value for awaiting command completion.
443          */
444         switch (OperationCode) {
445         case BusLogic_InquireInstalledDevicesID0to7:
446         case BusLogic_InquireInstalledDevicesID8to15:
447         case BusLogic_InquireTargetDevices:
448                 /* Approximately 60 seconds. */
449                 TimeoutCounter = 60 * 10000;
450                 break;
451         default:
452                 /* Approximately 1 second. */
453                 TimeoutCounter = 10000;
454                 break;
455         }
456         /*
457            Receive any Reply Bytes, waiting for either the Command Complete bit to
458            be set in the Interrupt Register, or for the Interrupt Handler to set the
459            Host Adapter Command Completed bit in the Host Adapter structure.
460          */
461         while (--TimeoutCounter >= 0) {
462                 InterruptRegister.All = BusLogic_ReadInterruptRegister(HostAdapter);
463                 StatusRegister.All = BusLogic_ReadStatusRegister(HostAdapter);
464                 if (InterruptRegister.ir.CommandComplete)
465                         break;
466                 if (HostAdapter->HostAdapterCommandCompleted)
467                         break;
468                 if (StatusRegister.sr.DataInRegisterReady) {
469                         if (++ReplyBytes <= ReplyLength)
470                                 *ReplyPointer++ = BusLogic_ReadDataInRegister(HostAdapter);
471                         else
472                                 BusLogic_ReadDataInRegister(HostAdapter);
473                 }
474                 if (OperationCode == BusLogic_FetchHostAdapterLocalRAM && StatusRegister.sr.HostAdapterReady)
475                         break;
476                 udelay(100);
477         }
478         if (TimeoutCounter < 0) {
479                 BusLogic_CommandFailureReason = "Timeout waiting for Command Complete";
480                 Result = -2;
481                 goto Done;
482         }
483         /*
484            Clear any pending Command Complete Interrupt.
485          */
486         BusLogic_InterruptReset(HostAdapter);
487         /*
488            Provide tracing information if requested.
489          */
490         if (BusLogic_GlobalOptions.TraceConfiguration) {
491                 int i;
492                 BusLogic_Notice("BusLogic_Command(%02X) Status = %02X: %2d ==> %2d:", HostAdapter, OperationCode, StatusRegister.All, ReplyLength, ReplyBytes);
493                 if (ReplyLength > ReplyBytes)
494                         ReplyLength = ReplyBytes;
495                 for (i = 0; i < ReplyLength; i++)
496                         BusLogic_Notice(" %02X", HostAdapter, ((unsigned char *) ReplyData)[i]);
497                 BusLogic_Notice("\n", HostAdapter);
498         }
499         /*
500            Process Command Invalid conditions.
501          */
502         if (StatusRegister.sr.CommandInvalid) {
503                 /*
504                    Some early BusLogic Host Adapters may not recover properly from
505                    a Command Invalid condition, so if this appears to be the case,
506                    a Soft Reset is issued to the Host Adapter.  Potentially invalid
507                    commands are never attempted after Mailbox Initialization is
508                    performed, so there should be no Host Adapter state lost by a
509                    Soft Reset in response to a Command Invalid condition.
510                  */
511                 udelay(1000);
512                 StatusRegister.All = BusLogic_ReadStatusRegister(HostAdapter);
513                 if (StatusRegister.sr.CommandInvalid ||
514                     StatusRegister.sr.Reserved ||
515                     StatusRegister.sr.DataInRegisterReady ||
516                     StatusRegister.sr.CommandParameterRegisterBusy || !StatusRegister.sr.HostAdapterReady || !StatusRegister.sr.InitializationRequired || StatusRegister.sr.DiagnosticActive || StatusRegister.sr.DiagnosticFailure) {
517                         BusLogic_SoftReset(HostAdapter);
518                         udelay(1000);
519                 }
520                 BusLogic_CommandFailureReason = "Command Invalid";
521                 Result = -1;
522                 goto Done;
523         }
524         /*
525            Handle Excess Parameters Supplied conditions.
526          */
527         if (ParameterLength > 0) {
528                 BusLogic_CommandFailureReason = "Excess Parameters Supplied";
529                 Result = -1;
530                 goto Done;
531         }
532         /*
533            Indicate the command completed successfully.
534          */
535         BusLogic_CommandFailureReason = NULL;
536         Result = ReplyBytes;
537         /*
538            Restore the interrupt status if necessary and return.
539          */
540       Done:
541         if (!HostAdapter->IRQ_ChannelAcquired)
542                 local_irq_restore(ProcessorFlags);
543         return Result;
544 }
545
546
547 /*
548   BusLogic_AppendProbeAddressISA appends a single ISA I/O Address to the list
549   of I/O Address and Bus Probe Information to be checked for potential BusLogic
550   Host Adapters.
551 */
552
553 static void __init BusLogic_AppendProbeAddressISA(unsigned long IO_Address)
554 {
555         struct BusLogic_ProbeInfo *ProbeInfo;
556         if (BusLogic_ProbeInfoCount >= BusLogic_MaxHostAdapters)
557                 return;
558         ProbeInfo = &BusLogic_ProbeInfoList[BusLogic_ProbeInfoCount++];
559         ProbeInfo->HostAdapterType = BusLogic_MultiMaster;
560         ProbeInfo->HostAdapterBusType = BusLogic_ISA_Bus;
561         ProbeInfo->IO_Address = IO_Address;
562         ProbeInfo->PCI_Device = NULL;
563 }
564
565
566 /*
567   BusLogic_InitializeProbeInfoListISA initializes the list of I/O Address and
568   Bus Probe Information to be checked for potential BusLogic SCSI Host Adapters
569   only from the list of standard BusLogic MultiMaster ISA I/O Addresses.
570 */
571
572 static void __init BusLogic_InitializeProbeInfoListISA(struct BusLogic_HostAdapter
573                                                        *PrototypeHostAdapter)
574 {
575         /*
576            If BusLogic Driver Options specifications requested that ISA Bus Probes
577            be inhibited, do not proceed further.
578          */
579         if (BusLogic_ProbeOptions.NoProbeISA)
580                 return;
581         /*
582            Append the list of standard BusLogic MultiMaster ISA I/O Addresses.
583          */
584         if (BusLogic_ProbeOptions.LimitedProbeISA ? BusLogic_ProbeOptions.Probe330 : check_region(0x330, BusLogic_MultiMasterAddressCount) == 0)
585                 BusLogic_AppendProbeAddressISA(0x330);
586         if (BusLogic_ProbeOptions.LimitedProbeISA ? BusLogic_ProbeOptions.Probe334 : check_region(0x334, BusLogic_MultiMasterAddressCount) == 0)
587                 BusLogic_AppendProbeAddressISA(0x334);
588         if (BusLogic_ProbeOptions.LimitedProbeISA ? BusLogic_ProbeOptions.Probe230 : check_region(0x230, BusLogic_MultiMasterAddressCount) == 0)
589                 BusLogic_AppendProbeAddressISA(0x230);
590         if (BusLogic_ProbeOptions.LimitedProbeISA ? BusLogic_ProbeOptions.Probe234 : check_region(0x234, BusLogic_MultiMasterAddressCount) == 0)
591                 BusLogic_AppendProbeAddressISA(0x234);
592         if (BusLogic_ProbeOptions.LimitedProbeISA ? BusLogic_ProbeOptions.Probe130 : check_region(0x130, BusLogic_MultiMasterAddressCount) == 0)
593                 BusLogic_AppendProbeAddressISA(0x130);
594         if (BusLogic_ProbeOptions.LimitedProbeISA ? BusLogic_ProbeOptions.Probe134 : check_region(0x134, BusLogic_MultiMasterAddressCount) == 0)
595                 BusLogic_AppendProbeAddressISA(0x134);
596 }
597
598
599 #ifdef CONFIG_PCI
600
601
602 /*
603   BusLogic_SortProbeInfo sorts a section of BusLogic_ProbeInfoList in order
604   of increasing PCI Bus and Device Number.
605 */
606
607 static void __init BusLogic_SortProbeInfo(struct BusLogic_ProbeInfo *ProbeInfoList, int ProbeInfoCount)
608 {
609         int LastInterchange = ProbeInfoCount - 1, Bound, j;
610         while (LastInterchange > 0) {
611                 Bound = LastInterchange;
612                 LastInterchange = 0;
613                 for (j = 0; j < Bound; j++) {
614                         struct BusLogic_ProbeInfo *ProbeInfo1 = &ProbeInfoList[j];
615                         struct BusLogic_ProbeInfo *ProbeInfo2 = &ProbeInfoList[j + 1];
616                         if (ProbeInfo1->Bus > ProbeInfo2->Bus || (ProbeInfo1->Bus == ProbeInfo2->Bus && (ProbeInfo1->Device > ProbeInfo2->Device))) {
617                                 struct BusLogic_ProbeInfo TempProbeInfo;
618                                 memcpy(&TempProbeInfo, ProbeInfo1, sizeof(struct BusLogic_ProbeInfo));
619                                 memcpy(ProbeInfo1, ProbeInfo2, sizeof(struct BusLogic_ProbeInfo));
620                                 memcpy(ProbeInfo2, &TempProbeInfo, sizeof(struct BusLogic_ProbeInfo));
621                                 LastInterchange = j;
622                         }
623                 }
624         }
625 }
626
627
628 /*
629   BusLogic_InitializeMultiMasterProbeInfo initializes the list of I/O Address
630   and Bus Probe Information to be checked for potential BusLogic MultiMaster
631   SCSI Host Adapters by interrogating the PCI Configuration Space on PCI
632   machines as well as from the list of standard BusLogic MultiMaster ISA
633   I/O Addresses.  It returns the number of PCI MultiMaster Host Adapters found.
634 */
635
636 static int __init BusLogic_InitializeMultiMasterProbeInfo(struct BusLogic_HostAdapter
637                                                           *PrototypeHostAdapter)
638 {
639         struct BusLogic_ProbeInfo *PrimaryProbeInfo = &BusLogic_ProbeInfoList[BusLogic_ProbeInfoCount];
640         int NonPrimaryPCIMultiMasterIndex = BusLogic_ProbeInfoCount + 1;
641         int NonPrimaryPCIMultiMasterCount = 0, PCIMultiMasterCount = 0;
642         boolean ForceBusDeviceScanningOrder = false;
643         boolean ForceBusDeviceScanningOrderChecked = false;
644         boolean StandardAddressSeen[6];
645         struct pci_dev *PCI_Device = NULL;
646         int i;
647         if (BusLogic_ProbeInfoCount >= BusLogic_MaxHostAdapters)
648                 return 0;
649         BusLogic_ProbeInfoCount++;
650         for (i = 0; i < 6; i++)
651                 StandardAddressSeen[i] = false;
652         /*
653            Iterate over the MultiMaster PCI Host Adapters.  For each enumerated host
654            adapter, determine whether its ISA Compatible I/O Port is enabled and if
655            so, whether it is assigned the Primary I/O Address.  A host adapter that is
656            assigned the Primary I/O Address will always be the preferred boot device.
657            The MultiMaster BIOS will first recognize a host adapter at the Primary I/O
658            Address, then any other PCI host adapters, and finally any host adapters
659            located at the remaining standard ISA I/O Addresses.  When a PCI host
660            adapter is found with its ISA Compatible I/O Port enabled, a command is
661            issued to disable the ISA Compatible I/O Port, and it is noted that the
662            particular standard ISA I/O Address need not be probed.
663          */
664         PrimaryProbeInfo->IO_Address = 0;
665         while ((PCI_Device = pci_find_device(PCI_VENDOR_ID_BUSLOGIC, PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER, PCI_Device)) != NULL) {
666                 struct BusLogic_HostAdapter *HostAdapter = PrototypeHostAdapter;
667                 struct BusLogic_PCIHostAdapterInformation PCIHostAdapterInformation;
668                 enum BusLogic_ISACompatibleIOPort ModifyIOAddressRequest;
669                 unsigned char Bus;
670                 unsigned char Device;
671                 unsigned int IRQ_Channel;
672                 unsigned long BaseAddress0;
673                 unsigned long BaseAddress1;
674                 unsigned long IO_Address;
675                 unsigned long PCI_Address;
676
677                 if (pci_enable_device(PCI_Device))
678                         continue;
679
680                 if (pci_set_dma_mask(PCI_Device, DMA_32BIT_MASK ))
681                         continue;
682
683                 Bus = PCI_Device->bus->number;
684                 Device = PCI_Device->devfn >> 3;
685                 IRQ_Channel = PCI_Device->irq;
686                 IO_Address = BaseAddress0 = pci_resource_start(PCI_Device, 0);
687                 PCI_Address = BaseAddress1 = pci_resource_start(PCI_Device, 1);
688
689                 if (pci_resource_flags(PCI_Device, 0) & IORESOURCE_MEM) {
690                         BusLogic_Error("BusLogic: Base Address0 0x%X not I/O for " "MultiMaster Host Adapter\n", NULL, BaseAddress0);
691                         BusLogic_Error("at PCI Bus %d Device %d I/O Address 0x%X\n", NULL, Bus, Device, IO_Address);
692                         continue;
693                 }
694                 if (pci_resource_flags(PCI_Device, 1) & IORESOURCE_IO) {
695                         BusLogic_Error("BusLogic: Base Address1 0x%X not Memory for " "MultiMaster Host Adapter\n", NULL, BaseAddress1);
696                         BusLogic_Error("at PCI Bus %d Device %d PCI Address 0x%X\n", NULL, Bus, Device, PCI_Address);
697                         continue;
698                 }
699                 if (IRQ_Channel == 0) {
700                         BusLogic_Error("BusLogic: IRQ Channel %d invalid for " "MultiMaster Host Adapter\n", NULL, IRQ_Channel);
701                         BusLogic_Error("at PCI Bus %d Device %d I/O Address 0x%X\n", NULL, Bus, Device, IO_Address);
702                         continue;
703                 }
704                 if (BusLogic_GlobalOptions.TraceProbe) {
705                         BusLogic_Notice("BusLogic: PCI MultiMaster Host Adapter " "detected at\n", NULL);
706                         BusLogic_Notice("BusLogic: PCI Bus %d Device %d I/O Address " "0x%X PCI Address 0x%X\n", NULL, Bus, Device, IO_Address, PCI_Address);
707                 }
708                 /*
709                    Issue the Inquire PCI Host Adapter Information command to determine
710                    the ISA Compatible I/O Port.  If the ISA Compatible I/O Port is
711                    known and enabled, note that the particular Standard ISA I/O
712                    Address should not be probed.
713                  */
714                 HostAdapter->IO_Address = IO_Address;
715                 BusLogic_InterruptReset(HostAdapter);
716                 if (BusLogic_Command(HostAdapter, BusLogic_InquirePCIHostAdapterInformation, NULL, 0, &PCIHostAdapterInformation, sizeof(PCIHostAdapterInformation))
717                     == sizeof(PCIHostAdapterInformation)) {
718                         if (PCIHostAdapterInformation.ISACompatibleIOPort < 6)
719                                 StandardAddressSeen[PCIHostAdapterInformation.ISACompatibleIOPort] = true;
720                 } else
721                         PCIHostAdapterInformation.ISACompatibleIOPort = BusLogic_IO_Disable;
722                 /*
723                  * Issue the Modify I/O Address command to disable the ISA Compatible
724                  * I/O Port.  On PCI Host Adapters, the Modify I/O Address command
725                  * allows modification of the ISA compatible I/O Address that the Host
726                  * Adapter responds to; it does not affect the PCI compliant I/O Address
727                  * assigned at system initialization.
728                  */
729                 ModifyIOAddressRequest = BusLogic_IO_Disable;
730                 BusLogic_Command(HostAdapter, BusLogic_ModifyIOAddress, &ModifyIOAddressRequest, sizeof(ModifyIOAddressRequest), NULL, 0);
731                 /*
732                    For the first MultiMaster Host Adapter enumerated, issue the Fetch
733                    Host Adapter Local RAM command to read byte 45 of the AutoSCSI area,
734                    for the setting of the "Use Bus And Device # For PCI Scanning Seq."
735                    option.  Issue the Inquire Board ID command since this option is
736                    only valid for the BT-948/958/958D.
737                  */
738                 if (!ForceBusDeviceScanningOrderChecked) {
739                         struct BusLogic_FetchHostAdapterLocalRAMRequest FetchHostAdapterLocalRAMRequest;
740                         struct BusLogic_AutoSCSIByte45 AutoSCSIByte45;
741                         struct BusLogic_BoardID BoardID;
742                         FetchHostAdapterLocalRAMRequest.ByteOffset = BusLogic_AutoSCSI_BaseOffset + 45;
743                         FetchHostAdapterLocalRAMRequest.ByteCount = sizeof(AutoSCSIByte45);
744                         BusLogic_Command(HostAdapter, BusLogic_FetchHostAdapterLocalRAM, &FetchHostAdapterLocalRAMRequest, sizeof(FetchHostAdapterLocalRAMRequest), &AutoSCSIByte45, sizeof(AutoSCSIByte45));
745                         BusLogic_Command(HostAdapter, BusLogic_InquireBoardID, NULL, 0, &BoardID, sizeof(BoardID));
746                         if (BoardID.FirmwareVersion1stDigit == '5')
747                                 ForceBusDeviceScanningOrder = AutoSCSIByte45.ForceBusDeviceScanningOrder;
748                         ForceBusDeviceScanningOrderChecked = true;
749                 }
750                 /*
751                    Determine whether this MultiMaster Host Adapter has its ISA
752                    Compatible I/O Port enabled and is assigned the Primary I/O Address.
753                    If it does, then it is the Primary MultiMaster Host Adapter and must
754                    be recognized first.  If it does not, then it is added to the list
755                    for probing after any Primary MultiMaster Host Adapter is probed.
756                  */
757                 if (PCIHostAdapterInformation.ISACompatibleIOPort == BusLogic_IO_330) {
758                         PrimaryProbeInfo->HostAdapterType = BusLogic_MultiMaster;
759                         PrimaryProbeInfo->HostAdapterBusType = BusLogic_PCI_Bus;
760                         PrimaryProbeInfo->IO_Address = IO_Address;
761                         PrimaryProbeInfo->PCI_Address = PCI_Address;
762                         PrimaryProbeInfo->Bus = Bus;
763                         PrimaryProbeInfo->Device = Device;
764                         PrimaryProbeInfo->IRQ_Channel = IRQ_Channel;
765                         PrimaryProbeInfo->PCI_Device = PCI_Device;
766                         PCIMultiMasterCount++;
767                 } else if (BusLogic_ProbeInfoCount < BusLogic_MaxHostAdapters) {
768                         struct BusLogic_ProbeInfo *ProbeInfo = &BusLogic_ProbeInfoList[BusLogic_ProbeInfoCount++];
769                         ProbeInfo->HostAdapterType = BusLogic_MultiMaster;
770                         ProbeInfo->HostAdapterBusType = BusLogic_PCI_Bus;
771                         ProbeInfo->IO_Address = IO_Address;
772                         ProbeInfo->PCI_Address = PCI_Address;
773                         ProbeInfo->Bus = Bus;
774                         ProbeInfo->Device = Device;
775                         ProbeInfo->IRQ_Channel = IRQ_Channel;
776                         ProbeInfo->PCI_Device = PCI_Device;
777                         NonPrimaryPCIMultiMasterCount++;
778                         PCIMultiMasterCount++;
779                 } else
780                         BusLogic_Warning("BusLogic: Too many Host Adapters " "detected\n", NULL);
781         }
782         /*
783            If the AutoSCSI "Use Bus And Device # For PCI Scanning Seq." option is ON
784            for the first enumerated MultiMaster Host Adapter, and if that host adapter
785            is a BT-948/958/958D, then the MultiMaster BIOS will recognize MultiMaster
786            Host Adapters in the order of increasing PCI Bus and Device Number.  In
787            that case, sort the probe information into the same order the BIOS uses.
788            If this option is OFF, then the MultiMaster BIOS will recognize MultiMaster
789            Host Adapters in the order they are enumerated by the PCI BIOS, and hence
790            no sorting is necessary.
791          */
792         if (ForceBusDeviceScanningOrder)
793                 BusLogic_SortProbeInfo(&BusLogic_ProbeInfoList[NonPrimaryPCIMultiMasterIndex], NonPrimaryPCIMultiMasterCount);
794         /*
795            If no PCI MultiMaster Host Adapter is assigned the Primary I/O Address,
796            then the Primary I/O Address must be probed explicitly before any PCI
797            host adapters are probed.
798          */
799         if (!BusLogic_ProbeOptions.NoProbeISA)
800                 if (PrimaryProbeInfo->IO_Address == 0 && (BusLogic_ProbeOptions.LimitedProbeISA ? BusLogic_ProbeOptions.Probe330 : check_region(0x330, BusLogic_MultiMasterAddressCount) == 0)) {
801                         PrimaryProbeInfo->HostAdapterType = BusLogic_MultiMaster;
802                         PrimaryProbeInfo->HostAdapterBusType = BusLogic_ISA_Bus;
803                         PrimaryProbeInfo->IO_Address = 0x330;
804                 }
805         /*
806            Append the list of standard BusLogic MultiMaster ISA I/O Addresses,
807            omitting the Primary I/O Address which has already been handled.
808          */
809         if (!BusLogic_ProbeOptions.NoProbeISA) {
810                 if (!StandardAddressSeen[1] && (BusLogic_ProbeOptions.LimitedProbeISA ? BusLogic_ProbeOptions.Probe334 : check_region(0x334, BusLogic_MultiMasterAddressCount) == 0))
811                         BusLogic_AppendProbeAddressISA(0x334);
812                 if (!StandardAddressSeen[2] && (BusLogic_ProbeOptions.LimitedProbeISA ? BusLogic_ProbeOptions.Probe230 : check_region(0x230, BusLogic_MultiMasterAddressCount) == 0))
813                         BusLogic_AppendProbeAddressISA(0x230);
814                 if (!StandardAddressSeen[3] && (BusLogic_ProbeOptions.LimitedProbeISA ? BusLogic_ProbeOptions.Probe234 : check_region(0x234, BusLogic_MultiMasterAddressCount) == 0))
815                         BusLogic_AppendProbeAddressISA(0x234);
816                 if (!StandardAddressSeen[4] && (BusLogic_ProbeOptions.LimitedProbeISA ? BusLogic_ProbeOptions.Probe130 : check_region(0x130, BusLogic_MultiMasterAddressCount) == 0))
817                         BusLogic_AppendProbeAddressISA(0x130);
818                 if (!StandardAddressSeen[5] && (BusLogic_ProbeOptions.LimitedProbeISA ? BusLogic_ProbeOptions.Probe134 : check_region(0x134, BusLogic_MultiMasterAddressCount) == 0))
819                         BusLogic_AppendProbeAddressISA(0x134);
820         }
821         /*
822            Iterate over the older non-compliant MultiMaster PCI Host Adapters,
823            noting the PCI bus location and assigned IRQ Channel.
824          */
825         PCI_Device = NULL;
826         while ((PCI_Device = pci_find_device(PCI_VENDOR_ID_BUSLOGIC, PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER_NC, PCI_Device)) != NULL) {
827                 unsigned char Bus;
828                 unsigned char Device;
829                 unsigned int IRQ_Channel;
830                 unsigned long IO_Address;
831
832                 if (pci_enable_device(PCI_Device))
833                         continue;
834
835                 if (pci_set_dma_mask(PCI_Device, DMA_32BIT_MASK))
836                         continue;
837
838                 Bus = PCI_Device->bus->number;
839                 Device = PCI_Device->devfn >> 3;
840                 IRQ_Channel = PCI_Device->irq;
841                 IO_Address = pci_resource_start(PCI_Device, 0);
842
843                 if (IO_Address == 0 || IRQ_Channel == 0)
844                         continue;
845                 for (i = 0; i < BusLogic_ProbeInfoCount; i++) {
846                         struct BusLogic_ProbeInfo *ProbeInfo = &BusLogic_ProbeInfoList[i];
847                         if (ProbeInfo->IO_Address == IO_Address && ProbeInfo->HostAdapterType == BusLogic_MultiMaster) {
848                                 ProbeInfo->HostAdapterBusType = BusLogic_PCI_Bus;
849                                 ProbeInfo->PCI_Address = 0;
850                                 ProbeInfo->Bus = Bus;
851                                 ProbeInfo->Device = Device;
852                                 ProbeInfo->IRQ_Channel = IRQ_Channel;
853                                 ProbeInfo->PCI_Device = PCI_Device;
854                                 break;
855                         }
856                 }
857         }
858         return PCIMultiMasterCount;
859 }
860
861
862 /*
863   BusLogic_InitializeFlashPointProbeInfo initializes the list of I/O Address
864   and Bus Probe Information to be checked for potential BusLogic FlashPoint
865   Host Adapters by interrogating the PCI Configuration Space.  It returns the
866   number of FlashPoint Host Adapters found.
867 */
868
869 static int __init BusLogic_InitializeFlashPointProbeInfo(struct BusLogic_HostAdapter
870                                                          *PrototypeHostAdapter)
871 {
872         int FlashPointIndex = BusLogic_ProbeInfoCount, FlashPointCount = 0;
873         struct pci_dev *PCI_Device = NULL;
874         /*
875            Interrogate PCI Configuration Space for any FlashPoint Host Adapters.
876          */
877         while ((PCI_Device = pci_find_device(PCI_VENDOR_ID_BUSLOGIC, PCI_DEVICE_ID_BUSLOGIC_FLASHPOINT, PCI_Device)) != NULL) {
878                 unsigned char Bus;
879                 unsigned char Device;
880                 unsigned int IRQ_Channel;
881                 unsigned long BaseAddress0;
882                 unsigned long BaseAddress1;
883                 unsigned long IO_Address;
884                 unsigned long PCI_Address;
885
886                 if (pci_enable_device(PCI_Device))
887                         continue;
888
889                 if (pci_set_dma_mask(PCI_Device, DMA_32BIT_MASK))
890                         continue;
891
892                 Bus = PCI_Device->bus->number;
893                 Device = PCI_Device->devfn >> 3;
894                 IRQ_Channel = PCI_Device->irq;
895                 IO_Address = BaseAddress0 = pci_resource_start(PCI_Device, 0);
896                 PCI_Address = BaseAddress1 = pci_resource_start(PCI_Device, 1);
897 #ifndef CONFIG_SCSI_OMIT_FLASHPOINT
898                 if (pci_resource_flags(PCI_Device, 0) & IORESOURCE_MEM) {
899                         BusLogic_Error("BusLogic: Base Address0 0x%X not I/O for " "FlashPoint Host Adapter\n", NULL, BaseAddress0);
900                         BusLogic_Error("at PCI Bus %d Device %d I/O Address 0x%X\n", NULL, Bus, Device, IO_Address);
901                         continue;
902                 }
903                 if (pci_resource_flags(PCI_Device, 1) & IORESOURCE_IO) {
904                         BusLogic_Error("BusLogic: Base Address1 0x%X not Memory for " "FlashPoint Host Adapter\n", NULL, BaseAddress1);
905                         BusLogic_Error("at PCI Bus %d Device %d PCI Address 0x%X\n", NULL, Bus, Device, PCI_Address);
906                         continue;
907                 }
908                 if (IRQ_Channel == 0) {
909                         BusLogic_Error("BusLogic: IRQ Channel %d invalid for " "FlashPoint Host Adapter\n", NULL, IRQ_Channel);
910                         BusLogic_Error("at PCI Bus %d Device %d I/O Address 0x%X\n", NULL, Bus, Device, IO_Address);
911                         continue;
912                 }
913                 if (BusLogic_GlobalOptions.TraceProbe) {
914                         BusLogic_Notice("BusLogic: FlashPoint Host Adapter " "detected at\n", NULL);
915                         BusLogic_Notice("BusLogic: PCI Bus %d Device %d I/O Address " "0x%X PCI Address 0x%X\n", NULL, Bus, Device, IO_Address, PCI_Address);
916                 }
917                 if (BusLogic_ProbeInfoCount < BusLogic_MaxHostAdapters) {
918                         struct BusLogic_ProbeInfo *ProbeInfo = &BusLogic_ProbeInfoList[BusLogic_ProbeInfoCount++];
919                         ProbeInfo->HostAdapterType = BusLogic_FlashPoint;
920                         ProbeInfo->HostAdapterBusType = BusLogic_PCI_Bus;
921                         ProbeInfo->IO_Address = IO_Address;
922                         ProbeInfo->PCI_Address = PCI_Address;
923                         ProbeInfo->Bus = Bus;
924                         ProbeInfo->Device = Device;
925                         ProbeInfo->IRQ_Channel = IRQ_Channel;
926                         ProbeInfo->PCI_Device = PCI_Device;
927                         FlashPointCount++;
928                 } else
929                         BusLogic_Warning("BusLogic: Too many Host Adapters " "detected\n", NULL);
930 #else
931                 BusLogic_Error("BusLogic: FlashPoint Host Adapter detected at " "PCI Bus %d Device %d\n", NULL, Bus, Device);
932                 BusLogic_Error("BusLogic: I/O Address 0x%X PCI Address 0x%X, irq %d, " "but FlashPoint\n", NULL, IO_Address, PCI_Address, IRQ_Channel);
933                 BusLogic_Error("BusLogic: support was omitted in this kernel " "configuration.\n", NULL);
934 #endif
935         }
936         /*
937            The FlashPoint BIOS will scan for FlashPoint Host Adapters in the order of
938            increasing PCI Bus and Device Number, so sort the probe information into
939            the same order the BIOS uses.
940          */
941         BusLogic_SortProbeInfo(&BusLogic_ProbeInfoList[FlashPointIndex], FlashPointCount);
942         return FlashPointCount;
943 }
944
945
946 /*
947   BusLogic_InitializeProbeInfoList initializes the list of I/O Address and Bus
948   Probe Information to be checked for potential BusLogic SCSI Host Adapters by
949   interrogating the PCI Configuration Space on PCI machines as well as from the
950   list of standard BusLogic MultiMaster ISA I/O Addresses.  By default, if both
951   FlashPoint and PCI MultiMaster Host Adapters are present, this driver will
952   probe for FlashPoint Host Adapters first unless the BIOS primary disk is
953   controlled by the first PCI MultiMaster Host Adapter, in which case
954   MultiMaster Host Adapters will be probed first.  The BusLogic Driver Options
955   specifications "MultiMasterFirst" and "FlashPointFirst" can be used to force
956   a particular probe order.
957 */
958
959 static void __init BusLogic_InitializeProbeInfoList(struct BusLogic_HostAdapter
960                                                     *PrototypeHostAdapter)
961 {
962         /*
963            If a PCI BIOS is present, interrogate it for MultiMaster and FlashPoint
964            Host Adapters; otherwise, default to the standard ISA MultiMaster probe.
965          */
966         if (!BusLogic_ProbeOptions.NoProbePCI) {
967                 if (BusLogic_ProbeOptions.MultiMasterFirst) {
968                         BusLogic_InitializeMultiMasterProbeInfo(PrototypeHostAdapter);
969                         BusLogic_InitializeFlashPointProbeInfo(PrototypeHostAdapter);
970                 } else if (BusLogic_ProbeOptions.FlashPointFirst) {
971                         BusLogic_InitializeFlashPointProbeInfo(PrototypeHostAdapter);
972                         BusLogic_InitializeMultiMasterProbeInfo(PrototypeHostAdapter);
973                 } else {
974                         int FlashPointCount = BusLogic_InitializeFlashPointProbeInfo(PrototypeHostAdapter);
975                         int PCIMultiMasterCount = BusLogic_InitializeMultiMasterProbeInfo(PrototypeHostAdapter);
976                         if (FlashPointCount > 0 && PCIMultiMasterCount > 0) {
977                                 struct BusLogic_ProbeInfo *ProbeInfo = &BusLogic_ProbeInfoList[FlashPointCount];
978                                 struct BusLogic_HostAdapter *HostAdapter = PrototypeHostAdapter;
979                                 struct BusLogic_FetchHostAdapterLocalRAMRequest FetchHostAdapterLocalRAMRequest;
980                                 struct BusLogic_BIOSDriveMapByte Drive0MapByte;
981                                 while (ProbeInfo->HostAdapterBusType != BusLogic_PCI_Bus)
982                                         ProbeInfo++;
983                                 HostAdapter->IO_Address = ProbeInfo->IO_Address;
984                                 FetchHostAdapterLocalRAMRequest.ByteOffset = BusLogic_BIOS_BaseOffset + BusLogic_BIOS_DriveMapOffset + 0;
985                                 FetchHostAdapterLocalRAMRequest.ByteCount = sizeof(Drive0MapByte);
986                                 BusLogic_Command(HostAdapter, BusLogic_FetchHostAdapterLocalRAM, &FetchHostAdapterLocalRAMRequest, sizeof(FetchHostAdapterLocalRAMRequest), &Drive0MapByte, sizeof(Drive0MapByte));
987                                 /*
988                                    If the Map Byte for BIOS Drive 0 indicates that BIOS Drive 0
989                                    is controlled by this PCI MultiMaster Host Adapter, then
990                                    reverse the probe order so that MultiMaster Host Adapters are
991                                    probed before FlashPoint Host Adapters.
992                                  */
993                                 if (Drive0MapByte.DiskGeometry != BusLogic_BIOS_Disk_Not_Installed) {
994                                         struct BusLogic_ProbeInfo SavedProbeInfo[BusLogic_MaxHostAdapters];
995                                         int MultiMasterCount = BusLogic_ProbeInfoCount - FlashPointCount;
996                                         memcpy(SavedProbeInfo, BusLogic_ProbeInfoList, BusLogic_ProbeInfoCount * sizeof(struct BusLogic_ProbeInfo));
997                                         memcpy(&BusLogic_ProbeInfoList[0], &SavedProbeInfo[FlashPointCount], MultiMasterCount * sizeof(struct BusLogic_ProbeInfo));
998                                         memcpy(&BusLogic_ProbeInfoList[MultiMasterCount], &SavedProbeInfo[0], FlashPointCount * sizeof(struct BusLogic_ProbeInfo));
999                                 }
1000                         }
1001                 }
1002         } else
1003                 BusLogic_InitializeProbeInfoListISA(PrototypeHostAdapter);
1004 }
1005
1006
1007 #endif                          /* CONFIG_PCI */
1008
1009
1010 /*
1011   BusLogic_Failure prints a standardized error message, and then returns false.
1012 */
1013
1014 static boolean BusLogic_Failure(struct BusLogic_HostAdapter *HostAdapter, char *ErrorMessage)
1015 {
1016         BusLogic_AnnounceDriver(HostAdapter);
1017         if (HostAdapter->HostAdapterBusType == BusLogic_PCI_Bus) {
1018                 BusLogic_Error("While configuring BusLogic PCI Host Adapter at\n", HostAdapter);
1019                 BusLogic_Error("Bus %d Device %d I/O Address 0x%X PCI Address 0x%X:\n", HostAdapter, HostAdapter->Bus, HostAdapter->Device, HostAdapter->IO_Address, HostAdapter->PCI_Address);
1020         } else
1021                 BusLogic_Error("While configuring BusLogic Host Adapter at " "I/O Address 0x%X:\n", HostAdapter, HostAdapter->IO_Address);
1022         BusLogic_Error("%s FAILED - DETACHING\n", HostAdapter, ErrorMessage);
1023         if (BusLogic_CommandFailureReason != NULL)
1024                 BusLogic_Error("ADDITIONAL FAILURE INFO - %s\n", HostAdapter, BusLogic_CommandFailureReason);
1025         return false;
1026 }
1027
1028
1029 /*
1030   BusLogic_ProbeHostAdapter probes for a BusLogic Host Adapter.
1031 */
1032
1033 static boolean __init BusLogic_ProbeHostAdapter(struct BusLogic_HostAdapter *HostAdapter)
1034 {
1035         union BusLogic_StatusRegister StatusRegister;
1036         union BusLogic_InterruptRegister InterruptRegister;
1037         union BusLogic_GeometryRegister GeometryRegister;
1038         /*
1039            FlashPoint Host Adapters are Probed by the FlashPoint SCCB Manager.
1040          */
1041         if (BusLogic_FlashPointHostAdapterP(HostAdapter)) {
1042                 struct FlashPoint_Info *FlashPointInfo = &HostAdapter->FlashPointInfo;
1043                 FlashPointInfo->BaseAddress = (u32) HostAdapter->IO_Address;
1044                 FlashPointInfo->IRQ_Channel = HostAdapter->IRQ_Channel;
1045                 FlashPointInfo->Present = false;
1046                 if (!(FlashPoint_ProbeHostAdapter(FlashPointInfo) == 0 && FlashPointInfo->Present)) {
1047                         BusLogic_Error("BusLogic: FlashPoint Host Adapter detected at " "PCI Bus %d Device %d\n", HostAdapter, HostAdapter->Bus, HostAdapter->Device);
1048                         BusLogic_Error("BusLogic: I/O Address 0x%X PCI Address 0x%X, " "but FlashPoint\n", HostAdapter, HostAdapter->IO_Address, HostAdapter->PCI_Address);
1049                         BusLogic_Error("BusLogic: Probe Function failed to validate it.\n", HostAdapter);
1050                         return false;
1051                 }
1052                 if (BusLogic_GlobalOptions.TraceProbe)
1053                         BusLogic_Notice("BusLogic_Probe(0x%X): FlashPoint Found\n", HostAdapter, HostAdapter->IO_Address);
1054                 /*
1055                    Indicate the Host Adapter Probe completed successfully.
1056                  */
1057                 return true;
1058         }
1059         /*
1060            Read the Status, Interrupt, and Geometry Registers to test if there are I/O
1061            ports that respond, and to check the values to determine if they are from a
1062            BusLogic Host Adapter.  A nonexistent I/O port will return 0xFF, in which
1063            case there is definitely no BusLogic Host Adapter at this base I/O Address.
1064            The test here is a subset of that used by the BusLogic Host Adapter BIOS.
1065          */
1066         StatusRegister.All = BusLogic_ReadStatusRegister(HostAdapter);
1067         InterruptRegister.All = BusLogic_ReadInterruptRegister(HostAdapter);
1068         GeometryRegister.All = BusLogic_ReadGeometryRegister(HostAdapter);
1069         if (BusLogic_GlobalOptions.TraceProbe)
1070                 BusLogic_Notice("BusLogic_Probe(0x%X): Status 0x%02X, Interrupt 0x%02X, " "Geometry 0x%02X\n", HostAdapter, HostAdapter->IO_Address, StatusRegister.All, InterruptRegister.All, GeometryRegister.All);
1071         if (StatusRegister.All == 0 || StatusRegister.sr.DiagnosticActive || StatusRegister.sr.CommandParameterRegisterBusy || StatusRegister.sr.Reserved || StatusRegister.sr.CommandInvalid || InterruptRegister.ir.Reserved != 0)
1072                 return false;
1073         /*
1074            Check the undocumented Geometry Register to test if there is an I/O port
1075            that responded.  Adaptec Host Adapters do not implement the Geometry
1076            Register, so this test helps serve to avoid incorrectly recognizing an
1077            Adaptec 1542A or 1542B as a BusLogic.  Unfortunately, the Adaptec 1542C
1078            series does respond to the Geometry Register I/O port, but it will be
1079            rejected later when the Inquire Extended Setup Information command is
1080            issued in BusLogic_CheckHostAdapter.  The AMI FastDisk Host Adapter is a
1081            BusLogic clone that implements the same interface as earlier BusLogic
1082            Host Adapters, including the undocumented commands, and is therefore
1083            supported by this driver.  However, the AMI FastDisk always returns 0x00
1084            upon reading the Geometry Register, so the extended translation option
1085            should always be left disabled on the AMI FastDisk.
1086          */
1087         if (GeometryRegister.All == 0xFF)
1088                 return false;
1089         /*
1090            Indicate the Host Adapter Probe completed successfully.
1091          */
1092         return true;
1093 }
1094
1095
1096 /*
1097   BusLogic_HardwareResetHostAdapter issues a Hardware Reset to the Host Adapter
1098   and waits for Host Adapter Diagnostics to complete.  If HardReset is true, a
1099   Hard Reset is performed which also initiates a SCSI Bus Reset.  Otherwise, a
1100   Soft Reset is performed which only resets the Host Adapter without forcing a
1101   SCSI Bus Reset.
1102 */
1103
1104 static boolean BusLogic_HardwareResetHostAdapter(struct BusLogic_HostAdapter
1105                                                  *HostAdapter, boolean HardReset)
1106 {
1107         union BusLogic_StatusRegister StatusRegister;
1108         int TimeoutCounter;
1109         /*
1110            FlashPoint Host Adapters are Hard Reset by the FlashPoint SCCB Manager.
1111          */
1112         if (BusLogic_FlashPointHostAdapterP(HostAdapter)) {
1113                 struct FlashPoint_Info *FlashPointInfo = &HostAdapter->FlashPointInfo;
1114                 FlashPointInfo->HostSoftReset = !HardReset;
1115                 FlashPointInfo->ReportDataUnderrun = true;
1116                 HostAdapter->CardHandle = FlashPoint_HardwareResetHostAdapter(FlashPointInfo);
1117                 if (HostAdapter->CardHandle == FlashPoint_BadCardHandle)
1118                         return false;
1119                 /*
1120                    Indicate the Host Adapter Hard Reset completed successfully.
1121                  */
1122                 return true;
1123         }
1124         /*
1125            Issue a Hard Reset or Soft Reset Command to the Host Adapter.  The Host
1126            Adapter should respond by setting Diagnostic Active in the Status Register.
1127          */
1128         if (HardReset)
1129                 BusLogic_HardReset(HostAdapter);
1130         else
1131                 BusLogic_SoftReset(HostAdapter);
1132         /*
1133            Wait until Diagnostic Active is set in the Status Register.
1134          */
1135         TimeoutCounter = 5 * 10000;
1136         while (--TimeoutCounter >= 0) {
1137                 StatusRegister.All = BusLogic_ReadStatusRegister(HostAdapter);
1138                 if (StatusRegister.sr.DiagnosticActive)
1139                         break;
1140                 udelay(100);
1141         }
1142         if (BusLogic_GlobalOptions.TraceHardwareReset)
1143                 BusLogic_Notice("BusLogic_HardwareReset(0x%X): Diagnostic Active, " "Status 0x%02X\n", HostAdapter, HostAdapter->IO_Address, StatusRegister.All);
1144         if (TimeoutCounter < 0)
1145                 return false;
1146         /*
1147            Wait 100 microseconds to allow completion of any initial diagnostic
1148            activity which might leave the contents of the Status Register
1149            unpredictable.
1150          */
1151         udelay(100);
1152         /*
1153            Wait until Diagnostic Active is reset in the Status Register.
1154          */
1155         TimeoutCounter = 10 * 10000;
1156         while (--TimeoutCounter >= 0) {
1157                 StatusRegister.All = BusLogic_ReadStatusRegister(HostAdapter);
1158                 if (!StatusRegister.sr.DiagnosticActive)
1159                         break;
1160                 udelay(100);
1161         }
1162         if (BusLogic_GlobalOptions.TraceHardwareReset)
1163                 BusLogic_Notice("BusLogic_HardwareReset(0x%X): Diagnostic Completed, " "Status 0x%02X\n", HostAdapter, HostAdapter->IO_Address, StatusRegister.All);
1164         if (TimeoutCounter < 0)
1165                 return false;
1166         /*
1167            Wait until at least one of the Diagnostic Failure, Host Adapter Ready,
1168            or Data In Register Ready bits is set in the Status Register.
1169          */
1170         TimeoutCounter = 10000;
1171         while (--TimeoutCounter >= 0) {
1172                 StatusRegister.All = BusLogic_ReadStatusRegister(HostAdapter);
1173                 if (StatusRegister.sr.DiagnosticFailure || StatusRegister.sr.HostAdapterReady || StatusRegister.sr.DataInRegisterReady)
1174                         break;
1175                 udelay(100);
1176         }
1177         if (BusLogic_GlobalOptions.TraceHardwareReset)
1178                 BusLogic_Notice("BusLogic_HardwareReset(0x%X): Host Adapter Ready, " "Status 0x%02X\n", HostAdapter, HostAdapter->IO_Address, StatusRegister.All);
1179         if (TimeoutCounter < 0)
1180                 return false;
1181         /*
1182            If Diagnostic Failure is set or Host Adapter Ready is reset, then an
1183            error occurred during the Host Adapter diagnostics.  If Data In Register
1184            Ready is set, then there is an Error Code available.
1185          */
1186         if (StatusRegister.sr.DiagnosticFailure || !StatusRegister.sr.HostAdapterReady) {
1187                 BusLogic_CommandFailureReason = NULL;
1188                 BusLogic_Failure(HostAdapter, "HARD RESET DIAGNOSTICS");
1189                 BusLogic_Error("HOST ADAPTER STATUS REGISTER = %02X\n", HostAdapter, StatusRegister.All);
1190                 if (StatusRegister.sr.DataInRegisterReady) {
1191                         unsigned char ErrorCode = BusLogic_ReadDataInRegister(HostAdapter);
1192                         BusLogic_Error("HOST ADAPTER ERROR CODE = %d\n", HostAdapter, ErrorCode);
1193                 }
1194                 return false;
1195         }
1196         /*
1197            Indicate the Host Adapter Hard Reset completed successfully.
1198          */
1199         return true;
1200 }
1201
1202
1203 /*
1204   BusLogic_CheckHostAdapter checks to be sure this really is a BusLogic
1205   Host Adapter.
1206 */
1207
1208 static boolean __init BusLogic_CheckHostAdapter(struct BusLogic_HostAdapter *HostAdapter)
1209 {
1210         struct BusLogic_ExtendedSetupInformation ExtendedSetupInformation;
1211         unsigned char RequestedReplyLength;
1212         boolean Result = true;
1213         /*
1214            FlashPoint Host Adapters do not require this protection.
1215          */
1216         if (BusLogic_FlashPointHostAdapterP(HostAdapter))
1217                 return true;
1218         /*
1219            Issue the Inquire Extended Setup Information command.  Only genuine
1220            BusLogic Host Adapters and true clones support this command.  Adaptec 1542C
1221            series Host Adapters that respond to the Geometry Register I/O port will
1222            fail this command.
1223          */
1224         RequestedReplyLength = sizeof(ExtendedSetupInformation);
1225         if (BusLogic_Command(HostAdapter, BusLogic_InquireExtendedSetupInformation, &RequestedReplyLength, sizeof(RequestedReplyLength), &ExtendedSetupInformation, sizeof(ExtendedSetupInformation))
1226             != sizeof(ExtendedSetupInformation))
1227                 Result = false;
1228         /*
1229            Provide tracing information if requested and return.
1230          */
1231         if (BusLogic_GlobalOptions.TraceProbe)
1232                 BusLogic_Notice("BusLogic_Check(0x%X): MultiMaster %s\n", HostAdapter, HostAdapter->IO_Address, (Result ? "Found" : "Not Found"));
1233         return Result;
1234 }
1235
1236
1237 /*
1238   BusLogic_ReadHostAdapterConfiguration reads the Configuration Information
1239   from Host Adapter and initializes the Host Adapter structure.
1240 */
1241
1242 static boolean __init BusLogic_ReadHostAdapterConfiguration(struct BusLogic_HostAdapter
1243                                                             *HostAdapter)
1244 {
1245         struct BusLogic_BoardID BoardID;
1246         struct BusLogic_Configuration Configuration;
1247         struct BusLogic_SetupInformation SetupInformation;
1248         struct BusLogic_ExtendedSetupInformation ExtendedSetupInformation;
1249         unsigned char HostAdapterModelNumber[5];
1250         unsigned char FirmwareVersion3rdDigit;
1251         unsigned char FirmwareVersionLetter;
1252         struct BusLogic_PCIHostAdapterInformation PCIHostAdapterInformation;
1253         struct BusLogic_FetchHostAdapterLocalRAMRequest FetchHostAdapterLocalRAMRequest;
1254         struct BusLogic_AutoSCSIData AutoSCSIData;
1255         union BusLogic_GeometryRegister GeometryRegister;
1256         unsigned char RequestedReplyLength;
1257         unsigned char *TargetPointer, Character;
1258         int TargetID, i;
1259         /*
1260            Configuration Information for FlashPoint Host Adapters is provided in the
1261            FlashPoint_Info structure by the FlashPoint SCCB Manager's Probe Function.
1262            Initialize fields in the Host Adapter structure from the FlashPoint_Info
1263            structure.
1264          */
1265         if (BusLogic_FlashPointHostAdapterP(HostAdapter)) {
1266                 struct FlashPoint_Info *FlashPointInfo = &HostAdapter->FlashPointInfo;
1267                 TargetPointer = HostAdapter->ModelName;
1268                 *TargetPointer++ = 'B';
1269                 *TargetPointer++ = 'T';
1270                 *TargetPointer++ = '-';
1271                 for (i = 0; i < sizeof(FlashPointInfo->ModelNumber); i++)
1272                         *TargetPointer++ = FlashPointInfo->ModelNumber[i];
1273                 *TargetPointer++ = '\0';
1274                 strcpy(HostAdapter->FirmwareVersion, FlashPoint_FirmwareVersion);
1275                 HostAdapter->SCSI_ID = FlashPointInfo->SCSI_ID;
1276                 HostAdapter->ExtendedTranslationEnabled = FlashPointInfo->ExtendedTranslationEnabled;
1277                 HostAdapter->ParityCheckingEnabled = FlashPointInfo->ParityCheckingEnabled;
1278                 HostAdapter->BusResetEnabled = !FlashPointInfo->HostSoftReset;
1279                 HostAdapter->LevelSensitiveInterrupt = true;
1280                 HostAdapter->HostWideSCSI = FlashPointInfo->HostWideSCSI;
1281                 HostAdapter->HostDifferentialSCSI = false;
1282                 HostAdapter->HostSupportsSCAM = true;
1283                 HostAdapter->HostUltraSCSI = true;
1284                 HostAdapter->ExtendedLUNSupport = true;
1285                 HostAdapter->TerminationInfoValid = true;
1286                 HostAdapter->LowByteTerminated = FlashPointInfo->LowByteTerminated;
1287                 HostAdapter->HighByteTerminated = FlashPointInfo->HighByteTerminated;
1288                 HostAdapter->SCAM_Enabled = FlashPointInfo->SCAM_Enabled;
1289                 HostAdapter->SCAM_Level2 = FlashPointInfo->SCAM_Level2;
1290                 HostAdapter->DriverScatterGatherLimit = BusLogic_ScatterGatherLimit;
1291                 HostAdapter->MaxTargetDevices = (HostAdapter->HostWideSCSI ? 16 : 8);
1292                 HostAdapter->MaxLogicalUnits = 32;
1293                 HostAdapter->InitialCCBs = 4 * BusLogic_CCB_AllocationGroupSize;
1294                 HostAdapter->IncrementalCCBs = BusLogic_CCB_AllocationGroupSize;
1295                 HostAdapter->DriverQueueDepth = 255;
1296                 HostAdapter->HostAdapterQueueDepth = HostAdapter->DriverQueueDepth;
1297                 HostAdapter->SynchronousPermitted = FlashPointInfo->SynchronousPermitted;
1298                 HostAdapter->FastPermitted = FlashPointInfo->FastPermitted;
1299                 HostAdapter->UltraPermitted = FlashPointInfo->UltraPermitted;
1300                 HostAdapter->WidePermitted = FlashPointInfo->WidePermitted;
1301                 HostAdapter->DisconnectPermitted = FlashPointInfo->DisconnectPermitted;
1302                 HostAdapter->TaggedQueuingPermitted = 0xFFFF;
1303                 goto Common;
1304         }
1305         /*
1306            Issue the Inquire Board ID command.
1307          */
1308         if (BusLogic_Command(HostAdapter, BusLogic_InquireBoardID, NULL, 0, &BoardID, sizeof(BoardID)) != sizeof(BoardID))
1309                 return BusLogic_Failure(HostAdapter, "INQUIRE BOARD ID");
1310         /*
1311            Issue the Inquire Configuration command.
1312          */
1313         if (BusLogic_Command(HostAdapter, BusLogic_InquireConfiguration, NULL, 0, &Configuration, sizeof(Configuration))
1314             != sizeof(Configuration))
1315                 return BusLogic_Failure(HostAdapter, "INQUIRE CONFIGURATION");
1316         /*
1317            Issue the Inquire Setup Information command.
1318          */
1319         RequestedReplyLength = sizeof(SetupInformation);
1320         if (BusLogic_Command(HostAdapter, BusLogic_InquireSetupInformation, &RequestedReplyLength, sizeof(RequestedReplyLength), &SetupInformation, sizeof(SetupInformation))
1321             != sizeof(SetupInformation))
1322                 return BusLogic_Failure(HostAdapter, "INQUIRE SETUP INFORMATION");
1323         /*
1324            Issue the Inquire Extended Setup Information command.
1325          */
1326         RequestedReplyLength = sizeof(ExtendedSetupInformation);
1327         if (BusLogic_Command(HostAdapter, BusLogic_InquireExtendedSetupInformation, &RequestedReplyLength, sizeof(RequestedReplyLength), &ExtendedSetupInformation, sizeof(ExtendedSetupInformation))
1328             != sizeof(ExtendedSetupInformation))
1329                 return BusLogic_Failure(HostAdapter, "INQUIRE EXTENDED SETUP INFORMATION");
1330         /*
1331            Issue the Inquire Firmware Version 3rd Digit command.
1332          */
1333         FirmwareVersion3rdDigit = '\0';
1334         if (BoardID.FirmwareVersion1stDigit > '0')
1335                 if (BusLogic_Command(HostAdapter, BusLogic_InquireFirmwareVersion3rdDigit, NULL, 0, &FirmwareVersion3rdDigit, sizeof(FirmwareVersion3rdDigit))
1336                     != sizeof(FirmwareVersion3rdDigit))
1337                         return BusLogic_Failure(HostAdapter, "INQUIRE FIRMWARE 3RD DIGIT");
1338         /*
1339            Issue the Inquire Host Adapter Model Number command.
1340          */
1341         if (ExtendedSetupInformation.BusType == 'A' && BoardID.FirmwareVersion1stDigit == '2')
1342                 /* BusLogic BT-542B ISA 2.xx */
1343                 strcpy(HostAdapterModelNumber, "542B");
1344         else if (ExtendedSetupInformation.BusType == 'E' && BoardID.FirmwareVersion1stDigit == '2' && (BoardID.FirmwareVersion2ndDigit <= '1' || (BoardID.FirmwareVersion2ndDigit == '2' && FirmwareVersion3rdDigit == '0')))
1345                 /* BusLogic BT-742A EISA 2.1x or 2.20 */
1346                 strcpy(HostAdapterModelNumber, "742A");
1347         else if (ExtendedSetupInformation.BusType == 'E' && BoardID.FirmwareVersion1stDigit == '0')
1348                 /* AMI FastDisk EISA Series 441 0.x */
1349                 strcpy(HostAdapterModelNumber, "747A");
1350         else {
1351                 RequestedReplyLength = sizeof(HostAdapterModelNumber);
1352                 if (BusLogic_Command(HostAdapter, BusLogic_InquireHostAdapterModelNumber, &RequestedReplyLength, sizeof(RequestedReplyLength), &HostAdapterModelNumber, sizeof(HostAdapterModelNumber))
1353                     != sizeof(HostAdapterModelNumber))
1354                         return BusLogic_Failure(HostAdapter, "INQUIRE HOST ADAPTER MODEL NUMBER");
1355         }
1356         /*
1357            BusLogic MultiMaster Host Adapters can be identified by their model number
1358            and the major version number of their firmware as follows:
1359
1360            5.xx       BusLogic "W" Series Host Adapters:
1361            BT-948/958/958D
1362            4.xx       BusLogic "C" Series Host Adapters:
1363            BT-946C/956C/956CD/747C/757C/757CD/445C/545C/540CF
1364            3.xx       BusLogic "S" Series Host Adapters:
1365            BT-747S/747D/757S/757D/445S/545S/542D
1366            BT-542B/742A (revision H)
1367            2.xx       BusLogic "A" Series Host Adapters:
1368            BT-542B/742A (revision G and below)
1369            0.xx       AMI FastDisk VLB/EISA BusLogic Clone Host Adapter
1370          */
1371         /*
1372            Save the Model Name and Host Adapter Name in the Host Adapter structure.
1373          */
1374         TargetPointer = HostAdapter->ModelName;
1375         *TargetPointer++ = 'B';
1376         *TargetPointer++ = 'T';
1377         *TargetPointer++ = '-';
1378         for (i = 0; i < sizeof(HostAdapterModelNumber); i++) {
1379                 Character = HostAdapterModelNumber[i];
1380                 if (Character == ' ' || Character == '\0')
1381                         break;
1382                 *TargetPointer++ = Character;
1383         }
1384         *TargetPointer++ = '\0';
1385         /*
1386            Save the Firmware Version in the Host Adapter structure.
1387          */
1388         TargetPointer = HostAdapter->FirmwareVersion;
1389         *TargetPointer++ = BoardID.FirmwareVersion1stDigit;
1390         *TargetPointer++ = '.';
1391         *TargetPointer++ = BoardID.FirmwareVersion2ndDigit;
1392         if (FirmwareVersion3rdDigit != ' ' && FirmwareVersion3rdDigit != '\0')
1393                 *TargetPointer++ = FirmwareVersion3rdDigit;
1394         *TargetPointer = '\0';
1395         /*
1396            Issue the Inquire Firmware Version Letter command.
1397          */
1398         if (strcmp(HostAdapter->FirmwareVersion, "3.3") >= 0) {
1399                 if (BusLogic_Command(HostAdapter, BusLogic_InquireFirmwareVersionLetter, NULL, 0, &FirmwareVersionLetter, sizeof(FirmwareVersionLetter))
1400                     != sizeof(FirmwareVersionLetter))
1401                         return BusLogic_Failure(HostAdapter, "INQUIRE FIRMWARE VERSION LETTER");
1402                 if (FirmwareVersionLetter != ' ' && FirmwareVersionLetter != '\0')
1403                         *TargetPointer++ = FirmwareVersionLetter;
1404                 *TargetPointer = '\0';
1405         }
1406         /*
1407            Save the Host Adapter SCSI ID in the Host Adapter structure.
1408          */
1409         HostAdapter->SCSI_ID = Configuration.HostAdapterID;
1410         /*
1411            Determine the Bus Type and save it in the Host Adapter structure, determine
1412            and save the IRQ Channel if necessary, and determine and save the DMA
1413            Channel for ISA Host Adapters.
1414          */
1415         HostAdapter->HostAdapterBusType = BusLogic_HostAdapterBusTypes[HostAdapter->ModelName[3] - '4'];
1416         if (HostAdapter->IRQ_Channel == 0) {
1417                 if (Configuration.IRQ_Channel9)
1418                         HostAdapter->IRQ_Channel = 9;
1419                 else if (Configuration.IRQ_Channel10)
1420                         HostAdapter->IRQ_Channel = 10;
1421                 else if (Configuration.IRQ_Channel11)
1422                         HostAdapter->IRQ_Channel = 11;
1423                 else if (Configuration.IRQ_Channel12)
1424                         HostAdapter->IRQ_Channel = 12;
1425                 else if (Configuration.IRQ_Channel14)
1426                         HostAdapter->IRQ_Channel = 14;
1427                 else if (Configuration.IRQ_Channel15)
1428                         HostAdapter->IRQ_Channel = 15;
1429         }
1430         if (HostAdapter->HostAdapterBusType == BusLogic_ISA_Bus) {
1431                 if (Configuration.DMA_Channel5)
1432                         HostAdapter->DMA_Channel = 5;
1433                 else if (Configuration.DMA_Channel6)
1434                         HostAdapter->DMA_Channel = 6;
1435                 else if (Configuration.DMA_Channel7)
1436                         HostAdapter->DMA_Channel = 7;
1437         }
1438         /*
1439            Determine whether Extended Translation is enabled and save it in
1440            the Host Adapter structure.
1441          */
1442         GeometryRegister.All = BusLogic_ReadGeometryRegister(HostAdapter);
1443         HostAdapter->ExtendedTranslationEnabled = GeometryRegister.gr.ExtendedTranslationEnabled;
1444         /*
1445            Save the Scatter Gather Limits, Level Sensitive Interrupt flag, Wide
1446            SCSI flag, Differential SCSI flag, SCAM Supported flag, and
1447            Ultra SCSI flag in the Host Adapter structure.
1448          */
1449         HostAdapter->HostAdapterScatterGatherLimit = ExtendedSetupInformation.ScatterGatherLimit;
1450         HostAdapter->DriverScatterGatherLimit = HostAdapter->HostAdapterScatterGatherLimit;
1451         if (HostAdapter->HostAdapterScatterGatherLimit > BusLogic_ScatterGatherLimit)
1452                 HostAdapter->DriverScatterGatherLimit = BusLogic_ScatterGatherLimit;
1453         if (ExtendedSetupInformation.Misc.LevelSensitiveInterrupt)
1454                 HostAdapter->LevelSensitiveInterrupt = true;
1455         HostAdapter->HostWideSCSI = ExtendedSetupInformation.HostWideSCSI;
1456         HostAdapter->HostDifferentialSCSI = ExtendedSetupInformation.HostDifferentialSCSI;
1457         HostAdapter->HostSupportsSCAM = ExtendedSetupInformation.HostSupportsSCAM;
1458         HostAdapter->HostUltraSCSI = ExtendedSetupInformation.HostUltraSCSI;
1459         /*
1460            Determine whether Extended LUN Format CCBs are supported and save the
1461            information in the Host Adapter structure.
1462          */
1463         if (HostAdapter->FirmwareVersion[0] == '5' || (HostAdapter->FirmwareVersion[0] == '4' && HostAdapter->HostWideSCSI))
1464                 HostAdapter->ExtendedLUNSupport = true;
1465         /*
1466            Issue the Inquire PCI Host Adapter Information command to read the
1467            Termination Information from "W" series MultiMaster Host Adapters.
1468          */
1469         if (HostAdapter->FirmwareVersion[0] == '5') {
1470                 if (BusLogic_Command(HostAdapter, BusLogic_InquirePCIHostAdapterInformation, NULL, 0, &PCIHostAdapterInformation, sizeof(PCIHostAdapterInformation))
1471                     != sizeof(PCIHostAdapterInformation))
1472                         return BusLogic_Failure(HostAdapter, "INQUIRE PCI HOST ADAPTER INFORMATION");
1473                 /*
1474                    Save the Termination Information in the Host Adapter structure.
1475                  */
1476                 if (PCIHostAdapterInformation.GenericInfoValid) {
1477                         HostAdapter->TerminationInfoValid = true;
1478                         HostAdapter->LowByteTerminated = PCIHostAdapterInformation.LowByteTerminated;
1479                         HostAdapter->HighByteTerminated = PCIHostAdapterInformation.HighByteTerminated;
1480                 }
1481         }
1482         /*
1483            Issue the Fetch Host Adapter Local RAM command to read the AutoSCSI data
1484            from "W" and "C" series MultiMaster Host Adapters.
1485          */
1486         if (HostAdapter->FirmwareVersion[0] >= '4') {
1487                 FetchHostAdapterLocalRAMRequest.ByteOffset = BusLogic_AutoSCSI_BaseOffset;
1488                 FetchHostAdapterLocalRAMRequest.ByteCount = sizeof(AutoSCSIData);
1489                 if (BusLogic_Command(HostAdapter, BusLogic_FetchHostAdapterLocalRAM, &FetchHostAdapterLocalRAMRequest, sizeof(FetchHostAdapterLocalRAMRequest), &AutoSCSIData, sizeof(AutoSCSIData))
1490                     != sizeof(AutoSCSIData))
1491                         return BusLogic_Failure(HostAdapter, "FETCH HOST ADAPTER LOCAL RAM");
1492                 /*
1493                    Save the Parity Checking Enabled, Bus Reset Enabled, and Termination
1494                    Information in the Host Adapter structure.
1495                  */
1496                 HostAdapter->ParityCheckingEnabled = AutoSCSIData.ParityCheckingEnabled;
1497                 HostAdapter->BusResetEnabled = AutoSCSIData.BusResetEnabled;
1498                 if (HostAdapter->FirmwareVersion[0] == '4') {
1499                         HostAdapter->TerminationInfoValid = true;
1500                         HostAdapter->LowByteTerminated = AutoSCSIData.LowByteTerminated;
1501                         HostAdapter->HighByteTerminated = AutoSCSIData.HighByteTerminated;
1502                 }
1503                 /*
1504                    Save the Wide Permitted, Fast Permitted, Synchronous Permitted,
1505                    Disconnect Permitted, Ultra Permitted, and SCAM Information in the
1506                    Host Adapter structure.
1507                  */
1508                 HostAdapter->WidePermitted = AutoSCSIData.WidePermitted;
1509                 HostAdapter->FastPermitted = AutoSCSIData.FastPermitted;
1510                 HostAdapter->SynchronousPermitted = AutoSCSIData.SynchronousPermitted;
1511                 HostAdapter->DisconnectPermitted = AutoSCSIData.DisconnectPermitted;
1512                 if (HostAdapter->HostUltraSCSI)
1513                         HostAdapter->UltraPermitted = AutoSCSIData.UltraPermitted;
1514                 if (HostAdapter->HostSupportsSCAM) {
1515                         HostAdapter->SCAM_Enabled = AutoSCSIData.SCAM_Enabled;
1516                         HostAdapter->SCAM_Level2 = AutoSCSIData.SCAM_Level2;
1517                 }
1518         }
1519         /*
1520            Initialize fields in the Host Adapter structure for "S" and "A" series
1521            MultiMaster Host Adapters.
1522          */
1523         if (HostAdapter->FirmwareVersion[0] < '4') {
1524                 if (SetupInformation.SynchronousInitiationEnabled) {
1525                         HostAdapter->SynchronousPermitted = 0xFF;
1526                         if (HostAdapter->HostAdapterBusType == BusLogic_EISA_Bus) {
1527                                 if (ExtendedSetupInformation.Misc.FastOnEISA)
1528                                         HostAdapter->FastPermitted = 0xFF;
1529                                 if (strcmp(HostAdapter->ModelName, "BT-757") == 0)
1530                                         HostAdapter->WidePermitted = 0xFF;
1531                         }
1532                 }
1533                 HostAdapter->DisconnectPermitted = 0xFF;
1534                 HostAdapter->ParityCheckingEnabled = SetupInformation.ParityCheckingEnabled;
1535                 HostAdapter->BusResetEnabled = true;
1536         }
1537         /*
1538            Determine the maximum number of Target IDs and Logical Units supported by
1539            this driver for Wide and Narrow Host Adapters.
1540          */
1541         HostAdapter->MaxTargetDevices = (HostAdapter->HostWideSCSI ? 16 : 8);
1542         HostAdapter->MaxLogicalUnits = (HostAdapter->ExtendedLUNSupport ? 32 : 8);
1543         /*
1544            Select appropriate values for the Mailbox Count, Driver Queue Depth,
1545            Initial CCBs, and Incremental CCBs variables based on whether or not Strict
1546            Round Robin Mode is supported.  If Strict Round Robin Mode is supported,
1547            then there is no performance degradation in using the maximum possible
1548            number of Outgoing and Incoming Mailboxes and allowing the Tagged and
1549            Untagged Queue Depths to determine the actual utilization.  If Strict Round
1550            Robin Mode is not supported, then the Host Adapter must scan all the
1551            Outgoing Mailboxes whenever an Outgoing Mailbox entry is made, which can
1552            cause a substantial performance penalty.  The host adapters actually have
1553            room to store the following number of CCBs internally; that is, they can
1554            internally queue and manage this many active commands on the SCSI bus
1555            simultaneously.  Performance measurements demonstrate that the Driver Queue
1556            Depth should be set to the Mailbox Count, rather than the Host Adapter
1557            Queue Depth (internal CCB capacity), as it is more efficient to have the
1558            queued commands waiting in Outgoing Mailboxes if necessary than to block
1559            the process in the higher levels of the SCSI Subsystem.
1560
1561            192          BT-948/958/958D
1562            100          BT-946C/956C/956CD/747C/757C/757CD/445C
1563            50   BT-545C/540CF
1564            30   BT-747S/747D/757S/757D/445S/545S/542D/542B/742A
1565          */
1566         if (HostAdapter->FirmwareVersion[0] == '5')
1567                 HostAdapter->HostAdapterQueueDepth = 192;
1568         else if (HostAdapter->FirmwareVersion[0] == '4')
1569                 HostAdapter->HostAdapterQueueDepth = (HostAdapter->HostAdapterBusType != BusLogic_ISA_Bus ? 100 : 50);
1570         else
1571                 HostAdapter->HostAdapterQueueDepth = 30;
1572         if (strcmp(HostAdapter->FirmwareVersion, "3.31") >= 0) {
1573                 HostAdapter->StrictRoundRobinModeSupport = true;
1574                 HostAdapter->MailboxCount = BusLogic_MaxMailboxes;
1575         } else {
1576                 HostAdapter->StrictRoundRobinModeSupport = false;
1577                 HostAdapter->MailboxCount = 32;
1578         }
1579         HostAdapter->DriverQueueDepth = HostAdapter->MailboxCount;
1580         HostAdapter->InitialCCBs = 4 * BusLogic_CCB_AllocationGroupSize;
1581         HostAdapter->IncrementalCCBs = BusLogic_CCB_AllocationGroupSize;
1582         /*
1583            Tagged Queuing support is available and operates properly on all "W" series
1584            MultiMaster Host Adapters, on "C" series MultiMaster Host Adapters with
1585            firmware version 4.22 and above, and on "S" series MultiMaster Host
1586            Adapters with firmware version 3.35 and above.
1587          */
1588         HostAdapter->TaggedQueuingPermitted = 0;
1589         switch (HostAdapter->FirmwareVersion[0]) {
1590         case '5':
1591                 HostAdapter->TaggedQueuingPermitted = 0xFFFF;
1592                 break;
1593         case '4':
1594                 if (strcmp(HostAdapter->FirmwareVersion, "4.22") >= 0)
1595                         HostAdapter->TaggedQueuingPermitted = 0xFFFF;
1596                 break;
1597         case '3':
1598                 if (strcmp(HostAdapter->FirmwareVersion, "3.35") >= 0)
1599                         HostAdapter->TaggedQueuingPermitted = 0xFFFF;
1600                 break;
1601         }
1602         /*
1603            Determine the Host Adapter BIOS Address if the BIOS is enabled and
1604            save it in the Host Adapter structure.  The BIOS is disabled if the
1605            BIOS_Address is 0.
1606          */
1607         HostAdapter->BIOS_Address = ExtendedSetupInformation.BIOS_Address << 12;
1608         /*
1609            ISA Host Adapters require Bounce Buffers if there is more than 16MB memory.
1610          */
1611         if (HostAdapter->HostAdapterBusType == BusLogic_ISA_Bus && (void *) high_memory > (void *) MAX_DMA_ADDRESS)
1612                 HostAdapter->BounceBuffersRequired = true;
1613         /*
1614            BusLogic BT-445S Host Adapters prior to board revision E have a hardware
1615            bug whereby when the BIOS is enabled, transfers to/from the same address
1616            range the BIOS occupies modulo 16MB are handled incorrectly.  Only properly
1617            functioning BT-445S Host Adapters have firmware version 3.37, so require
1618            that ISA Bounce Buffers be used for the buggy BT-445S models if there is
1619            more than 16MB memory.
1620          */
1621         if (HostAdapter->BIOS_Address > 0 && strcmp(HostAdapter->ModelName, "BT-445S") == 0 && strcmp(HostAdapter->FirmwareVersion, "3.37") < 0 && (void *) high_memory > (void *) MAX_DMA_ADDRESS)
1622                 HostAdapter->BounceBuffersRequired = true;
1623         /*
1624            Initialize parameters common to MultiMaster and FlashPoint Host Adapters.
1625          */
1626       Common:
1627         /*
1628            Initialize the Host Adapter Full Model Name from the Model Name.
1629          */
1630         strcpy(HostAdapter->FullModelName, "BusLogic ");
1631         strcat(HostAdapter->FullModelName, HostAdapter->ModelName);
1632         /*
1633            Select an appropriate value for the Tagged Queue Depth either from a
1634            BusLogic Driver Options specification, or based on whether this Host
1635            Adapter requires that ISA Bounce Buffers be used.  The Tagged Queue Depth
1636            is left at 0 for automatic determination in BusLogic_SelectQueueDepths.
1637            Initialize the Untagged Queue Depth.
1638          */
1639         for (TargetID = 0; TargetID < BusLogic_MaxTargetDevices; TargetID++) {
1640                 unsigned char QueueDepth = 0;
1641                 if (HostAdapter->DriverOptions != NULL && HostAdapter->DriverOptions->QueueDepth[TargetID] > 0)
1642                         QueueDepth = HostAdapter->DriverOptions->QueueDepth[TargetID];
1643                 else if (HostAdapter->BounceBuffersRequired)
1644                         QueueDepth = BusLogic_TaggedQueueDepthBB;
1645                 HostAdapter->QueueDepth[TargetID] = QueueDepth;
1646         }
1647         if (HostAdapter->BounceBuffersRequired)
1648                 HostAdapter->UntaggedQueueDepth = BusLogic_UntaggedQueueDepthBB;
1649         else
1650                 HostAdapter->UntaggedQueueDepth = BusLogic_UntaggedQueueDepth;
1651         if (HostAdapter->DriverOptions != NULL)
1652                 HostAdapter->CommonQueueDepth = HostAdapter->DriverOptions->CommonQueueDepth;
1653         if (HostAdapter->CommonQueueDepth > 0 && HostAdapter->CommonQueueDepth < HostAdapter->UntaggedQueueDepth)
1654                 HostAdapter->UntaggedQueueDepth = HostAdapter->CommonQueueDepth;
1655         /*
1656            Tagged Queuing is only allowed if Disconnect/Reconnect is permitted.
1657            Therefore, mask the Tagged Queuing Permitted Default bits with the
1658            Disconnect/Reconnect Permitted bits.
1659          */
1660         HostAdapter->TaggedQueuingPermitted &= HostAdapter->DisconnectPermitted;
1661         /*
1662            Combine the default Tagged Queuing Permitted bits with any BusLogic Driver
1663            Options Tagged Queuing specification.
1664          */
1665         if (HostAdapter->DriverOptions != NULL)
1666                 HostAdapter->TaggedQueuingPermitted =
1667                     (HostAdapter->DriverOptions->TaggedQueuingPermitted & HostAdapter->DriverOptions->TaggedQueuingPermittedMask) | (HostAdapter->TaggedQueuingPermitted & ~HostAdapter->DriverOptions->TaggedQueuingPermittedMask);
1668
1669         /*
1670            Select an appropriate value for Bus Settle Time either from a BusLogic
1671            Driver Options specification, or from BusLogic_DefaultBusSettleTime.
1672          */
1673         if (HostAdapter->DriverOptions != NULL && HostAdapter->DriverOptions->BusSettleTime > 0)
1674                 HostAdapter->BusSettleTime = HostAdapter->DriverOptions->BusSettleTime;
1675         else
1676                 HostAdapter->BusSettleTime = BusLogic_DefaultBusSettleTime;
1677         /*
1678            Indicate reading the Host Adapter Configuration completed successfully.
1679          */
1680         return true;
1681 }
1682
1683
1684 /*
1685   BusLogic_ReportHostAdapterConfiguration reports the configuration of
1686   Host Adapter.
1687 */
1688
1689 static boolean __init BusLogic_ReportHostAdapterConfiguration(struct BusLogic_HostAdapter
1690                                                               *HostAdapter)
1691 {
1692         unsigned short AllTargetsMask = (1 << HostAdapter->MaxTargetDevices) - 1;
1693         unsigned short SynchronousPermitted, FastPermitted;
1694         unsigned short UltraPermitted, WidePermitted;
1695         unsigned short DisconnectPermitted, TaggedQueuingPermitted;
1696         boolean CommonSynchronousNegotiation, CommonTaggedQueueDepth;
1697         char SynchronousString[BusLogic_MaxTargetDevices + 1];
1698         char WideString[BusLogic_MaxTargetDevices + 1];
1699         char DisconnectString[BusLogic_MaxTargetDevices + 1];
1700         char TaggedQueuingString[BusLogic_MaxTargetDevices + 1];
1701         char *SynchronousMessage = SynchronousString;
1702         char *WideMessage = WideString;
1703         char *DisconnectMessage = DisconnectString;
1704         char *TaggedQueuingMessage = TaggedQueuingString;
1705         int TargetID;
1706         BusLogic_Info("Configuring BusLogic Model %s %s%s%s%s SCSI Host Adapter\n",
1707                       HostAdapter, HostAdapter->ModelName,
1708                       BusLogic_HostAdapterBusNames[HostAdapter->HostAdapterBusType], (HostAdapter->HostWideSCSI ? " Wide" : ""), (HostAdapter->HostDifferentialSCSI ? " Differential" : ""), (HostAdapter->HostUltraSCSI ? " Ultra" : ""));
1709         BusLogic_Info("  Firmware Version: %s, I/O Address: 0x%X, " "IRQ Channel: %d/%s\n", HostAdapter, HostAdapter->FirmwareVersion, HostAdapter->IO_Address, HostAdapter->IRQ_Channel, (HostAdapter->LevelSensitiveInterrupt ? "Level" : "Edge"));
1710         if (HostAdapter->HostAdapterBusType != BusLogic_PCI_Bus) {
1711                 BusLogic_Info("  DMA Channel: ", HostAdapter);
1712                 if (HostAdapter->DMA_Channel > 0)
1713                         BusLogic_Info("%d, ", HostAdapter, HostAdapter->DMA_Channel);
1714                 else
1715                         BusLogic_Info("None, ", HostAdapter);
1716                 if (HostAdapter->BIOS_Address > 0)
1717                         BusLogic_Info("BIOS Address: 0x%X, ", HostAdapter, HostAdapter->BIOS_Address);
1718                 else
1719                         BusLogic_Info("BIOS Address: None, ", HostAdapter);
1720         } else {
1721                 BusLogic_Info("  PCI Bus: %d, Device: %d, Address: ", HostAdapter, HostAdapter->Bus, HostAdapter->Device);
1722                 if (HostAdapter->PCI_Address > 0)
1723                         BusLogic_Info("0x%X, ", HostAdapter, HostAdapter->PCI_Address);
1724                 else
1725                         BusLogic_Info("Unassigned, ", HostAdapter);
1726         }
1727         BusLogic_Info("Host Adapter SCSI ID: %d\n", HostAdapter, HostAdapter->SCSI_ID);
1728         BusLogic_Info("  Parity Checking: %s, Extended Translation: %s\n", HostAdapter, (HostAdapter->ParityCheckingEnabled ? "Enabled" : "Disabled"), (HostAdapter->ExtendedTranslationEnabled ? "Enabled" : "Disabled"));
1729         AllTargetsMask &= ~(1 << HostAdapter->SCSI_ID);
1730         SynchronousPermitted = HostAdapter->SynchronousPermitted & AllTargetsMask;
1731         FastPermitted = HostAdapter->FastPermitted & AllTargetsMask;
1732         UltraPermitted = HostAdapter->UltraPermitted & AllTargetsMask;
1733         if ((BusLogic_MultiMasterHostAdapterP(HostAdapter) && (HostAdapter->FirmwareVersion[0] >= '4' || HostAdapter->HostAdapterBusType == BusLogic_EISA_Bus)) || BusLogic_FlashPointHostAdapterP(HostAdapter)) {
1734                 CommonSynchronousNegotiation = false;
1735                 if (SynchronousPermitted == 0) {
1736                         SynchronousMessage = "Disabled";
1737                         CommonSynchronousNegotiation = true;
1738                 } else if (SynchronousPermitted == AllTargetsMask) {
1739                         if (FastPermitted == 0) {
1740                                 SynchronousMessage = "Slow";
1741                                 CommonSynchronousNegotiation = true;
1742                         } else if (FastPermitted == AllTargetsMask) {
1743                                 if (UltraPermitted == 0) {
1744                                         SynchronousMessage = "Fast";
1745                                         CommonSynchronousNegotiation = true;
1746                                 } else if (UltraPermitted == AllTargetsMask) {
1747                                         SynchronousMessage = "Ultra";
1748                                         CommonSynchronousNegotiation = true;
1749                                 }
1750                         }
1751                 }
1752                 if (!CommonSynchronousNegotiation) {
1753                         for (TargetID = 0; TargetID < HostAdapter->MaxTargetDevices; TargetID++)
1754                                 SynchronousString[TargetID] = ((!(SynchronousPermitted & (1 << TargetID))) ? 'N' : (!(FastPermitted & (1 << TargetID)) ? 'S' : (!(UltraPermitted & (1 << TargetID)) ? 'F' : 'U')));
1755                         SynchronousString[HostAdapter->SCSI_ID] = '#';
1756                         SynchronousString[HostAdapter->MaxTargetDevices] = '\0';
1757                 }
1758         } else
1759                 SynchronousMessage = (SynchronousPermitted == 0 ? "Disabled" : "Enabled");
1760         WidePermitted = HostAdapter->WidePermitted & AllTargetsMask;
1761         if (WidePermitted == 0)
1762                 WideMessage = "Disabled";
1763         else if (WidePermitted == AllTargetsMask)
1764                 WideMessage = "Enabled";
1765         else {
1766                 for (TargetID = 0; TargetID < HostAdapter->MaxTargetDevices; TargetID++)
1767                         WideString[TargetID] = ((WidePermitted & (1 << TargetID)) ? 'Y' : 'N');
1768                 WideString[HostAdapter->SCSI_ID] = '#';
1769                 WideString[HostAdapter->MaxTargetDevices] = '\0';
1770         }
1771         DisconnectPermitted = HostAdapter->DisconnectPermitted & AllTargetsMask;
1772         if (DisconnectPermitted == 0)
1773                 DisconnectMessage = "Disabled";
1774         else if (DisconnectPermitted == AllTargetsMask)
1775                 DisconnectMessage = "Enabled";
1776         else {
1777                 for (TargetID = 0; TargetID < HostAdapter->MaxTargetDevices; TargetID++)
1778                         DisconnectString[TargetID] = ((DisconnectPermitted & (1 << TargetID)) ? 'Y' : 'N');
1779                 DisconnectString[HostAdapter->SCSI_ID] = '#';
1780                 DisconnectString[HostAdapter->MaxTargetDevices] = '\0';
1781         }
1782         TaggedQueuingPermitted = HostAdapter->TaggedQueuingPermitted & AllTargetsMask;
1783         if (TaggedQueuingPermitted == 0)
1784                 TaggedQueuingMessage = "Disabled";
1785         else if (TaggedQueuingPermitted == AllTargetsMask)
1786                 TaggedQueuingMessage = "Enabled";
1787         else {
1788                 for (TargetID = 0; TargetID < HostAdapter->MaxTargetDevices; TargetID++)
1789                         TaggedQueuingString[TargetID] = ((TaggedQueuingPermitted & (1 << TargetID)) ? 'Y' : 'N');
1790                 TaggedQueuingString[HostAdapter->SCSI_ID] = '#';
1791                 TaggedQueuingString[HostAdapter->MaxTargetDevices] = '\0';
1792         }
1793         BusLogic_Info("  Synchronous Negotiation: %s, Wide Negotiation: %s\n", HostAdapter, SynchronousMessage, WideMessage);
1794         BusLogic_Info("  Disconnect/Reconnect: %s, Tagged Queuing: %s\n", HostAdapter, DisconnectMessage, TaggedQueuingMessage);
1795         if (BusLogic_MultiMasterHostAdapterP(HostAdapter)) {
1796                 BusLogic_Info("  Scatter/Gather Limit: %d of %d segments, " "Mailboxes: %d\n", HostAdapter, HostAdapter->DriverScatterGatherLimit, HostAdapter->HostAdapterScatterGatherLimit, HostAdapter->MailboxCount);
1797                 BusLogic_Info("  Driver Queue Depth: %d, " "Host Adapter Queue Depth: %d\n", HostAdapter, HostAdapter->DriverQueueDepth, HostAdapter->HostAdapterQueueDepth);
1798         } else
1799                 BusLogic_Info("  Driver Queue Depth: %d, " "Scatter/Gather Limit: %d segments\n", HostAdapter, HostAdapter->DriverQueueDepth, HostAdapter->DriverScatterGatherLimit);
1800         BusLogic_Info("  Tagged Queue Depth: ", HostAdapter);
1801         CommonTaggedQueueDepth = true;
1802         for (TargetID = 1; TargetID < HostAdapter->MaxTargetDevices; TargetID++)
1803                 if (HostAdapter->QueueDepth[TargetID] != HostAdapter->QueueDepth[0]) {
1804                         CommonTaggedQueueDepth = false;
1805                         break;
1806                 }
1807         if (CommonTaggedQueueDepth) {
1808                 if (HostAdapter->QueueDepth[0] > 0)
1809                         BusLogic_Info("%d", HostAdapter, HostAdapter->QueueDepth[0]);
1810                 else
1811                         BusLogic_Info("Automatic", HostAdapter);
1812         } else
1813                 BusLogic_Info("Individual", HostAdapter);
1814         BusLogic_Info(", Untagged Queue Depth: %d\n", HostAdapter, HostAdapter->UntaggedQueueDepth);
1815         if (HostAdapter->TerminationInfoValid) {
1816                 if (HostAdapter->HostWideSCSI)
1817                         BusLogic_Info("  SCSI Bus Termination: %s", HostAdapter, (HostAdapter->LowByteTerminated ? (HostAdapter->HighByteTerminated ? "Both Enabled" : "Low Enabled")
1818                                                                                   : (HostAdapter->HighByteTerminated ? "High Enabled" : "Both Disabled")));
1819                 else
1820                         BusLogic_Info("  SCSI Bus Termination: %s", HostAdapter, (HostAdapter->LowByteTerminated ? "Enabled" : "Disabled"));
1821                 if (HostAdapter->HostSupportsSCAM)
1822                         BusLogic_Info(", SCAM: %s", HostAdapter, (HostAdapter->SCAM_Enabled ? (HostAdapter->SCAM_Level2 ? "Enabled, Level 2" : "Enabled, Level 1")
1823                                                                   : "Disabled"));
1824                 BusLogic_Info("\n", HostAdapter);
1825         }
1826         /*
1827            Indicate reporting the Host Adapter configuration completed successfully.
1828          */
1829         return true;
1830 }
1831
1832
1833 /*
1834   BusLogic_AcquireResources acquires the system resources necessary to use
1835   Host Adapter.
1836 */
1837
1838 static boolean __init BusLogic_AcquireResources(struct BusLogic_HostAdapter *HostAdapter)
1839 {
1840         if (HostAdapter->IRQ_Channel == 0) {
1841                 BusLogic_Error("NO LEGAL INTERRUPT CHANNEL ASSIGNED - DETACHING\n", HostAdapter);
1842                 return false;
1843         }
1844         /*
1845            Acquire shared access to the IRQ Channel.
1846          */
1847         if (request_irq(HostAdapter->IRQ_Channel, BusLogic_InterruptHandler, IRQF_SHARED, HostAdapter->FullModelName, HostAdapter) < 0) {
1848                 BusLogic_Error("UNABLE TO ACQUIRE IRQ CHANNEL %d - DETACHING\n", HostAdapter, HostAdapter->IRQ_Channel);
1849                 return false;
1850         }
1851         HostAdapter->IRQ_ChannelAcquired = true;
1852         /*
1853            Acquire exclusive access to the DMA Channel.
1854          */
1855         if (HostAdapter->DMA_Channel > 0) {
1856                 if (request_dma(HostAdapter->DMA_Channel, HostAdapter->FullModelName) < 0) {
1857                         BusLogic_Error("UNABLE TO ACQUIRE DMA CHANNEL %d - DETACHING\n", HostAdapter, HostAdapter->DMA_Channel);
1858                         return false;
1859                 }
1860                 set_dma_mode(HostAdapter->DMA_Channel, DMA_MODE_CASCADE);
1861                 enable_dma(HostAdapter->DMA_Channel);
1862                 HostAdapter->DMA_ChannelAcquired = true;
1863         }
1864         /*
1865            Indicate the System Resource Acquisition completed successfully,
1866          */
1867         return true;
1868 }
1869
1870
1871 /*
1872   BusLogic_ReleaseResources releases any system resources previously acquired
1873   by BusLogic_AcquireResources.
1874 */
1875
1876 static void BusLogic_ReleaseResources(struct BusLogic_HostAdapter *HostAdapter)
1877 {
1878         /*
1879            Release shared access to the IRQ Channel.
1880          */
1881         if (HostAdapter->IRQ_ChannelAcquired)
1882                 free_irq(HostAdapter->IRQ_Channel, HostAdapter);
1883         /*
1884            Release exclusive access to the DMA Channel.
1885          */
1886         if (HostAdapter->DMA_ChannelAcquired)
1887                 free_dma(HostAdapter->DMA_Channel);
1888         /*
1889            Release any allocated memory structs not released elsewhere
1890          */
1891         if (HostAdapter->MailboxSpace)
1892                 pci_free_consistent(HostAdapter->PCI_Device, HostAdapter->MailboxSize, HostAdapter->MailboxSpace, HostAdapter->MailboxSpaceHandle);
1893         HostAdapter->MailboxSpace = NULL;
1894         HostAdapter->MailboxSpaceHandle = 0;
1895         HostAdapter->MailboxSize = 0;
1896 }
1897
1898
1899 /*
1900   BusLogic_InitializeHostAdapter initializes Host Adapter.  This is the only
1901   function called during SCSI Host Adapter detection which modifies the state
1902   of the Host Adapter from its initial power on or hard reset state.
1903 */
1904
1905 static boolean BusLogic_InitializeHostAdapter(struct BusLogic_HostAdapter
1906                                               *HostAdapter)
1907 {
1908         struct BusLogic_ExtendedMailboxRequest ExtendedMailboxRequest;
1909         enum BusLogic_RoundRobinModeRequest RoundRobinModeRequest;
1910         enum BusLogic_SetCCBFormatRequest SetCCBFormatRequest;
1911         int TargetID;
1912         /*
1913            Initialize the pointers to the first and last CCBs that are queued for
1914            completion processing.
1915          */
1916         HostAdapter->FirstCompletedCCB = NULL;
1917         HostAdapter->LastCompletedCCB = NULL;
1918         /*
1919            Initialize the Bus Device Reset Pending CCB, Tagged Queuing Active,
1920            Command Successful Flag, Active Commands, and Commands Since Reset
1921            for each Target Device.
1922          */
1923         for (TargetID = 0; TargetID < HostAdapter->MaxTargetDevices; TargetID++) {
1924                 HostAdapter->BusDeviceResetPendingCCB[TargetID] = NULL;
1925                 HostAdapter->TargetFlags[TargetID].TaggedQueuingActive = false;
1926                 HostAdapter->TargetFlags[TargetID].CommandSuccessfulFlag = false;
1927                 HostAdapter->ActiveCommands[TargetID] = 0;
1928                 HostAdapter->CommandsSinceReset[TargetID] = 0;
1929         }
1930         /*
1931            FlashPoint Host Adapters do not use Outgoing and Incoming Mailboxes.
1932          */
1933         if (BusLogic_FlashPointHostAdapterP(HostAdapter))
1934                 goto Done;
1935         /*
1936            Initialize the Outgoing and Incoming Mailbox pointers.
1937          */
1938         HostAdapter->MailboxSize = HostAdapter->MailboxCount * (sizeof(struct BusLogic_OutgoingMailbox) + sizeof(struct BusLogic_IncomingMailbox));
1939         HostAdapter->MailboxSpace = pci_alloc_consistent(HostAdapter->PCI_Device, HostAdapter->MailboxSize, &HostAdapter->MailboxSpaceHandle);
1940         if (HostAdapter->MailboxSpace == NULL)
1941                 return BusLogic_Failure(HostAdapter, "MAILBOX ALLOCATION");
1942         HostAdapter->FirstOutgoingMailbox = (struct BusLogic_OutgoingMailbox *) HostAdapter->MailboxSpace;
1943         HostAdapter->LastOutgoingMailbox = HostAdapter->FirstOutgoingMailbox + HostAdapter->MailboxCount - 1;
1944         HostAdapter->NextOutgoingMailbox = HostAdapter->FirstOutgoingMailbox;
1945         HostAdapter->FirstIncomingMailbox = (struct BusLogic_IncomingMailbox *) (HostAdapter->LastOutgoingMailbox + 1);
1946         HostAdapter->LastIncomingMailbox = HostAdapter->FirstIncomingMailbox + HostAdapter->MailboxCount - 1;
1947         HostAdapter->NextIncomingMailbox = HostAdapter->FirstIncomingMailbox;
1948
1949         /*
1950            Initialize the Outgoing and Incoming Mailbox structures.
1951          */
1952         memset(HostAdapter->FirstOutgoingMailbox, 0, HostAdapter->MailboxCount * sizeof(struct BusLogic_OutgoingMailbox));
1953         memset(HostAdapter->FirstIncomingMailbox, 0, HostAdapter->MailboxCount * sizeof(struct BusLogic_IncomingMailbox));
1954         /*
1955            Initialize the Host Adapter's Pointer to the Outgoing/Incoming Mailboxes.
1956          */
1957         ExtendedMailboxRequest.MailboxCount = HostAdapter->MailboxCount;
1958         ExtendedMailboxRequest.BaseMailboxAddress = (u32) HostAdapter->MailboxSpaceHandle;
1959         if (BusLogic_Command(HostAdapter, BusLogic_InitializeExtendedMailbox, &ExtendedMailboxRequest, sizeof(ExtendedMailboxRequest), NULL, 0) < 0)
1960                 return BusLogic_Failure(HostAdapter, "MAILBOX INITIALIZATION");
1961         /*
1962            Enable Strict Round Robin Mode if supported by the Host Adapter.  In
1963            Strict Round Robin Mode, the Host Adapter only looks at the next Outgoing
1964            Mailbox for each new command, rather than scanning through all the
1965            Outgoing Mailboxes to find any that have new commands in them.  Strict
1966            Round Robin Mode is significantly more efficient.
1967          */
1968         if (HostAdapter->StrictRoundRobinModeSupport) {
1969                 RoundRobinModeRequest = BusLogic_StrictRoundRobinMode;
1970                 if (BusLogic_Command(HostAdapter, BusLogic_EnableStrictRoundRobinMode, &RoundRobinModeRequest, sizeof(RoundRobinModeRequest), NULL, 0) < 0)
1971                         return BusLogic_Failure(HostAdapter, "ENABLE STRICT ROUND ROBIN MODE");
1972         }
1973         /*
1974            For Host Adapters that support Extended LUN Format CCBs, issue the Set CCB
1975            Format command to allow 32 Logical Units per Target Device.
1976          */
1977         if (HostAdapter->ExtendedLUNSupport) {
1978                 SetCCBFormatRequest = BusLogic_ExtendedLUNFormatCCB;
1979                 if (BusLogic_Command(HostAdapter, BusLogic_SetCCBFormat, &SetCCBFormatRequest, sizeof(SetCCBFormatRequest), NULL, 0) < 0)
1980                         return BusLogic_Failure(HostAdapter, "SET CCB FORMAT");
1981         }
1982         /*
1983            Announce Successful Initialization.
1984          */
1985       Done:
1986         if (!HostAdapter->HostAdapterInitialized) {
1987                 BusLogic_Info("*** %s Initialized Successfully ***\n", HostAdapter, HostAdapter->FullModelName);
1988                 BusLogic_Info("\n", HostAdapter);
1989         } else
1990                 BusLogic_Warning("*** %s Initialized Successfully ***\n", HostAdapter, HostAdapter->FullModelName);
1991         HostAdapter->HostAdapterInitialized = true;
1992         /*
1993            Indicate the Host Adapter Initialization completed successfully.
1994          */
1995         return true;
1996 }
1997
1998
1999 /*
2000   BusLogic_TargetDeviceInquiry inquires about the Target Devices accessible
2001   through Host Adapter.
2002 */
2003
2004 static boolean __init BusLogic_TargetDeviceInquiry(struct BusLogic_HostAdapter
2005                                                    *HostAdapter)
2006 {
2007         u16 InstalledDevices;
2008         u8 InstalledDevicesID0to7[8];
2009         struct BusLogic_SetupInformation SetupInformation;
2010         u8 SynchronousPeriod[BusLogic_MaxTargetDevices];
2011         unsigned char RequestedReplyLength;
2012         int TargetID;
2013         /*
2014            Wait a few seconds between the Host Adapter Hard Reset which initiates
2015            a SCSI Bus Reset and issuing any SCSI Commands.  Some SCSI devices get
2016            confused if they receive SCSI Commands too soon after a SCSI Bus Reset.
2017          */
2018         BusLogic_Delay(HostAdapter->BusSettleTime);
2019         /*
2020            FlashPoint Host Adapters do not provide for Target Device Inquiry.
2021          */
2022         if (BusLogic_FlashPointHostAdapterP(HostAdapter))
2023                 return true;
2024         /*
2025            Inhibit the Target Device Inquiry if requested.
2026          */
2027         if (HostAdapter->DriverOptions != NULL && HostAdapter->DriverOptions->LocalOptions.InhibitTargetInquiry)
2028                 return true;
2029         /*
2030            Issue the Inquire Target Devices command for host adapters with firmware
2031            version 4.25 or later, or the Inquire Installed Devices ID 0 to 7 command
2032            for older host adapters.  This is necessary to force Synchronous Transfer
2033            Negotiation so that the Inquire Setup Information and Inquire Synchronous
2034            Period commands will return valid data.  The Inquire Target Devices command
2035            is preferable to Inquire Installed Devices ID 0 to 7 since it only probes
2036            Logical Unit 0 of each Target Device.
2037          */
2038         if (strcmp(HostAdapter->FirmwareVersion, "4.25") >= 0) {
2039
2040                 /*
2041                  * Issue a Inquire Target Devices command.  Inquire Target Devices only
2042                  * tests Logical Unit 0 of each Target Device unlike the Inquire Installed
2043                  * Devices commands which test Logical Units 0 - 7.  Two bytes are
2044                  * returned, where byte 0 bit 0 set indicates that Target Device 0 exists,
2045                  * and so on.
2046                  */
2047
2048                 if (BusLogic_Command(HostAdapter, BusLogic_InquireTargetDevices, NULL, 0, &InstalledDevices, sizeof(InstalledDevices))
2049                     != sizeof(InstalledDevices))
2050                         return BusLogic_Failure(HostAdapter, "INQUIRE TARGET DEVICES");
2051                 for (TargetID = 0; TargetID < HostAdapter->MaxTargetDevices; TargetID++)
2052                         HostAdapter->TargetFlags[TargetID].TargetExists = (InstalledDevices & (1 << TargetID) ? true : false);
2053         } else {
2054
2055                 /*
2056                  * Issue an Inquire Installed Devices command.  For each Target Device,
2057                  * a byte is returned where bit 0 set indicates that Logical Unit 0
2058                  * exists, bit 1 set indicates that Logical Unit 1 exists, and so on.
2059                  */
2060
2061                 if (BusLogic_Command(HostAdapter, BusLogic_InquireInstalledDevicesID0to7, NULL, 0, &InstalledDevicesID0to7, sizeof(InstalledDevicesID0to7))
2062                     != sizeof(InstalledDevicesID0to7))
2063                         return BusLogic_Failure(HostAdapter, "INQUIRE INSTALLED DEVICES ID 0 TO 7");
2064                 for (TargetID = 0; TargetID < 8; TargetID++)
2065                         HostAdapter->TargetFlags[TargetID].TargetExists = (InstalledDevicesID0to7[TargetID] != 0 ? true : false);
2066         }
2067         /*
2068            Issue the Inquire Setup Information command.
2069          */
2070         RequestedReplyLength = sizeof(SetupInformation);
2071         if (BusLogic_Command(HostAdapter, BusLogic_InquireSetupInformation, &RequestedReplyLength, sizeof(RequestedReplyLength), &SetupInformation, sizeof(SetupInformation))
2072             != sizeof(SetupInformation))
2073                 return BusLogic_Failure(HostAdapter, "INQUIRE SETUP INFORMATION");
2074         for (TargetID = 0; TargetID < HostAdapter->MaxTargetDevices; TargetID++)
2075                 HostAdapter->SynchronousOffset[TargetID] = (TargetID < 8 ? SetupInformation.SynchronousValuesID0to7[TargetID].Offset : SetupInformation.SynchronousValuesID8to15[TargetID - 8].Offset);
2076         if (strcmp(HostAdapter->FirmwareVersion, "5.06L") >= 0)
2077                 for (TargetID = 0; TargetID < HostAdapter->MaxTargetDevices; TargetID++)
2078                         HostAdapter->TargetFlags[TargetID].WideTransfersActive = (TargetID < 8 ? (SetupInformation.WideTransfersActiveID0to7 & (1 << TargetID)
2079                                                                                                   ? true : false)
2080                                                                                   : (SetupInformation.WideTransfersActiveID8to15 & (1 << (TargetID - 8))
2081                                                                                      ? true : false));
2082         /*
2083            Issue the Inquire Synchronous Period command.
2084          */
2085         if (HostAdapter->FirmwareVersion[0] >= '3') {
2086
2087                 /* Issue a Inquire Synchronous Period command.  For each Target Device,
2088                  * a byte is returned which represents the Synchronous Transfer Period
2089                  * in units of 10 nanoseconds.
2090                  */
2091
2092                 RequestedReplyLength = sizeof(SynchronousPeriod);
2093                 if (BusLogic_Command(HostAdapter, BusLogic_InquireSynchronousPeriod, &RequestedReplyLength, sizeof(RequestedReplyLength), &SynchronousPeriod, sizeof(SynchronousPeriod))
2094                     != sizeof(SynchronousPeriod))
2095                         return BusLogic_Failure(HostAdapter, "INQUIRE SYNCHRONOUS PERIOD");
2096                 for (TargetID = 0; TargetID < HostAdapter->MaxTargetDevices; TargetID++)
2097                         HostAdapter->SynchronousPeriod[TargetID] = SynchronousPeriod[TargetID];
2098         } else
2099                 for (TargetID = 0; TargetID < HostAdapter->MaxTargetDevices; TargetID++)
2100                         if (SetupInformation.SynchronousValuesID0to7[TargetID].Offset > 0)
2101                                 HostAdapter->SynchronousPeriod[TargetID] = 20 + 5 * SetupInformation.SynchronousValuesID0to7[TargetID]
2102                                     .TransferPeriod;
2103         /*
2104            Indicate the Target Device Inquiry completed successfully.
2105          */
2106         return true;
2107 }
2108
2109 /*
2110   BusLogic_InitializeHostStructure initializes the fields in the SCSI Host
2111   structure.  The base, io_port, n_io_ports, irq, and dma_channel fields in the
2112   SCSI Host structure are intentionally left uninitialized, as this driver
2113   handles acquisition and release of these resources explicitly, as well as
2114   ensuring exclusive access to the Host Adapter hardware and data structures
2115   through explicit acquisition and release of the Host Adapter's Lock.
2116 */
2117
2118 static void __init BusLogic_InitializeHostStructure(struct BusLogic_HostAdapter
2119                                                     *HostAdapter, struct Scsi_Host *Host)
2120 {
2121         Host->max_id = HostAdapter->MaxTargetDevices;
2122         Host->max_lun = HostAdapter->MaxLogicalUnits;
2123         Host->max_channel = 0;
2124         Host->unique_id = HostAdapter->IO_Address;
2125         Host->this_id = HostAdapter->SCSI_ID;
2126         Host->can_queue = HostAdapter->DriverQueueDepth;
2127         Host->sg_tablesize = HostAdapter->DriverScatterGatherLimit;
2128         Host->unchecked_isa_dma = HostAdapter->BounceBuffersRequired;
2129         Host->cmd_per_lun = HostAdapter->UntaggedQueueDepth;
2130 }
2131
2132 /*
2133   BusLogic_SlaveConfigure will actually set the queue depth on individual
2134   scsi devices as they are permanently added to the device chain.  We
2135   shamelessly rip off the SelectQueueDepths code to make this work mostly
2136   like it used to.  Since we don't get called once at the end of the scan
2137   but instead get called for each device, we have to do things a bit
2138   differently.
2139 */
2140 static int BusLogic_SlaveConfigure(struct scsi_device *Device)
2141 {
2142         struct BusLogic_HostAdapter *HostAdapter = (struct BusLogic_HostAdapter *) Device->host->hostdata;
2143         int TargetID = Device->id;
2144         int QueueDepth = HostAdapter->QueueDepth[TargetID];
2145
2146         if (HostAdapter->TargetFlags[TargetID].TaggedQueuingSupported && (HostAdapter->TaggedQueuingPermitted & (1 << TargetID))) {
2147                 if (QueueDepth == 0)
2148                         QueueDepth = BusLogic_MaxAutomaticTaggedQueueDepth;
2149                 HostAdapter->QueueDepth[TargetID] = QueueDepth;
2150                 scsi_adjust_queue_depth(Device, MSG_SIMPLE_TAG, QueueDepth);
2151         } else {
2152                 HostAdapter->TaggedQueuingPermitted &= ~(1 << TargetID);
2153                 QueueDepth = HostAdapter->UntaggedQueueDepth;
2154                 HostAdapter->QueueDepth[TargetID] = QueueDepth;
2155                 scsi_adjust_queue_depth(Device, 0, QueueDepth);
2156         }
2157         QueueDepth = 0;
2158         for (TargetID = 0; TargetID < HostAdapter->MaxTargetDevices; TargetID++)
2159                 if (HostAdapter->TargetFlags[TargetID].TargetExists) {
2160                         QueueDepth += HostAdapter->QueueDepth[TargetID];
2161                 }
2162         if (QueueDepth > HostAdapter->AllocatedCCBs)
2163                 BusLogic_CreateAdditionalCCBs(HostAdapter, QueueDepth - HostAdapter->AllocatedCCBs, false);
2164         return 0;
2165 }
2166
2167 /*
2168   BusLogic_DetectHostAdapter probes for BusLogic Host Adapters at the standard
2169   I/O Addresses where they may be located, initializing, registering, and
2170   reporting the configuration of each BusLogic Host Adapter it finds.  It
2171   returns the number of BusLogic Host Adapters successfully initialized and
2172   registered.
2173 */
2174
2175 static int __init BusLogic_init(void)
2176 {
2177         int BusLogicHostAdapterCount = 0, DriverOptionsIndex = 0, ProbeIndex;
2178         struct BusLogic_HostAdapter *PrototypeHostAdapter;
2179
2180 #ifdef MODULE
2181         if (BusLogic)
2182                 BusLogic_Setup(BusLogic);
2183 #endif
2184
2185         if (BusLogic_ProbeOptions.NoProbe)
2186                 return -ENODEV;
2187         BusLogic_ProbeInfoList = (struct BusLogic_ProbeInfo *)
2188             kmalloc(BusLogic_MaxHostAdapters * sizeof(struct BusLogic_ProbeInfo), GFP_ATOMIC);
2189         if (BusLogic_ProbeInfoList == NULL) {
2190                 BusLogic_Error("BusLogic: Unable to allocate Probe Info List\n", NULL);
2191                 return -ENOMEM;
2192         }
2193         memset(BusLogic_ProbeInfoList, 0, BusLogic_MaxHostAdapters * sizeof(struct BusLogic_ProbeInfo));
2194         PrototypeHostAdapter = (struct BusLogic_HostAdapter *)
2195             kmalloc(sizeof(struct BusLogic_HostAdapter), GFP_ATOMIC);
2196         if (PrototypeHostAdapter == NULL) {
2197                 kfree(BusLogic_ProbeInfoList);
2198                 BusLogic_Error("BusLogic: Unable to allocate Prototype " "Host Adapter\n", NULL);
2199                 return -ENOMEM;
2200         }
2201         memset(PrototypeHostAdapter, 0, sizeof(struct BusLogic_HostAdapter));
2202 #ifdef MODULE
2203         if (BusLogic != NULL)
2204                 BusLogic_Setup(BusLogic);
2205 #endif
2206         BusLogic_InitializeProbeInfoList(PrototypeHostAdapter);
2207         for (ProbeIndex = 0; ProbeIndex < BusLogic_ProbeInfoCount; ProbeIndex++) {
2208                 struct BusLogic_ProbeInfo *ProbeInfo = &BusLogic_ProbeInfoList[ProbeIndex];
2209                 struct BusLogic_HostAdapter *HostAdapter = PrototypeHostAdapter;
2210                 struct Scsi_Host *Host;
2211                 if (ProbeInfo->IO_Address == 0)
2212                         continue;
2213                 memset(HostAdapter, 0, sizeof(struct BusLogic_HostAdapter));
2214                 HostAdapter->HostAdapterType = ProbeInfo->HostAdapterType;
2215                 HostAdapter->HostAdapterBusType = ProbeInfo->HostAdapterBusType;
2216                 HostAdapter->IO_Address = ProbeInfo->IO_Address;
2217                 HostAdapter->PCI_Address = ProbeInfo->PCI_Address;
2218                 HostAdapter->Bus = ProbeInfo->Bus;
2219                 HostAdapter->Device = ProbeInfo->Device;
2220                 HostAdapter->PCI_Device = ProbeInfo->PCI_Device;
2221                 HostAdapter->IRQ_Channel = ProbeInfo->IRQ_Channel;
2222                 HostAdapter->AddressCount = BusLogic_HostAdapterAddressCount[HostAdapter->HostAdapterType];
2223                 /*
2224                    Probe the Host Adapter.  If unsuccessful, abort further initialization.
2225                  */
2226                 if (!BusLogic_ProbeHostAdapter(HostAdapter))
2227                         continue;
2228                 /*
2229                    Hard Reset the Host Adapter.  If unsuccessful, abort further
2230                    initialization.
2231                  */
2232                 if (!BusLogic_HardwareResetHostAdapter(HostAdapter, true))
2233                         continue;
2234                 /*
2235                    Check the Host Adapter.  If unsuccessful, abort further initialization.
2236                  */
2237                 if (!BusLogic_CheckHostAdapter(HostAdapter))
2238                         continue;
2239                 /*
2240                    Initialize the Driver Options field if provided.
2241                  */
2242                 if (DriverOptionsIndex < BusLogic_DriverOptionsCount)
2243                         HostAdapter->DriverOptions = &BusLogic_DriverOptions[DriverOptionsIndex++];
2244                 /*
2245                    Announce the Driver Version and Date, Author's Name, Copyright Notice,
2246                    and Electronic Mail Address.
2247                  */
2248                 BusLogic_AnnounceDriver(HostAdapter);
2249                 /*
2250                    Register usage of the I/O Address range.  From this point onward, any
2251                    failure will be assumed to be due to a problem with the Host Adapter,
2252                    rather than due to having mistakenly identified this port as belonging
2253                    to a BusLogic Host Adapter.  The I/O Address range will not be
2254                    released, thereby preventing it from being incorrectly identified as
2255                    any other type of Host Adapter.
2256                  */
2257                 if (!request_region(HostAdapter->IO_Address, HostAdapter->AddressCount, "BusLogic"))
2258                         continue;
2259                 /*
2260                    Register the SCSI Host structure.
2261                  */
2262
2263                 Host = scsi_host_alloc(&Bus_Logic_template, sizeof(struct BusLogic_HostAdapter));
2264                 if (Host == NULL) {
2265                         release_region(HostAdapter->IO_Address, HostAdapter->AddressCount);
2266                         continue;
2267                 }
2268                 HostAdapter = (struct BusLogic_HostAdapter *) Host->hostdata;
2269                 memcpy(HostAdapter, PrototypeHostAdapter, sizeof(struct BusLogic_HostAdapter));
2270                 HostAdapter->SCSI_Host = Host;
2271                 HostAdapter->HostNumber = Host->host_no;
2272                 /*
2273                    Add Host Adapter to the end of the list of registered BusLogic
2274                    Host Adapters.
2275                  */
2276                 list_add_tail(&HostAdapter->host_list, &BusLogic_host_list);
2277
2278                 /*
2279                    Read the Host Adapter Configuration, Configure the Host Adapter,
2280                    Acquire the System Resources necessary to use the Host Adapter, then
2281                    Create the Initial CCBs, Initialize the Host Adapter, and finally
2282                    perform Target Device Inquiry.
2283                  */
2284                 if (BusLogic_ReadHostAdapterConfiguration(HostAdapter) &&
2285                     BusLogic_ReportHostAdapterConfiguration(HostAdapter) && BusLogic_AcquireResources(HostAdapter) && BusLogic_CreateInitialCCBs(HostAdapter) && BusLogic_InitializeHostAdapter(HostAdapter) && BusLogic_TargetDeviceInquiry(HostAdapter)) {
2286                         /*
2287                            Initialization has been completed successfully.  Release and
2288                            re-register usage of the I/O Address range so that the Model
2289                            Name of the Host Adapter will appear, and initialize the SCSI
2290                            Host structure.
2291                          */
2292                         release_region(HostAdapter->IO_Address, HostAdapter->AddressCount);
2293                         if (!request_region(HostAdapter->IO_Address, HostAdapter->AddressCount, HostAdapter->FullModelName)) {
2294                                 printk(KERN_WARNING "BusLogic: Release and re-register of " "port 0x%04lx failed \n", (unsigned long) HostAdapter->IO_Address);
2295                                 BusLogic_DestroyCCBs(HostAdapter);
2296                                 BusLogic_ReleaseResources(HostAdapter);
2297                                 list_del(&HostAdapter->host_list);
2298                                 scsi_host_put(Host);
2299                         } else {
2300                                 BusLogic_InitializeHostStructure(HostAdapter, Host);
2301                                 scsi_add_host(Host, HostAdapter->PCI_Device ? &HostAdapter->PCI_Device->dev : NULL);
2302                                 scsi_scan_host(Host);
2303                                 BusLogicHostAdapterCount++;
2304                         }
2305                 } else {
2306                         /*
2307                            An error occurred during Host Adapter Configuration Querying, Host
2308                            Adapter Configuration, Resource Acquisition, CCB Creation, Host
2309                            Adapter Initialization, or Target Device Inquiry, so remove Host
2310                            Adapter from the list of registered BusLogic Host Adapters, destroy
2311                            the CCBs, Release the System Resources, and Unregister the SCSI
2312                            Host.
2313                          */
2314                         BusLogic_DestroyCCBs(HostAdapter);
2315                         BusLogic_ReleaseResources(HostAdapter);
2316                         list_del(&HostAdapter->host_list);
2317                         scsi_host_put(Host);
2318                 }
2319         }
2320         kfree(PrototypeHostAdapter);
2321         kfree(BusLogic_ProbeInfoList);
2322         BusLogic_ProbeInfoList = NULL;
2323         return 0;
2324 }
2325
2326
2327 /*
2328   BusLogic_ReleaseHostAdapter releases all resources previously acquired to
2329   support a specific Host Adapter, including the I/O Address range, and
2330   unregisters the BusLogic Host Adapter.
2331 */
2332
2333 static int __exit BusLogic_ReleaseHostAdapter(struct BusLogic_HostAdapter *HostAdapter)
2334 {
2335         struct Scsi_Host *Host = HostAdapter->SCSI_Host;
2336
2337         scsi_remove_host(Host);
2338
2339         /*
2340            FlashPoint Host Adapters must first be released by the FlashPoint
2341            SCCB Manager.
2342          */
2343         if (BusLogic_FlashPointHostAdapterP(HostAdapter))
2344                 FlashPoint_ReleaseHostAdapter(HostAdapter->CardHandle);
2345         /*
2346            Destroy the CCBs and release any system resources acquired to
2347            support Host Adapter.
2348          */
2349         BusLogic_DestroyCCBs(HostAdapter);
2350         BusLogic_ReleaseResources(HostAdapter);
2351         /*
2352            Release usage of the I/O Address range.
2353          */
2354         release_region(HostAdapter->IO_Address, HostAdapter->AddressCount);
2355         /*
2356            Remove Host Adapter from the list of registered BusLogic Host Adapters.
2357          */
2358         list_del(&HostAdapter->host_list);
2359
2360         scsi_host_put(Host);
2361         return 0;
2362 }
2363
2364
2365 /*
2366   BusLogic_QueueCompletedCCB queues CCB for completion processing.
2367 */
2368
2369 static void BusLogic_QueueCompletedCCB(struct BusLogic_CCB *CCB)
2370 {
2371         struct BusLogic_HostAdapter *HostAdapter = CCB->HostAdapter;
2372         CCB->Status = BusLogic_CCB_Completed;
2373         CCB->Next = NULL;
2374         if (HostAdapter->FirstCompletedCCB == NULL) {
2375                 HostAdapter->FirstCompletedCCB = CCB;
2376                 HostAdapter->LastCompletedCCB = CCB;
2377         } else {
2378                 HostAdapter->LastCompletedCCB->Next = CCB;
2379                 HostAdapter->LastCompletedCCB = CCB;
2380         }
2381         HostAdapter->ActiveCommands[CCB->TargetID]--;
2382 }
2383
2384
2385 /*
2386   BusLogic_ComputeResultCode computes a SCSI Subsystem Result Code from
2387   the Host Adapter Status and Target Device Status.
2388 */
2389
2390 static int BusLogic_ComputeResultCode(struct BusLogic_HostAdapter *HostAdapter, enum BusLogic_HostAdapterStatus HostAdapterStatus, enum BusLogic_TargetDeviceStatus TargetDeviceStatus)
2391 {
2392         int HostStatus;
2393         switch (HostAdapterStatus) {
2394         case BusLogic_CommandCompletedNormally:
2395         case BusLogic_LinkedCommandCompleted:
2396         case BusLogic_LinkedCommandCompletedWithFlag:
2397                 HostStatus = DID_OK;
2398                 break;
2399         case BusLogic_SCSISelectionTimeout:
2400                 HostStatus = DID_TIME_OUT;
2401                 break;
2402         case BusLogic_InvalidOutgoingMailboxActionCode:
2403         case BusLogic_InvalidCommandOperationCode:
2404         case BusLogic_InvalidCommandParameter:
2405                 BusLogic_Warning("BusLogic Driver Protocol Error 0x%02X\n", HostAdapter, HostAdapterStatus);
2406         case BusLogic_DataUnderRun:
2407         case BusLogic_DataOverRun:
2408         case BusLogic_UnexpectedBusFree:
2409         case BusLogic_LinkedCCBhasInvalidLUN:
2410         case BusLogic_AutoRequestSenseFailed:
2411         case BusLogic_TaggedQueuingMessageRejected:
2412         case BusLogic_UnsupportedMessageReceived:
2413         case BusLogic_HostAdapterHardwareFailed:
2414         case BusLogic_TargetDeviceReconnectedImproperly:
2415         case BusLogic_AbortQueueGenerated:
2416         case BusLogic_HostAdapterSoftwareError:
2417         case BusLogic_HostAdapterHardwareTimeoutError:
2418         case BusLogic_SCSIParityErrorDetected:
2419                 HostStatus = DID_ERROR;
2420                 break;
2421         case BusLogic_InvalidBusPhaseRequested:
2422         case BusLogic_TargetFailedResponseToATN:
2423         case BusLogic_HostAdapterAssertedRST:
2424         case BusLogic_OtherDeviceAssertedRST:
2425         case BusLogic_HostAdapterAssertedBusDeviceReset:
2426                 HostStatus = DID_RESET;
2427                 break;
2428         default:
2429                 BusLogic_Warning("Unknown Host Adapter Status 0x%02X\n", HostAdapter, HostAdapterStatus);
2430                 HostStatus = DID_ERROR;
2431                 break;
2432         }
2433         return (HostStatus << 16) | TargetDeviceStatus;
2434 }
2435
2436
2437 /*
2438   BusLogic_ScanIncomingMailboxes scans the Incoming Mailboxes saving any
2439   Incoming Mailbox entries for completion processing.
2440 */
2441
2442 static void BusLogic_ScanIncomingMailboxes(struct BusLogic_HostAdapter *HostAdapter)
2443 {
2444         /*
2445            Scan through the Incoming Mailboxes in Strict Round Robin fashion, saving
2446            any completed CCBs for further processing.  It is essential that for each
2447            CCB and SCSI Command issued, command completion processing is performed
2448            exactly once.  Therefore, only Incoming Mailboxes with completion code
2449            Command Completed Without Error, Command Completed With Error, or Command
2450            Aborted At Host Request are saved for completion processing.  When an
2451            Incoming Mailbox has a completion code of Aborted Command Not Found, the
2452            CCB had already completed or been aborted before the current Abort request
2453            was processed, and so completion processing has already occurred and no
2454            further action should be taken.
2455          */
2456         struct BusLogic_IncomingMailbox *NextIncomingMailbox = HostAdapter->NextIncomingMailbox;
2457         enum BusLogic_CompletionCode CompletionCode;
2458         while ((CompletionCode = NextIncomingMailbox->CompletionCode) != BusLogic_IncomingMailboxFree) {
2459                 /*
2460                    We are only allowed to do this because we limit our architectures we
2461                    run on to machines where bus_to_virt() actually works.  There *needs*
2462                    to be a dma_addr_to_virt() in the new PCI DMA mapping interface to
2463                    replace bus_to_virt() or else this code is going to become very
2464                    innefficient.
2465                  */
2466                 struct BusLogic_CCB *CCB = (struct BusLogic_CCB *) Bus_to_Virtual(NextIncomingMailbox->CCB);
2467                 if (CompletionCode != BusLogic_AbortedCommandNotFound) {
2468                         if (CCB->Status == BusLogic_CCB_Active || CCB->Status == BusLogic_CCB_Reset) {
2469                                 /*
2470                                    Save the Completion Code for this CCB and queue the CCB
2471                                    for completion processing.
2472                                  */
2473                                 CCB->CompletionCode = CompletionCode;
2474                                 BusLogic_QueueCompletedCCB(CCB);
2475                         } else {
2476                                 /*
2477                                    If a CCB ever appears in an Incoming Mailbox and is not marked
2478                                    as status Active or Reset, then there is most likely a bug in
2479                                    the Host Adapter firmware.
2480                                  */
2481                                 BusLogic_Warning("Illegal CCB #%ld status %d in " "Incoming Mailbox\n", HostAdapter, CCB->SerialNumber, CCB->Status);
2482                         }
2483                 }
2484                 NextIncomingMailbox->CompletionCode = BusLogic_IncomingMailboxFree;
2485                 if (++NextIncomingMailbox > HostAdapter->LastIncomingMailbox)
2486                         NextIncomingMailbox = HostAdapter->FirstIncomingMailbox;
2487         }
2488         HostAdapter->NextIncomingMailbox = NextIncomingMailbox;
2489 }
2490
2491
2492 /*
2493   BusLogic_ProcessCompletedCCBs iterates over the completed CCBs for Host
2494   Adapter setting the SCSI Command Result Codes, deallocating the CCBs, and
2495   calling the SCSI Subsystem Completion Routines.  The Host Adapter's Lock
2496   should already have been acquired by the caller.
2497 */
2498
2499 static void BusLogic_ProcessCompletedCCBs(struct BusLogic_HostAdapter *HostAdapter)
2500 {
2501         if (HostAdapter->ProcessCompletedCCBsActive)
2502                 return;
2503         HostAdapter->ProcessCompletedCCBsActive = true;
2504         while (HostAdapter->FirstCompletedCCB != NULL) {
2505                 struct BusLogic_CCB *CCB = HostAdapter->FirstCompletedCCB;
2506                 struct scsi_cmnd *Command = CCB->Command;
2507                 HostAdapter->FirstCompletedCCB = CCB->Next;
2508                 if (HostAdapter->FirstCompletedCCB == NULL)
2509                         HostAdapter->LastCompletedCCB = NULL;
2510                 /*
2511                    Process the Completed CCB.
2512                  */
2513                 if (CCB->Opcode == BusLogic_BusDeviceReset) {
2514                         int TargetID = CCB->TargetID;
2515                         BusLogic_Warning("Bus Device Reset CCB #%ld to Target " "%d Completed\n", HostAdapter, CCB->SerialNumber, TargetID);
2516                         BusLogic_IncrementErrorCounter(&HostAdapter->TargetStatistics[TargetID].BusDeviceResetsCompleted);
2517                         HostAdapter->TargetFlags[TargetID].TaggedQueuingActive = false;
2518                         HostAdapter->CommandsSinceReset[TargetID] = 0;
2519                         HostAdapter->LastResetCompleted[TargetID] = jiffies;
2520                         /*
2521                            Place CCB back on the Host Adapter's free list.
2522                          */
2523                         BusLogic_DeallocateCCB(CCB);
2524 #if 0                           /* this needs to be redone different for new EH */
2525                         /*
2526                            Bus Device Reset CCBs have the Command field non-NULL only when a
2527                            Bus Device Reset was requested for a Command that did not have a
2528                            currently active CCB in the Host Adapter (i.e., a Synchronous
2529                            Bus Device Reset), and hence would not have its Completion Routine
2530                            called otherwise.
2531                          */
2532                         while (Command != NULL) {
2533                                 struct scsi_cmnd *NextCommand = Command->reset_chain;
2534                                 Command->reset_chain = NULL;
2535                                 Command->result = DID_RESET << 16;
2536                                 Command->scsi_done(Command);
2537                                 Command = NextCommand;
2538                         }
2539 #endif
2540                         /*
2541                            Iterate over the CCBs for this Host Adapter performing completion
2542                            processing for any CCBs marked as Reset for this Target.
2543                          */
2544                         for (CCB = HostAdapter->All_CCBs; CCB != NULL; CCB = CCB->NextAll)
2545                                 if (CCB->Status == BusLogic_CCB_Reset && CCB->TargetID == TargetID) {
2546                                         Command = CCB->Command;
2547                                         BusLogic_DeallocateCCB(CCB);
2548                                         HostAdapter->ActiveCommands[TargetID]--;
2549                                         Command->result = DID_RESET << 16;
2550                                         Command->scsi_done(Command);
2551                                 }
2552                         HostAdapter->BusDeviceResetPendingCCB[TargetID] = NULL;
2553                 } else {
2554                         /*
2555                            Translate the Completion Code, Host Adapter Status, and Target
2556                            Device Status into a SCSI Subsystem Result Code.
2557                          */
2558                         switch (CCB->CompletionCode) {
2559                         case BusLogic_IncomingMailboxFree:
2560                         case BusLogic_AbortedCommandNotFound:
2561                         case BusLogic_InvalidCCB:
2562                                 BusLogic_Warning("CCB #%ld to Target %d Impossible State\n", HostAdapter, CCB->SerialNumber, CCB->TargetID);
2563                                 break;
2564                         case BusLogic_CommandCompletedWithoutError:
2565                                 HostAdapter->TargetStatistics[CCB->TargetID]
2566                                     .CommandsCompleted++;
2567                                 HostAdapter->TargetFlags[CCB->TargetID]
2568                                     .CommandSuccessfulFlag = true;
2569                                 Command->result = DID_OK << 16;
2570                                 break;
2571                         case BusLogic_CommandAbortedAtHostRequest:
2572                                 BusLogic_Warning("CCB #%ld to Target %d Aborted\n", HostAdapter, CCB->SerialNumber, CCB->TargetID);
2573                                 BusLogic_IncrementErrorCounter(&HostAdapter->TargetStatistics[CCB->TargetID]
2574                                                                .CommandAbortsCompleted);
2575                                 Command->result = DID_ABORT << 16;
2576                                 break;
2577                         case BusLogic_CommandCompletedWithError:
2578                                 Command->result = BusLogic_ComputeResultCode(HostAdapter, CCB->HostAdapterStatus, CCB->TargetDeviceStatus);
2579                                 if (CCB->HostAdapterStatus != BusLogic_SCSISelectionTimeout) {
2580                                         HostAdapter->TargetStatistics[CCB->TargetID]
2581                                             .CommandsCompleted++;
2582                                         if (BusLogic_GlobalOptions.TraceErrors) {
2583                                                 int i;
2584                                                 BusLogic_Notice("CCB #%ld Target %d: Result %X Host "
2585                                                                 "Adapter Status %02X " "Target Status %02X\n", HostAdapter, CCB->SerialNumber, CCB->TargetID, Command->result, CCB->HostAdapterStatus, CCB->TargetDeviceStatus);
2586                                                 BusLogic_Notice("CDB   ", HostAdapter);
2587                                                 for (i = 0; i < CCB->CDB_Length; i++)
2588                                                         BusLogic_Notice(" %02X", HostAdapter, CCB->CDB[i]);
2589                                                 BusLogic_Notice("\n", HostAdapter);
2590                                                 BusLogic_Notice("Sense ", HostAdapter);
2591                                                 for (i = 0; i < CCB->SenseDataLength; i++)
2592                                                         BusLogic_Notice(" %02X", HostAdapter, Command->sense_buffer[i]);
2593                                                 BusLogic_Notice("\n", HostAdapter);
2594                                         }
2595                                 }
2596                                 break;
2597                         }
2598                         /*
2599                            When an INQUIRY command completes normally, save the
2600                            CmdQue (Tagged Queuing Supported) and WBus16 (16 Bit
2601                            Wide Data Transfers Supported) bits.
2602                          */
2603                         if (CCB->CDB[0] == INQUIRY && CCB->CDB[1] == 0 && CCB->HostAdapterStatus == BusLogic_CommandCompletedNormally) {
2604                                 struct BusLogic_TargetFlags *TargetFlags = &HostAdapter->TargetFlags[CCB->TargetID];
2605                                 struct SCSI_Inquiry *InquiryResult = (struct SCSI_Inquiry *) Command->request_buffer;
2606                                 TargetFlags->TargetExists = true;
2607                                 TargetFlags->TaggedQueuingSupported = InquiryResult->CmdQue;
2608                                 TargetFlags->WideTransfersSupported = InquiryResult->WBus16;
2609                         }
2610                         /*
2611                            Place CCB back on the Host Adapter's free list.
2612                          */
2613                         BusLogic_DeallocateCCB(CCB);
2614                         /*
2615                            Call the SCSI Command Completion Routine.
2616                          */
2617                         Command->scsi_done(Command);
2618                 }
2619         }
2620         HostAdapter->ProcessCompletedCCBsActive = false;
2621 }
2622
2623
2624 /*
2625   BusLogic_InterruptHandler handles hardware interrupts from BusLogic Host
2626   Adapters.
2627 */
2628
2629 static irqreturn_t BusLogic_InterruptHandler(int IRQ_Channel, void *DeviceIdentifier, struct pt_regs *InterruptRegisters)
2630 {
2631         struct BusLogic_HostAdapter *HostAdapter = (struct BusLogic_HostAdapter *) DeviceIdentifier;
2632         unsigned long ProcessorFlags;
2633         /*
2634            Acquire exclusive access to Host Adapter.
2635          */
2636         spin_lock_irqsave(HostAdapter->SCSI_Host->host_lock, ProcessorFlags);
2637         /*
2638            Handle Interrupts appropriately for each Host Adapter type.
2639          */
2640         if (BusLogic_MultiMasterHostAdapterP(HostAdapter)) {
2641                 union BusLogic_InterruptRegister InterruptRegister;
2642                 /*
2643                    Read the Host Adapter Interrupt Register.
2644                  */
2645                 InterruptRegister.All = BusLogic_ReadInterruptRegister(HostAdapter);
2646                 if (InterruptRegister.ir.InterruptValid) {
2647                         /*
2648                            Acknowledge the interrupt and reset the Host Adapter
2649                            Interrupt Register.
2650                          */
2651                         BusLogic_InterruptReset(HostAdapter);
2652                         /*
2653                            Process valid External SCSI Bus Reset and Incoming Mailbox
2654                            Loaded Interrupts.  Command Complete Interrupts are noted,
2655                            and Outgoing Mailbox Available Interrupts are ignored, as
2656                            they are never enabled.
2657                          */
2658                         if (InterruptRegister.ir.ExternalBusReset)
2659                                 HostAdapter->HostAdapterExternalReset = true;
2660                         else if (InterruptRegister.ir.IncomingMailboxLoaded)
2661                                 BusLogic_ScanIncomingMailboxes(HostAdapter);
2662                         else if (InterruptRegister.ir.CommandComplete)
2663                                 HostAdapter->HostAdapterCommandCompleted = true;
2664                 }
2665         } else {
2666                 /*
2667                    Check if there is a pending interrupt for this Host Adapter.
2668                  */
2669                 if (FlashPoint_InterruptPending(HostAdapter->CardHandle))
2670                         switch (FlashPoint_HandleInterrupt(HostAdapter->CardHandle)) {
2671                         case FlashPoint_NormalInterrupt:
2672                                 break;
2673                         case FlashPoint_ExternalBusReset:
2674                                 HostAdapter->HostAdapterExternalReset = true;
2675                                 break;
2676                         case FlashPoint_InternalError:
2677                                 BusLogic_Warning("Internal FlashPoint Error detected" " - Resetting Host Adapter\n", HostAdapter);
2678                                 HostAdapter->HostAdapterInternalError = true;
2679                                 break;
2680                         }
2681         }
2682         /*
2683            Process any completed CCBs.
2684          */
2685         if (HostAdapter->FirstCompletedCCB != NULL)
2686                 BusLogic_ProcessCompletedCCBs(HostAdapter);
2687         /*
2688            Reset the Host Adapter if requested.
2689          */
2690         if (HostAdapter->HostAdapterExternalReset) {
2691                 BusLogic_Warning("Resetting %s due to External SCSI Bus Reset\n", HostAdapter, HostAdapter->FullModelName);
2692                 BusLogic_IncrementErrorCounter(&HostAdapter->ExternalHostAdapterResets);
2693                 BusLogic_ResetHostAdapter(HostAdapter, false);
2694                 HostAdapter->HostAdapterExternalReset = false;
2695         } else if (HostAdapter->HostAdapterInternalError) {
2696                 BusLogic_Warning("Resetting %s due to Host Adapter Internal Error\n", HostAdapter, HostAdapter->FullModelName);
2697                 BusLogic_IncrementErrorCounter(&HostAdapter->HostAdapterInternalErrors);
2698                 BusLogic_ResetHostAdapter(HostAdapter, true);
2699                 HostAdapter->HostAdapterInternalError = false;
2700         }
2701         /*
2702            Release exclusive access to Host Adapter.
2703          */
2704         spin_unlock_irqrestore(HostAdapter->SCSI_Host->host_lock, ProcessorFlags);
2705         return IRQ_HANDLED;
2706 }
2707
2708
2709 /*
2710   BusLogic_WriteOutgoingMailbox places CCB and Action Code into an Outgoing
2711   Mailbox for execution by Host Adapter.  The Host Adapter's Lock should
2712   already have been acquired by the caller.
2713 */
2714
2715 static boolean BusLogic_WriteOutgoingMailbox(struct BusLogic_HostAdapter
2716                                              *HostAdapter, enum BusLogic_ActionCode ActionCode, struct BusLogic_CCB *CCB)
2717 {
2718         struct BusLogic_OutgoingMailbox *NextOutgoingMailbox;
2719         NextOutgoingMailbox = HostAdapter->NextOutgoingMailbox;
2720         if (NextOutgoingMailbox->ActionCode == BusLogic_OutgoingMailboxFree) {
2721                 CCB->Status = BusLogic_CCB_Active;
2722                 /*
2723                    The CCB field must be written before the Action Code field since
2724                    the Host Adapter is operating asynchronously and the locking code
2725                    does not protect against simultaneous access by the Host Adapter.
2726                  */
2727                 NextOutgoingMailbox->CCB = CCB->DMA_Handle;
2728                 NextOutgoingMailbox->ActionCode = ActionCode;
2729                 BusLogic_StartMailboxCommand(HostAdapter);
2730                 if (++NextOutgoingMailbox > HostAdapter->LastOutgoingMailbox)
2731                         NextOutgoingMailbox = HostAdapter->FirstOutgoingMailbox;
2732                 HostAdapter->NextOutgoingMailbox = NextOutgoingMailbox;
2733                 if (ActionCode == BusLogic_MailboxStartCommand) {
2734                         HostAdapter->ActiveCommands[CCB->TargetID]++;
2735                         if (CCB->Opcode != BusLogic_BusDeviceReset)
2736                                 HostAdapter->TargetStatistics[CCB->TargetID].CommandsAttempted++;
2737                 }
2738                 return true;
2739         }
2740         return false;
2741 }
2742
2743 /* Error Handling (EH) support */
2744
2745 static int BusLogic_host_reset(struct scsi_cmnd * SCpnt)
2746 {
2747         struct BusLogic_HostAdapter *HostAdapter = (struct BusLogic_HostAdapter *) SCpnt->device->host->hostdata;
2748
2749         unsigned int id = SCpnt->device->id;
2750         struct BusLogic_TargetStatistics *stats = &HostAdapter->TargetStatistics[id];
2751         int rc;
2752
2753         spin_lock_irq(SCpnt->device->host->host_lock);
2754
2755         BusLogic_IncrementErrorCounter(&stats->HostAdapterResetsRequested);
2756
2757         rc = BusLogic_ResetHostAdapter(HostAdapter, false);
2758         spin_unlock_irq(SCpnt->device->host->host_lock);
2759         return rc;
2760 }
2761
2762 /*
2763   BusLogic_QueueCommand creates a CCB for Command and places it into an
2764   Outgoing Mailbox for execution by the associated Host Adapter.
2765 */
2766
2767 static int BusLogic_QueueCommand(struct scsi_cmnd *Command, void (*CompletionRoutine) (struct scsi_cmnd *))
2768 {
2769         struct BusLogic_HostAdapter *HostAdapter = (struct BusLogic_HostAdapter *) Command->device->host->hostdata;
2770         struct BusLogic_TargetFlags *TargetFlags = &HostAdapter->TargetFlags[Command->device->id];
2771         struct BusLogic_TargetStatistics *TargetStatistics = HostAdapter->TargetStatistics;
2772         unsigned char *CDB = Command->cmnd;
2773         int CDB_Length = Command->cmd_len;
2774         int TargetID = Command->device->id;
2775         int LogicalUnit = Command->device->lun;
2776         void *BufferPointer = Command->request_buffer;
2777         int BufferLength = Command->request_bufflen;
2778         int SegmentCount = Command->use_sg;
2779         struct BusLogic_CCB *CCB;
2780         /*
2781            SCSI REQUEST_SENSE commands will be executed automatically by the Host
2782            Adapter for any errors, so they should not be executed explicitly unless
2783            the Sense Data is zero indicating that no error occurred.
2784          */
2785         if (CDB[0] == REQUEST_SENSE && Command->sense_buffer[0] != 0) {
2786                 Command->result = DID_OK << 16;
2787                 CompletionRoutine(Command);
2788                 return 0;
2789         }
2790         /*
2791            Allocate a CCB from the Host Adapter's free list.  In the unlikely event
2792            that there are none available and memory allocation fails, wait 1 second
2793            and try again.  If that fails, the Host Adapter is probably hung so signal
2794            an error as a Host Adapter Hard Reset should be initiated soon.
2795          */
2796         CCB = BusLogic_AllocateCCB(HostAdapter);
2797         if (CCB == NULL) {
2798                 spin_unlock_irq(HostAdapter->SCSI_Host->host_lock);
2799                 BusLogic_Delay(1);
2800                 spin_lock_irq(HostAdapter->SCSI_Host->host_lock);
2801                 CCB = BusLogic_AllocateCCB(HostAdapter);
2802                 if (CCB == NULL) {
2803                         Command->result = DID_ERROR << 16;
2804                         CompletionRoutine(Command);
2805                         return 0;
2806                 }
2807         }
2808         /*
2809            Initialize the fields in the BusLogic Command Control Block (CCB).
2810          */
2811         if (SegmentCount == 0 && BufferLength != 0) {
2812                 CCB->Opcode = BusLogic_InitiatorCCB;
2813                 CCB->DataLength = BufferLength;
2814                 CCB->DataPointer = pci_map_single(HostAdapter->PCI_Device,
2815                                 BufferPointer, BufferLength,
2816                                 Command->sc_data_direction);
2817         } else if (SegmentCount != 0) {
2818                 struct scatterlist *ScatterList = (struct scatterlist *) BufferPointer;
2819                 int Segment, Count;
2820
2821                 Count = pci_map_sg(HostAdapter->PCI_Device, ScatterList, SegmentCount,
2822                                 Command->sc_data_direction);
2823                 CCB->Opcode = BusLogic_InitiatorCCB_ScatterGather;
2824                 CCB->DataLength = Count * sizeof(struct BusLogic_ScatterGatherSegment);
2825                 if (BusLogic_MultiMasterHostAdapterP(HostAdapter))
2826                         CCB->DataPointer = (unsigned int) CCB->DMA_Handle + ((unsigned long) &CCB->ScatterGatherList - (unsigned long) CCB);
2827                 else
2828                         CCB->DataPointer = Virtual_to_32Bit_Virtual(CCB->ScatterGatherList);
2829                 for (Segment = 0; Segment < Count; Segment++) {
2830                         CCB->ScatterGatherList[Segment].SegmentByteCount = sg_dma_len(ScatterList + Segment);
2831                         CCB->ScatterGatherList[Segment].SegmentDataPointer = sg_dma_address(ScatterList + Segment);
2832                 }
2833         } else {
2834                 CCB->Opcode = BusLogic_InitiatorCCB;
2835                 CCB->DataLength = BufferLength;
2836                 CCB->DataPointer = 0;
2837         }
2838         switch (CDB[0]) {
2839         case READ_6:
2840         case READ_10:
2841                 CCB->DataDirection = BusLogic_DataInLengthChecked;
2842                 TargetStatistics[TargetID].ReadCommands++;
2843                 BusLogic_IncrementByteCounter(&TargetStatistics[TargetID].TotalBytesRead, BufferLength);
2844                 BusLogic_IncrementSizeBucket(TargetStatistics[TargetID].ReadCommandSizeBuckets, BufferLength);
2845                 break;
2846         case WRITE_6:
2847         case WRITE_10:
2848                 CCB->DataDirection = BusLogic_DataOutLengthChecked;
2849                 TargetStatistics[TargetID].WriteCommands++;
2850                 BusLogic_IncrementByteCounter(&TargetStatistics[TargetID].TotalBytesWritten, BufferLength);
2851                 BusLogic_IncrementSizeBucket(TargetStatistics[TargetID].WriteCommandSizeBuckets, BufferLength);
2852                 break;
2853         default:
2854                 CCB->DataDirection = BusLogic_UncheckedDataTransfer;
2855                 break;
2856         }
2857         CCB->CDB_Length = CDB_Length;
2858         CCB->HostAdapterStatus = 0;
2859         CCB->TargetDeviceStatus = 0;
2860         CCB->TargetID = TargetID;
2861         CCB->LogicalUnit = LogicalUnit;
2862         CCB->TagEnable = false;
2863         CCB->LegacyTagEnable = false;
2864         /*
2865            BusLogic recommends that after a Reset the first couple of commands that
2866            are sent to a Target Device be sent in a non Tagged Queue fashion so that
2867            the Host Adapter and Target Device can establish Synchronous and Wide
2868            Transfer before Queue Tag messages can interfere with the Synchronous and
2869            Wide Negotiation messages.  By waiting to enable Tagged Queuing until after
2870            the first BusLogic_MaxTaggedQueueDepth commands have been queued, it is
2871            assured that after a Reset any pending commands are requeued before Tagged
2872            Queuing is enabled and that the Tagged Queuing message will not occur while
2873            the partition table is being printed.  In addition, some devices do not
2874            properly handle the transition from non-tagged to tagged commands, so it is
2875            necessary to wait until there are no pending commands for a target device
2876            before queuing tagged commands.
2877          */
2878         if (HostAdapter->CommandsSinceReset[TargetID]++ >=
2879             BusLogic_MaxTaggedQueueDepth && !TargetFlags->TaggedQueuingActive && HostAdapter->ActiveCommands[TargetID] == 0 && TargetFlags->TaggedQueuingSupported && (HostAdapter->TaggedQueuingPermitted & (1 << TargetID))) {
2880                 TargetFlags->TaggedQueuingActive = true;
2881                 BusLogic_Notice("Tagged Queuing now active for Target %d\n", HostAdapter, TargetID);
2882         }
2883         if (TargetFlags->TaggedQueuingActive) {
2884                 enum BusLogic_QueueTag QueueTag = BusLogic_SimpleQueueTag;
2885                 /*
2886                    When using Tagged Queuing with Simple Queue Tags, it appears that disk
2887                    drive controllers do not guarantee that a queued command will not
2888                    remain in a disconnected state indefinitely if commands that read or
2889                    write nearer the head position continue to arrive without interruption.
2890                    Therefore, for each Target Device this driver keeps track of the last
2891                    time either the queue was empty or an Ordered Queue Tag was issued.  If
2892                    more than 4 seconds (one fifth of the 20 second disk timeout) have
2893                    elapsed since this last sequence point, this command will be issued
2894                    with an Ordered Queue Tag rather than a Simple Queue Tag, which forces
2895                    the Target Device to complete all previously queued commands before
2896                    this command may be executed.
2897                  */
2898                 if (HostAdapter->ActiveCommands[TargetID] == 0)
2899                         HostAdapter->LastSequencePoint[TargetID] = jiffies;
2900                 else if (time_after(jiffies, HostAdapter->LastSequencePoint[TargetID] + 4 * HZ)) {
2901                         HostAdapter->LastSequencePoint[TargetID] = jiffies;
2902                         QueueTag = BusLogic_OrderedQueueTag;
2903                 }
2904                 if (HostAdapter->ExtendedLUNSupport) {
2905                         CCB->TagEnable = true;
2906                         CCB->QueueTag = QueueTag;
2907                 } else {
2908                         CCB->LegacyTagEnable = true;
2909                         CCB->LegacyQueueTag = QueueTag;
2910                 }
2911         }
2912         memcpy(CCB->CDB, CDB, CDB_Length);
2913         CCB->SenseDataLength = sizeof(Command->sense_buffer);
2914         CCB->SenseDataPointer = pci_map_single(HostAdapter->PCI_Device, Command->sense_buffer, CCB->SenseDataLength, PCI_DMA_FROMDEVICE);
2915         CCB->Command = Command;
2916         Command->scsi_done = CompletionRoutine;
2917         if (BusLogic_MultiMasterHostAdapterP(HostAdapter)) {
2918                 /*
2919                    Place the CCB in an Outgoing Mailbox.  The higher levels of the SCSI
2920                    Subsystem should not attempt to queue more commands than can be placed
2921                    in Outgoing Mailboxes, so there should always be one free.  In the
2922                    unlikely event that there are none available, wait 1 second and try
2923                    again.  If that fails, the Host Adapter is probably hung so signal an
2924                    error as a Host Adapter Hard Reset should be initiated soon.
2925                  */
2926                 if (!BusLogic_WriteOutgoingMailbox(HostAdapter, BusLogic_MailboxStartCommand, CCB)) {
2927                         spin_unlock_irq(HostAdapter->SCSI_Host->host_lock);
2928                         BusLogic_Warning("Unable to write Outgoing Mailbox - " "Pausing for 1 second\n", HostAdapter);
2929                         BusLogic_Delay(1);
2930                         spin_lock_irq(HostAdapter->SCSI_Host->host_lock);
2931                         if (!BusLogic_WriteOutgoingMailbox(HostAdapter, BusLogic_MailboxStartCommand, CCB)) {
2932                                 BusLogic_Warning("Still unable to write Outgoing Mailbox - " "Host Adapter Dead?\n", HostAdapter);
2933                                 BusLogic_DeallocateCCB(CCB);
2934                                 Command->result = DID_ERROR << 16;
2935                                 Command->scsi_done(Command);
2936                         }
2937                 }
2938         } else {
2939                 /*
2940                    Call the FlashPoint SCCB Manager to start execution of the CCB.
2941                  */
2942                 CCB->Status = BusLogic_CCB_Active;
2943                 HostAdapter->ActiveCommands[TargetID]++;
2944                 TargetStatistics[TargetID].CommandsAttempted++;
2945                 FlashPoint_StartCCB(HostAdapter->CardHandle, CCB);
2946                 /*
2947                    The Command may have already completed and BusLogic_QueueCompletedCCB
2948                    been called, or it may still be pending.
2949                  */
2950                 if (CCB->Status == BusLogic_CCB_Completed)
2951                         BusLogic_ProcessCompletedCCBs(HostAdapter);
2952         }
2953         return 0;
2954 }
2955
2956
2957 /*
2958   BusLogic_AbortCommand aborts Command if possible.
2959 */
2960
2961 static int BusLogic_AbortCommand(struct scsi_cmnd *Command)
2962 {
2963         struct BusLogic_HostAdapter *HostAdapter = (struct BusLogic_HostAdapter *) Command->device->host->hostdata;
2964
2965         int TargetID = Command->device->id;
2966         struct BusLogic_CCB *CCB;
2967         BusLogic_IncrementErrorCounter(&HostAdapter->TargetStatistics[TargetID].CommandAbortsRequested);
2968         /*
2969            Attempt to find an Active CCB for this Command.  If no Active CCB for this
2970            Command is found, then no Abort is necessary.
2971          */
2972         for (CCB = HostAdapter->All_CCBs; CCB != NULL; CCB = CCB->NextAll)
2973                 if (CCB->Command == Command)
2974                         break;
2975         if (CCB == NULL) {
2976                 BusLogic_Warning("Unable to Abort Command to Target %d - " "No CCB Found\n", HostAdapter, TargetID);
2977                 return SUCCESS;
2978         } else if (CCB->Status == BusLogic_CCB_Completed) {
2979                 BusLogic_Warning("Unable to Abort Command to Target %d - " "CCB Completed\n", HostAdapter, TargetID);
2980                 return SUCCESS;
2981         } else if (CCB->Status == BusLogic_CCB_Reset) {
2982                 BusLogic_Warning("Unable to Abort Command to Target %d - " "CCB Reset\n", HostAdapter, TargetID);
2983                 return SUCCESS;
2984         }
2985         if (BusLogic_MultiMasterHostAdapterP(HostAdapter)) {
2986                 /*
2987                    Attempt to Abort this CCB.  MultiMaster Firmware versions prior to 5.xx
2988                    do not generate Abort Tag messages, but only generate the non-tagged
2989                    Abort message.  Since non-tagged commands are not sent by the Host
2990                    Adapter until the queue of outstanding tagged commands has completed,
2991                    and the Abort message is treated as a non-tagged command, it is
2992                    effectively impossible to abort commands when Tagged Queuing is active.
2993                    Firmware version 5.xx does generate Abort Tag messages, so it is
2994                    possible to abort commands when Tagged Queuing is active.
2995                  */
2996                 if (HostAdapter->TargetFlags[TargetID].TaggedQueuingActive && HostAdapter->FirmwareVersion[0] < '5') {
2997                         BusLogic_Warning("Unable to Abort CCB #%ld to Target %d - " "Abort Tag Not Supported\n", HostAdapter, CCB->SerialNumber, TargetID);
2998                         return FAILURE;
2999                 } else if (BusLogic_WriteOutgoingMailbox(HostAdapter, BusLogic_MailboxAbortCommand, CCB)) {
3000                         BusLogic_Warning("Aborting CCB #%ld to Target %d\n", HostAdapter, CCB->SerialNumber, TargetID);
3001                         BusLogic_IncrementErrorCounter(&HostAdapter->TargetStatistics[TargetID].CommandAbortsAttempted);
3002                         return SUCCESS;
3003                 } else {
3004                         BusLogic_Warning("Unable to Abort CCB #%ld to Target %d - " "No Outgoing Mailboxes\n", HostAdapter, CCB->SerialNumber, TargetID);
3005                         return FAILURE;
3006                 }
3007         } else {
3008                 /*
3009                    Call the FlashPoint SCCB Manager to abort execution of the CCB.
3010                  */
3011                 BusLogic_Warning("Aborting CCB #%ld to Target %d\n", HostAdapter, CCB->SerialNumber, TargetID);
3012                 BusLogic_IncrementErrorCounter(&HostAdapter->TargetStatistics[TargetID].CommandAbortsAttempted);
3013                 FlashPoint_AbortCCB(HostAdapter->CardHandle, CCB);
3014                 /*
3015                    The Abort may have already been completed and
3016                    BusLogic_QueueCompletedCCB been called, or it
3017                    may still be pending.
3018                  */
3019                 if (CCB->Status == BusLogic_CCB_Completed) {
3020                         BusLogic_ProcessCompletedCCBs(HostAdapter);
3021                 }
3022                 return SUCCESS;
3023         }
3024         return SUCCESS;
3025 }
3026
3027 /*
3028   BusLogic_ResetHostAdapter resets Host Adapter if possible, marking all
3029   currently executing SCSI Commands as having been Reset.
3030 */
3031
3032 static int BusLogic_ResetHostAdapter(struct BusLogic_HostAdapter *HostAdapter, boolean HardReset)
3033 {
3034         struct BusLogic_CCB *CCB;
3035         int TargetID;
3036
3037         /*
3038          * Attempt to Reset and Reinitialize the Host Adapter.
3039          */
3040
3041         if (!(BusLogic_HardwareResetHostAdapter(HostAdapter, HardReset) && BusLogic_InitializeHostAdapter(HostAdapter))) {
3042                 BusLogic_Error("Resetting %s Failed\n", HostAdapter, HostAdapter->FullModelName);
3043                 return FAILURE;
3044         }
3045
3046         /*
3047          * Deallocate all currently executing CCBs.
3048          */
3049
3050         for (CCB = HostAdapter->All_CCBs; CCB != NULL; CCB = CCB->NextAll)
3051                 if (CCB->Status == BusLogic_CCB_Active)
3052                         BusLogic_DeallocateCCB(CCB);
3053         /*
3054          * Wait a few seconds between the Host Adapter Hard Reset which
3055          * initiates a SCSI Bus Reset and issuing any SCSI Commands.  Some
3056          * SCSI devices get confused if they receive SCSI Commands too soon
3057          * after a SCSI Bus Reset.
3058          */
3059
3060         if (HardReset) {
3061                 spin_unlock_irq(HostAdapter->SCSI_Host->host_lock);
3062                 BusLogic_Delay(HostAdapter->BusSettleTime);
3063                 spin_lock_irq(HostAdapter->SCSI_Host->host_lock);
3064         }
3065
3066         for (TargetID = 0; TargetID < HostAdapter->MaxTargetDevices; TargetID++) {
3067                 HostAdapter->LastResetAttempted[TargetID] = jiffies;
3068                 HostAdapter->LastResetCompleted[TargetID] = jiffies;
3069         }
3070         return SUCCESS;
3071 }
3072
3073 /*
3074   BusLogic_BIOSDiskParameters returns the Heads/Sectors/Cylinders BIOS Disk
3075   Parameters for Disk.  The default disk geometry is 64 heads, 32 sectors, and
3076   the appropriate number of cylinders so as not to exceed drive capacity.  In
3077   order for disks equal to or larger than 1 GB to be addressable by the BIOS
3078   without exceeding the BIOS limitation of 1024 cylinders, Extended Translation
3079   may be enabled in AutoSCSI on FlashPoint Host Adapters and on "W" and "C"
3080   series MultiMaster Host Adapters, or by a dip switch setting on "S" and "A"
3081   series MultiMaster Host Adapters.  With Extended Translation enabled, drives
3082   between 1 GB inclusive and 2 GB exclusive are given a disk geometry of 128
3083   heads and 32 sectors, and drives above 2 GB inclusive are given a disk
3084   geometry of 255 heads and 63 sectors.  However, if the BIOS detects that the
3085   Extended Translation setting does not match the geometry in the partition
3086   table, then the translation inferred from the partition table will be used by
3087   the BIOS, and a warning may be displayed.
3088 */
3089
3090 static int BusLogic_BIOSDiskParameters(struct scsi_device *sdev, struct block_device *Device, sector_t capacity, int *Parameters)
3091 {
3092         struct BusLogic_HostAdapter *HostAdapter = (struct BusLogic_HostAdapter *) sdev->host->hostdata;
3093         struct BIOS_DiskParameters *DiskParameters = (struct BIOS_DiskParameters *) Parameters;
3094         unsigned char *buf;
3095         if (HostAdapter->ExtendedTranslationEnabled && capacity >= 2 * 1024 * 1024 /* 1 GB in 512 byte sectors */ ) {
3096                 if (capacity >= 4 * 1024 * 1024 /* 2 GB in 512 byte sectors */ ) {
3097                         DiskParameters->Heads = 255;
3098                         DiskParameters->Sectors = 63;
3099                 } else {
3100                         DiskParameters->Heads = 128;
3101                         DiskParameters->Sectors = 32;
3102                 }
3103         } else {
3104                 DiskParameters->Heads = 64;
3105                 DiskParameters->Sectors = 32;
3106         }
3107         DiskParameters->Cylinders = (unsigned long) capacity / (DiskParameters->Heads * DiskParameters->Sectors);
3108         buf = scsi_bios_ptable(Device);
3109         if (buf == NULL)
3110                 return 0;
3111         /*
3112            If the boot sector partition table flag is valid, search for a partition
3113            table entry whose end_head matches one of the standard BusLogic geometry
3114            translations (64/32, 128/32, or 255/63).
3115          */
3116         if (*(unsigned short *) (buf + 64) == 0xAA55) {
3117                 struct partition *FirstPartitionEntry = (struct partition *) buf;
3118                 struct partition *PartitionEntry = FirstPartitionEntry;
3119                 int SavedCylinders = DiskParameters->Cylinders, PartitionNumber;
3120                 unsigned char PartitionEntryEndHead = 0, PartitionEntryEndSector = 0;
3121                 for (PartitionNumber = 0; PartitionNumber < 4; PartitionNumber++) {
3122                         PartitionEntryEndHead = PartitionEntry->end_head;
3123                         PartitionEntryEndSector = PartitionEntry->end_sector & 0x3F;
3124                         if (PartitionEntryEndHead == 64 - 1) {
3125                                 DiskParameters->Heads = 64;
3126                                 DiskParameters->Sectors = 32;
3127                                 break;
3128                         } else if (PartitionEntryEndHead == 128 - 1) {
3129                                 DiskParameters->Heads = 128;
3130                                 DiskParameters->Sectors = 32;
3131                                 break;
3132                         } else if (PartitionEntryEndHead == 255 - 1) {
3133                                 DiskParameters->Heads = 255;
3134                                 DiskParameters->Sectors = 63;
3135                                 break;
3136                         }
3137                         PartitionEntry++;
3138                 }
3139                 if (PartitionNumber == 4) {
3140                         PartitionEntryEndHead = FirstPartitionEntry->end_head;
3141                         PartitionEntryEndSector = FirstPartitionEntry->end_sector & 0x3F;
3142                 }
3143                 DiskParameters->Cylinders = (unsigned long) capacity / (DiskParameters->Heads * DiskParameters->Sectors);
3144                 if (PartitionNumber < 4 && PartitionEntryEndSector == DiskParameters->Sectors) {
3145                         if (DiskParameters->Cylinders != SavedCylinders)
3146                                 BusLogic_Warning("Adopting Geometry %d/%d from Partition Table\n", HostAdapter, DiskParameters->Heads, DiskParameters->Sectors);
3147                 } else if (PartitionEntryEndHead > 0 || PartitionEntryEndSector > 0) {
3148                         BusLogic_Warning("Warning: Partition Table appears to " "have Geometry %d/%d which is\n", HostAdapter, PartitionEntryEndHead + 1, PartitionEntryEndSector);
3149                         BusLogic_Warning("not compatible with current BusLogic " "Host Adapter Geometry %d/%d\n", HostAdapter, DiskParameters->Heads, DiskParameters->Sectors);
3150                 }
3151         }
3152         kfree(buf);
3153         return 0;
3154 }
3155
3156
3157 /*
3158   BugLogic_ProcDirectoryInfo implements /proc/scsi/BusLogic/<N>.
3159 */
3160
3161 static int BusLogic_ProcDirectoryInfo(struct Scsi_Host *shost, char *ProcBuffer, char **StartPointer, off_t Offset, int BytesAvailable, int WriteFlag)
3162 {
3163         struct BusLogic_HostAdapter *HostAdapter = (struct BusLogic_HostAdapter *) shost->hostdata;
3164         struct BusLogic_TargetStatistics *TargetStatistics;
3165         int TargetID, Length;
3166         char *Buffer;
3167
3168         TargetStatistics = HostAdapter->TargetStatistics;
3169         if (WriteFlag) {
3170                 HostAdapter->ExternalHostAdapterResets = 0;
3171                 HostAdapter->HostAdapterInternalErrors = 0;
3172                 memset(TargetStatistics, 0, BusLogic_MaxTargetDevices * sizeof(struct BusLogic_TargetStatistics));
3173                 return 0;
3174         }
3175         Buffer = HostAdapter->MessageBuffer;
3176         Length = HostAdapter->MessageBufferLength;
3177         Length += sprintf(&Buffer[Length], "\n\
3178 Current Driver Queue Depth:     %d\n\
3179 Currently Allocated CCBs:       %d\n", HostAdapter->DriverQueueDepth, HostAdapter->AllocatedCCBs);
3180         Length += sprintf(&Buffer[Length], "\n\n\
3181                            DATA TRANSFER STATISTICS\n\
3182 \n\
3183 Target  Tagged Queuing  Queue Depth  Active  Attempted  Completed\n\
3184 ======  ==============  ===========  ======  =========  =========\n");
3185         for (TargetID = 0; TargetID < HostAdapter->MaxTargetDevices; TargetID++) {
3186                 struct BusLogic_TargetFlags *TargetFlags = &HostAdapter->TargetFlags[TargetID];
3187                 if (!TargetFlags->TargetExists)
3188                         continue;
3189                 Length += sprintf(&Buffer[Length], "  %2d       %s", TargetID, (TargetFlags->TaggedQueuingSupported ? (TargetFlags->TaggedQueuingActive ? "    Active" : (HostAdapter->TaggedQueuingPermitted & (1 << TargetID)
3190                                                                                                                                                                     ? "  Permitted" : "   Disabled"))
3191                                                                           : "Not Supported"));
3192                 Length += sprintf(&Buffer[Length],
3193                                   "         %3d       %3u    %9u        %9u\n", HostAdapter->QueueDepth[TargetID], HostAdapter->ActiveCommands[TargetID], TargetStatistics[TargetID].CommandsAttempted, TargetStatistics[TargetID].CommandsCompleted);
3194         }
3195         Length += sprintf(&Buffer[Length], "\n\
3196 Target  Read Commands  Write Commands   Total Bytes Read    Total Bytes Written\n\
3197 ======  =============  ==============  ===================  ===================\n");
3198         for (TargetID = 0; TargetID < HostAdapter->MaxTargetDevices; TargetID++) {
3199                 struct BusLogic_TargetFlags *TargetFlags = &HostAdapter->TargetFlags[TargetID];
3200                 if (!TargetFlags->TargetExists)
3201                         continue;
3202                 Length += sprintf(&Buffer[Length], "  %2d         %9u    %9u", TargetID, TargetStatistics[TargetID].ReadCommands, TargetStatistics[TargetID].WriteCommands);
3203                 if (TargetStatistics[TargetID].TotalBytesRead.Billions > 0)
3204                         Length += sprintf(&Buffer[Length], "     %9u%09u", TargetStatistics[TargetID].TotalBytesRead.Billions, TargetStatistics[TargetID].TotalBytesRead.Units);
3205                 else
3206                         Length += sprintf(&Buffer[Length], "            %9u", TargetStatistics[TargetID].TotalBytesRead.Units);
3207                 if (TargetStatistics[TargetID].TotalBytesWritten.Billions > 0)
3208                         Length += sprintf(&Buffer[Length], "   %9u%09u\n", TargetStatistics[TargetID].TotalBytesWritten.Billions, TargetStatistics[TargetID].TotalBytesWritten.Units);
3209                 else
3210                         Length += sprintf(&Buffer[Length], "         %9u\n", TargetStatistics[TargetID].TotalBytesWritten.Units);
3211         }
3212         Length += sprintf(&Buffer[Length], "\n\
3213 Target  Command    0-1KB      1-2KB      2-4KB      4-8KB     8-16KB\n\
3214 ======  =======  =========  =========  =========  =========  =========\n");
3215         for (TargetID = 0; TargetID < HostAdapter->MaxTargetDevices; TargetID++) {
3216                 struct BusLogic_TargetFlags *TargetFlags = &HostAdapter->TargetFlags[TargetID];
3217                 if (!TargetFlags->TargetExists)
3218                         continue;
3219                 Length +=
3220                     sprintf(&Buffer[Length],
3221                             "  %2d       Read    %9u  %9u  %9u  %9u  %9u\n", TargetID,
3222                             TargetStatistics[TargetID].ReadCommandSizeBuckets[0],
3223                             TargetStatistics[TargetID].ReadCommandSizeBuckets[1], TargetStatistics[TargetID].ReadCommandSizeBuckets[2], TargetStatistics[TargetID].ReadCommandSizeBuckets[3], TargetStatistics[TargetID].ReadCommandSizeBuckets[4]);
3224                 Length +=
3225                     sprintf(&Buffer[Length],
3226                             "  %2d       Write   %9u  %9u  %9u  %9u  %9u\n", TargetID,
3227                             TargetStatistics[TargetID].WriteCommandSizeBuckets[0],
3228                             TargetStatistics[TargetID].WriteCommandSizeBuckets[1], TargetStatistics[TargetID].WriteCommandSizeBuckets[2], TargetStatistics[TargetID].WriteCommandSizeBuckets[3], TargetStatistics[TargetID].WriteCommandSizeBuckets[4]);
3229         }
3230         Length += sprintf(&Buffer[Length], "\n\
3231 Target  Command   16-32KB    32-64KB   64-128KB   128-256KB   256KB+\n\
3232 ======  =======  =========  =========  =========  =========  =========\n");
3233         for (TargetID = 0; TargetID < HostAdapter->MaxTargetDevices; TargetID++) {
3234                 struct BusLogic_TargetFlags *TargetFlags = &HostAdapter->TargetFlags[TargetID];
3235                 if (!TargetFlags->TargetExists)
3236                         continue;
3237                 Length +=
3238                     sprintf(&Buffer[Length],
3239                             "  %2d       Read    %9u  %9u  %9u  %9u  %9u\n", TargetID,
3240                             TargetStatistics[TargetID].ReadCommandSizeBuckets[5],
3241                             TargetStatistics[TargetID].ReadCommandSizeBuckets[6], TargetStatistics[TargetID].ReadCommandSizeBuckets[7], TargetStatistics[TargetID].ReadCommandSizeBuckets[8], TargetStatistics[TargetID].ReadCommandSizeBuckets[9]);
3242                 Length +=
3243                     sprintf(&Buffer[Length],
3244                             "  %2d       Write   %9u  %9u  %9u  %9u  %9u\n", TargetID,
3245                             TargetStatistics[TargetID].WriteCommandSizeBuckets[5],
3246                             TargetStatistics[TargetID].WriteCommandSizeBuckets[6], TargetStatistics[TargetID].WriteCommandSizeBuckets[7], TargetStatistics[TargetID].WriteCommandSizeBuckets[8], TargetStatistics[TargetID].WriteCommandSizeBuckets[9]);
3247         }
3248         Length += sprintf(&Buffer[Length], "\n\n\
3249                            ERROR RECOVERY STATISTICS\n\
3250 \n\
3251           Command Aborts      Bus Device Resets   Host Adapter Resets\n\
3252 Target  Requested Completed  Requested Completed  Requested Completed\n\
3253   ID    \\\\\\\\ Attempted ////  \\\\\\\\ Attempted ////  \\\\\\\\ Attempted ////\n\
3254 ======   ===== ===== =====    ===== ===== =====    ===== ===== =====\n");
3255         for (TargetID = 0; TargetID < HostAdapter->MaxTargetDevices; TargetID++) {
3256                 struct BusLogic_TargetFlags *TargetFlags = &HostAdapter->TargetFlags[TargetID];
3257                 if (!TargetFlags->TargetExists)
3258                         continue;
3259                 Length += sprintf(&Buffer[Length], "\
3260   %2d    %5d %5d %5d    %5d %5d %5d        %5d %5d %5d\n", TargetID, TargetStatistics[TargetID].CommandAbortsRequested, TargetStatistics[TargetID].CommandAbortsAttempted, TargetStatistics[TargetID].CommandAbortsCompleted, TargetStatistics[TargetID].BusDeviceResetsRequested, TargetStatistics[TargetID].BusDeviceResetsAttempted, TargetStatistics[TargetID].BusDeviceResetsCompleted, TargetStatistics[TargetID].HostAdapterResetsRequested, TargetStatistics[TargetID].HostAdapterResetsAttempted, TargetStatistics[TargetID].HostAdapterResetsCompleted);
3261         }
3262         Length += sprintf(&Buffer[Length], "\nExternal Host Adapter Resets: %d\n", HostAdapter->ExternalHostAdapterResets);
3263         Length += sprintf(&Buffer[Length], "Host Adapter Internal Errors: %d\n", HostAdapter->HostAdapterInternalErrors);
3264         if (Length >= BusLogic_MessageBufferSize)
3265                 BusLogic_Error("Message Buffer length %d exceeds size %d\n", HostAdapter, Length, BusLogic_MessageBufferSize);
3266         if ((Length -= Offset) <= 0)
3267                 return 0;
3268         if (Length >= BytesAvailable)
3269                 Length = BytesAvailable;
3270         memcpy(ProcBuffer, HostAdapter->MessageBuffer + Offset, Length);
3271         *StartPointer = ProcBuffer;
3272         return Length;
3273 }
3274
3275
3276 /*
3277   BusLogic_Message prints Driver Messages.
3278 */
3279
3280 static void BusLogic_Message(enum BusLogic_MessageLevel MessageLevel, char *Format, struct BusLogic_HostAdapter *HostAdapter, ...)
3281 {
3282         static char Buffer[BusLogic_LineBufferSize];
3283         static boolean BeginningOfLine = true;
3284         va_list Arguments;
3285         int Length = 0;
3286         va_start(Arguments, HostAdapter);
3287         Length = vsprintf(Buffer, Format, Arguments);
3288         va_end(Arguments);
3289         if (MessageLevel == BusLogic_AnnounceLevel) {
3290                 static int AnnouncementLines = 0;
3291                 strcpy(&HostAdapter->MessageBuffer[HostAdapter->MessageBufferLength], Buffer);
3292                 HostAdapter->MessageBufferLength += Length;
3293                 if (++AnnouncementLines <= 2)
3294                         printk("%sscsi: %s", BusLogic_MessageLevelMap[MessageLevel], Buffer);
3295         } else if (MessageLevel == BusLogic_InfoLevel) {
3296                 strcpy(&HostAdapter->MessageBuffer[HostAdapter->MessageBufferLength], Buffer);
3297                 HostAdapter->MessageBufferLength += Length;
3298                 if (BeginningOfLine) {
3299                         if (Buffer[0] != '\n' || Length > 1)
3300                                 printk("%sscsi%d: %s", BusLogic_MessageLevelMap[MessageLevel], HostAdapter->HostNumber, Buffer);
3301                 } else
3302                         printk("%s", Buffer);
3303         } else {
3304                 if (BeginningOfLine) {
3305                         if (HostAdapter != NULL && HostAdapter->HostAdapterInitialized)
3306                                 printk("%sscsi%d: %s", BusLogic_MessageLevelMap[MessageLevel], HostAdapter->HostNumber, Buffer);
3307                         else
3308                                 printk("%s%s", BusLogic_MessageLevelMap[MessageLevel], Buffer);
3309                 } else
3310                         printk("%s", Buffer);
3311         }
3312         BeginningOfLine = (Buffer[Length - 1] == '\n');
3313 }
3314
3315
3316 /*
3317   BusLogic_ParseKeyword parses an individual option keyword.  It returns true
3318   and updates the pointer if the keyword is recognized and false otherwise.
3319 */
3320
3321 static boolean __init BusLogic_ParseKeyword(char **StringPointer, char *Keyword)
3322 {
3323         char *Pointer = *StringPointer;
3324         while (*Keyword != '\0') {
3325                 char StringChar = *Pointer++;
3326                 char KeywordChar = *Keyword++;
3327                 if (StringChar >= 'A' && StringChar <= 'Z')
3328                         StringChar += 'a' - 'Z';
3329                 if (KeywordChar >= 'A' && KeywordChar <= 'Z')
3330                         KeywordChar += 'a' - 'Z';
3331                 if (StringChar != KeywordChar)
3332                         return false;
3333         }
3334         *StringPointer = Pointer;
3335         return true;
3336 }
3337
3338
3339 /*
3340   BusLogic_ParseDriverOptions handles processing of BusLogic Driver Options
3341   specifications.
3342
3343   BusLogic Driver Options may be specified either via the Linux Kernel Command
3344   Line or via the Loadable Kernel Module Installation Facility.  Driver Options
3345   for multiple host adapters may be specified either by separating the option
3346   strings by a semicolon, or by specifying multiple "BusLogic=" strings on the
3347   command line.  Individual option specifications for a single host adapter are
3348   separated by commas.  The Probing and Debugging Options apply to all host
3349   adapters whereas the remaining options apply individually only to the
3350   selected host adapter.
3351
3352   The BusLogic Driver Probing Options are described in
3353   <file:Documentation/scsi/BusLogic.txt>.
3354 */
3355
3356 static int __init BusLogic_ParseDriverOptions(char *OptionsString)
3357 {
3358         while (true) {
3359                 struct BusLogic_DriverOptions *DriverOptions = &BusLogic_DriverOptions[BusLogic_DriverOptionsCount++];
3360                 int TargetID;
3361                 memset(DriverOptions, 0, sizeof(struct BusLogic_DriverOptions));
3362                 while (*OptionsString != '\0' && *OptionsString != ';') {
3363                         /* Probing Options. */
3364                         if (BusLogic_ParseKeyword(&OptionsString, "IO:")) {
3365                                 unsigned long IO_Address = simple_strtoul(OptionsString, &OptionsString, 0);
3366                                 BusLogic_ProbeOptions.LimitedProbeISA = true;
3367                                 switch (IO_Address) {
3368                                 case 0x330:
3369                                         BusLogic_ProbeOptions.Probe330 = true;
3370                                         break;
3371                                 case 0x334:
3372                                         BusLogic_ProbeOptions.Probe334 = true;
3373                                         break;
3374                                 case 0x230:
3375                                         BusLogic_ProbeOptions.Probe230 = true;
3376                                         break;
3377                                 case 0x234:
3378                                         BusLogic_ProbeOptions.Probe234 = true;
3379                                         break;
3380                                 case 0x130:
3381                                         BusLogic_ProbeOptions.Probe130 = true;
3382                                         break;
3383                                 case 0x134:
3384                                         BusLogic_ProbeOptions.Probe134 = true;
3385                                         break;
3386                                 default:
3387                                         BusLogic_Error("BusLogic: Invalid Driver Options " "(invalid I/O Address 0x%X)\n", NULL, IO_Address);
3388                                         return 0;
3389                                 }
3390                         } else if (BusLogic_ParseKeyword(&OptionsString, "NoProbeISA"))
3391                                 BusLogic_ProbeOptions.NoProbeISA = true;
3392                         else if (BusLogic_ParseKeyword(&OptionsString, "NoProbePCI"))
3393                                 BusLogic_ProbeOptions.NoProbePCI = true;
3394                         else if (BusLogic_ParseKeyword(&OptionsString, "NoProbe"))
3395                                 BusLogic_ProbeOptions.NoProbe = true;
3396                         else if (BusLogic_ParseKeyword(&OptionsString, "NoSortPCI"))
3397                                 BusLogic_ProbeOptions.NoSortPCI = true;
3398                         else if (BusLogic_ParseKeyword(&OptionsString, "MultiMasterFirst"))
3399                                 BusLogic_ProbeOptions.MultiMasterFirst = true;
3400                         else if (BusLogic_ParseKeyword(&OptionsString, "FlashPointFirst"))
3401                                 BusLogic_ProbeOptions.FlashPointFirst = true;
3402                         /* Tagged Queuing Options. */
3403                         else if (BusLogic_ParseKeyword(&OptionsString, "QueueDepth:[") || BusLogic_ParseKeyword(&OptionsString, "QD:[")) {
3404                                 for (TargetID = 0; TargetID < BusLogic_MaxTargetDevices; TargetID++) {
3405                                         unsigned short QueueDepth = simple_strtoul(OptionsString, &OptionsString, 0);
3406                                         if (QueueDepth > BusLogic_MaxTaggedQueueDepth) {
3407                                                 BusLogic_Error("BusLogic: Invalid Driver Options " "(invalid Queue Depth %d)\n", NULL, QueueDepth);
3408                                                 return 0;
3409                                         }
3410                                         DriverOptions->QueueDepth[TargetID] = QueueDepth;
3411                                         if (*OptionsString == ',')
3412                                                 OptionsString++;
3413                                         else if (*OptionsString == ']')
3414                                                 break;
3415                                         else {
3416                                                 BusLogic_Error("BusLogic: Invalid Driver Options " "(',' or ']' expected at '%s')\n", NULL, OptionsString);
3417                                                 return 0;
3418                                         }
3419                                 }
3420                                 if (*OptionsString != ']') {
3421                                         BusLogic_Error("BusLogic: Invalid Driver Options " "(']' expected at '%s')\n", NULL, OptionsString);
3422                                         return 0;
3423                                 } else
3424                                         OptionsString++;
3425                         } else if (BusLogic_ParseKeyword(&OptionsString, "QueueDepth:") || BusLogic_ParseKeyword(&OptionsString, "QD:")) {
3426                                 unsigned short QueueDepth = simple_strtoul(OptionsString, &OptionsString, 0);
3427                                 if (QueueDepth == 0 || QueueDepth > BusLogic_MaxTaggedQueueDepth) {
3428                                         BusLogic_Error("BusLogic: Invalid Driver Options " "(invalid Queue Depth %d)\n", NULL, QueueDepth);
3429                                         return 0;
3430                                 }
3431                                 DriverOptions->CommonQueueDepth = QueueDepth;
3432                                 for (TargetID = 0; TargetID < BusLogic_MaxTargetDevices; TargetID++)
3433                                         DriverOptions->QueueDepth[TargetID] = QueueDepth;
3434                         } else if (BusLogic_ParseKeyword(&OptionsString, "TaggedQueuing:") || BusLogic_ParseKeyword(&OptionsString, "TQ:")) {
3435                                 if (BusLogic_ParseKeyword(&OptionsString, "Default")) {
3436                                         DriverOptions->TaggedQueuingPermitted = 0x0000;
3437                                         DriverOptions->TaggedQueuingPermittedMask = 0x0000;
3438                                 } else if (BusLogic_ParseKeyword(&OptionsString, "Enable")) {
3439                                         DriverOptions->TaggedQueuingPermitted = 0xFFFF;
3440                                         DriverOptions->TaggedQueuingPermittedMask = 0xFFFF;
3441                                 } else if (BusLogic_ParseKeyword(&OptionsString, "Disable")) {
3442                                         DriverOptions->TaggedQueuingPermitted = 0x0000;
3443                                         DriverOptions->TaggedQueuingPermittedMask = 0xFFFF;
3444                                 } else {
3445                                         unsigned short TargetBit;
3446                                         for (TargetID = 0, TargetBit = 1; TargetID < BusLogic_MaxTargetDevices; TargetID++, TargetBit <<= 1)
3447                                                 switch (*OptionsString++) {
3448                                                 case 'Y':
3449                                                         DriverOptions->TaggedQueuingPermitted |= TargetBit;
3450                                                         DriverOptions->TaggedQueuingPermittedMask |= TargetBit;
3451                                                         break;
3452                                                 case 'N':
3453                                                         DriverOptions->TaggedQueuingPermitted &= ~TargetBit;
3454                                                         DriverOptions->TaggedQueuingPermittedMask |= TargetBit;
3455                                                         break;
3456                                                 case 'X':
3457                                                         break;
3458                                                 default:
3459                                                         OptionsString--;
3460                                                         TargetID = BusLogic_MaxTargetDevices;
3461                                                         break;
3462                                                 }
3463                                 }
3464                         }
3465                         /* Miscellaneous Options. */
3466                         else if (BusLogic_ParseKeyword(&OptionsString, "BusSettleTime:") || BusLogic_ParseKeyword(&OptionsString, "BST:")) {
3467                                 unsigned short BusSettleTime = simple_strtoul(OptionsString, &OptionsString, 0);
3468                                 if (BusSettleTime > 5 * 60) {
3469                                         BusLogic_Error("BusLogic: Invalid Driver Options " "(invalid Bus Settle Time %d)\n", NULL, BusSettleTime);
3470                                         return 0;
3471                                 }
3472                                 DriverOptions->BusSettleTime = BusSettleTime;
3473                         } else if (BusLogic_ParseKeyword(&OptionsString, "InhibitTargetInquiry"))
3474                                 DriverOptions->LocalOptions.InhibitTargetInquiry = true;
3475                         /* Debugging Options. */
3476                         else if (BusLogic_ParseKeyword(&OptionsString, "TraceProbe"))
3477                                 BusLogic_GlobalOptions.TraceProbe = true;
3478                         else if (BusLogic_ParseKeyword(&OptionsString, "TraceHardwareReset"))
3479                                 BusLogic_GlobalOptions.TraceHardwareReset = true;
3480                         else if (BusLogic_ParseKeyword(&OptionsString, "TraceConfiguration"))
3481                                 BusLogic_GlobalOptions.TraceConfiguration = true;
3482                         else if (BusLogic_ParseKeyword(&OptionsString, "TraceErrors"))
3483                                 BusLogic_GlobalOptions.TraceErrors = true;
3484                         else if (BusLogic_ParseKeyword(&OptionsString, "Debug")) {
3485                                 BusLogic_GlobalOptions.TraceProbe = true;
3486                                 BusLogic_GlobalOptions.TraceHardwareReset = true;
3487                                 BusLogic_GlobalOptions.TraceConfiguration = true;
3488                                 BusLogic_GlobalOptions.TraceErrors = true;
3489                         }
3490                         if (*OptionsString == ',')
3491                                 OptionsString++;
3492                         else if (*OptionsString != ';' && *OptionsString != '\0') {
3493                                 BusLogic_Error("BusLogic: Unexpected Driver Option '%s' " "ignored\n", NULL, OptionsString);
3494                                 *OptionsString = '\0';
3495                         }
3496                 }
3497                 if (!(BusLogic_DriverOptionsCount == 0 || BusLogic_ProbeInfoCount == 0 || BusLogic_DriverOptionsCount == BusLogic_ProbeInfoCount)) {
3498                         BusLogic_Error("BusLogic: Invalid Driver Options " "(all or no I/O Addresses must be specified)\n", NULL);
3499                         return 0;
3500                 }
3501                 /*
3502                    Tagged Queuing is disabled when the Queue Depth is 1 since queuing
3503                    multiple commands is not possible.
3504                  */
3505                 for (TargetID = 0; TargetID < BusLogic_MaxTargetDevices; TargetID++)
3506                         if (DriverOptions->QueueDepth[TargetID] == 1) {
3507                                 unsigned short TargetBit = 1 << TargetID;
3508                                 DriverOptions->TaggedQueuingPermitted &= ~TargetBit;
3509                                 DriverOptions->TaggedQueuingPermittedMask |= TargetBit;
3510                         }
3511                 if (*OptionsString == ';')
3512                         OptionsString++;
3513                 if (*OptionsString == '\0')
3514                         return 0;
3515         }
3516         return 1;
3517 }
3518
3519 /*
3520   Get it all started
3521 */
3522
3523 static struct scsi_host_template Bus_Logic_template = {
3524         .module = THIS_MODULE,
3525         .proc_name = "BusLogic",
3526         .proc_info = BusLogic_ProcDirectoryInfo,
3527         .name = "BusLogic",
3528         .info = BusLogic_DriverInfo,
3529         .queuecommand = BusLogic_QueueCommand,
3530         .slave_configure = BusLogic_SlaveConfigure,
3531         .bios_param = BusLogic_BIOSDiskParameters,
3532         .eh_host_reset_handler = BusLogic_host_reset,
3533 #if 0
3534         .eh_abort_handler = BusLogic_AbortCommand,
3535 #endif
3536         .unchecked_isa_dma = 1,
3537         .max_sectors = 128,
3538         .use_clustering = ENABLE_CLUSTERING,
3539 };
3540
3541 /*
3542   BusLogic_Setup handles processing of Kernel Command Line Arguments.
3543 */
3544
3545 static int __init BusLogic_Setup(char *str)
3546 {
3547         int ints[3];
3548
3549         (void) get_options(str, ARRAY_SIZE(ints), ints);
3550
3551         if (ints[0] != 0) {
3552                 BusLogic_Error("BusLogic: Obsolete Command Line Entry " "Format Ignored\n", NULL);
3553                 return 0;
3554         }
3555         if (str == NULL || *str == '\0')
3556                 return 0;
3557         return BusLogic_ParseDriverOptions(str);
3558 }
3559
3560 /*
3561  * Exit function.  Deletes all hosts associated with this driver.
3562  */
3563
3564 static void __exit BusLogic_exit(void)
3565 {
3566         struct BusLogic_HostAdapter *ha, *next;
3567
3568         list_for_each_entry_safe(ha, next, &BusLogic_host_list, host_list)
3569                 BusLogic_ReleaseHostAdapter(ha);
3570 }
3571
3572 __setup("BusLogic=", BusLogic_Setup);
3573
3574 module_init(BusLogic_init);
3575 module_exit(BusLogic_exit);