Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma
[platform/kernel/linux-rpi.git] / drivers / scsi / BusLogic.c
1 // SPDX-License-Identifier: GPL-2.0-only
2
3 /*
4
5   Linux Driver for BusLogic MultiMaster and FlashPoint SCSI Host Adapters
6
7   Copyright 1995-1998 by Leonard N. Zubkoff <lnz@dandelion.com>
8
9
10   The author respectfully requests that any modifications to this software be
11   sent directly to him for evaluation and testing.
12
13   Special thanks to Wayne Yen, Jin-Lon Hon, and Alex Win of BusLogic, whose
14   advice has been invaluable, to David Gentzel, for writing the original Linux
15   BusLogic driver, and to Paul Gortmaker, for being such a dedicated test site.
16
17   Finally, special thanks to Mylex/BusLogic for making the FlashPoint SCCB
18   Manager available as freely redistributable source code.
19
20 */
21
22 #define blogic_drvr_version             "2.1.17"
23 #define blogic_drvr_date                "12 September 2013"
24
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/interrupt.h>
28 #include <linux/types.h>
29 #include <linux/blkdev.h>
30 #include <linux/delay.h>
31 #include <linux/ioport.h>
32 #include <linux/mm.h>
33 #include <linux/stat.h>
34 #include <linux/pci.h>
35 #include <linux/spinlock.h>
36 #include <linux/jiffies.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/slab.h>
39 #include <linux/msdos_partition.h>
40 #include <scsi/scsicam.h>
41
42 #include <asm/dma.h>
43 #include <asm/io.h>
44
45 #include <scsi/scsi.h>
46 #include <scsi/scsi_cmnd.h>
47 #include <scsi/scsi_device.h>
48 #include <scsi/scsi_host.h>
49 #include <scsi/scsi_tcq.h>
50 #include "BusLogic.h"
51 #include "FlashPoint.c"
52
53 #ifndef FAILURE
54 #define FAILURE (-1)
55 #endif
56
57 static struct scsi_host_template blogic_template;
58
59 /*
60   blogic_drvr_options_count is a count of the number of BusLogic Driver
61   Options specifications provided via the Linux Kernel Command Line or via
62   the Loadable Kernel Module Installation Facility.
63 */
64
65 static int blogic_drvr_options_count;
66
67
68 /*
69   blogic_drvr_options is an array of Driver Options structures representing
70   BusLogic Driver Options specifications provided via the Linux Kernel Command
71   Line or via the Loadable Kernel Module Installation Facility.
72 */
73
74 static struct blogic_drvr_options blogic_drvr_options[BLOGIC_MAX_ADAPTERS];
75
76
77 /*
78   BusLogic can be assigned a string by insmod.
79 */
80
81 MODULE_LICENSE("GPL");
82 #ifdef MODULE
83 static char *BusLogic;
84 module_param(BusLogic, charp, 0);
85 #endif
86
87
88 /*
89   blogic_probe_options is a set of Probe Options to be applied across
90   all BusLogic Host Adapters.
91 */
92
93 static struct blogic_probe_options blogic_probe_options;
94
95
96 /*
97   blogic_global_options is a set of Global Options to be applied across
98   all BusLogic Host Adapters.
99 */
100
101 static struct blogic_global_options blogic_global_options;
102
103 static LIST_HEAD(blogic_host_list);
104
105 /*
106   blogic_probeinfo_count is the number of entries in blogic_probeinfo_list.
107 */
108
109 static int blogic_probeinfo_count;
110
111
112 /*
113   blogic_probeinfo_list is the list of I/O Addresses and Bus Probe Information
114   to be checked for potential BusLogic Host Adapters.  It is initialized by
115   interrogating the PCI Configuration Space on PCI machines as well as from the
116   list of standard BusLogic I/O Addresses.
117 */
118
119 static struct blogic_probeinfo *blogic_probeinfo_list;
120
121
122 /*
123   blogic_cmd_failure_reason holds a string identifying the reason why a
124   call to blogic_cmd failed.  It is only non-NULL when blogic_cmd
125   returns a failure code.
126 */
127
128 static char *blogic_cmd_failure_reason;
129
130 /*
131   blogic_announce_drvr announces the Driver Version and Date, Author's
132   Name, Copyright Notice, and Electronic Mail Address.
133 */
134
135 static void blogic_announce_drvr(struct blogic_adapter *adapter)
136 {
137         blogic_announce("***** BusLogic SCSI Driver Version " blogic_drvr_version " of " blogic_drvr_date " *****\n", adapter);
138         blogic_announce("Copyright 1995-1998 by Leonard N. Zubkoff <lnz@dandelion.com>\n", adapter);
139 }
140
141
142 /*
143   blogic_drvr_info returns the Host Adapter Name to identify this SCSI
144   Driver and Host Adapter.
145 */
146
147 static const char *blogic_drvr_info(struct Scsi_Host *host)
148 {
149         struct blogic_adapter *adapter =
150                                 (struct blogic_adapter *) host->hostdata;
151         return adapter->full_model;
152 }
153
154 /*
155   blogic_init_ccbs initializes a group of Command Control Blocks (CCBs)
156   for Host Adapter from the blk_size bytes located at blk_pointer.  The newly
157   created CCBs are added to Host Adapter's free list.
158 */
159
160 static void blogic_init_ccbs(struct blogic_adapter *adapter, void *blk_pointer,
161                                 int blk_size, dma_addr_t blkp)
162 {
163         struct blogic_ccb *ccb = (struct blogic_ccb *) blk_pointer;
164         unsigned int offset = 0;
165         memset(blk_pointer, 0, blk_size);
166         ccb->allocgrp_head = blkp;
167         ccb->allocgrp_size = blk_size;
168         while ((blk_size -= sizeof(struct blogic_ccb)) >= 0) {
169                 ccb->status = BLOGIC_CCB_FREE;
170                 ccb->adapter = adapter;
171                 ccb->dma_handle = (u32) blkp + offset;
172                 if (blogic_flashpoint_type(adapter)) {
173                         ccb->callback = blogic_qcompleted_ccb;
174                         ccb->base_addr = adapter->fpinfo.base_addr;
175                 }
176                 ccb->next = adapter->free_ccbs;
177                 ccb->next_all = adapter->all_ccbs;
178                 adapter->free_ccbs = ccb;
179                 adapter->all_ccbs = ccb;
180                 adapter->alloc_ccbs++;
181                 ccb++;
182                 offset += sizeof(struct blogic_ccb);
183         }
184 }
185
186
187 /*
188   blogic_create_initccbs allocates the initial CCBs for Host Adapter.
189 */
190
191 static bool __init blogic_create_initccbs(struct blogic_adapter *adapter)
192 {
193         int blk_size = BLOGIC_CCB_GRP_ALLOCSIZE * sizeof(struct blogic_ccb);
194         void *blk_pointer;
195         dma_addr_t blkp;
196
197         while (adapter->alloc_ccbs < adapter->initccbs) {
198                 blk_pointer = dma_alloc_coherent(&adapter->pci_device->dev,
199                                 blk_size, &blkp, GFP_KERNEL);
200                 if (blk_pointer == NULL) {
201                         blogic_err("UNABLE TO ALLOCATE CCB GROUP - DETACHING\n",
202                                         adapter);
203                         return false;
204                 }
205                 blogic_init_ccbs(adapter, blk_pointer, blk_size, blkp);
206         }
207         return true;
208 }
209
210
211 /*
212   blogic_destroy_ccbs deallocates the CCBs for Host Adapter.
213 */
214
215 static void blogic_destroy_ccbs(struct blogic_adapter *adapter)
216 {
217         struct blogic_ccb *next_ccb = adapter->all_ccbs, *ccb, *lastccb = NULL;
218         adapter->all_ccbs = NULL;
219         adapter->free_ccbs = NULL;
220         while ((ccb = next_ccb) != NULL) {
221                 next_ccb = ccb->next_all;
222                 if (ccb->allocgrp_head) {
223                         if (lastccb)
224                                 dma_free_coherent(&adapter->pci_device->dev,
225                                                 lastccb->allocgrp_size, lastccb,
226                                                 lastccb->allocgrp_head);
227                         lastccb = ccb;
228                 }
229         }
230         if (lastccb)
231                 dma_free_coherent(&adapter->pci_device->dev,
232                                 lastccb->allocgrp_size, lastccb,
233                                 lastccb->allocgrp_head);
234 }
235
236
237 /*
238   blogic_create_addlccbs allocates Additional CCBs for Host Adapter.  If
239   allocation fails and there are no remaining CCBs available, the Driver Queue
240   Depth is decreased to a known safe value to avoid potential deadlocks when
241   multiple host adapters share the same IRQ Channel.
242 */
243
244 static void blogic_create_addlccbs(struct blogic_adapter *adapter,
245                                         int addl_ccbs, bool print_success)
246 {
247         int blk_size = BLOGIC_CCB_GRP_ALLOCSIZE * sizeof(struct blogic_ccb);
248         int prev_alloc = adapter->alloc_ccbs;
249         void *blk_pointer;
250         dma_addr_t blkp;
251         if (addl_ccbs <= 0)
252                 return;
253         while (adapter->alloc_ccbs - prev_alloc < addl_ccbs) {
254                 blk_pointer = dma_alloc_coherent(&adapter->pci_device->dev,
255                                 blk_size, &blkp, GFP_KERNEL);
256                 if (blk_pointer == NULL)
257                         break;
258                 blogic_init_ccbs(adapter, blk_pointer, blk_size, blkp);
259         }
260         if (adapter->alloc_ccbs > prev_alloc) {
261                 if (print_success)
262                         blogic_notice("Allocated %d additional CCBs (total now %d)\n", adapter, adapter->alloc_ccbs - prev_alloc, adapter->alloc_ccbs);
263                 return;
264         }
265         blogic_notice("Failed to allocate additional CCBs\n", adapter);
266         if (adapter->drvr_qdepth > adapter->alloc_ccbs - adapter->tgt_count) {
267                 adapter->drvr_qdepth = adapter->alloc_ccbs - adapter->tgt_count;
268                 adapter->scsi_host->can_queue = adapter->drvr_qdepth;
269         }
270 }
271
272 /*
273   blogic_alloc_ccb allocates a CCB from Host Adapter's free list,
274   allocating more memory from the Kernel if necessary.  The Host Adapter's
275   Lock should already have been acquired by the caller.
276 */
277
278 static struct blogic_ccb *blogic_alloc_ccb(struct blogic_adapter *adapter)
279 {
280         static unsigned long serial;
281         struct blogic_ccb *ccb;
282         ccb = adapter->free_ccbs;
283         if (ccb != NULL) {
284                 ccb->serial = ++serial;
285                 adapter->free_ccbs = ccb->next;
286                 ccb->next = NULL;
287                 if (adapter->free_ccbs == NULL)
288                         blogic_create_addlccbs(adapter, adapter->inc_ccbs,
289                                                 true);
290                 return ccb;
291         }
292         blogic_create_addlccbs(adapter, adapter->inc_ccbs, true);
293         ccb = adapter->free_ccbs;
294         if (ccb == NULL)
295                 return NULL;
296         ccb->serial = ++serial;
297         adapter->free_ccbs = ccb->next;
298         ccb->next = NULL;
299         return ccb;
300 }
301
302
303 /*
304   blogic_dealloc_ccb deallocates a CCB, returning it to the Host Adapter's
305   free list.  The Host Adapter's Lock should already have been acquired by the
306   caller.
307 */
308
309 static void blogic_dealloc_ccb(struct blogic_ccb *ccb, int dma_unmap)
310 {
311         struct blogic_adapter *adapter = ccb->adapter;
312
313         if (ccb->command != NULL)
314                 scsi_dma_unmap(ccb->command);
315         if (dma_unmap)
316                 dma_unmap_single(&adapter->pci_device->dev, ccb->sensedata,
317                          ccb->sense_datalen, DMA_FROM_DEVICE);
318
319         ccb->command = NULL;
320         ccb->status = BLOGIC_CCB_FREE;
321         ccb->next = adapter->free_ccbs;
322         adapter->free_ccbs = ccb;
323 }
324
325
326 /*
327   blogic_cmd sends the command opcode to adapter, optionally
328   providing paramlen bytes of param and receiving at most
329   replylen bytes of reply; any excess reply data is received but
330   discarded.
331
332   On success, this function returns the number of reply bytes read from
333   the Host Adapter (including any discarded data); on failure, it returns
334   -1 if the command was invalid, or -2 if a timeout occurred.
335
336   blogic_cmd is called exclusively during host adapter detection and
337   initialization, so performance and latency are not critical, and exclusive
338   access to the Host Adapter hardware is assumed.  Once the host adapter and
339   driver are initialized, the only Host Adapter command that is issued is the
340   single byte Execute Mailbox Command operation code, which does not require
341   waiting for the Host Adapter Ready bit to be set in the Status Register.
342 */
343
344 static int blogic_cmd(struct blogic_adapter *adapter, enum blogic_opcode opcode,
345                         void *param, int paramlen, void *reply, int replylen)
346 {
347         unsigned char *param_p = (unsigned char *) param;
348         unsigned char *reply_p = (unsigned char *) reply;
349         union blogic_stat_reg statusreg;
350         union blogic_int_reg intreg;
351         unsigned long processor_flag = 0;
352         int reply_b = 0, result;
353         long timeout;
354         /*
355            Clear out the Reply Data if provided.
356          */
357         if (replylen > 0)
358                 memset(reply, 0, replylen);
359         /*
360            If the IRQ Channel has not yet been acquired, then interrupts
361            must be disabled while issuing host adapter commands since a
362            Command Complete interrupt could occur if the IRQ Channel was
363            previously enabled by another BusLogic Host Adapter or another
364            driver sharing the same IRQ Channel.
365          */
366         if (!adapter->irq_acquired)
367                 local_irq_save(processor_flag);
368         /*
369            Wait for the Host Adapter Ready bit to be set and the
370            Command/Parameter Register Busy bit to be reset in the Status
371            Register.
372          */
373         timeout = 10000;
374         while (--timeout >= 0) {
375                 statusreg.all = blogic_rdstatus(adapter);
376                 if (statusreg.sr.adapter_ready && !statusreg.sr.cmd_param_busy)
377                         break;
378                 udelay(100);
379         }
380         if (timeout < 0) {
381                 blogic_cmd_failure_reason =
382                                 "Timeout waiting for Host Adapter Ready";
383                 result = -2;
384                 goto done;
385         }
386         /*
387            Write the opcode to the Command/Parameter Register.
388          */
389         adapter->adapter_cmd_complete = false;
390         blogic_setcmdparam(adapter, opcode);
391         /*
392            Write any additional Parameter Bytes.
393          */
394         timeout = 10000;
395         while (paramlen > 0 && --timeout >= 0) {
396                 /*
397                    Wait 100 microseconds to give the Host Adapter enough
398                    time to determine whether the last value written to the
399                    Command/Parameter Register was valid or not. If the
400                    Command Complete bit is set in the Interrupt Register,
401                    then the Command Invalid bit in the Status Register will
402                    be reset if the Operation Code or Parameter was valid
403                    and the command has completed, or set if the Operation
404                    Code or Parameter was invalid. If the Data In Register
405                    Ready bit is set in the Status Register, then the
406                    Operation Code was valid, and data is waiting to be read
407                    back from the Host Adapter. Otherwise, wait for the
408                    Command/Parameter Register Busy bit in the Status
409                    Register to be reset.
410                  */
411                 udelay(100);
412                 intreg.all = blogic_rdint(adapter);
413                 statusreg.all = blogic_rdstatus(adapter);
414                 if (intreg.ir.cmd_complete)
415                         break;
416                 if (adapter->adapter_cmd_complete)
417                         break;
418                 if (statusreg.sr.datain_ready)
419                         break;
420                 if (statusreg.sr.cmd_param_busy)
421                         continue;
422                 blogic_setcmdparam(adapter, *param_p++);
423                 paramlen--;
424         }
425         if (timeout < 0) {
426                 blogic_cmd_failure_reason =
427                                 "Timeout waiting for Parameter Acceptance";
428                 result = -2;
429                 goto done;
430         }
431         /*
432            The Modify I/O Address command does not cause a Command Complete
433            Interrupt.
434          */
435         if (opcode == BLOGIC_MOD_IOADDR) {
436                 statusreg.all = blogic_rdstatus(adapter);
437                 if (statusreg.sr.cmd_invalid) {
438                         blogic_cmd_failure_reason =
439                                         "Modify I/O Address Invalid";
440                         result = -1;
441                         goto done;
442                 }
443                 if (blogic_global_options.trace_config)
444                         blogic_notice("blogic_cmd(%02X) Status = %02X: (Modify I/O Address)\n", adapter, opcode, statusreg.all);
445                 result = 0;
446                 goto done;
447         }
448         /*
449            Select an appropriate timeout value for awaiting command completion.
450          */
451         switch (opcode) {
452         case BLOGIC_INQ_DEV0TO7:
453         case BLOGIC_INQ_DEV8TO15:
454         case BLOGIC_INQ_DEV:
455                 /* Approximately 60 seconds. */
456                 timeout = 60 * 10000;
457                 break;
458         default:
459                 /* Approximately 1 second. */
460                 timeout = 10000;
461                 break;
462         }
463         /*
464            Receive any Reply Bytes, waiting for either the Command
465            Complete bit to be set in the Interrupt Register, or for the
466            Interrupt Handler to set the Host Adapter Command Completed
467            bit in the Host Adapter structure.
468          */
469         while (--timeout >= 0) {
470                 intreg.all = blogic_rdint(adapter);
471                 statusreg.all = blogic_rdstatus(adapter);
472                 if (intreg.ir.cmd_complete)
473                         break;
474                 if (adapter->adapter_cmd_complete)
475                         break;
476                 if (statusreg.sr.datain_ready) {
477                         if (++reply_b <= replylen)
478                                 *reply_p++ = blogic_rddatain(adapter);
479                         else
480                                 blogic_rddatain(adapter);
481                 }
482                 if (opcode == BLOGIC_FETCH_LOCALRAM &&
483                                 statusreg.sr.adapter_ready)
484                         break;
485                 udelay(100);
486         }
487         if (timeout < 0) {
488                 blogic_cmd_failure_reason =
489                                         "Timeout waiting for Command Complete";
490                 result = -2;
491                 goto done;
492         }
493         /*
494            Clear any pending Command Complete Interrupt.
495          */
496         blogic_intreset(adapter);
497         /*
498            Provide tracing information if requested.
499          */
500         if (blogic_global_options.trace_config) {
501                 int i;
502                 blogic_notice("blogic_cmd(%02X) Status = %02X: %2d ==> %2d:",
503                                 adapter, opcode, statusreg.all, replylen,
504                                 reply_b);
505                 if (replylen > reply_b)
506                         replylen = reply_b;
507                 for (i = 0; i < replylen; i++)
508                         blogic_notice(" %02X", adapter,
509                                         ((unsigned char *) reply)[i]);
510                 blogic_notice("\n", adapter);
511         }
512         /*
513            Process Command Invalid conditions.
514          */
515         if (statusreg.sr.cmd_invalid) {
516                 /*
517                    Some early BusLogic Host Adapters may not recover
518                    properly from a Command Invalid condition, so if this
519                    appears to be the case, a Soft Reset is issued to the
520                    Host Adapter.  Potentially invalid commands are never
521                    attempted after Mailbox Initialization is performed,
522                    so there should be no Host Adapter state lost by a
523                    Soft Reset in response to a Command Invalid condition.
524                  */
525                 udelay(1000);
526                 statusreg.all = blogic_rdstatus(adapter);
527                 if (statusreg.sr.cmd_invalid || statusreg.sr.rsvd ||
528                                 statusreg.sr.datain_ready ||
529                                 statusreg.sr.cmd_param_busy ||
530                                 !statusreg.sr.adapter_ready ||
531                                 !statusreg.sr.init_reqd ||
532                                 statusreg.sr.diag_active ||
533                                 statusreg.sr.diag_failed) {
534                         blogic_softreset(adapter);
535                         udelay(1000);
536                 }
537                 blogic_cmd_failure_reason = "Command Invalid";
538                 result = -1;
539                 goto done;
540         }
541         /*
542            Handle Excess Parameters Supplied conditions.
543          */
544         if (paramlen > 0) {
545                 blogic_cmd_failure_reason = "Excess Parameters Supplied";
546                 result = -1;
547                 goto done;
548         }
549         /*
550            Indicate the command completed successfully.
551          */
552         blogic_cmd_failure_reason = NULL;
553         result = reply_b;
554         /*
555            Restore the interrupt status if necessary and return.
556          */
557 done:
558         if (!adapter->irq_acquired)
559                 local_irq_restore(processor_flag);
560         return result;
561 }
562
563
564 /*
565   blogic_sort_probeinfo sorts a section of blogic_probeinfo_list in order
566   of increasing PCI Bus and Device Number.
567 */
568
569 static void __init blogic_sort_probeinfo(struct blogic_probeinfo
570                                         *probeinfo_list, int probeinfo_cnt)
571 {
572         int last_exchange = probeinfo_cnt - 1, bound, j;
573
574         while (last_exchange > 0) {
575                 bound = last_exchange;
576                 last_exchange = 0;
577                 for (j = 0; j < bound; j++) {
578                         struct blogic_probeinfo *probeinfo1 =
579                                                         &probeinfo_list[j];
580                         struct blogic_probeinfo *probeinfo2 =
581                                                         &probeinfo_list[j + 1];
582                         if (probeinfo1->bus > probeinfo2->bus ||
583                                 (probeinfo1->bus == probeinfo2->bus &&
584                                 (probeinfo1->dev > probeinfo2->dev))) {
585                                 struct blogic_probeinfo tmp_probeinfo;
586
587                                 memcpy(&tmp_probeinfo, probeinfo1,
588                                         sizeof(struct blogic_probeinfo));
589                                 memcpy(probeinfo1, probeinfo2,
590                                         sizeof(struct blogic_probeinfo));
591                                 memcpy(probeinfo2, &tmp_probeinfo,
592                                         sizeof(struct blogic_probeinfo));
593                                 last_exchange = j;
594                         }
595                 }
596         }
597 }
598
599
600 /*
601   blogic_init_mm_probeinfo initializes the list of I/O Address
602   and Bus Probe Information to be checked for potential BusLogic MultiMaster
603   SCSI Host Adapters by interrogating the PCI Configuration Space on PCI
604   machines as well as from the list of standard BusLogic MultiMaster ISA
605   I/O Addresses.  It returns the number of PCI MultiMaster Host Adapters found.
606 */
607
608 static int __init blogic_init_mm_probeinfo(struct blogic_adapter *adapter)
609 {
610         struct blogic_probeinfo *pr_probeinfo =
611                 &blogic_probeinfo_list[blogic_probeinfo_count];
612         int nonpr_mmindex = blogic_probeinfo_count + 1;
613         int nonpr_mmcount = 0, mmcount = 0;
614         bool force_scan_order = false;
615         bool force_scan_order_checked = false;
616         struct pci_dev *pci_device = NULL;
617         int i;
618         if (blogic_probeinfo_count >= BLOGIC_MAX_ADAPTERS)
619                 return 0;
620         blogic_probeinfo_count++;
621         /*
622            Iterate over the MultiMaster PCI Host Adapters.  For each
623            enumerated host adapter, determine whether its ISA Compatible
624            I/O Port is enabled and if so, whether it is assigned the
625            Primary I/O Address.  A host adapter that is assigned the
626            Primary I/O Address will always be the preferred boot device.
627            The MultiMaster BIOS will first recognize a host adapter at
628            the Primary I/O Address, then any other PCI host adapters,
629            and finally any host adapters located at the remaining
630            standard ISA I/O Addresses.  When a PCI host adapter is found
631            with its ISA Compatible I/O Port enabled, a command is issued
632            to disable the ISA Compatible I/O Port, and it is noted that the
633            particular standard ISA I/O Address need not be probed.
634          */
635         pr_probeinfo->io_addr = 0;
636         while ((pci_device = pci_get_device(PCI_VENDOR_ID_BUSLOGIC,
637                                         PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER,
638                                         pci_device)) != NULL) {
639                 struct blogic_adapter *host_adapter = adapter;
640                 struct blogic_adapter_info adapter_info;
641                 enum blogic_isa_ioport mod_ioaddr_req;
642                 unsigned char bus;
643                 unsigned char device;
644                 unsigned int irq_ch;
645                 unsigned long base_addr0;
646                 unsigned long base_addr1;
647                 unsigned long io_addr;
648                 unsigned long pci_addr;
649
650                 if (pci_enable_device(pci_device))
651                         continue;
652
653                 if (dma_set_mask(&pci_device->dev, DMA_BIT_MASK(32)))
654                         continue;
655
656                 bus = pci_device->bus->number;
657                 device = pci_device->devfn >> 3;
658                 irq_ch = pci_device->irq;
659                 io_addr = base_addr0 = pci_resource_start(pci_device, 0);
660                 pci_addr = base_addr1 = pci_resource_start(pci_device, 1);
661
662                 if (pci_resource_flags(pci_device, 0) & IORESOURCE_MEM) {
663                         blogic_err("BusLogic: Base Address0 0x%lX not I/O for MultiMaster Host Adapter\n", NULL, base_addr0);
664                         blogic_err("at PCI Bus %d Device %d I/O Address 0x%lX\n", NULL, bus, device, io_addr);
665                         continue;
666                 }
667                 if (pci_resource_flags(pci_device, 1) & IORESOURCE_IO) {
668                         blogic_err("BusLogic: Base Address1 0x%lX not Memory for MultiMaster Host Adapter\n", NULL, base_addr1);
669                         blogic_err("at PCI Bus %d Device %d PCI Address 0x%lX\n", NULL, bus, device, pci_addr);
670                         continue;
671                 }
672                 if (irq_ch == 0) {
673                         blogic_err("BusLogic: IRQ Channel %d invalid for MultiMaster Host Adapter\n", NULL, irq_ch);
674                         blogic_err("at PCI Bus %d Device %d I/O Address 0x%lX\n", NULL, bus, device, io_addr);
675                         continue;
676                 }
677                 if (blogic_global_options.trace_probe) {
678                         blogic_notice("BusLogic: PCI MultiMaster Host Adapter detected at\n", NULL);
679                         blogic_notice("BusLogic: PCI Bus %d Device %d I/O Address 0x%lX PCI Address 0x%lX\n", NULL, bus, device, io_addr, pci_addr);
680                 }
681                 /*
682                    Issue the Inquire PCI Host Adapter Information command to determine
683                    the ISA Compatible I/O Port.  If the ISA Compatible I/O Port is
684                    known and enabled, note that the particular Standard ISA I/O
685                    Address should not be probed.
686                  */
687                 host_adapter->io_addr = io_addr;
688                 blogic_intreset(host_adapter);
689                 if (blogic_cmd(host_adapter, BLOGIC_INQ_PCI_INFO, NULL, 0,
690                                 &adapter_info, sizeof(adapter_info)) !=
691                                 sizeof(adapter_info))
692                         adapter_info.isa_port = BLOGIC_IO_DISABLE;
693                 /*
694                    Issue the Modify I/O Address command to disable the
695                    ISA Compatible I/O Port. On PCI Host Adapters, the
696                    Modify I/O Address command allows modification of the
697                    ISA compatible I/O Address that the Host Adapter
698                    responds to; it does not affect the PCI compliant
699                    I/O Address assigned at system initialization.
700                  */
701                 mod_ioaddr_req = BLOGIC_IO_DISABLE;
702                 blogic_cmd(host_adapter, BLOGIC_MOD_IOADDR, &mod_ioaddr_req,
703                                 sizeof(mod_ioaddr_req), NULL, 0);
704                 /*
705                    For the first MultiMaster Host Adapter enumerated,
706                    issue the Fetch Host Adapter Local RAM command to read
707                    byte 45 of the AutoSCSI area, for the setting of the
708                    "Use Bus And Device # For PCI Scanning Seq." option.
709                    Issue the Inquire Board ID command since this option is
710                    only valid for the BT-948/958/958D.
711                  */
712                 if (!force_scan_order_checked) {
713                         struct blogic_fetch_localram fetch_localram;
714                         struct blogic_autoscsi_byte45 autoscsi_byte45;
715                         struct blogic_board_id id;
716
717                         fetch_localram.offset = BLOGIC_AUTOSCSI_BASE + 45;
718                         fetch_localram.count = sizeof(autoscsi_byte45);
719                         blogic_cmd(host_adapter, BLOGIC_FETCH_LOCALRAM,
720                                         &fetch_localram, sizeof(fetch_localram),
721                                         &autoscsi_byte45,
722                                         sizeof(autoscsi_byte45));
723                         blogic_cmd(host_adapter, BLOGIC_GET_BOARD_ID, NULL, 0,
724                                         &id, sizeof(id));
725                         if (id.fw_ver_digit1 == '5')
726                                 force_scan_order =
727                                         autoscsi_byte45.force_scan_order;
728                         force_scan_order_checked = true;
729                 }
730                 /*
731                    Determine whether this MultiMaster Host Adapter has its
732                    ISA Compatible I/O Port enabled and is assigned the
733                    Primary I/O Address. If it does, then it is the Primary
734                    MultiMaster Host Adapter and must be recognized first.
735                    If it does not, then it is added to the list for probing
736                    after any Primary MultiMaster Host Adapter is probed.
737                  */
738                 if (adapter_info.isa_port == BLOGIC_IO_330) {
739                         pr_probeinfo->adapter_type = BLOGIC_MULTIMASTER;
740                         pr_probeinfo->adapter_bus_type = BLOGIC_PCI_BUS;
741                         pr_probeinfo->io_addr = io_addr;
742                         pr_probeinfo->pci_addr = pci_addr;
743                         pr_probeinfo->bus = bus;
744                         pr_probeinfo->dev = device;
745                         pr_probeinfo->irq_ch = irq_ch;
746                         pr_probeinfo->pci_device = pci_dev_get(pci_device);
747                         mmcount++;
748                 } else if (blogic_probeinfo_count < BLOGIC_MAX_ADAPTERS) {
749                         struct blogic_probeinfo *probeinfo =
750                                 &blogic_probeinfo_list[blogic_probeinfo_count++];
751                         probeinfo->adapter_type = BLOGIC_MULTIMASTER;
752                         probeinfo->adapter_bus_type = BLOGIC_PCI_BUS;
753                         probeinfo->io_addr = io_addr;
754                         probeinfo->pci_addr = pci_addr;
755                         probeinfo->bus = bus;
756                         probeinfo->dev = device;
757                         probeinfo->irq_ch = irq_ch;
758                         probeinfo->pci_device = pci_dev_get(pci_device);
759                         nonpr_mmcount++;
760                         mmcount++;
761                 } else
762                         blogic_warn("BusLogic: Too many Host Adapters detected\n", NULL);
763         }
764         /*
765            If the AutoSCSI "Use Bus And Device # For PCI Scanning Seq."
766            option is ON for the first enumerated MultiMaster Host Adapter,
767            and if that host adapter is a BT-948/958/958D, then the
768            MultiMaster BIOS will recognize MultiMaster Host Adapters in
769            the order of increasing PCI Bus and Device Number. In that case,
770            sort the probe information into the same order the BIOS uses.
771            If this option is OFF, then the MultiMaster BIOS will recognize
772            MultiMaster Host Adapters in the order they are enumerated by
773            the PCI BIOS, and hence no sorting is necessary.
774          */
775         if (force_scan_order)
776                 blogic_sort_probeinfo(&blogic_probeinfo_list[nonpr_mmindex],
777                                         nonpr_mmcount);
778         /*
779            Iterate over the older non-compliant MultiMaster PCI Host Adapters,
780            noting the PCI bus location and assigned IRQ Channel.
781          */
782         pci_device = NULL;
783         while ((pci_device = pci_get_device(PCI_VENDOR_ID_BUSLOGIC,
784                                         PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER_NC,
785                                         pci_device)) != NULL) {
786                 unsigned char bus;
787                 unsigned char device;
788                 unsigned int irq_ch;
789                 unsigned long io_addr;
790
791                 if (pci_enable_device(pci_device))
792                         continue;
793
794                 if (dma_set_mask(&pci_device->dev, DMA_BIT_MASK(32)))
795                         continue;
796
797                 bus = pci_device->bus->number;
798                 device = pci_device->devfn >> 3;
799                 irq_ch = pci_device->irq;
800                 io_addr = pci_resource_start(pci_device, 0);
801
802                 if (io_addr == 0 || irq_ch == 0)
803                         continue;
804                 for (i = 0; i < blogic_probeinfo_count; i++) {
805                         struct blogic_probeinfo *probeinfo =
806                                                 &blogic_probeinfo_list[i];
807                         if (probeinfo->io_addr == io_addr &&
808                                 probeinfo->adapter_type == BLOGIC_MULTIMASTER) {
809                                 probeinfo->adapter_bus_type = BLOGIC_PCI_BUS;
810                                 probeinfo->pci_addr = 0;
811                                 probeinfo->bus = bus;
812                                 probeinfo->dev = device;
813                                 probeinfo->irq_ch = irq_ch;
814                                 probeinfo->pci_device = pci_dev_get(pci_device);
815                                 break;
816                         }
817                 }
818         }
819         return mmcount;
820 }
821
822
823 /*
824   blogic_init_fp_probeinfo initializes the list of I/O Address
825   and Bus Probe Information to be checked for potential BusLogic FlashPoint
826   Host Adapters by interrogating the PCI Configuration Space.  It returns the
827   number of FlashPoint Host Adapters found.
828 */
829
830 static int __init blogic_init_fp_probeinfo(struct blogic_adapter *adapter)
831 {
832         int fpindex = blogic_probeinfo_count, fpcount = 0;
833         struct pci_dev *pci_device = NULL;
834         /*
835            Interrogate PCI Configuration Space for any FlashPoint Host Adapters.
836          */
837         while ((pci_device = pci_get_device(PCI_VENDOR_ID_BUSLOGIC,
838                                         PCI_DEVICE_ID_BUSLOGIC_FLASHPOINT,
839                                         pci_device)) != NULL) {
840                 unsigned char bus;
841                 unsigned char device;
842                 unsigned int irq_ch;
843                 unsigned long base_addr0;
844                 unsigned long base_addr1;
845                 unsigned long io_addr;
846                 unsigned long pci_addr;
847
848                 if (pci_enable_device(pci_device))
849                         continue;
850
851                 if (dma_set_mask(&pci_device->dev, DMA_BIT_MASK(32)))
852                         continue;
853
854                 bus = pci_device->bus->number;
855                 device = pci_device->devfn >> 3;
856                 irq_ch = pci_device->irq;
857                 io_addr = base_addr0 = pci_resource_start(pci_device, 0);
858                 pci_addr = base_addr1 = pci_resource_start(pci_device, 1);
859 #ifdef CONFIG_SCSI_FLASHPOINT
860                 if (pci_resource_flags(pci_device, 0) & IORESOURCE_MEM) {
861                         blogic_err("BusLogic: Base Address0 0x%lX not I/O for FlashPoint Host Adapter\n", NULL, base_addr0);
862                         blogic_err("at PCI Bus %d Device %d I/O Address 0x%lX\n", NULL, bus, device, io_addr);
863                         continue;
864                 }
865                 if (pci_resource_flags(pci_device, 1) & IORESOURCE_IO) {
866                         blogic_err("BusLogic: Base Address1 0x%lX not Memory for FlashPoint Host Adapter\n", NULL, base_addr1);
867                         blogic_err("at PCI Bus %d Device %d PCI Address 0x%lX\n", NULL, bus, device, pci_addr);
868                         continue;
869                 }
870                 if (irq_ch == 0) {
871                         blogic_err("BusLogic: IRQ Channel %d invalid for FlashPoint Host Adapter\n", NULL, irq_ch);
872                         blogic_err("at PCI Bus %d Device %d I/O Address 0x%lX\n", NULL, bus, device, io_addr);
873                         continue;
874                 }
875                 if (blogic_global_options.trace_probe) {
876                         blogic_notice("BusLogic: FlashPoint Host Adapter detected at\n", NULL);
877                         blogic_notice("BusLogic: PCI Bus %d Device %d I/O Address 0x%lX PCI Address 0x%lX\n", NULL, bus, device, io_addr, pci_addr);
878                 }
879                 if (blogic_probeinfo_count < BLOGIC_MAX_ADAPTERS) {
880                         struct blogic_probeinfo *probeinfo =
881                                 &blogic_probeinfo_list[blogic_probeinfo_count++];
882                         probeinfo->adapter_type = BLOGIC_FLASHPOINT;
883                         probeinfo->adapter_bus_type = BLOGIC_PCI_BUS;
884                         probeinfo->io_addr = io_addr;
885                         probeinfo->pci_addr = pci_addr;
886                         probeinfo->bus = bus;
887                         probeinfo->dev = device;
888                         probeinfo->irq_ch = irq_ch;
889                         probeinfo->pci_device = pci_dev_get(pci_device);
890                         fpcount++;
891                 } else
892                         blogic_warn("BusLogic: Too many Host Adapters detected\n", NULL);
893 #else
894                 blogic_err("BusLogic: FlashPoint Host Adapter detected at PCI Bus %d Device %d\n", NULL, bus, device);
895                 blogic_err("BusLogic: I/O Address 0x%lX PCI Address 0x%lX, irq %d, but FlashPoint\n", NULL, io_addr, pci_addr, irq_ch);
896                 blogic_err("BusLogic: support was omitted in this kernel configuration.\n", NULL);
897 #endif
898         }
899         /*
900            The FlashPoint BIOS will scan for FlashPoint Host Adapters in the order of
901            increasing PCI Bus and Device Number, so sort the probe information into
902            the same order the BIOS uses.
903          */
904         blogic_sort_probeinfo(&blogic_probeinfo_list[fpindex], fpcount);
905         return fpcount;
906 }
907
908
909 /*
910   blogic_init_probeinfo_list initializes the list of I/O Address and Bus
911   Probe Information to be checked for potential BusLogic SCSI Host Adapters by
912   interrogating the PCI Configuration Space on PCI machines as well as from the
913   list of standard BusLogic MultiMaster ISA I/O Addresses.  By default, if both
914   FlashPoint and PCI MultiMaster Host Adapters are present, this driver will
915   probe for FlashPoint Host Adapters first unless the BIOS primary disk is
916   controlled by the first PCI MultiMaster Host Adapter, in which case
917   MultiMaster Host Adapters will be probed first.  The BusLogic Driver Options
918   specifications "MultiMasterFirst" and "FlashPointFirst" can be used to force
919   a particular probe order.
920 */
921
922 static void __init blogic_init_probeinfo_list(struct blogic_adapter *adapter)
923 {
924         /*
925            If a PCI BIOS is present, interrogate it for MultiMaster and
926            FlashPoint Host Adapters; otherwise, default to the standard
927            ISA MultiMaster probe.
928          */
929         if (!blogic_probe_options.noprobe_pci) {
930                 if (blogic_probe_options.multimaster_first) {
931                         blogic_init_mm_probeinfo(adapter);
932                         blogic_init_fp_probeinfo(adapter);
933                 } else if (blogic_probe_options.flashpoint_first) {
934                         blogic_init_fp_probeinfo(adapter);
935                         blogic_init_mm_probeinfo(adapter);
936                 } else {
937                         int fpcount = blogic_init_fp_probeinfo(adapter);
938                         int mmcount = blogic_init_mm_probeinfo(adapter);
939                         if (fpcount > 0 && mmcount > 0) {
940                                 struct blogic_probeinfo *probeinfo =
941                                         &blogic_probeinfo_list[fpcount];
942                                 struct blogic_adapter *myadapter = adapter;
943                                 struct blogic_fetch_localram fetch_localram;
944                                 struct blogic_bios_drvmap d0_mapbyte;
945
946                                 while (probeinfo->adapter_bus_type !=
947                                                 BLOGIC_PCI_BUS)
948                                         probeinfo++;
949                                 myadapter->io_addr = probeinfo->io_addr;
950                                 fetch_localram.offset =
951                                         BLOGIC_BIOS_BASE + BLOGIC_BIOS_DRVMAP;
952                                 fetch_localram.count = sizeof(d0_mapbyte);
953                                 blogic_cmd(myadapter, BLOGIC_FETCH_LOCALRAM,
954                                                 &fetch_localram,
955                                                 sizeof(fetch_localram),
956                                                 &d0_mapbyte,
957                                                 sizeof(d0_mapbyte));
958                                 /*
959                                    If the Map Byte for BIOS Drive 0 indicates
960                                    that BIOS Drive 0 is controlled by this
961                                    PCI MultiMaster Host Adapter, then reverse
962                                    the probe order so that MultiMaster Host
963                                    Adapters are probed before FlashPoint Host
964                                    Adapters.
965                                  */
966                                 if (d0_mapbyte.diskgeom != BLOGIC_BIOS_NODISK) {
967                                         struct blogic_probeinfo saved_probeinfo[BLOGIC_MAX_ADAPTERS];
968                                         int mmcount = blogic_probeinfo_count - fpcount;
969
970                                         memcpy(saved_probeinfo,
971                                                 blogic_probeinfo_list,
972                                                 blogic_probeinfo_count * sizeof(struct blogic_probeinfo));
973                                         memcpy(&blogic_probeinfo_list[0],
974                                                 &saved_probeinfo[fpcount],
975                                                 mmcount * sizeof(struct blogic_probeinfo));
976                                         memcpy(&blogic_probeinfo_list[mmcount],
977                                                 &saved_probeinfo[0],
978                                                 fpcount * sizeof(struct blogic_probeinfo));
979                                 }
980                         }
981                 }
982         }
983 }
984
985
986 /*
987   blogic_failure prints a standardized error message, and then returns false.
988 */
989
990 static bool blogic_failure(struct blogic_adapter *adapter, char *msg)
991 {
992         blogic_announce_drvr(adapter);
993         if (adapter->adapter_bus_type == BLOGIC_PCI_BUS) {
994                 blogic_err("While configuring BusLogic PCI Host Adapter at\n",
995                                 adapter);
996                 blogic_err("Bus %d Device %d I/O Address 0x%lX PCI Address 0x%lX:\n", adapter, adapter->bus, adapter->dev, adapter->io_addr, adapter->pci_addr);
997         } else
998                 blogic_err("While configuring BusLogic Host Adapter at I/O Address 0x%lX:\n", adapter, adapter->io_addr);
999         blogic_err("%s FAILED - DETACHING\n", adapter, msg);
1000         if (blogic_cmd_failure_reason != NULL)
1001                 blogic_err("ADDITIONAL FAILURE INFO - %s\n", adapter,
1002                                 blogic_cmd_failure_reason);
1003         return false;
1004 }
1005
1006
1007 /*
1008   blogic_probe probes for a BusLogic Host Adapter.
1009 */
1010
1011 static bool __init blogic_probe(struct blogic_adapter *adapter)
1012 {
1013         union blogic_stat_reg statusreg;
1014         union blogic_int_reg intreg;
1015         union blogic_geo_reg georeg;
1016         /*
1017            FlashPoint Host Adapters are Probed by the FlashPoint SCCB Manager.
1018          */
1019         if (blogic_flashpoint_type(adapter)) {
1020                 struct fpoint_info *fpinfo = &adapter->fpinfo;
1021                 fpinfo->base_addr = (u32) adapter->io_addr;
1022                 fpinfo->irq_ch = adapter->irq_ch;
1023                 fpinfo->present = false;
1024                 if (!(FlashPoint_ProbeHostAdapter(fpinfo) == 0 &&
1025                                         fpinfo->present)) {
1026                         blogic_err("BusLogic: FlashPoint Host Adapter detected at PCI Bus %d Device %d\n", adapter, adapter->bus, adapter->dev);
1027                         blogic_err("BusLogic: I/O Address 0x%lX PCI Address 0x%lX, but FlashPoint\n", adapter, adapter->io_addr, adapter->pci_addr);
1028                         blogic_err("BusLogic: Probe Function failed to validate it.\n", adapter);
1029                         return false;
1030                 }
1031                 if (blogic_global_options.trace_probe)
1032                         blogic_notice("BusLogic_Probe(0x%lX): FlashPoint Found\n", adapter, adapter->io_addr);
1033                 /*
1034                    Indicate the Host Adapter Probe completed successfully.
1035                  */
1036                 return true;
1037         }
1038         /*
1039            Read the Status, Interrupt, and Geometry Registers to test if there are I/O
1040            ports that respond, and to check the values to determine if they are from a
1041            BusLogic Host Adapter.  A nonexistent I/O port will return 0xFF, in which
1042            case there is definitely no BusLogic Host Adapter at this base I/O Address.
1043            The test here is a subset of that used by the BusLogic Host Adapter BIOS.
1044          */
1045         statusreg.all = blogic_rdstatus(adapter);
1046         intreg.all = blogic_rdint(adapter);
1047         georeg.all = blogic_rdgeom(adapter);
1048         if (blogic_global_options.trace_probe)
1049                 blogic_notice("BusLogic_Probe(0x%lX): Status 0x%02X, Interrupt 0x%02X, Geometry 0x%02X\n", adapter, adapter->io_addr, statusreg.all, intreg.all, georeg.all);
1050         if (statusreg.all == 0 || statusreg.sr.diag_active ||
1051                         statusreg.sr.cmd_param_busy || statusreg.sr.rsvd ||
1052                         statusreg.sr.cmd_invalid || intreg.ir.rsvd != 0)
1053                 return false;
1054         /*
1055            Check the undocumented Geometry Register to test if there is
1056            an I/O port that responded.  Adaptec Host Adapters do not
1057            implement the Geometry Register, so this test helps serve to
1058            avoid incorrectly recognizing an Adaptec 1542A or 1542B as a
1059            BusLogic.  Unfortunately, the Adaptec 1542C series does respond
1060            to the Geometry Register I/O port, but it will be rejected
1061            later when the Inquire Extended Setup Information command is
1062            issued in blogic_checkadapter.  The AMI FastDisk Host Adapter
1063            is a BusLogic clone that implements the same interface as
1064            earlier BusLogic Host Adapters, including the undocumented
1065            commands, and is therefore supported by this driver. However,
1066            the AMI FastDisk always returns 0x00 upon reading the Geometry
1067            Register, so the extended translation option should always be
1068            left disabled on the AMI FastDisk.
1069          */
1070         if (georeg.all == 0xFF)
1071                 return false;
1072         /*
1073            Indicate the Host Adapter Probe completed successfully.
1074          */
1075         return true;
1076 }
1077
1078
1079 /*
1080   blogic_hwreset issues a Hardware Reset to the Host Adapter
1081   and waits for Host Adapter Diagnostics to complete.  If hard_reset is true, a
1082   Hard Reset is performed which also initiates a SCSI Bus Reset.  Otherwise, a
1083   Soft Reset is performed which only resets the Host Adapter without forcing a
1084   SCSI Bus Reset.
1085 */
1086
1087 static bool blogic_hwreset(struct blogic_adapter *adapter, bool hard_reset)
1088 {
1089         union blogic_stat_reg statusreg;
1090         int timeout;
1091         /*
1092            FlashPoint Host Adapters are Hard Reset by the FlashPoint
1093            SCCB Manager.
1094          */
1095         if (blogic_flashpoint_type(adapter)) {
1096                 struct fpoint_info *fpinfo = &adapter->fpinfo;
1097                 fpinfo->softreset = !hard_reset;
1098                 fpinfo->report_underrun = true;
1099                 adapter->cardhandle =
1100                         FlashPoint_HardwareResetHostAdapter(fpinfo);
1101                 if (adapter->cardhandle == (void *)FPOINT_BADCARD_HANDLE)
1102                         return false;
1103                 /*
1104                    Indicate the Host Adapter Hard Reset completed successfully.
1105                  */
1106                 return true;
1107         }
1108         /*
1109            Issue a Hard Reset or Soft Reset Command to the Host Adapter.
1110            The Host Adapter should respond by setting Diagnostic Active in
1111            the Status Register.
1112          */
1113         if (hard_reset)
1114                 blogic_hardreset(adapter);
1115         else
1116                 blogic_softreset(adapter);
1117         /*
1118            Wait until Diagnostic Active is set in the Status Register.
1119          */
1120         timeout = 5 * 10000;
1121         while (--timeout >= 0) {
1122                 statusreg.all = blogic_rdstatus(adapter);
1123                 if (statusreg.sr.diag_active)
1124                         break;
1125                 udelay(100);
1126         }
1127         if (blogic_global_options.trace_hw_reset)
1128                 blogic_notice("BusLogic_HardwareReset(0x%lX): Diagnostic Active, Status 0x%02X\n", adapter, adapter->io_addr, statusreg.all);
1129         if (timeout < 0)
1130                 return false;
1131         /*
1132            Wait 100 microseconds to allow completion of any initial diagnostic
1133            activity which might leave the contents of the Status Register
1134            unpredictable.
1135          */
1136         udelay(100);
1137         /*
1138            Wait until Diagnostic Active is reset in the Status Register.
1139          */
1140         timeout = 10 * 10000;
1141         while (--timeout >= 0) {
1142                 statusreg.all = blogic_rdstatus(adapter);
1143                 if (!statusreg.sr.diag_active)
1144                         break;
1145                 udelay(100);
1146         }
1147         if (blogic_global_options.trace_hw_reset)
1148                 blogic_notice("BusLogic_HardwareReset(0x%lX): Diagnostic Completed, Status 0x%02X\n", adapter, adapter->io_addr, statusreg.all);
1149         if (timeout < 0)
1150                 return false;
1151         /*
1152            Wait until at least one of the Diagnostic Failure, Host Adapter
1153            Ready, or Data In Register Ready bits is set in the Status Register.
1154          */
1155         timeout = 10000;
1156         while (--timeout >= 0) {
1157                 statusreg.all = blogic_rdstatus(adapter);
1158                 if (statusreg.sr.diag_failed || statusreg.sr.adapter_ready ||
1159                                 statusreg.sr.datain_ready)
1160                         break;
1161                 udelay(100);
1162         }
1163         if (blogic_global_options.trace_hw_reset)
1164                 blogic_notice("BusLogic_HardwareReset(0x%lX): Host Adapter Ready, Status 0x%02X\n", adapter, adapter->io_addr, statusreg.all);
1165         if (timeout < 0)
1166                 return false;
1167         /*
1168            If Diagnostic Failure is set or Host Adapter Ready is reset,
1169            then an error occurred during the Host Adapter diagnostics.
1170            If Data In Register Ready is set, then there is an Error Code
1171            available.
1172          */
1173         if (statusreg.sr.diag_failed || !statusreg.sr.adapter_ready) {
1174                 blogic_cmd_failure_reason = NULL;
1175                 blogic_failure(adapter, "HARD RESET DIAGNOSTICS");
1176                 blogic_err("HOST ADAPTER STATUS REGISTER = %02X\n", adapter,
1177                                 statusreg.all);
1178                 if (statusreg.sr.datain_ready)
1179                         blogic_err("HOST ADAPTER ERROR CODE = %d\n", adapter,
1180                                         blogic_rddatain(adapter));
1181                 return false;
1182         }
1183         /*
1184            Indicate the Host Adapter Hard Reset completed successfully.
1185          */
1186         return true;
1187 }
1188
1189
1190 /*
1191   blogic_checkadapter checks to be sure this really is a BusLogic
1192   Host Adapter.
1193 */
1194
1195 static bool __init blogic_checkadapter(struct blogic_adapter *adapter)
1196 {
1197         struct blogic_ext_setup ext_setupinfo;
1198         unsigned char req_replylen;
1199         bool result = true;
1200         /*
1201            FlashPoint Host Adapters do not require this protection.
1202          */
1203         if (blogic_flashpoint_type(adapter))
1204                 return true;
1205         /*
1206            Issue the Inquire Extended Setup Information command. Only genuine
1207            BusLogic Host Adapters and true clones support this command.
1208            Adaptec 1542C series Host Adapters that respond to the Geometry
1209            Register I/O port will fail this command.
1210          */
1211         req_replylen = sizeof(ext_setupinfo);
1212         if (blogic_cmd(adapter, BLOGIC_INQ_EXTSETUP, &req_replylen,
1213                                 sizeof(req_replylen), &ext_setupinfo,
1214                                 sizeof(ext_setupinfo)) != sizeof(ext_setupinfo))
1215                 result = false;
1216         /*
1217            Provide tracing information if requested and return.
1218          */
1219         if (blogic_global_options.trace_probe)
1220                 blogic_notice("BusLogic_Check(0x%lX): MultiMaster %s\n", adapter,
1221                                 adapter->io_addr,
1222                                 (result ? "Found" : "Not Found"));
1223         return result;
1224 }
1225
1226
1227 /*
1228   blogic_rdconfig reads the Configuration Information
1229   from Host Adapter and initializes the Host Adapter structure.
1230 */
1231
1232 static bool __init blogic_rdconfig(struct blogic_adapter *adapter)
1233 {
1234         struct blogic_board_id id;
1235         struct blogic_config config;
1236         struct blogic_setup_info setupinfo;
1237         struct blogic_ext_setup ext_setupinfo;
1238         unsigned char model[5];
1239         unsigned char fw_ver_digit3;
1240         unsigned char fw_ver_letter;
1241         struct blogic_adapter_info adapter_info;
1242         struct blogic_fetch_localram fetch_localram;
1243         struct blogic_autoscsi autoscsi;
1244         union blogic_geo_reg georeg;
1245         unsigned char req_replylen;
1246         unsigned char *tgt, ch;
1247         int tgt_id, i;
1248         /*
1249            Configuration Information for FlashPoint Host Adapters is
1250            provided in the fpoint_info structure by the FlashPoint
1251            SCCB Manager's Probe Function. Initialize fields in the
1252            Host Adapter structure from the fpoint_info structure.
1253          */
1254         if (blogic_flashpoint_type(adapter)) {
1255                 struct fpoint_info *fpinfo = &adapter->fpinfo;
1256                 tgt = adapter->model;
1257                 *tgt++ = 'B';
1258                 *tgt++ = 'T';
1259                 *tgt++ = '-';
1260                 for (i = 0; i < sizeof(fpinfo->model); i++)
1261                         *tgt++ = fpinfo->model[i];
1262                 *tgt++ = '\0';
1263                 strcpy(adapter->fw_ver, FLASHPOINT_FW_VER);
1264                 adapter->scsi_id = fpinfo->scsi_id;
1265                 adapter->ext_trans_enable = fpinfo->ext_trans_enable;
1266                 adapter->parity = fpinfo->parity;
1267                 adapter->reset_enabled = !fpinfo->softreset;
1268                 adapter->level_int = true;
1269                 adapter->wide = fpinfo->wide;
1270                 adapter->differential = false;
1271                 adapter->scam = true;
1272                 adapter->ultra = true;
1273                 adapter->ext_lun = true;
1274                 adapter->terminfo_valid = true;
1275                 adapter->low_term = fpinfo->low_term;
1276                 adapter->high_term = fpinfo->high_term;
1277                 adapter->scam_enabled = fpinfo->scam_enabled;
1278                 adapter->scam_lev2 = fpinfo->scam_lev2;
1279                 adapter->drvr_sglimit = BLOGIC_SG_LIMIT;
1280                 adapter->maxdev = (adapter->wide ? 16 : 8);
1281                 adapter->maxlun = 32;
1282                 adapter->initccbs = 4 * BLOGIC_CCB_GRP_ALLOCSIZE;
1283                 adapter->inc_ccbs = BLOGIC_CCB_GRP_ALLOCSIZE;
1284                 adapter->drvr_qdepth = 255;
1285                 adapter->adapter_qdepth = adapter->drvr_qdepth;
1286                 adapter->sync_ok = fpinfo->sync_ok;
1287                 adapter->fast_ok = fpinfo->fast_ok;
1288                 adapter->ultra_ok = fpinfo->ultra_ok;
1289                 adapter->wide_ok = fpinfo->wide_ok;
1290                 adapter->discon_ok = fpinfo->discon_ok;
1291                 adapter->tagq_ok = 0xFFFF;
1292                 goto common;
1293         }
1294         /*
1295            Issue the Inquire Board ID command.
1296          */
1297         if (blogic_cmd(adapter, BLOGIC_GET_BOARD_ID, NULL, 0, &id,
1298                                 sizeof(id)) != sizeof(id))
1299                 return blogic_failure(adapter, "INQUIRE BOARD ID");
1300         /*
1301            Issue the Inquire Configuration command.
1302          */
1303         if (blogic_cmd(adapter, BLOGIC_INQ_CONFIG, NULL, 0, &config,
1304                                 sizeof(config))
1305             != sizeof(config))
1306                 return blogic_failure(adapter, "INQUIRE CONFIGURATION");
1307         /*
1308            Issue the Inquire Setup Information command.
1309          */
1310         req_replylen = sizeof(setupinfo);
1311         if (blogic_cmd(adapter, BLOGIC_INQ_SETUPINFO, &req_replylen,
1312                                 sizeof(req_replylen), &setupinfo,
1313                                 sizeof(setupinfo)) != sizeof(setupinfo))
1314                 return blogic_failure(adapter, "INQUIRE SETUP INFORMATION");
1315         /*
1316            Issue the Inquire Extended Setup Information command.
1317          */
1318         req_replylen = sizeof(ext_setupinfo);
1319         if (blogic_cmd(adapter, BLOGIC_INQ_EXTSETUP, &req_replylen,
1320                                 sizeof(req_replylen), &ext_setupinfo,
1321                                 sizeof(ext_setupinfo)) != sizeof(ext_setupinfo))
1322                 return blogic_failure(adapter,
1323                                         "INQUIRE EXTENDED SETUP INFORMATION");
1324         /*
1325            Issue the Inquire Firmware Version 3rd Digit command.
1326          */
1327         fw_ver_digit3 = '\0';
1328         if (id.fw_ver_digit1 > '0')
1329                 if (blogic_cmd(adapter, BLOGIC_INQ_FWVER_D3, NULL, 0,
1330                                 &fw_ver_digit3,
1331                                 sizeof(fw_ver_digit3)) != sizeof(fw_ver_digit3))
1332                         return blogic_failure(adapter,
1333                                                 "INQUIRE FIRMWARE 3RD DIGIT");
1334         /*
1335            Issue the Inquire Host Adapter Model Number command.
1336          */
1337         if (ext_setupinfo.bus_type == 'A' && id.fw_ver_digit1 == '2')
1338                 /* BusLogic BT-542B ISA 2.xx */
1339                 strcpy(model, "542B");
1340         else if (ext_setupinfo.bus_type == 'E' && id.fw_ver_digit1 == '2' &&
1341                         (id.fw_ver_digit2 <= '1' || (id.fw_ver_digit2 == '2' &&
1342                                                      fw_ver_digit3 == '0')))
1343                 /* BusLogic BT-742A EISA 2.1x or 2.20 */
1344                 strcpy(model, "742A");
1345         else if (ext_setupinfo.bus_type == 'E' && id.fw_ver_digit1 == '0')
1346                 /* AMI FastDisk EISA Series 441 0.x */
1347                 strcpy(model, "747A");
1348         else {
1349                 req_replylen = sizeof(model);
1350                 if (blogic_cmd(adapter, BLOGIC_INQ_MODELNO, &req_replylen,
1351                                         sizeof(req_replylen), &model,
1352                                         sizeof(model)) != sizeof(model))
1353                         return blogic_failure(adapter,
1354                                         "INQUIRE HOST ADAPTER MODEL NUMBER");
1355         }
1356         /*
1357            BusLogic MultiMaster Host Adapters can be identified by their
1358            model number and the major version number of their firmware
1359            as follows:
1360
1361            5.xx       BusLogic "W" Series Host Adapters:
1362            BT-948/958/958D
1363            4.xx       BusLogic "C" Series Host Adapters:
1364            BT-946C/956C/956CD/747C/757C/757CD/445C/545C/540CF
1365            3.xx       BusLogic "S" Series Host Adapters:
1366            BT-747S/747D/757S/757D/445S/545S/542D
1367            BT-542B/742A (revision H)
1368            2.xx       BusLogic "A" Series Host Adapters:
1369            BT-542B/742A (revision G and below)
1370            0.xx       AMI FastDisk VLB/EISA BusLogic Clone Host Adapter
1371          */
1372         /*
1373            Save the Model Name and Host Adapter Name in the Host Adapter
1374            structure.
1375          */
1376         tgt = adapter->model;
1377         *tgt++ = 'B';
1378         *tgt++ = 'T';
1379         *tgt++ = '-';
1380         for (i = 0; i < sizeof(model); i++) {
1381                 ch = model[i];
1382                 if (ch == ' ' || ch == '\0')
1383                         break;
1384                 *tgt++ = ch;
1385         }
1386         *tgt++ = '\0';
1387         /*
1388            Save the Firmware Version in the Host Adapter structure.
1389          */
1390         tgt = adapter->fw_ver;
1391         *tgt++ = id.fw_ver_digit1;
1392         *tgt++ = '.';
1393         *tgt++ = id.fw_ver_digit2;
1394         if (fw_ver_digit3 != ' ' && fw_ver_digit3 != '\0')
1395                 *tgt++ = fw_ver_digit3;
1396         *tgt = '\0';
1397         /*
1398            Issue the Inquire Firmware Version Letter command.
1399          */
1400         if (strcmp(adapter->fw_ver, "3.3") >= 0) {
1401                 if (blogic_cmd(adapter, BLOGIC_INQ_FWVER_LETTER, NULL, 0,
1402                                 &fw_ver_letter,
1403                                 sizeof(fw_ver_letter)) != sizeof(fw_ver_letter))
1404                         return blogic_failure(adapter,
1405                                         "INQUIRE FIRMWARE VERSION LETTER");
1406                 if (fw_ver_letter != ' ' && fw_ver_letter != '\0')
1407                         *tgt++ = fw_ver_letter;
1408                 *tgt = '\0';
1409         }
1410         /*
1411            Save the Host Adapter SCSI ID in the Host Adapter structure.
1412          */
1413         adapter->scsi_id = config.id;
1414         /*
1415            Determine the Bus Type and save it in the Host Adapter structure,
1416            determine and save the IRQ Channel if necessary, and determine
1417            and save the DMA Channel for ISA Host Adapters.
1418          */
1419         adapter->adapter_bus_type =
1420                         blogic_adater_bus_types[adapter->model[3] - '4'];
1421         if (adapter->irq_ch == 0) {
1422                 if (config.irq_ch9)
1423                         adapter->irq_ch = 9;
1424                 else if (config.irq_ch10)
1425                         adapter->irq_ch = 10;
1426                 else if (config.irq_ch11)
1427                         adapter->irq_ch = 11;
1428                 else if (config.irq_ch12)
1429                         adapter->irq_ch = 12;
1430                 else if (config.irq_ch14)
1431                         adapter->irq_ch = 14;
1432                 else if (config.irq_ch15)
1433                         adapter->irq_ch = 15;
1434         }
1435         /*
1436            Determine whether Extended Translation is enabled and save it in
1437            the Host Adapter structure.
1438          */
1439         georeg.all = blogic_rdgeom(adapter);
1440         adapter->ext_trans_enable = georeg.gr.ext_trans_enable;
1441         /*
1442            Save the Scatter Gather Limits, Level Sensitive Interrupt flag, Wide
1443            SCSI flag, Differential SCSI flag, SCAM Supported flag, and
1444            Ultra SCSI flag in the Host Adapter structure.
1445          */
1446         adapter->adapter_sglimit = ext_setupinfo.sg_limit;
1447         adapter->drvr_sglimit = adapter->adapter_sglimit;
1448         if (adapter->adapter_sglimit > BLOGIC_SG_LIMIT)
1449                 adapter->drvr_sglimit = BLOGIC_SG_LIMIT;
1450         if (ext_setupinfo.misc.level_int)
1451                 adapter->level_int = true;
1452         adapter->wide = ext_setupinfo.wide;
1453         adapter->differential = ext_setupinfo.differential;
1454         adapter->scam = ext_setupinfo.scam;
1455         adapter->ultra = ext_setupinfo.ultra;
1456         /*
1457            Determine whether Extended LUN Format CCBs are supported and save the
1458            information in the Host Adapter structure.
1459          */
1460         if (adapter->fw_ver[0] == '5' || (adapter->fw_ver[0] == '4' &&
1461                                 adapter->wide))
1462                 adapter->ext_lun = true;
1463         /*
1464            Issue the Inquire PCI Host Adapter Information command to read the
1465            Termination Information from "W" series MultiMaster Host Adapters.
1466          */
1467         if (adapter->fw_ver[0] == '5') {
1468                 if (blogic_cmd(adapter, BLOGIC_INQ_PCI_INFO, NULL, 0,
1469                                 &adapter_info,
1470                                 sizeof(adapter_info)) != sizeof(adapter_info))
1471                         return blogic_failure(adapter,
1472                                         "INQUIRE PCI HOST ADAPTER INFORMATION");
1473                 /*
1474                    Save the Termination Information in the Host Adapter
1475                    structure.
1476                  */
1477                 if (adapter_info.genericinfo_valid) {
1478                         adapter->terminfo_valid = true;
1479                         adapter->low_term = adapter_info.low_term;
1480                         adapter->high_term = adapter_info.high_term;
1481                 }
1482         }
1483         /*
1484            Issue the Fetch Host Adapter Local RAM command to read the
1485            AutoSCSI data from "W" and "C" series MultiMaster Host Adapters.
1486          */
1487         if (adapter->fw_ver[0] >= '4') {
1488                 fetch_localram.offset = BLOGIC_AUTOSCSI_BASE;
1489                 fetch_localram.count = sizeof(autoscsi);
1490                 if (blogic_cmd(adapter, BLOGIC_FETCH_LOCALRAM, &fetch_localram,
1491                                         sizeof(fetch_localram), &autoscsi,
1492                                         sizeof(autoscsi)) != sizeof(autoscsi))
1493                         return blogic_failure(adapter,
1494                                                 "FETCH HOST ADAPTER LOCAL RAM");
1495                 /*
1496                    Save the Parity Checking Enabled, Bus Reset Enabled,
1497                    and Termination Information in the Host Adapter structure.
1498                  */
1499                 adapter->parity = autoscsi.parity;
1500                 adapter->reset_enabled = autoscsi.reset_enabled;
1501                 if (adapter->fw_ver[0] == '4') {
1502                         adapter->terminfo_valid = true;
1503                         adapter->low_term = autoscsi.low_term;
1504                         adapter->high_term = autoscsi.high_term;
1505                 }
1506                 /*
1507                    Save the Wide Permitted, Fast Permitted, Synchronous
1508                    Permitted, Disconnect Permitted, Ultra Permitted, and
1509                    SCAM Information in the Host Adapter structure.
1510                  */
1511                 adapter->wide_ok = autoscsi.wide_ok;
1512                 adapter->fast_ok = autoscsi.fast_ok;
1513                 adapter->sync_ok = autoscsi.sync_ok;
1514                 adapter->discon_ok = autoscsi.discon_ok;
1515                 if (adapter->ultra)
1516                         adapter->ultra_ok = autoscsi.ultra_ok;
1517                 if (adapter->scam) {
1518                         adapter->scam_enabled = autoscsi.scam_enabled;
1519                         adapter->scam_lev2 = autoscsi.scam_lev2;
1520                 }
1521         }
1522         /*
1523            Initialize fields in the Host Adapter structure for "S" and "A"
1524            series MultiMaster Host Adapters.
1525          */
1526         if (adapter->fw_ver[0] < '4') {
1527                 if (setupinfo.sync) {
1528                         adapter->sync_ok = 0xFF;
1529                         if (adapter->adapter_bus_type == BLOGIC_EISA_BUS) {
1530                                 if (ext_setupinfo.misc.fast_on_eisa)
1531                                         adapter->fast_ok = 0xFF;
1532                                 if (strcmp(adapter->model, "BT-757") == 0)
1533                                         adapter->wide_ok = 0xFF;
1534                         }
1535                 }
1536                 adapter->discon_ok = 0xFF;
1537                 adapter->parity = setupinfo.parity;
1538                 adapter->reset_enabled = true;
1539         }
1540         /*
1541            Determine the maximum number of Target IDs and Logical Units
1542            supported by this driver for Wide and Narrow Host Adapters.
1543          */
1544         adapter->maxdev = (adapter->wide ? 16 : 8);
1545         adapter->maxlun = (adapter->ext_lun ? 32 : 8);
1546         /*
1547            Select appropriate values for the Mailbox Count, Driver Queue Depth,
1548            Initial CCBs, and Incremental CCBs variables based on whether
1549            or not Strict Round Robin Mode is supported.  If Strict Round
1550            Robin Mode is supported, then there is no performance degradation
1551            in using the maximum possible number of Outgoing and Incoming
1552            Mailboxes and allowing the Tagged and Untagged Queue Depths to
1553            determine the actual utilization.  If Strict Round Robin Mode is
1554            not supported, then the Host Adapter must scan all the Outgoing
1555            Mailboxes whenever an Outgoing Mailbox entry is made, which can
1556            cause a substantial performance penalty.  The host adapters
1557            actually have room to store the following number of CCBs
1558            internally; that is, they can internally queue and manage this
1559            many active commands on the SCSI bus simultaneously.  Performance
1560            measurements demonstrate that the Driver Queue Depth should be
1561            set to the Mailbox Count, rather than the Host Adapter Queue
1562            Depth (internal CCB capacity), as it is more efficient to have the
1563            queued commands waiting in Outgoing Mailboxes if necessary than
1564            to block the process in the higher levels of the SCSI Subsystem.
1565
1566            192          BT-948/958/958D
1567            100          BT-946C/956C/956CD/747C/757C/757CD/445C
1568            50   BT-545C/540CF
1569            30   BT-747S/747D/757S/757D/445S/545S/542D/542B/742A
1570          */
1571         if (adapter->fw_ver[0] == '5')
1572                 adapter->adapter_qdepth = 192;
1573         else if (adapter->fw_ver[0] == '4')
1574                 adapter->adapter_qdepth = 100;
1575         else
1576                 adapter->adapter_qdepth = 30;
1577         if (strcmp(adapter->fw_ver, "3.31") >= 0) {
1578                 adapter->strict_rr = true;
1579                 adapter->mbox_count = BLOGIC_MAX_MAILBOX;
1580         } else {
1581                 adapter->strict_rr = false;
1582                 adapter->mbox_count = 32;
1583         }
1584         adapter->drvr_qdepth = adapter->mbox_count;
1585         adapter->initccbs = 4 * BLOGIC_CCB_GRP_ALLOCSIZE;
1586         adapter->inc_ccbs = BLOGIC_CCB_GRP_ALLOCSIZE;
1587         /*
1588            Tagged Queuing support is available and operates properly on
1589            all "W" series MultiMaster Host Adapters, on "C" series
1590            MultiMaster Host Adapters with firmware version 4.22 and above,
1591            and on "S" series MultiMaster Host Adapters with firmware version
1592            3.35 and above.
1593          */
1594         adapter->tagq_ok = 0;
1595         switch (adapter->fw_ver[0]) {
1596         case '5':
1597                 adapter->tagq_ok = 0xFFFF;
1598                 break;
1599         case '4':
1600                 if (strcmp(adapter->fw_ver, "4.22") >= 0)
1601                         adapter->tagq_ok = 0xFFFF;
1602                 break;
1603         case '3':
1604                 if (strcmp(adapter->fw_ver, "3.35") >= 0)
1605                         adapter->tagq_ok = 0xFFFF;
1606                 break;
1607         }
1608         /*
1609            Determine the Host Adapter BIOS Address if the BIOS is enabled and
1610            save it in the Host Adapter structure.  The BIOS is disabled if the
1611            bios_addr is 0.
1612          */
1613         adapter->bios_addr = ext_setupinfo.bios_addr << 12;
1614         /*
1615            BusLogic BT-445S Host Adapters prior to board revision E have a
1616            hardware bug whereby when the BIOS is enabled, transfers to/from
1617            the same address range the BIOS occupies modulo 16MB are handled
1618            incorrectly.  Only properly functioning BT-445S Host Adapters
1619            have firmware version 3.37.
1620          */
1621         if (adapter->bios_addr > 0 &&
1622             strcmp(adapter->model, "BT-445S") == 0 &&
1623             strcmp(adapter->fw_ver, "3.37") < 0)
1624                 return blogic_failure(adapter, "Too old firmware");
1625         /*
1626            Initialize parameters common to MultiMaster and FlashPoint
1627            Host Adapters.
1628          */
1629 common:
1630         /*
1631            Initialize the Host Adapter Full Model Name from the Model Name.
1632          */
1633         strcpy(adapter->full_model, "BusLogic ");
1634         strcat(adapter->full_model, adapter->model);
1635         /*
1636            Select an appropriate value for the Tagged Queue Depth either from a
1637            BusLogic Driver Options specification, or based on whether this Host
1638            Adapter requires that ISA Bounce Buffers be used.  The Tagged Queue
1639            Depth is left at 0 for automatic determination in
1640            BusLogic_SelectQueueDepths. Initialize the Untagged Queue Depth.
1641          */
1642         for (tgt_id = 0; tgt_id < BLOGIC_MAXDEV; tgt_id++) {
1643                 unsigned char qdepth = 0;
1644                 if (adapter->drvr_opts != NULL &&
1645                                 adapter->drvr_opts->qdepth[tgt_id] > 0)
1646                         qdepth = adapter->drvr_opts->qdepth[tgt_id];
1647                 adapter->qdepth[tgt_id] = qdepth;
1648         }
1649         adapter->untag_qdepth = BLOGIC_UNTAG_DEPTH;
1650         if (adapter->drvr_opts != NULL)
1651                 adapter->common_qdepth = adapter->drvr_opts->common_qdepth;
1652         if (adapter->common_qdepth > 0 &&
1653                         adapter->common_qdepth < adapter->untag_qdepth)
1654                 adapter->untag_qdepth = adapter->common_qdepth;
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         adapter->tagq_ok &= adapter->discon_ok;
1661         /*
1662            Combine the default Tagged Queuing Permitted bits with any
1663            BusLogic Driver Options Tagged Queuing specification.
1664          */
1665         if (adapter->drvr_opts != NULL)
1666                 adapter->tagq_ok = (adapter->drvr_opts->tagq_ok &
1667                                 adapter->drvr_opts->tagq_ok_mask) |
1668                         (adapter->tagq_ok & ~adapter->drvr_opts->tagq_ok_mask);
1669
1670         /*
1671            Select an appropriate value for Bus Settle Time either from a
1672            BusLogic Driver Options specification, or from
1673            BLOGIC_BUS_SETTLE_TIME.
1674          */
1675         if (adapter->drvr_opts != NULL &&
1676                         adapter->drvr_opts->bus_settle_time > 0)
1677                 adapter->bus_settle_time = adapter->drvr_opts->bus_settle_time;
1678         else
1679                 adapter->bus_settle_time = BLOGIC_BUS_SETTLE_TIME;
1680         /*
1681            Indicate reading the Host Adapter Configuration completed
1682            successfully.
1683          */
1684         return true;
1685 }
1686
1687
1688 /*
1689   blogic_reportconfig reports the configuration of Host Adapter.
1690 */
1691
1692 static bool __init blogic_reportconfig(struct blogic_adapter *adapter)
1693 {
1694         unsigned short alltgt_mask = (1 << adapter->maxdev) - 1;
1695         unsigned short sync_ok, fast_ok;
1696         unsigned short ultra_ok, wide_ok;
1697         unsigned short discon_ok, tagq_ok;
1698         bool common_syncneg, common_tagq_depth;
1699         char syncstr[BLOGIC_MAXDEV + 1];
1700         char widestr[BLOGIC_MAXDEV + 1];
1701         char discon_str[BLOGIC_MAXDEV + 1];
1702         char tagq_str[BLOGIC_MAXDEV + 1];
1703         char *syncmsg = syncstr;
1704         char *widemsg = widestr;
1705         char *discon_msg = discon_str;
1706         char *tagq_msg = tagq_str;
1707         int tgt_id;
1708
1709         blogic_info("Configuring BusLogic Model %s %s%s%s%s SCSI Host Adapter\n", adapter, adapter->model, blogic_adapter_busnames[adapter->adapter_bus_type], (adapter->wide ? " Wide" : ""), (adapter->differential ? " Differential" : ""), (adapter->ultra ? " Ultra" : ""));
1710         blogic_info("  Firmware Version: %s, I/O Address: 0x%lX, IRQ Channel: %d/%s\n", adapter, adapter->fw_ver, adapter->io_addr, adapter->irq_ch, (adapter->level_int ? "Level" : "Edge"));
1711         if (adapter->adapter_bus_type != BLOGIC_PCI_BUS) {
1712                 blogic_info("  DMA Channel: None, ", adapter);
1713                 if (adapter->bios_addr > 0)
1714                         blogic_info("BIOS Address: 0x%X, ", adapter,
1715                                         adapter->bios_addr);
1716                 else
1717                         blogic_info("BIOS Address: None, ", adapter);
1718         } else {
1719                 blogic_info("  PCI Bus: %d, Device: %d, Address: ", adapter,
1720                                 adapter->bus, adapter->dev);
1721                 if (adapter->pci_addr > 0)
1722                         blogic_info("0x%lX, ", adapter, adapter->pci_addr);
1723                 else
1724                         blogic_info("Unassigned, ", adapter);
1725         }
1726         blogic_info("Host Adapter SCSI ID: %d\n", adapter, adapter->scsi_id);
1727         blogic_info("  Parity Checking: %s, Extended Translation: %s\n",
1728                         adapter, (adapter->parity ? "Enabled" : "Disabled"),
1729                         (adapter->ext_trans_enable ? "Enabled" : "Disabled"));
1730         alltgt_mask &= ~(1 << adapter->scsi_id);
1731         sync_ok = adapter->sync_ok & alltgt_mask;
1732         fast_ok = adapter->fast_ok & alltgt_mask;
1733         ultra_ok = adapter->ultra_ok & alltgt_mask;
1734         if ((blogic_multimaster_type(adapter) &&
1735                         (adapter->fw_ver[0] >= '4' ||
1736                          adapter->adapter_bus_type == BLOGIC_EISA_BUS)) ||
1737                         blogic_flashpoint_type(adapter)) {
1738                 common_syncneg = false;
1739                 if (sync_ok == 0) {
1740                         syncmsg = "Disabled";
1741                         common_syncneg = true;
1742                 } else if (sync_ok == alltgt_mask) {
1743                         if (fast_ok == 0) {
1744                                 syncmsg = "Slow";
1745                                 common_syncneg = true;
1746                         } else if (fast_ok == alltgt_mask) {
1747                                 if (ultra_ok == 0) {
1748                                         syncmsg = "Fast";
1749                                         common_syncneg = true;
1750                                 } else if (ultra_ok == alltgt_mask) {
1751                                         syncmsg = "Ultra";
1752                                         common_syncneg = true;
1753                                 }
1754                         }
1755                 }
1756                 if (!common_syncneg) {
1757                         for (tgt_id = 0; tgt_id < adapter->maxdev; tgt_id++)
1758                                 syncstr[tgt_id] = ((!(sync_ok & (1 << tgt_id))) ? 'N' : (!(fast_ok & (1 << tgt_id)) ? 'S' : (!(ultra_ok & (1 << tgt_id)) ? 'F' : 'U')));
1759                         syncstr[adapter->scsi_id] = '#';
1760                         syncstr[adapter->maxdev] = '\0';
1761                 }
1762         } else
1763                 syncmsg = (sync_ok == 0 ? "Disabled" : "Enabled");
1764         wide_ok = adapter->wide_ok & alltgt_mask;
1765         if (wide_ok == 0)
1766                 widemsg = "Disabled";
1767         else if (wide_ok == alltgt_mask)
1768                 widemsg = "Enabled";
1769         else {
1770                 for (tgt_id = 0; tgt_id < adapter->maxdev; tgt_id++)
1771                         widestr[tgt_id] = ((wide_ok & (1 << tgt_id)) ? 'Y' : 'N');
1772                 widestr[adapter->scsi_id] = '#';
1773                 widestr[adapter->maxdev] = '\0';
1774         }
1775         discon_ok = adapter->discon_ok & alltgt_mask;
1776         if (discon_ok == 0)
1777                 discon_msg = "Disabled";
1778         else if (discon_ok == alltgt_mask)
1779                 discon_msg = "Enabled";
1780         else {
1781                 for (tgt_id = 0; tgt_id < adapter->maxdev; tgt_id++)
1782                         discon_str[tgt_id] = ((discon_ok & (1 << tgt_id)) ? 'Y' : 'N');
1783                 discon_str[adapter->scsi_id] = '#';
1784                 discon_str[adapter->maxdev] = '\0';
1785         }
1786         tagq_ok = adapter->tagq_ok & alltgt_mask;
1787         if (tagq_ok == 0)
1788                 tagq_msg = "Disabled";
1789         else if (tagq_ok == alltgt_mask)
1790                 tagq_msg = "Enabled";
1791         else {
1792                 for (tgt_id = 0; tgt_id < adapter->maxdev; tgt_id++)
1793                         tagq_str[tgt_id] = ((tagq_ok & (1 << tgt_id)) ? 'Y' : 'N');
1794                 tagq_str[adapter->scsi_id] = '#';
1795                 tagq_str[adapter->maxdev] = '\0';
1796         }
1797         blogic_info("  Synchronous Negotiation: %s, Wide Negotiation: %s\n",
1798                         adapter, syncmsg, widemsg);
1799         blogic_info("  Disconnect/Reconnect: %s, Tagged Queuing: %s\n", adapter,
1800                         discon_msg, tagq_msg);
1801         if (blogic_multimaster_type(adapter)) {
1802                 blogic_info("  Scatter/Gather Limit: %d of %d segments, Mailboxes: %d\n", adapter, adapter->drvr_sglimit, adapter->adapter_sglimit, adapter->mbox_count);
1803                 blogic_info("  Driver Queue Depth: %d, Host Adapter Queue Depth: %d\n", adapter, adapter->drvr_qdepth, adapter->adapter_qdepth);
1804         } else
1805                 blogic_info("  Driver Queue Depth: %d, Scatter/Gather Limit: %d segments\n", adapter, adapter->drvr_qdepth, adapter->drvr_sglimit);
1806         blogic_info("  Tagged Queue Depth: ", adapter);
1807         common_tagq_depth = true;
1808         for (tgt_id = 1; tgt_id < adapter->maxdev; tgt_id++)
1809                 if (adapter->qdepth[tgt_id] != adapter->qdepth[0]) {
1810                         common_tagq_depth = false;
1811                         break;
1812                 }
1813         if (common_tagq_depth) {
1814                 if (adapter->qdepth[0] > 0)
1815                         blogic_info("%d", adapter, adapter->qdepth[0]);
1816                 else
1817                         blogic_info("Automatic", adapter);
1818         } else
1819                 blogic_info("Individual", adapter);
1820         blogic_info(", Untagged Queue Depth: %d\n", adapter,
1821                         adapter->untag_qdepth);
1822         if (adapter->terminfo_valid) {
1823                 if (adapter->wide)
1824                         blogic_info("  SCSI Bus Termination: %s", adapter,
1825                                 (adapter->low_term ? (adapter->high_term ? "Both Enabled" : "Low Enabled") : (adapter->high_term ? "High Enabled" : "Both Disabled")));
1826                 else
1827                         blogic_info("  SCSI Bus Termination: %s", adapter,
1828                                 (adapter->low_term ? "Enabled" : "Disabled"));
1829                 if (adapter->scam)
1830                         blogic_info(", SCAM: %s", adapter,
1831                                 (adapter->scam_enabled ? (adapter->scam_lev2 ? "Enabled, Level 2" : "Enabled, Level 1") : "Disabled"));
1832                 blogic_info("\n", adapter);
1833         }
1834         /*
1835            Indicate reporting the Host Adapter configuration completed
1836            successfully.
1837          */
1838         return true;
1839 }
1840
1841
1842 /*
1843   blogic_getres acquires the system resources necessary to use
1844   Host Adapter.
1845 */
1846
1847 static bool __init blogic_getres(struct blogic_adapter *adapter)
1848 {
1849         if (adapter->irq_ch == 0) {
1850                 blogic_err("NO LEGAL INTERRUPT CHANNEL ASSIGNED - DETACHING\n",
1851                                 adapter);
1852                 return false;
1853         }
1854         /*
1855            Acquire shared access to the IRQ Channel.
1856          */
1857         if (request_irq(adapter->irq_ch, blogic_inthandler, IRQF_SHARED,
1858                                 adapter->full_model, adapter) < 0) {
1859                 blogic_err("UNABLE TO ACQUIRE IRQ CHANNEL %d - DETACHING\n",
1860                                 adapter, adapter->irq_ch);
1861                 return false;
1862         }
1863         adapter->irq_acquired = true;
1864         /*
1865            Indicate the System Resource Acquisition completed successfully,
1866          */
1867         return true;
1868 }
1869
1870
1871 /*
1872   blogic_relres releases any system resources previously acquired
1873   by blogic_getres.
1874 */
1875
1876 static void blogic_relres(struct blogic_adapter *adapter)
1877 {
1878         /*
1879            Release shared access to the IRQ Channel.
1880          */
1881         if (adapter->irq_acquired)
1882                 free_irq(adapter->irq_ch, adapter);
1883         /*
1884            Release any allocated memory structs not released elsewhere
1885          */
1886         if (adapter->mbox_space)
1887                 dma_free_coherent(&adapter->pci_device->dev, adapter->mbox_sz,
1888                         adapter->mbox_space, adapter->mbox_space_handle);
1889         pci_dev_put(adapter->pci_device);
1890         adapter->mbox_space = NULL;
1891         adapter->mbox_space_handle = 0;
1892         adapter->mbox_sz = 0;
1893 }
1894
1895
1896 /*
1897   blogic_initadapter initializes Host Adapter.  This is the only
1898   function called during SCSI Host Adapter detection which modifies the state
1899   of the Host Adapter from its initial power on or hard reset state.
1900 */
1901
1902 static bool blogic_initadapter(struct blogic_adapter *adapter)
1903 {
1904         struct blogic_extmbox_req extmbox_req;
1905         enum blogic_rr_req rr_req;
1906         enum blogic_setccb_fmt setccb_fmt;
1907         int tgt_id;
1908
1909         /*
1910            Initialize the pointers to the first and last CCBs that are
1911            queued for completion processing.
1912          */
1913         adapter->firstccb = NULL;
1914         adapter->lastccb = NULL;
1915
1916         /*
1917            Initialize the Bus Device Reset Pending CCB, Tagged Queuing Active,
1918            Command Successful Flag, Active Commands, and Commands Since Reset
1919            for each Target Device.
1920          */
1921         for (tgt_id = 0; tgt_id < adapter->maxdev; tgt_id++) {
1922                 adapter->bdr_pend[tgt_id] = NULL;
1923                 adapter->tgt_flags[tgt_id].tagq_active = false;
1924                 adapter->tgt_flags[tgt_id].cmd_good = false;
1925                 adapter->active_cmds[tgt_id] = 0;
1926                 adapter->cmds_since_rst[tgt_id] = 0;
1927         }
1928
1929         /*
1930            FlashPoint Host Adapters do not use Outgoing and Incoming Mailboxes.
1931          */
1932         if (blogic_flashpoint_type(adapter))
1933                 goto done;
1934
1935         /*
1936            Initialize the Outgoing and Incoming Mailbox pointers.
1937          */
1938         adapter->mbox_sz = adapter->mbox_count * (sizeof(struct blogic_outbox) + sizeof(struct blogic_inbox));
1939         adapter->mbox_space = dma_alloc_coherent(&adapter->pci_device->dev,
1940                                 adapter->mbox_sz, &adapter->mbox_space_handle,
1941                                 GFP_KERNEL);
1942         if (adapter->mbox_space == NULL)
1943                 return blogic_failure(adapter, "MAILBOX ALLOCATION");
1944         adapter->first_outbox = (struct blogic_outbox *) adapter->mbox_space;
1945         adapter->last_outbox = adapter->first_outbox + adapter->mbox_count - 1;
1946         adapter->next_outbox = adapter->first_outbox;
1947         adapter->first_inbox = (struct blogic_inbox *) (adapter->last_outbox + 1);
1948         adapter->last_inbox = adapter->first_inbox + adapter->mbox_count - 1;
1949         adapter->next_inbox = adapter->first_inbox;
1950
1951         /*
1952            Initialize the Outgoing and Incoming Mailbox structures.
1953          */
1954         memset(adapter->first_outbox, 0,
1955                         adapter->mbox_count * sizeof(struct blogic_outbox));
1956         memset(adapter->first_inbox, 0,
1957                         adapter->mbox_count * sizeof(struct blogic_inbox));
1958
1959         /*
1960            Initialize the Host Adapter's Pointer to the Outgoing/Incoming
1961            Mailboxes.
1962          */
1963         extmbox_req.mbox_count = adapter->mbox_count;
1964         extmbox_req.base_mbox_addr = (u32) adapter->mbox_space_handle;
1965         if (blogic_cmd(adapter, BLOGIC_INIT_EXT_MBOX, &extmbox_req,
1966                                 sizeof(extmbox_req), NULL, 0) < 0)
1967                 return blogic_failure(adapter, "MAILBOX INITIALIZATION");
1968         /*
1969            Enable Strict Round Robin Mode if supported by the Host Adapter. In
1970            Strict Round Robin Mode, the Host Adapter only looks at the next
1971            Outgoing Mailbox for each new command, rather than scanning
1972            through all the Outgoing Mailboxes to find any that have new
1973            commands in them.  Strict Round Robin Mode is significantly more
1974            efficient.
1975          */
1976         if (adapter->strict_rr) {
1977                 rr_req = BLOGIC_STRICT_RR_MODE;
1978                 if (blogic_cmd(adapter, BLOGIC_STRICT_RR, &rr_req,
1979                                         sizeof(rr_req), NULL, 0) < 0)
1980                         return blogic_failure(adapter,
1981                                         "ENABLE STRICT ROUND ROBIN MODE");
1982         }
1983
1984         /*
1985            For Host Adapters that support Extended LUN Format CCBs, issue the
1986            Set CCB Format command to allow 32 Logical Units per Target Device.
1987          */
1988         if (adapter->ext_lun) {
1989                 setccb_fmt = BLOGIC_EXT_LUN_CCB;
1990                 if (blogic_cmd(adapter, BLOGIC_SETCCB_FMT, &setccb_fmt,
1991                                         sizeof(setccb_fmt), NULL, 0) < 0)
1992                         return blogic_failure(adapter, "SET CCB FORMAT");
1993         }
1994
1995         /*
1996            Announce Successful Initialization.
1997          */
1998 done:
1999         if (!adapter->adapter_initd) {
2000                 blogic_info("*** %s Initialized Successfully ***\n", adapter,
2001                                 adapter->full_model);
2002                 blogic_info("\n", adapter);
2003         } else
2004                 blogic_warn("*** %s Initialized Successfully ***\n", adapter,
2005                                 adapter->full_model);
2006         adapter->adapter_initd = true;
2007
2008         /*
2009            Indicate the Host Adapter Initialization completed successfully.
2010          */
2011         return true;
2012 }
2013
2014
2015 /*
2016   blogic_inquiry inquires about the Target Devices accessible
2017   through Host Adapter.
2018 */
2019
2020 static bool __init blogic_inquiry(struct blogic_adapter *adapter)
2021 {
2022         u16 installed_devs;
2023         u8 installed_devs0to7[8];
2024         struct blogic_setup_info setupinfo;
2025         u8 sync_period[BLOGIC_MAXDEV];
2026         unsigned char req_replylen;
2027         int tgt_id;
2028
2029         /*
2030            Wait a few seconds between the Host Adapter Hard Reset which
2031            initiates a SCSI Bus Reset and issuing any SCSI Commands. Some
2032            SCSI devices get confused if they receive SCSI Commands too soon
2033            after a SCSI Bus Reset.
2034          */
2035         blogic_delay(adapter->bus_settle_time);
2036         /*
2037            FlashPoint Host Adapters do not provide for Target Device Inquiry.
2038          */
2039         if (blogic_flashpoint_type(adapter))
2040                 return true;
2041         /*
2042            Inhibit the Target Device Inquiry if requested.
2043          */
2044         if (adapter->drvr_opts != NULL && adapter->drvr_opts->stop_tgt_inquiry)
2045                 return true;
2046         /*
2047            Issue the Inquire Target Devices command for host adapters with
2048            firmware version 4.25 or later, or the Inquire Installed Devices
2049            ID 0 to 7 command for older host adapters.  This is necessary to
2050            force Synchronous Transfer Negotiation so that the Inquire Setup
2051            Information and Inquire Synchronous Period commands will return
2052            valid data.  The Inquire Target Devices command is preferable to
2053            Inquire Installed Devices ID 0 to 7 since it only probes Logical
2054            Unit 0 of each Target Device.
2055          */
2056         if (strcmp(adapter->fw_ver, "4.25") >= 0) {
2057
2058                 /*
2059                    Issue a Inquire Target Devices command. Inquire Target
2060                    Devices only tests Logical Unit 0 of each Target Device
2061                    unlike the Inquire Installed Devices commands which test
2062                    Logical Units 0 - 7.  Two bytes are returned, where byte
2063                    0 bit 0 set indicates that Target Device 0 exists, and so on.
2064                  */
2065
2066                 if (blogic_cmd(adapter, BLOGIC_INQ_DEV, NULL, 0,
2067                                         &installed_devs, sizeof(installed_devs))
2068                     != sizeof(installed_devs))
2069                         return blogic_failure(adapter, "INQUIRE TARGET DEVICES");
2070                 for (tgt_id = 0; tgt_id < adapter->maxdev; tgt_id++)
2071                         adapter->tgt_flags[tgt_id].tgt_exists =
2072                                 (installed_devs & (1 << tgt_id) ? true : false);
2073         } else {
2074
2075                 /*
2076                    Issue an Inquire Installed Devices command. For each
2077                    Target Device, a byte is returned where bit 0 set
2078                    indicates that Logical Unit 0 * exists, bit 1 set
2079                    indicates that Logical Unit 1 exists, and so on.
2080                  */
2081
2082                 if (blogic_cmd(adapter, BLOGIC_INQ_DEV0TO7, NULL, 0,
2083                                 &installed_devs0to7, sizeof(installed_devs0to7))
2084                     != sizeof(installed_devs0to7))
2085                         return blogic_failure(adapter,
2086                                         "INQUIRE INSTALLED DEVICES ID 0 TO 7");
2087                 for (tgt_id = 0; tgt_id < 8; tgt_id++)
2088                         adapter->tgt_flags[tgt_id].tgt_exists =
2089                                 installed_devs0to7[tgt_id] != 0;
2090         }
2091         /*
2092            Issue the Inquire Setup Information command.
2093          */
2094         req_replylen = sizeof(setupinfo);
2095         if (blogic_cmd(adapter, BLOGIC_INQ_SETUPINFO, &req_replylen,
2096                         sizeof(req_replylen), &setupinfo, sizeof(setupinfo))
2097             != sizeof(setupinfo))
2098                 return blogic_failure(adapter, "INQUIRE SETUP INFORMATION");
2099         for (tgt_id = 0; tgt_id < adapter->maxdev; tgt_id++)
2100                 adapter->sync_offset[tgt_id] = (tgt_id < 8 ? setupinfo.sync0to7[tgt_id].offset : setupinfo.sync8to15[tgt_id - 8].offset);
2101         if (strcmp(adapter->fw_ver, "5.06L") >= 0)
2102                 for (tgt_id = 0; tgt_id < adapter->maxdev; tgt_id++)
2103                         adapter->tgt_flags[tgt_id].wide_active = (tgt_id < 8 ? (setupinfo.wide_tx_active0to7 & (1 << tgt_id) ? true : false) : (setupinfo.wide_tx_active8to15 & (1 << (tgt_id - 8)) ? true : false));
2104         /*
2105            Issue the Inquire Synchronous Period command.
2106          */
2107         if (adapter->fw_ver[0] >= '3') {
2108
2109                 /* Issue a Inquire Synchronous Period command. For each
2110                    Target Device, a byte is returned which represents the
2111                    Synchronous Transfer Period in units of 10 nanoseconds.
2112                  */
2113
2114                 req_replylen = sizeof(sync_period);
2115                 if (blogic_cmd(adapter, BLOGIC_INQ_SYNC_PERIOD, &req_replylen,
2116                                 sizeof(req_replylen), &sync_period,
2117                                 sizeof(sync_period)) != sizeof(sync_period))
2118                         return blogic_failure(adapter,
2119                                         "INQUIRE SYNCHRONOUS PERIOD");
2120                 for (tgt_id = 0; tgt_id < adapter->maxdev; tgt_id++)
2121                         adapter->sync_period[tgt_id] = sync_period[tgt_id];
2122         } else
2123                 for (tgt_id = 0; tgt_id < adapter->maxdev; tgt_id++)
2124                         if (setupinfo.sync0to7[tgt_id].offset > 0)
2125                                 adapter->sync_period[tgt_id] = 20 + 5 * setupinfo.sync0to7[tgt_id].tx_period;
2126         /*
2127            Indicate the Target Device Inquiry completed successfully.
2128          */
2129         return true;
2130 }
2131
2132 /*
2133   blogic_inithoststruct initializes the fields in the SCSI Host
2134   structure.  The base, io_port, n_io_ports, irq, and dma_channel fields in the
2135   SCSI Host structure are intentionally left uninitialized, as this driver
2136   handles acquisition and release of these resources explicitly, as well as
2137   ensuring exclusive access to the Host Adapter hardware and data structures
2138   through explicit acquisition and release of the Host Adapter's Lock.
2139 */
2140
2141 static void __init blogic_inithoststruct(struct blogic_adapter *adapter,
2142                 struct Scsi_Host *host)
2143 {
2144         host->max_id = adapter->maxdev;
2145         host->max_lun = adapter->maxlun;
2146         host->max_channel = 0;
2147         host->unique_id = adapter->io_addr;
2148         host->this_id = adapter->scsi_id;
2149         host->can_queue = adapter->drvr_qdepth;
2150         host->sg_tablesize = adapter->drvr_sglimit;
2151         host->cmd_per_lun = adapter->untag_qdepth;
2152 }
2153
2154 /*
2155   blogic_slaveconfig will actually set the queue depth on individual
2156   scsi devices as they are permanently added to the device chain.  We
2157   shamelessly rip off the SelectQueueDepths code to make this work mostly
2158   like it used to.  Since we don't get called once at the end of the scan
2159   but instead get called for each device, we have to do things a bit
2160   differently.
2161 */
2162 static int blogic_slaveconfig(struct scsi_device *dev)
2163 {
2164         struct blogic_adapter *adapter =
2165                 (struct blogic_adapter *) dev->host->hostdata;
2166         int tgt_id = dev->id;
2167         int qdepth = adapter->qdepth[tgt_id];
2168
2169         if (adapter->tgt_flags[tgt_id].tagq_ok &&
2170                         (adapter->tagq_ok & (1 << tgt_id))) {
2171                 if (qdepth == 0)
2172                         qdepth = BLOGIC_MAX_AUTO_TAG_DEPTH;
2173                 adapter->qdepth[tgt_id] = qdepth;
2174                 scsi_change_queue_depth(dev, qdepth);
2175         } else {
2176                 adapter->tagq_ok &= ~(1 << tgt_id);
2177                 qdepth = adapter->untag_qdepth;
2178                 adapter->qdepth[tgt_id] = qdepth;
2179                 scsi_change_queue_depth(dev, qdepth);
2180         }
2181         qdepth = 0;
2182         for (tgt_id = 0; tgt_id < adapter->maxdev; tgt_id++)
2183                 if (adapter->tgt_flags[tgt_id].tgt_exists)
2184                         qdepth += adapter->qdepth[tgt_id];
2185         if (qdepth > adapter->alloc_ccbs)
2186                 blogic_create_addlccbs(adapter, qdepth - adapter->alloc_ccbs,
2187                                 false);
2188         return 0;
2189 }
2190
2191 /*
2192   blogic_init probes for BusLogic Host Adapters at the standard
2193   I/O Addresses where they may be located, initializing, registering, and
2194   reporting the configuration of each BusLogic Host Adapter it finds.  It
2195   returns the number of BusLogic Host Adapters successfully initialized and
2196   registered.
2197 */
2198
2199 static int __init blogic_init(void)
2200 {
2201         int adapter_count = 0, drvr_optindex = 0, probeindex;
2202         struct blogic_adapter *adapter;
2203         int ret = 0;
2204
2205 #ifdef MODULE
2206         if (BusLogic)
2207                 blogic_setup(BusLogic);
2208 #endif
2209
2210         if (blogic_probe_options.noprobe)
2211                 return -ENODEV;
2212         blogic_probeinfo_list =
2213             kcalloc(BLOGIC_MAX_ADAPTERS, sizeof(struct blogic_probeinfo),
2214                             GFP_KERNEL);
2215         if (blogic_probeinfo_list == NULL) {
2216                 blogic_err("BusLogic: Unable to allocate Probe Info List\n",
2217                                 NULL);
2218                 return -ENOMEM;
2219         }
2220
2221         adapter = kzalloc(sizeof(struct blogic_adapter), GFP_KERNEL);
2222         if (adapter == NULL) {
2223                 kfree(blogic_probeinfo_list);
2224                 blogic_err("BusLogic: Unable to allocate Prototype Host Adapter\n", NULL);
2225                 return -ENOMEM;
2226         }
2227
2228 #ifdef MODULE
2229         if (BusLogic != NULL)
2230                 blogic_setup(BusLogic);
2231 #endif
2232         blogic_init_probeinfo_list(adapter);
2233         for (probeindex = 0; probeindex < blogic_probeinfo_count; probeindex++) {
2234                 struct blogic_probeinfo *probeinfo =
2235                         &blogic_probeinfo_list[probeindex];
2236                 struct blogic_adapter *myadapter = adapter;
2237                 struct Scsi_Host *host;
2238
2239                 if (probeinfo->io_addr == 0)
2240                         continue;
2241                 memset(myadapter, 0, sizeof(struct blogic_adapter));
2242                 myadapter->adapter_type = probeinfo->adapter_type;
2243                 myadapter->adapter_bus_type = probeinfo->adapter_bus_type;
2244                 myadapter->io_addr = probeinfo->io_addr;
2245                 myadapter->pci_addr = probeinfo->pci_addr;
2246                 myadapter->bus = probeinfo->bus;
2247                 myadapter->dev = probeinfo->dev;
2248                 myadapter->pci_device = probeinfo->pci_device;
2249                 myadapter->irq_ch = probeinfo->irq_ch;
2250                 myadapter->addr_count =
2251                         blogic_adapter_addr_count[myadapter->adapter_type];
2252
2253                 /*
2254                    Make sure region is free prior to probing.
2255                  */
2256                 if (!request_region(myadapter->io_addr, myadapter->addr_count,
2257                                         "BusLogic"))
2258                         continue;
2259                 /*
2260                    Probe the Host Adapter. If unsuccessful, abort further
2261                    initialization.
2262                  */
2263                 if (!blogic_probe(myadapter)) {
2264                         release_region(myadapter->io_addr,
2265                                         myadapter->addr_count);
2266                         continue;
2267                 }
2268                 /*
2269                    Hard Reset the Host Adapter.  If unsuccessful, abort further
2270                    initialization.
2271                  */
2272                 if (!blogic_hwreset(myadapter, true)) {
2273                         release_region(myadapter->io_addr,
2274                                         myadapter->addr_count);
2275                         continue;
2276                 }
2277                 /*
2278                    Check the Host Adapter.  If unsuccessful, abort further
2279                    initialization.
2280                  */
2281                 if (!blogic_checkadapter(myadapter)) {
2282                         release_region(myadapter->io_addr,
2283                                         myadapter->addr_count);
2284                         continue;
2285                 }
2286                 /*
2287                    Initialize the Driver Options field if provided.
2288                  */
2289                 if (drvr_optindex < blogic_drvr_options_count)
2290                         myadapter->drvr_opts =
2291                                 &blogic_drvr_options[drvr_optindex++];
2292                 /*
2293                    Announce the Driver Version and Date, Author's Name,
2294                    Copyright Notice, and Electronic Mail Address.
2295                  */
2296                 blogic_announce_drvr(myadapter);
2297                 /*
2298                    Register the SCSI Host structure.
2299                  */
2300
2301                 host = scsi_host_alloc(&blogic_template,
2302                                 sizeof(struct blogic_adapter));
2303                 if (host == NULL) {
2304                         release_region(myadapter->io_addr,
2305                                         myadapter->addr_count);
2306                         continue;
2307                 }
2308                 myadapter = (struct blogic_adapter *) host->hostdata;
2309                 memcpy(myadapter, adapter, sizeof(struct blogic_adapter));
2310                 myadapter->scsi_host = host;
2311                 myadapter->host_no = host->host_no;
2312                 /*
2313                    Add Host Adapter to the end of the list of registered
2314                    BusLogic Host Adapters.
2315                  */
2316                 list_add_tail(&myadapter->host_list, &blogic_host_list);
2317
2318                 /*
2319                    Read the Host Adapter Configuration, Configure the Host
2320                    Adapter, Acquire the System Resources necessary to use
2321                    the Host Adapter, then Create the Initial CCBs, Initialize
2322                    the Host Adapter, and finally perform Target Device
2323                    Inquiry. From this point onward, any failure will be
2324                    assumed to be due to a problem with the Host Adapter,
2325                    rather than due to having mistakenly identified this port
2326                    as belonging to a BusLogic Host Adapter. The I/O Address
2327                    range will not be released, thereby preventing it from
2328                    being incorrectly identified as any other type of Host
2329                    Adapter.
2330                  */
2331                 if (blogic_rdconfig(myadapter) &&
2332                     blogic_reportconfig(myadapter) &&
2333                     blogic_getres(myadapter) &&
2334                     blogic_create_initccbs(myadapter) &&
2335                     blogic_initadapter(myadapter) &&
2336                     blogic_inquiry(myadapter)) {
2337                         /*
2338                            Initialization has been completed successfully.
2339                            Release and re-register usage of the I/O Address
2340                            range so that the Model Name of the Host Adapter
2341                            will appear, and initialize the SCSI Host structure.
2342                          */
2343                         release_region(myadapter->io_addr,
2344                                        myadapter->addr_count);
2345                         if (!request_region(myadapter->io_addr,
2346                                             myadapter->addr_count,
2347                                             myadapter->full_model)) {
2348                                 printk(KERN_WARNING
2349                                         "BusLogic: Release and re-register of "
2350                                         "port 0x%04lx failed \n",
2351                                         (unsigned long)myadapter->io_addr);
2352                                 blogic_destroy_ccbs(myadapter);
2353                                 blogic_relres(myadapter);
2354                                 list_del(&myadapter->host_list);
2355                                 scsi_host_put(host);
2356                                 ret = -ENOMEM;
2357                         } else {
2358                                 blogic_inithoststruct(myadapter,
2359                                                                  host);
2360                                 if (scsi_add_host(host, myadapter->pci_device
2361                                                 ? &myadapter->pci_device->dev
2362                                                   : NULL)) {
2363                                         printk(KERN_WARNING
2364                                                "BusLogic: scsi_add_host()"
2365                                                "failed!\n");
2366                                         blogic_destroy_ccbs(myadapter);
2367                                         blogic_relres(myadapter);
2368                                         list_del(&myadapter->host_list);
2369                                         scsi_host_put(host);
2370                                         ret = -ENODEV;
2371                                 } else {
2372                                         scsi_scan_host(host);
2373                                         adapter_count++;
2374                                 }
2375                         }
2376                 } else {
2377                         /*
2378                            An error occurred during Host Adapter Configuration
2379                            Querying, Host Adapter Configuration, Resource
2380                            Acquisition, CCB Creation, Host Adapter
2381                            Initialization, or Target Device Inquiry, so
2382                            remove Host Adapter from the list of registered
2383                            BusLogic Host Adapters, destroy the CCBs, Release
2384                            the System Resources, and Unregister the SCSI
2385                            Host.
2386                          */
2387                         blogic_destroy_ccbs(myadapter);
2388                         blogic_relres(myadapter);
2389                         list_del(&myadapter->host_list);
2390                         scsi_host_put(host);
2391                         ret = -ENODEV;
2392                 }
2393         }
2394         kfree(adapter);
2395         kfree(blogic_probeinfo_list);
2396         blogic_probeinfo_list = NULL;
2397         return ret;
2398 }
2399
2400
2401 /*
2402   blogic_deladapter releases all resources previously acquired to
2403   support a specific Host Adapter, including the I/O Address range, and
2404   unregisters the BusLogic Host Adapter.
2405 */
2406
2407 static int __exit blogic_deladapter(struct blogic_adapter *adapter)
2408 {
2409         struct Scsi_Host *host = adapter->scsi_host;
2410
2411         scsi_remove_host(host);
2412
2413         /*
2414            FlashPoint Host Adapters must first be released by the FlashPoint
2415            SCCB Manager.
2416          */
2417         if (blogic_flashpoint_type(adapter))
2418                 FlashPoint_ReleaseHostAdapter(adapter->cardhandle);
2419         /*
2420            Destroy the CCBs and release any system resources acquired to
2421            support Host Adapter.
2422          */
2423         blogic_destroy_ccbs(adapter);
2424         blogic_relres(adapter);
2425         /*
2426            Release usage of the I/O Address range.
2427          */
2428         release_region(adapter->io_addr, adapter->addr_count);
2429         /*
2430            Remove Host Adapter from the list of registered BusLogic
2431            Host Adapters.
2432          */
2433         list_del(&adapter->host_list);
2434
2435         scsi_host_put(host);
2436         return 0;
2437 }
2438
2439
2440 /*
2441   blogic_qcompleted_ccb queues CCB for completion processing.
2442 */
2443
2444 static void blogic_qcompleted_ccb(struct blogic_ccb *ccb)
2445 {
2446         struct blogic_adapter *adapter = ccb->adapter;
2447
2448         ccb->status = BLOGIC_CCB_COMPLETE;
2449         ccb->next = NULL;
2450         if (adapter->firstccb == NULL) {
2451                 adapter->firstccb = ccb;
2452                 adapter->lastccb = ccb;
2453         } else {
2454                 adapter->lastccb->next = ccb;
2455                 adapter->lastccb = ccb;
2456         }
2457         adapter->active_cmds[ccb->tgt_id]--;
2458 }
2459
2460
2461 /*
2462   blogic_resultcode computes a SCSI Subsystem Result Code from
2463   the Host Adapter Status and Target Device Status.
2464 */
2465
2466 static int blogic_resultcode(struct blogic_adapter *adapter,
2467                 enum blogic_adapter_status adapter_status,
2468                 enum blogic_tgt_status tgt_status)
2469 {
2470         int hoststatus;
2471
2472         switch (adapter_status) {
2473         case BLOGIC_CMD_CMPLT_NORMAL:
2474         case BLOGIC_LINK_CMD_CMPLT:
2475         case BLOGIC_LINK_CMD_CMPLT_FLAG:
2476                 hoststatus = DID_OK;
2477                 break;
2478         case BLOGIC_SELECT_TIMEOUT:
2479                 hoststatus = DID_TIME_OUT;
2480                 break;
2481         case BLOGIC_INVALID_OUTBOX_CODE:
2482         case BLOGIC_INVALID_CMD_CODE:
2483         case BLOGIC_BAD_CMD_PARAM:
2484                 blogic_warn("BusLogic Driver Protocol Error 0x%02X\n",
2485                                 adapter, adapter_status);
2486                 fallthrough;
2487         case BLOGIC_DATA_UNDERRUN:
2488         case BLOGIC_DATA_OVERRUN:
2489         case BLOGIC_NOEXPECT_BUSFREE:
2490         case BLOGIC_LINKCCB_BADLUN:
2491         case BLOGIC_AUTOREQSENSE_FAIL:
2492         case BLOGIC_TAGQUEUE_REJECT:
2493         case BLOGIC_BAD_MSG_RCVD:
2494         case BLOGIC_HW_FAIL:
2495         case BLOGIC_BAD_RECONNECT:
2496         case BLOGIC_ABRT_QUEUE:
2497         case BLOGIC_ADAPTER_SW_ERROR:
2498         case BLOGIC_HW_TIMEOUT:
2499         case BLOGIC_PARITY_ERR:
2500                 hoststatus = DID_ERROR;
2501                 break;
2502         case BLOGIC_INVALID_BUSPHASE:
2503         case BLOGIC_NORESPONSE_TO_ATN:
2504         case BLOGIC_HW_RESET:
2505         case BLOGIC_RST_FROM_OTHERDEV:
2506         case BLOGIC_HW_BDR:
2507                 hoststatus = DID_RESET;
2508                 break;
2509         default:
2510                 blogic_warn("Unknown Host Adapter Status 0x%02X\n", adapter,
2511                                 adapter_status);
2512                 hoststatus = DID_ERROR;
2513                 break;
2514         }
2515         return (hoststatus << 16) | tgt_status;
2516 }
2517
2518
2519 /*
2520   blogic_scan_inbox scans the Incoming Mailboxes saving any
2521   Incoming Mailbox entries for completion processing.
2522 */
2523
2524 static void blogic_scan_inbox(struct blogic_adapter *adapter)
2525 {
2526         /*
2527            Scan through the Incoming Mailboxes in Strict Round Robin
2528            fashion, saving any completed CCBs for further processing. It
2529            is essential that for each CCB and SCSI Command issued, command
2530            completion processing is performed exactly once.  Therefore,
2531            only Incoming Mailboxes with completion code Command Completed
2532            Without Error, Command Completed With Error, or Command Aborted
2533            At Host Request are saved for completion processing. When an
2534            Incoming Mailbox has a completion code of Aborted Command Not
2535            Found, the CCB had already completed or been aborted before the
2536            current Abort request was processed, and so completion processing
2537            has already occurred and no further action should be taken.
2538          */
2539         struct blogic_inbox *next_inbox = adapter->next_inbox;
2540         enum blogic_cmplt_code comp_code;
2541
2542         while ((comp_code = next_inbox->comp_code) != BLOGIC_INBOX_FREE) {
2543                 /*
2544                    We are only allowed to do this because we limit our
2545                    architectures we run on to machines where bus_to_virt(
2546                    actually works.  There *needs* to be a dma_addr_to_virt()
2547                    in the new PCI DMA mapping interface to replace
2548                    bus_to_virt() or else this code is going to become very
2549                    innefficient.
2550                  */
2551                 struct blogic_ccb *ccb =
2552                         (struct blogic_ccb *) bus_to_virt(next_inbox->ccb);
2553                 if (comp_code != BLOGIC_CMD_NOTFOUND) {
2554                         if (ccb->status == BLOGIC_CCB_ACTIVE ||
2555                                         ccb->status == BLOGIC_CCB_RESET) {
2556                                 /*
2557                                    Save the Completion Code for this CCB and
2558                                    queue the CCB for completion processing.
2559                                  */
2560                                 ccb->comp_code = comp_code;
2561                                 blogic_qcompleted_ccb(ccb);
2562                         } else {
2563                                 /*
2564                                    If a CCB ever appears in an Incoming Mailbox
2565                                    and is not marked as status Active or Reset,
2566                                    then there is most likely a bug in
2567                                    the Host Adapter firmware.
2568                                  */
2569                                 blogic_warn("Illegal CCB #%ld status %d in Incoming Mailbox\n", adapter, ccb->serial, ccb->status);
2570                         }
2571                 }
2572                 next_inbox->comp_code = BLOGIC_INBOX_FREE;
2573                 if (++next_inbox > adapter->last_inbox)
2574                         next_inbox = adapter->first_inbox;
2575         }
2576         adapter->next_inbox = next_inbox;
2577 }
2578
2579
2580 /*
2581   blogic_process_ccbs iterates over the completed CCBs for Host
2582   Adapter setting the SCSI Command Result Codes, deallocating the CCBs, and
2583   calling the SCSI Subsystem Completion Routines.  The Host Adapter's Lock
2584   should already have been acquired by the caller.
2585 */
2586
2587 static void blogic_process_ccbs(struct blogic_adapter *adapter)
2588 {
2589         if (adapter->processing_ccbs)
2590                 return;
2591         adapter->processing_ccbs = true;
2592         while (adapter->firstccb != NULL) {
2593                 struct blogic_ccb *ccb = adapter->firstccb;
2594                 struct scsi_cmnd *command = ccb->command;
2595                 adapter->firstccb = ccb->next;
2596                 if (adapter->firstccb == NULL)
2597                         adapter->lastccb = NULL;
2598                 /*
2599                    Process the Completed CCB.
2600                  */
2601                 if (ccb->opcode == BLOGIC_BDR) {
2602                         int tgt_id = ccb->tgt_id;
2603
2604                         blogic_warn("Bus Device Reset CCB #%ld to Target %d Completed\n", adapter, ccb->serial, tgt_id);
2605                         blogic_inc_count(&adapter->tgt_stats[tgt_id].bdr_done);
2606                         adapter->tgt_flags[tgt_id].tagq_active = false;
2607                         adapter->cmds_since_rst[tgt_id] = 0;
2608                         adapter->last_resetdone[tgt_id] = jiffies;
2609                         /*
2610                            Place CCB back on the Host Adapter's free list.
2611                          */
2612                         blogic_dealloc_ccb(ccb, 1);
2613 #if 0                   /* this needs to be redone different for new EH */
2614                         /*
2615                            Bus Device Reset CCBs have the command field
2616                            non-NULL only when a Bus Device Reset was requested
2617                            for a command that did not have a currently active
2618                            CCB in the Host Adapter (i.e., a Synchronous Bus
2619                            Device Reset), and hence would not have its
2620                            Completion Routine called otherwise.
2621                          */
2622                         while (command != NULL) {
2623                                 struct scsi_cmnd *nxt_cmd =
2624                                         command->reset_chain;
2625                                 command->reset_chain = NULL;
2626                                 command->result = DID_RESET << 16;
2627                                 command->scsi_done(command);
2628                                 command = nxt_cmd;
2629                         }
2630 #endif
2631                         /*
2632                            Iterate over the CCBs for this Host Adapter
2633                            performing completion processing for any CCBs
2634                            marked as Reset for this Target.
2635                          */
2636                         for (ccb = adapter->all_ccbs; ccb != NULL;
2637                                         ccb = ccb->next_all)
2638                                 if (ccb->status == BLOGIC_CCB_RESET &&
2639                                                 ccb->tgt_id == tgt_id) {
2640                                         command = ccb->command;
2641                                         blogic_dealloc_ccb(ccb, 1);
2642                                         adapter->active_cmds[tgt_id]--;
2643                                         command->result = DID_RESET << 16;
2644                                         command->scsi_done(command);
2645                                 }
2646                         adapter->bdr_pend[tgt_id] = NULL;
2647                 } else {
2648                         /*
2649                            Translate the Completion Code, Host Adapter Status,
2650                            and Target Device Status into a SCSI Subsystem
2651                            Result Code.
2652                          */
2653                         switch (ccb->comp_code) {
2654                         case BLOGIC_INBOX_FREE:
2655                         case BLOGIC_CMD_NOTFOUND:
2656                         case BLOGIC_INVALID_CCB:
2657                                 blogic_warn("CCB #%ld to Target %d Impossible State\n", adapter, ccb->serial, ccb->tgt_id);
2658                                 break;
2659                         case BLOGIC_CMD_COMPLETE_GOOD:
2660                                 adapter->tgt_stats[ccb->tgt_id]
2661                                     .cmds_complete++;
2662                                 adapter->tgt_flags[ccb->tgt_id]
2663                                     .cmd_good = true;
2664                                 command->result = DID_OK << 16;
2665                                 break;
2666                         case BLOGIC_CMD_ABORT_BY_HOST:
2667                                 blogic_warn("CCB #%ld to Target %d Aborted\n",
2668                                         adapter, ccb->serial, ccb->tgt_id);
2669                                 blogic_inc_count(&adapter->tgt_stats[ccb->tgt_id].aborts_done);
2670                                 command->result = DID_ABORT << 16;
2671                                 break;
2672                         case BLOGIC_CMD_COMPLETE_ERROR:
2673                                 command->result = blogic_resultcode(adapter,
2674                                         ccb->adapter_status, ccb->tgt_status);
2675                                 if (ccb->adapter_status != BLOGIC_SELECT_TIMEOUT) {
2676                                         adapter->tgt_stats[ccb->tgt_id]
2677                                             .cmds_complete++;
2678                                         if (blogic_global_options.trace_err) {
2679                                                 int i;
2680                                                 blogic_notice("CCB #%ld Target %d: Result %X Host "
2681                                                                 "Adapter Status %02X Target Status %02X\n", adapter, ccb->serial, ccb->tgt_id, command->result, ccb->adapter_status, ccb->tgt_status);
2682                                                 blogic_notice("CDB   ", adapter);
2683                                                 for (i = 0; i < ccb->cdblen; i++)
2684                                                         blogic_notice(" %02X", adapter, ccb->cdb[i]);
2685                                                 blogic_notice("\n", adapter);
2686                                                 blogic_notice("Sense ", adapter);
2687                                                 for (i = 0; i < ccb->sense_datalen; i++)
2688                                                         blogic_notice(" %02X", adapter, command->sense_buffer[i]);
2689                                                 blogic_notice("\n", adapter);
2690                                         }
2691                                 }
2692                                 break;
2693                         }
2694                         /*
2695                            When an INQUIRY command completes normally, save the
2696                            CmdQue (Tagged Queuing Supported) and WBus16 (16 Bit
2697                            Wide Data Transfers Supported) bits.
2698                          */
2699                         if (ccb->cdb[0] == INQUIRY && ccb->cdb[1] == 0 &&
2700                                 ccb->adapter_status == BLOGIC_CMD_CMPLT_NORMAL) {
2701                                 struct blogic_tgt_flags *tgt_flags =
2702                                         &adapter->tgt_flags[ccb->tgt_id];
2703                                 struct scsi_inquiry *inquiry =
2704                                         (struct scsi_inquiry *) scsi_sglist(command);
2705                                 tgt_flags->tgt_exists = true;
2706                                 tgt_flags->tagq_ok = inquiry->CmdQue;
2707                                 tgt_flags->wide_ok = inquiry->WBus16;
2708                         }
2709                         /*
2710                            Place CCB back on the Host Adapter's free list.
2711                          */
2712                         blogic_dealloc_ccb(ccb, 1);
2713                         /*
2714                            Call the SCSI Command Completion Routine.
2715                          */
2716                         command->scsi_done(command);
2717                 }
2718         }
2719         adapter->processing_ccbs = false;
2720 }
2721
2722
2723 /*
2724   blogic_inthandler handles hardware interrupts from BusLogic Host
2725   Adapters.
2726 */
2727
2728 static irqreturn_t blogic_inthandler(int irq_ch, void *devid)
2729 {
2730         struct blogic_adapter *adapter = (struct blogic_adapter *) devid;
2731         unsigned long processor_flag;
2732         /*
2733            Acquire exclusive access to Host Adapter.
2734          */
2735         spin_lock_irqsave(adapter->scsi_host->host_lock, processor_flag);
2736         /*
2737            Handle Interrupts appropriately for each Host Adapter type.
2738          */
2739         if (blogic_multimaster_type(adapter)) {
2740                 union blogic_int_reg intreg;
2741                 /*
2742                    Read the Host Adapter Interrupt Register.
2743                  */
2744                 intreg.all = blogic_rdint(adapter);
2745                 if (intreg.ir.int_valid) {
2746                         /*
2747                            Acknowledge the interrupt and reset the Host Adapter
2748                            Interrupt Register.
2749                          */
2750                         blogic_intreset(adapter);
2751                         /*
2752                            Process valid External SCSI Bus Reset and Incoming
2753                            Mailbox Loaded Interrupts. Command Complete
2754                            Interrupts are noted, and Outgoing Mailbox Available
2755                            Interrupts are ignored, as they are never enabled.
2756                          */
2757                         if (intreg.ir.ext_busreset)
2758                                 adapter->adapter_extreset = true;
2759                         else if (intreg.ir.mailin_loaded)
2760                                 blogic_scan_inbox(adapter);
2761                         else if (intreg.ir.cmd_complete)
2762                                 adapter->adapter_cmd_complete = true;
2763                 }
2764         } else {
2765                 /*
2766                    Check if there is a pending interrupt for this Host Adapter.
2767                  */
2768                 if (FlashPoint_InterruptPending(adapter->cardhandle))
2769                         switch (FlashPoint_HandleInterrupt(adapter->cardhandle)) {
2770                         case FPOINT_NORMAL_INT:
2771                                 break;
2772                         case FPOINT_EXT_RESET:
2773                                 adapter->adapter_extreset = true;
2774                                 break;
2775                         case FPOINT_INTERN_ERR:
2776                                 blogic_warn("Internal FlashPoint Error detected - Resetting Host Adapter\n", adapter);
2777                                 adapter->adapter_intern_err = true;
2778                                 break;
2779                         }
2780         }
2781         /*
2782            Process any completed CCBs.
2783          */
2784         if (adapter->firstccb != NULL)
2785                 blogic_process_ccbs(adapter);
2786         /*
2787            Reset the Host Adapter if requested.
2788          */
2789         if (adapter->adapter_extreset) {
2790                 blogic_warn("Resetting %s due to External SCSI Bus Reset\n", adapter, adapter->full_model);
2791                 blogic_inc_count(&adapter->ext_resets);
2792                 blogic_resetadapter(adapter, false);
2793                 adapter->adapter_extreset = false;
2794         } else if (adapter->adapter_intern_err) {
2795                 blogic_warn("Resetting %s due to Host Adapter Internal Error\n", adapter, adapter->full_model);
2796                 blogic_inc_count(&adapter->adapter_intern_errors);
2797                 blogic_resetadapter(adapter, true);
2798                 adapter->adapter_intern_err = false;
2799         }
2800         /*
2801            Release exclusive access to Host Adapter.
2802          */
2803         spin_unlock_irqrestore(adapter->scsi_host->host_lock, processor_flag);
2804         return IRQ_HANDLED;
2805 }
2806
2807
2808 /*
2809   blogic_write_outbox places CCB and Action Code into an Outgoing
2810   Mailbox for execution by Host Adapter.  The Host Adapter's Lock should
2811   already have been acquired by the caller.
2812 */
2813
2814 static bool blogic_write_outbox(struct blogic_adapter *adapter,
2815                 enum blogic_action action, struct blogic_ccb *ccb)
2816 {
2817         struct blogic_outbox *next_outbox;
2818
2819         next_outbox = adapter->next_outbox;
2820         if (next_outbox->action == BLOGIC_OUTBOX_FREE) {
2821                 ccb->status = BLOGIC_CCB_ACTIVE;
2822                 /*
2823                    The CCB field must be written before the Action Code field
2824                    since the Host Adapter is operating asynchronously and the
2825                    locking code does not protect against simultaneous access
2826                    by the Host Adapter.
2827                  */
2828                 next_outbox->ccb = ccb->dma_handle;
2829                 next_outbox->action = action;
2830                 blogic_execmbox(adapter);
2831                 if (++next_outbox > adapter->last_outbox)
2832                         next_outbox = adapter->first_outbox;
2833                 adapter->next_outbox = next_outbox;
2834                 if (action == BLOGIC_MBOX_START) {
2835                         adapter->active_cmds[ccb->tgt_id]++;
2836                         if (ccb->opcode != BLOGIC_BDR)
2837                                 adapter->tgt_stats[ccb->tgt_id].cmds_tried++;
2838                 }
2839                 return true;
2840         }
2841         return false;
2842 }
2843
2844 /* Error Handling (EH) support */
2845
2846 static int blogic_hostreset(struct scsi_cmnd *SCpnt)
2847 {
2848         struct blogic_adapter *adapter =
2849                 (struct blogic_adapter *) SCpnt->device->host->hostdata;
2850
2851         unsigned int id = SCpnt->device->id;
2852         struct blogic_tgt_stats *stats = &adapter->tgt_stats[id];
2853         int rc;
2854
2855         spin_lock_irq(SCpnt->device->host->host_lock);
2856
2857         blogic_inc_count(&stats->adapter_reset_req);
2858
2859         rc = blogic_resetadapter(adapter, false);
2860         spin_unlock_irq(SCpnt->device->host->host_lock);
2861         return rc;
2862 }
2863
2864 /*
2865   blogic_qcmd creates a CCB for Command and places it into an
2866   Outgoing Mailbox for execution by the associated Host Adapter.
2867 */
2868
2869 static int blogic_qcmd_lck(struct scsi_cmnd *command,
2870                 void (*comp_cb) (struct scsi_cmnd *))
2871 {
2872         struct blogic_adapter *adapter =
2873                 (struct blogic_adapter *) command->device->host->hostdata;
2874         struct blogic_tgt_flags *tgt_flags =
2875                 &adapter->tgt_flags[command->device->id];
2876         struct blogic_tgt_stats *tgt_stats = adapter->tgt_stats;
2877         unsigned char *cdb = command->cmnd;
2878         int cdblen = command->cmd_len;
2879         int tgt_id = command->device->id;
2880         int lun = command->device->lun;
2881         int buflen = scsi_bufflen(command);
2882         int count;
2883         struct blogic_ccb *ccb;
2884         dma_addr_t sense_buf;
2885
2886         /*
2887            SCSI REQUEST_SENSE commands will be executed automatically by the
2888            Host Adapter for any errors, so they should not be executed
2889            explicitly unless the Sense Data is zero indicating that no error
2890            occurred.
2891          */
2892         if (cdb[0] == REQUEST_SENSE && command->sense_buffer[0] != 0) {
2893                 command->result = DID_OK << 16;
2894                 comp_cb(command);
2895                 return 0;
2896         }
2897         /*
2898            Allocate a CCB from the Host Adapter's free list. In the unlikely
2899            event that there are none available and memory allocation fails,
2900            wait 1 second and try again. If that fails, the Host Adapter is
2901            probably hung so signal an error as a Host Adapter Hard Reset
2902            should be initiated soon.
2903          */
2904         ccb = blogic_alloc_ccb(adapter);
2905         if (ccb == NULL) {
2906                 spin_unlock_irq(adapter->scsi_host->host_lock);
2907                 blogic_delay(1);
2908                 spin_lock_irq(adapter->scsi_host->host_lock);
2909                 ccb = blogic_alloc_ccb(adapter);
2910                 if (ccb == NULL) {
2911                         command->result = DID_ERROR << 16;
2912                         comp_cb(command);
2913                         return 0;
2914                 }
2915         }
2916
2917         /*
2918            Initialize the fields in the BusLogic Command Control Block (CCB).
2919          */
2920         count = scsi_dma_map(command);
2921         BUG_ON(count < 0);
2922         if (count) {
2923                 struct scatterlist *sg;
2924                 int i;
2925
2926                 ccb->opcode = BLOGIC_INITIATOR_CCB_SG;
2927                 ccb->datalen = count * sizeof(struct blogic_sg_seg);
2928                 if (blogic_multimaster_type(adapter))
2929                         ccb->data = (unsigned int) ccb->dma_handle +
2930                                         ((unsigned long) &ccb->sglist -
2931                                         (unsigned long) ccb);
2932                 else
2933                         ccb->data = virt_to_32bit_virt(ccb->sglist);
2934
2935                 scsi_for_each_sg(command, sg, count, i) {
2936                         ccb->sglist[i].segbytes = sg_dma_len(sg);
2937                         ccb->sglist[i].segdata = sg_dma_address(sg);
2938                 }
2939         } else if (!count) {
2940                 ccb->opcode = BLOGIC_INITIATOR_CCB;
2941                 ccb->datalen = buflen;
2942                 ccb->data = 0;
2943         }
2944
2945         switch (cdb[0]) {
2946         case READ_6:
2947         case READ_10:
2948                 ccb->datadir = BLOGIC_DATAIN_CHECKED;
2949                 tgt_stats[tgt_id].read_cmds++;
2950                 blogic_addcount(&tgt_stats[tgt_id].bytesread, buflen);
2951                 blogic_incszbucket(tgt_stats[tgt_id].read_sz_buckets, buflen);
2952                 break;
2953         case WRITE_6:
2954         case WRITE_10:
2955                 ccb->datadir = BLOGIC_DATAOUT_CHECKED;
2956                 tgt_stats[tgt_id].write_cmds++;
2957                 blogic_addcount(&tgt_stats[tgt_id].byteswritten, buflen);
2958                 blogic_incszbucket(tgt_stats[tgt_id].write_sz_buckets, buflen);
2959                 break;
2960         default:
2961                 ccb->datadir = BLOGIC_UNCHECKED_TX;
2962                 break;
2963         }
2964         ccb->cdblen = cdblen;
2965         ccb->adapter_status = 0;
2966         ccb->tgt_status = 0;
2967         ccb->tgt_id = tgt_id;
2968         ccb->lun = lun;
2969         ccb->tag_enable = false;
2970         ccb->legacytag_enable = false;
2971         /*
2972            BusLogic recommends that after a Reset the first couple of
2973            commands that are sent to a Target Device be sent in a non
2974            Tagged Queue fashion so that the Host Adapter and Target Device
2975            can establish Synchronous and Wide Transfer before Queue Tag
2976            messages can interfere with the Synchronous and Wide Negotiation
2977            messages.  By waiting to enable Tagged Queuing until after the
2978            first BLOGIC_MAX_TAG_DEPTH commands have been queued, it is
2979            assured that after a Reset any pending commands are requeued
2980            before Tagged Queuing is enabled and that the Tagged Queuing
2981            message will not occur while the partition table is being printed.
2982            In addition, some devices do not properly handle the transition
2983            from non-tagged to tagged commands, so it is necessary to wait
2984            until there are no pending commands for a target device
2985            before queuing tagged commands.
2986          */
2987         if (adapter->cmds_since_rst[tgt_id]++ >= BLOGIC_MAX_TAG_DEPTH &&
2988                         !tgt_flags->tagq_active &&
2989                         adapter->active_cmds[tgt_id] == 0
2990                         && tgt_flags->tagq_ok &&
2991                         (adapter->tagq_ok & (1 << tgt_id))) {
2992                 tgt_flags->tagq_active = true;
2993                 blogic_notice("Tagged Queuing now active for Target %d\n",
2994                                         adapter, tgt_id);
2995         }
2996         if (tgt_flags->tagq_active) {
2997                 enum blogic_queuetag queuetag = BLOGIC_SIMPLETAG;
2998                 /*
2999                    When using Tagged Queuing with Simple Queue Tags, it
3000                    appears that disk drive controllers do not guarantee that
3001                    a queued command will not remain in a disconnected state
3002                    indefinitely if commands that read or write nearer the
3003                    head position continue to arrive without interruption.
3004                    Therefore, for each Target Device this driver keeps track
3005                    of the last time either the queue was empty or an Ordered
3006                    Queue Tag was issued. If more than 4 seconds (one fifth
3007                    of the 20 second disk timeout) have elapsed since this
3008                    last sequence point, this command will be issued with an
3009                    Ordered Queue Tag rather than a Simple Queue Tag, which
3010                    forces the Target Device to complete all previously
3011                    queued commands before this command may be executed.
3012                  */
3013                 if (adapter->active_cmds[tgt_id] == 0)
3014                         adapter->last_seqpoint[tgt_id] = jiffies;
3015                 else if (time_after(jiffies,
3016                                 adapter->last_seqpoint[tgt_id] + 4 * HZ)) {
3017                         adapter->last_seqpoint[tgt_id] = jiffies;
3018                         queuetag = BLOGIC_ORDEREDTAG;
3019                 }
3020                 if (adapter->ext_lun) {
3021                         ccb->tag_enable = true;
3022                         ccb->queuetag = queuetag;
3023                 } else {
3024                         ccb->legacytag_enable = true;
3025                         ccb->legacy_tag = queuetag;
3026                 }
3027         }
3028         memcpy(ccb->cdb, cdb, cdblen);
3029         ccb->sense_datalen = SCSI_SENSE_BUFFERSIZE;
3030         ccb->command = command;
3031         sense_buf = dma_map_single(&adapter->pci_device->dev,
3032                                 command->sense_buffer, ccb->sense_datalen,
3033                                 DMA_FROM_DEVICE);
3034         if (dma_mapping_error(&adapter->pci_device->dev, sense_buf)) {
3035                 blogic_err("DMA mapping for sense data buffer failed\n",
3036                                 adapter);
3037                 blogic_dealloc_ccb(ccb, 0);
3038                 return SCSI_MLQUEUE_HOST_BUSY;
3039         }
3040         ccb->sensedata = sense_buf;
3041         command->scsi_done = comp_cb;
3042         if (blogic_multimaster_type(adapter)) {
3043                 /*
3044                    Place the CCB in an Outgoing Mailbox. The higher levels
3045                    of the SCSI Subsystem should not attempt to queue more
3046                    commands than can be placed in Outgoing Mailboxes, so
3047                    there should always be one free.  In the unlikely event
3048                    that there are none available, wait 1 second and try
3049                    again. If that fails, the Host Adapter is probably hung
3050                    so signal an error as a Host Adapter Hard Reset should
3051                    be initiated soon.
3052                  */
3053                 if (!blogic_write_outbox(adapter, BLOGIC_MBOX_START, ccb)) {
3054                         spin_unlock_irq(adapter->scsi_host->host_lock);
3055                         blogic_warn("Unable to write Outgoing Mailbox - Pausing for 1 second\n", adapter);
3056                         blogic_delay(1);
3057                         spin_lock_irq(adapter->scsi_host->host_lock);
3058                         if (!blogic_write_outbox(adapter, BLOGIC_MBOX_START,
3059                                                 ccb)) {
3060                                 blogic_warn("Still unable to write Outgoing Mailbox - Host Adapter Dead?\n", adapter);
3061                                 blogic_dealloc_ccb(ccb, 1);
3062                                 command->result = DID_ERROR << 16;
3063                                 command->scsi_done(command);
3064                         }
3065                 }
3066         } else {
3067                 /*
3068                    Call the FlashPoint SCCB Manager to start execution of
3069                    the CCB.
3070                  */
3071                 ccb->status = BLOGIC_CCB_ACTIVE;
3072                 adapter->active_cmds[tgt_id]++;
3073                 tgt_stats[tgt_id].cmds_tried++;
3074                 FlashPoint_StartCCB(adapter->cardhandle, ccb);
3075                 /*
3076                    The Command may have already completed and
3077                    blogic_qcompleted_ccb been called, or it may still be
3078                    pending.
3079                  */
3080                 if (ccb->status == BLOGIC_CCB_COMPLETE)
3081                         blogic_process_ccbs(adapter);
3082         }
3083         return 0;
3084 }
3085
3086 static DEF_SCSI_QCMD(blogic_qcmd)
3087
3088 #if 0
3089 /*
3090   blogic_abort aborts Command if possible.
3091 */
3092
3093 static int blogic_abort(struct scsi_cmnd *command)
3094 {
3095         struct blogic_adapter *adapter =
3096                 (struct blogic_adapter *) command->device->host->hostdata;
3097
3098         int tgt_id = command->device->id;
3099         struct blogic_ccb *ccb;
3100         blogic_inc_count(&adapter->tgt_stats[tgt_id].aborts_request);
3101
3102         /*
3103            Attempt to find an Active CCB for this Command. If no Active
3104            CCB for this Command is found, then no Abort is necessary.
3105          */
3106         for (ccb = adapter->all_ccbs; ccb != NULL; ccb = ccb->next_all)
3107                 if (ccb->command == command)
3108                         break;
3109         if (ccb == NULL) {
3110                 blogic_warn("Unable to Abort Command to Target %d - No CCB Found\n", adapter, tgt_id);
3111                 return SUCCESS;
3112         } else if (ccb->status == BLOGIC_CCB_COMPLETE) {
3113                 blogic_warn("Unable to Abort Command to Target %d - CCB Completed\n", adapter, tgt_id);
3114                 return SUCCESS;
3115         } else if (ccb->status == BLOGIC_CCB_RESET) {
3116                 blogic_warn("Unable to Abort Command to Target %d - CCB Reset\n", adapter, tgt_id);
3117                 return SUCCESS;
3118         }
3119         if (blogic_multimaster_type(adapter)) {
3120                 /*
3121                    Attempt to Abort this CCB.  MultiMaster Firmware versions
3122                    prior to 5.xx do not generate Abort Tag messages, but only
3123                    generate the non-tagged Abort message.  Since non-tagged
3124                    commands are not sent by the Host Adapter until the queue
3125                    of outstanding tagged commands has completed, and the
3126                    Abort message is treated as a non-tagged command, it is
3127                    effectively impossible to abort commands when Tagged
3128                    Queuing is active. Firmware version 5.xx does generate
3129                    Abort Tag messages, so it is possible to abort commands
3130                    when Tagged Queuing is active.
3131                  */
3132                 if (adapter->tgt_flags[tgt_id].tagq_active &&
3133                                 adapter->fw_ver[0] < '5') {
3134                         blogic_warn("Unable to Abort CCB #%ld to Target %d - Abort Tag Not Supported\n", adapter, ccb->serial, tgt_id);
3135                         return FAILURE;
3136                 } else if (blogic_write_outbox(adapter, BLOGIC_MBOX_ABORT,
3137                                         ccb)) {
3138                         blogic_warn("Aborting CCB #%ld to Target %d\n",
3139                                         adapter, ccb->serial, tgt_id);
3140                         blogic_inc_count(&adapter->tgt_stats[tgt_id].aborts_tried);
3141                         return SUCCESS;
3142                 } else {
3143                         blogic_warn("Unable to Abort CCB #%ld to Target %d - No Outgoing Mailboxes\n", adapter, ccb->serial, tgt_id);
3144                         return FAILURE;
3145                 }
3146         } else {
3147                 /*
3148                    Call the FlashPoint SCCB Manager to abort execution of
3149                    the CCB.
3150                  */
3151                 blogic_warn("Aborting CCB #%ld to Target %d\n", adapter,
3152                                 ccb->serial, tgt_id);
3153                 blogic_inc_count(&adapter->tgt_stats[tgt_id].aborts_tried);
3154                 FlashPoint_AbortCCB(adapter->cardhandle, ccb);
3155                 /*
3156                    The Abort may have already been completed and
3157                    blogic_qcompleted_ccb been called, or it
3158                    may still be pending.
3159                  */
3160                 if (ccb->status == BLOGIC_CCB_COMPLETE)
3161                         blogic_process_ccbs(adapter);
3162                 return SUCCESS;
3163         }
3164         return SUCCESS;
3165 }
3166
3167 #endif
3168 /*
3169   blogic_resetadapter resets Host Adapter if possible, marking all
3170   currently executing SCSI Commands as having been Reset.
3171 */
3172
3173 static int blogic_resetadapter(struct blogic_adapter *adapter, bool hard_reset)
3174 {
3175         struct blogic_ccb *ccb;
3176         int tgt_id;
3177
3178         /*
3179          * Attempt to Reset and Reinitialize the Host Adapter.
3180          */
3181
3182         if (!(blogic_hwreset(adapter, hard_reset) &&
3183                                 blogic_initadapter(adapter))) {
3184                 blogic_err("Resetting %s Failed\n", adapter,
3185                                                 adapter->full_model);
3186                 return FAILURE;
3187         }
3188
3189         /*
3190          * Deallocate all currently executing CCBs.
3191          */
3192
3193         for (ccb = adapter->all_ccbs; ccb != NULL; ccb = ccb->next_all)
3194                 if (ccb->status == BLOGIC_CCB_ACTIVE)
3195                         blogic_dealloc_ccb(ccb, 1);
3196         /*
3197          * Wait a few seconds between the Host Adapter Hard Reset which
3198          * initiates a SCSI Bus Reset and issuing any SCSI Commands.  Some
3199          * SCSI devices get confused if they receive SCSI Commands too soon
3200          * after a SCSI Bus Reset.
3201          */
3202
3203         if (hard_reset) {
3204                 spin_unlock_irq(adapter->scsi_host->host_lock);
3205                 blogic_delay(adapter->bus_settle_time);
3206                 spin_lock_irq(adapter->scsi_host->host_lock);
3207         }
3208
3209         for (tgt_id = 0; tgt_id < adapter->maxdev; tgt_id++) {
3210                 adapter->last_resettried[tgt_id] = jiffies;
3211                 adapter->last_resetdone[tgt_id] = jiffies;
3212         }
3213         return SUCCESS;
3214 }
3215
3216 /*
3217   blogic_diskparam returns the Heads/Sectors/Cylinders BIOS Disk
3218   Parameters for Disk.  The default disk geometry is 64 heads, 32 sectors, and
3219   the appropriate number of cylinders so as not to exceed drive capacity.  In
3220   order for disks equal to or larger than 1 GB to be addressable by the BIOS
3221   without exceeding the BIOS limitation of 1024 cylinders, Extended Translation
3222   may be enabled in AutoSCSI on FlashPoint Host Adapters and on "W" and "C"
3223   series MultiMaster Host Adapters, or by a dip switch setting on "S" and "A"
3224   series MultiMaster Host Adapters.  With Extended Translation enabled, drives
3225   between 1 GB inclusive and 2 GB exclusive are given a disk geometry of 128
3226   heads and 32 sectors, and drives above 2 GB inclusive are given a disk
3227   geometry of 255 heads and 63 sectors.  However, if the BIOS detects that the
3228   Extended Translation setting does not match the geometry in the partition
3229   table, then the translation inferred from the partition table will be used by
3230   the BIOS, and a warning may be displayed.
3231 */
3232
3233 static int blogic_diskparam(struct scsi_device *sdev, struct block_device *dev,
3234                 sector_t capacity, int *params)
3235 {
3236         struct blogic_adapter *adapter =
3237                                 (struct blogic_adapter *) sdev->host->hostdata;
3238         struct bios_diskparam *diskparam = (struct bios_diskparam *) params;
3239         unsigned char *buf;
3240
3241         if (adapter->ext_trans_enable && capacity >= 2 * 1024 * 1024 /* 1 GB in 512 byte sectors */) {
3242                 if (capacity >= 4 * 1024 * 1024 /* 2 GB in 512 byte sectors */) {
3243                         diskparam->heads = 255;
3244                         diskparam->sectors = 63;
3245                 } else {
3246                         diskparam->heads = 128;
3247                         diskparam->sectors = 32;
3248                 }
3249         } else {
3250                 diskparam->heads = 64;
3251                 diskparam->sectors = 32;
3252         }
3253         diskparam->cylinders = (unsigned long) capacity / (diskparam->heads * diskparam->sectors);
3254         buf = scsi_bios_ptable(dev);
3255         if (buf == NULL)
3256                 return 0;
3257         /*
3258            If the boot sector partition table flag is valid, search for
3259            a partition table entry whose end_head matches one of the
3260            standard BusLogic geometry translations (64/32, 128/32, or 255/63).
3261          */
3262         if (*(unsigned short *) (buf + 64) == MSDOS_LABEL_MAGIC) {
3263                 struct msdos_partition *part1_entry =
3264                                 (struct msdos_partition *)buf;
3265                 struct msdos_partition *part_entry = part1_entry;
3266                 int saved_cyl = diskparam->cylinders, part_no;
3267                 unsigned char part_end_head = 0, part_end_sector = 0;
3268
3269                 for (part_no = 0; part_no < 4; part_no++) {
3270                         part_end_head = part_entry->end_head;
3271                         part_end_sector = part_entry->end_sector & 0x3F;
3272                         if (part_end_head == 64 - 1) {
3273                                 diskparam->heads = 64;
3274                                 diskparam->sectors = 32;
3275                                 break;
3276                         } else if (part_end_head == 128 - 1) {
3277                                 diskparam->heads = 128;
3278                                 diskparam->sectors = 32;
3279                                 break;
3280                         } else if (part_end_head == 255 - 1) {
3281                                 diskparam->heads = 255;
3282                                 diskparam->sectors = 63;
3283                                 break;
3284                         }
3285                         part_entry++;
3286                 }
3287                 if (part_no == 4) {
3288                         part_end_head = part1_entry->end_head;
3289                         part_end_sector = part1_entry->end_sector & 0x3F;
3290                 }
3291                 diskparam->cylinders = (unsigned long) capacity / (diskparam->heads * diskparam->sectors);
3292                 if (part_no < 4 && part_end_sector == diskparam->sectors) {
3293                         if (diskparam->cylinders != saved_cyl)
3294                                 blogic_warn("Adopting Geometry %d/%d from Partition Table\n", adapter, diskparam->heads, diskparam->sectors);
3295                 } else if (part_end_head > 0 || part_end_sector > 0) {
3296                         blogic_warn("Warning: Partition Table appears to have Geometry %d/%d which is\n", adapter, part_end_head + 1, part_end_sector);
3297                         blogic_warn("not compatible with current BusLogic Host Adapter Geometry %d/%d\n", adapter, diskparam->heads, diskparam->sectors);
3298                 }
3299         }
3300         kfree(buf);
3301         return 0;
3302 }
3303
3304
3305 /*
3306   BugLogic_ProcDirectoryInfo implements /proc/scsi/BusLogic/<N>.
3307 */
3308
3309 static int blogic_write_info(struct Scsi_Host *shost, char *procbuf,
3310                                 int bytes_avail)
3311 {
3312         struct blogic_adapter *adapter =
3313                                 (struct blogic_adapter *) shost->hostdata;
3314         struct blogic_tgt_stats *tgt_stats;
3315
3316         tgt_stats = adapter->tgt_stats;
3317         adapter->ext_resets = 0;
3318         adapter->adapter_intern_errors = 0;
3319         memset(tgt_stats, 0, BLOGIC_MAXDEV * sizeof(struct blogic_tgt_stats));
3320         return 0;
3321 }
3322
3323 static int blogic_show_info(struct seq_file *m, struct Scsi_Host *shost)
3324 {
3325         struct blogic_adapter *adapter = (struct blogic_adapter *) shost->hostdata;
3326         struct blogic_tgt_stats *tgt_stats;
3327         int tgt;
3328
3329         tgt_stats = adapter->tgt_stats;
3330         seq_write(m, adapter->msgbuf, adapter->msgbuflen);
3331         seq_printf(m, "\n\
3332 Current Driver Queue Depth:     %d\n\
3333 Currently Allocated CCBs:       %d\n", adapter->drvr_qdepth, adapter->alloc_ccbs);
3334         seq_puts(m, "\n\n\
3335                            DATA TRANSFER STATISTICS\n\
3336 \n\
3337 Target  Tagged Queuing  Queue Depth  Active  Attempted  Completed\n\
3338 ======  ==============  ===========  ======  =========  =========\n");
3339         for (tgt = 0; tgt < adapter->maxdev; tgt++) {
3340                 struct blogic_tgt_flags *tgt_flags = &adapter->tgt_flags[tgt];
3341                 if (!tgt_flags->tgt_exists)
3342                         continue;
3343                 seq_printf(m, "  %2d    %s", tgt, (tgt_flags->tagq_ok ? (tgt_flags->tagq_active ? "    Active" : (adapter->tagq_ok & (1 << tgt)
3344                                                                                                                                                                     ? "  Permitted" : "   Disabled"))
3345                                                                           : "Not Supported"));
3346                 seq_printf(m,
3347                                   "         %3d       %3u    %9u        %9u\n", adapter->qdepth[tgt], adapter->active_cmds[tgt], tgt_stats[tgt].cmds_tried, tgt_stats[tgt].cmds_complete);
3348         }
3349         seq_puts(m, "\n\
3350 Target  Read Commands  Write Commands   Total Bytes Read    Total Bytes Written\n\
3351 ======  =============  ==============  ===================  ===================\n");
3352         for (tgt = 0; tgt < adapter->maxdev; tgt++) {
3353                 struct blogic_tgt_flags *tgt_flags = &adapter->tgt_flags[tgt];
3354                 if (!tgt_flags->tgt_exists)
3355                         continue;
3356                 seq_printf(m, "  %2d      %9u    %9u", tgt, tgt_stats[tgt].read_cmds, tgt_stats[tgt].write_cmds);
3357                 if (tgt_stats[tgt].bytesread.billions > 0)
3358                         seq_printf(m, "     %9u%09u", tgt_stats[tgt].bytesread.billions, tgt_stats[tgt].bytesread.units);
3359                 else
3360                         seq_printf(m, "         %9u", tgt_stats[tgt].bytesread.units);
3361                 if (tgt_stats[tgt].byteswritten.billions > 0)
3362                         seq_printf(m, "   %9u%09u\n", tgt_stats[tgt].byteswritten.billions, tgt_stats[tgt].byteswritten.units);
3363                 else
3364                         seq_printf(m, "      %9u\n", tgt_stats[tgt].byteswritten.units);
3365         }
3366         seq_puts(m, "\n\
3367 Target  Command    0-1KB      1-2KB      2-4KB      4-8KB     8-16KB\n\
3368 ======  =======  =========  =========  =========  =========  =========\n");
3369         for (tgt = 0; tgt < adapter->maxdev; tgt++) {
3370                 struct blogic_tgt_flags *tgt_flags = &adapter->tgt_flags[tgt];
3371                 if (!tgt_flags->tgt_exists)
3372                         continue;
3373                 seq_printf(m,
3374                             "  %2d       Read    %9u  %9u  %9u  %9u  %9u\n", tgt,
3375                             tgt_stats[tgt].read_sz_buckets[0],
3376                             tgt_stats[tgt].read_sz_buckets[1], tgt_stats[tgt].read_sz_buckets[2], tgt_stats[tgt].read_sz_buckets[3], tgt_stats[tgt].read_sz_buckets[4]);
3377                 seq_printf(m,
3378                             "  %2d       Write   %9u  %9u  %9u  %9u  %9u\n", tgt,
3379                             tgt_stats[tgt].write_sz_buckets[0],
3380                             tgt_stats[tgt].write_sz_buckets[1], tgt_stats[tgt].write_sz_buckets[2], tgt_stats[tgt].write_sz_buckets[3], tgt_stats[tgt].write_sz_buckets[4]);
3381         }
3382         seq_puts(m, "\n\
3383 Target  Command   16-32KB    32-64KB   64-128KB   128-256KB   256KB+\n\
3384 ======  =======  =========  =========  =========  =========  =========\n");
3385         for (tgt = 0; tgt < adapter->maxdev; tgt++) {
3386                 struct blogic_tgt_flags *tgt_flags = &adapter->tgt_flags[tgt];
3387                 if (!tgt_flags->tgt_exists)
3388                         continue;
3389                 seq_printf(m,
3390                             "  %2d       Read    %9u  %9u  %9u  %9u  %9u\n", tgt,
3391                             tgt_stats[tgt].read_sz_buckets[5],
3392                             tgt_stats[tgt].read_sz_buckets[6], tgt_stats[tgt].read_sz_buckets[7], tgt_stats[tgt].read_sz_buckets[8], tgt_stats[tgt].read_sz_buckets[9]);
3393                 seq_printf(m,
3394                             "  %2d       Write   %9u  %9u  %9u  %9u  %9u\n", tgt,
3395                             tgt_stats[tgt].write_sz_buckets[5],
3396                             tgt_stats[tgt].write_sz_buckets[6], tgt_stats[tgt].write_sz_buckets[7], tgt_stats[tgt].write_sz_buckets[8], tgt_stats[tgt].write_sz_buckets[9]);
3397         }
3398         seq_puts(m, "\n\n\
3399                            ERROR RECOVERY STATISTICS\n\
3400 \n\
3401           Command Aborts      Bus Device Resets   Host Adapter Resets\n\
3402 Target  Requested Completed  Requested Completed  Requested Completed\n\
3403   ID    \\\\\\\\ Attempted ////  \\\\\\\\ Attempted ////  \\\\\\\\ Attempted ////\n\
3404 ======   ===== ===== =====    ===== ===== =====    ===== ===== =====\n");
3405         for (tgt = 0; tgt < adapter->maxdev; tgt++) {
3406                 struct blogic_tgt_flags *tgt_flags = &adapter->tgt_flags[tgt];
3407                 if (!tgt_flags->tgt_exists)
3408                         continue;
3409                 seq_printf(m, "  %2d     %5d %5d %5d    %5d %5d %5d        %5d %5d %5d\n",
3410                            tgt, tgt_stats[tgt].aborts_request,
3411                            tgt_stats[tgt].aborts_tried,
3412                            tgt_stats[tgt].aborts_done,
3413                            tgt_stats[tgt].bdr_request,
3414                            tgt_stats[tgt].bdr_tried,
3415                            tgt_stats[tgt].bdr_done,
3416                            tgt_stats[tgt].adapter_reset_req,
3417                            tgt_stats[tgt].adapter_reset_attempt,
3418                            tgt_stats[tgt].adapter_reset_done);
3419         }
3420         seq_printf(m, "\nExternal Host Adapter Resets: %d\n", adapter->ext_resets);
3421         seq_printf(m, "Host Adapter Internal Errors: %d\n", adapter->adapter_intern_errors);
3422         return 0;
3423 }
3424
3425
3426 /*
3427   blogic_msg prints Driver Messages.
3428 */
3429 __printf(2, 4)
3430 static void blogic_msg(enum blogic_msglevel msglevel, char *fmt,
3431                         struct blogic_adapter *adapter, ...)
3432 {
3433         static char buf[BLOGIC_LINEBUF_SIZE];
3434         static bool begin = true;
3435         va_list args;
3436         int len = 0;
3437
3438         va_start(args, adapter);
3439         len = vscnprintf(buf, sizeof(buf), fmt, args);
3440         va_end(args);
3441         if (msglevel == BLOGIC_ANNOUNCE_LEVEL) {
3442                 static int msglines = 0;
3443                 strcpy(&adapter->msgbuf[adapter->msgbuflen], buf);
3444                 adapter->msgbuflen += len;
3445                 if (++msglines <= 2)
3446                         printk("%sscsi: %s", blogic_msglevelmap[msglevel], buf);
3447         } else if (msglevel == BLOGIC_INFO_LEVEL) {
3448                 strcpy(&adapter->msgbuf[adapter->msgbuflen], buf);
3449                 adapter->msgbuflen += len;
3450                 if (begin) {
3451                         if (buf[0] != '\n' || len > 1)
3452                                 printk("%sscsi%d: %s", blogic_msglevelmap[msglevel], adapter->host_no, buf);
3453                 } else
3454                         pr_cont("%s", buf);
3455         } else {
3456                 if (begin) {
3457                         if (adapter != NULL && adapter->adapter_initd)
3458                                 printk("%sscsi%d: %s", blogic_msglevelmap[msglevel], adapter->host_no, buf);
3459                         else
3460                                 printk("%s%s", blogic_msglevelmap[msglevel], buf);
3461                 } else
3462                         pr_cont("%s", buf);
3463         }
3464         begin = (buf[len - 1] == '\n');
3465 }
3466
3467
3468 /*
3469   blogic_parse parses an individual option keyword.  It returns true
3470   and updates the pointer if the keyword is recognized and false otherwise.
3471 */
3472
3473 static bool __init blogic_parse(char **str, char *keyword)
3474 {
3475         char *pointer = *str;
3476         while (*keyword != '\0') {
3477                 char strch = *pointer++;
3478                 char keywordch = *keyword++;
3479                 if (strch >= 'A' && strch <= 'Z')
3480                         strch += 'a' - 'Z';
3481                 if (keywordch >= 'A' && keywordch <= 'Z')
3482                         keywordch += 'a' - 'Z';
3483                 if (strch != keywordch)
3484                         return false;
3485         }
3486         *str = pointer;
3487         return true;
3488 }
3489
3490
3491 /*
3492   blogic_parseopts handles processing of BusLogic Driver Options
3493   specifications.
3494
3495   BusLogic Driver Options may be specified either via the Linux Kernel Command
3496   Line or via the Loadable Kernel Module Installation Facility.  Driver Options
3497   for multiple host adapters may be specified either by separating the option
3498   strings by a semicolon, or by specifying multiple "BusLogic=" strings on the
3499   command line.  Individual option specifications for a single host adapter are
3500   separated by commas.  The Probing and Debugging Options apply to all host
3501   adapters whereas the remaining options apply individually only to the
3502   selected host adapter.
3503
3504   The BusLogic Driver Probing Options are described in
3505   <file:Documentation/scsi/BusLogic.rst>.
3506 */
3507
3508 static int __init blogic_parseopts(char *options)
3509 {
3510         while (true) {
3511                 struct blogic_drvr_options *drvr_opts =
3512                         &blogic_drvr_options[blogic_drvr_options_count++];
3513                 int tgt_id;
3514
3515                 memset(drvr_opts, 0, sizeof(struct blogic_drvr_options));
3516                 while (*options != '\0' && *options != ';') {
3517                         if (blogic_parse(&options, "NoProbePCI"))
3518                                 blogic_probe_options.noprobe_pci = true;
3519                         else if (blogic_parse(&options, "NoProbe"))
3520                                 blogic_probe_options.noprobe = true;
3521                         else if (blogic_parse(&options, "NoSortPCI"))
3522                                 blogic_probe_options.nosort_pci = true;
3523                         else if (blogic_parse(&options, "MultiMasterFirst"))
3524                                 blogic_probe_options.multimaster_first = true;
3525                         else if (blogic_parse(&options, "FlashPointFirst"))
3526                                 blogic_probe_options.flashpoint_first = true;
3527                         /* Tagged Queuing Options. */
3528                         else if (blogic_parse(&options, "QueueDepth:[") ||
3529                                         blogic_parse(&options, "QD:[")) {
3530                                 for (tgt_id = 0; tgt_id < BLOGIC_MAXDEV; tgt_id++) {
3531                                         unsigned short qdepth = simple_strtoul(options, &options, 0);
3532                                         if (qdepth > BLOGIC_MAX_TAG_DEPTH) {
3533                                                 blogic_err("BusLogic: Invalid Driver Options (invalid Queue Depth %d)\n", NULL, qdepth);
3534                                                 return 0;
3535                                         }
3536                                         drvr_opts->qdepth[tgt_id] = qdepth;
3537                                         if (*options == ',')
3538                                                 options++;
3539                                         else if (*options == ']')
3540                                                 break;
3541                                         else {
3542                                                 blogic_err("BusLogic: Invalid Driver Options (',' or ']' expected at '%s')\n", NULL, options);
3543                                                 return 0;
3544                                         }
3545                                 }
3546                                 if (*options != ']') {
3547                                         blogic_err("BusLogic: Invalid Driver Options (']' expected at '%s')\n", NULL, options);
3548                                         return 0;
3549                                 } else
3550                                         options++;
3551                         } else if (blogic_parse(&options, "QueueDepth:") || blogic_parse(&options, "QD:")) {
3552                                 unsigned short qdepth = simple_strtoul(options, &options, 0);
3553                                 if (qdepth == 0 ||
3554                                                 qdepth > BLOGIC_MAX_TAG_DEPTH) {
3555                                         blogic_err("BusLogic: Invalid Driver Options (invalid Queue Depth %d)\n", NULL, qdepth);
3556                                         return 0;
3557                                 }
3558                                 drvr_opts->common_qdepth = qdepth;
3559                                 for (tgt_id = 0; tgt_id < BLOGIC_MAXDEV; tgt_id++)
3560                                         drvr_opts->qdepth[tgt_id] = qdepth;
3561                         } else if (blogic_parse(&options, "TaggedQueuing:") ||
3562                                         blogic_parse(&options, "TQ:")) {
3563                                 if (blogic_parse(&options, "Default")) {
3564                                         drvr_opts->tagq_ok = 0x0000;
3565                                         drvr_opts->tagq_ok_mask = 0x0000;
3566                                 } else if (blogic_parse(&options, "Enable")) {
3567                                         drvr_opts->tagq_ok = 0xFFFF;
3568                                         drvr_opts->tagq_ok_mask = 0xFFFF;
3569                                 } else if (blogic_parse(&options, "Disable")) {
3570                                         drvr_opts->tagq_ok = 0x0000;
3571                                         drvr_opts->tagq_ok_mask = 0xFFFF;
3572                                 } else {
3573                                         unsigned short tgt_bit;
3574                                         for (tgt_id = 0, tgt_bit = 1;
3575                                                 tgt_id < BLOGIC_MAXDEV;
3576                                                 tgt_id++, tgt_bit <<= 1)
3577                                                 switch (*options++) {
3578                                                 case 'Y':
3579                                                         drvr_opts->tagq_ok |= tgt_bit;
3580                                                         drvr_opts->tagq_ok_mask |= tgt_bit;
3581                                                         break;
3582                                                 case 'N':
3583                                                         drvr_opts->tagq_ok &= ~tgt_bit;
3584                                                         drvr_opts->tagq_ok_mask |= tgt_bit;
3585                                                         break;
3586                                                 case 'X':
3587                                                         break;
3588                                                 default:
3589                                                         options--;
3590                                                         tgt_id = BLOGIC_MAXDEV;
3591                                                         break;
3592                                                 }
3593                                 }
3594                         }
3595                         /* Miscellaneous Options. */
3596                         else if (blogic_parse(&options, "BusSettleTime:") ||
3597                                         blogic_parse(&options, "BST:")) {
3598                                 unsigned short bus_settle_time =
3599                                         simple_strtoul(options, &options, 0);
3600                                 if (bus_settle_time > 5 * 60) {
3601                                         blogic_err("BusLogic: Invalid Driver Options (invalid Bus Settle Time %d)\n", NULL, bus_settle_time);
3602                                         return 0;
3603                                 }
3604                                 drvr_opts->bus_settle_time = bus_settle_time;
3605                         } else if (blogic_parse(&options,
3606                                                 "InhibitTargetInquiry"))
3607                                 drvr_opts->stop_tgt_inquiry = true;
3608                         /* Debugging Options. */
3609                         else if (blogic_parse(&options, "TraceProbe"))
3610                                 blogic_global_options.trace_probe = true;
3611                         else if (blogic_parse(&options, "TraceHardwareReset"))
3612                                 blogic_global_options.trace_hw_reset = true;
3613                         else if (blogic_parse(&options, "TraceConfiguration"))
3614                                 blogic_global_options.trace_config = true;
3615                         else if (blogic_parse(&options, "TraceErrors"))
3616                                 blogic_global_options.trace_err = true;
3617                         else if (blogic_parse(&options, "Debug")) {
3618                                 blogic_global_options.trace_probe = true;
3619                                 blogic_global_options.trace_hw_reset = true;
3620                                 blogic_global_options.trace_config = true;
3621                                 blogic_global_options.trace_err = true;
3622                         }
3623                         if (*options == ',')
3624                                 options++;
3625                         else if (*options != ';' && *options != '\0') {
3626                                 blogic_err("BusLogic: Unexpected Driver Option '%s' ignored\n", NULL, options);
3627                                 *options = '\0';
3628                         }
3629                 }
3630                 if (!(blogic_drvr_options_count == 0 ||
3631                         blogic_probeinfo_count == 0 ||
3632                         blogic_drvr_options_count == blogic_probeinfo_count)) {
3633                         blogic_err("BusLogic: Invalid Driver Options (all or no I/O Addresses must be specified)\n", NULL);
3634                         return 0;
3635                 }
3636                 /*
3637                    Tagged Queuing is disabled when the Queue Depth is 1 since queuing
3638                    multiple commands is not possible.
3639                  */
3640                 for (tgt_id = 0; tgt_id < BLOGIC_MAXDEV; tgt_id++)
3641                         if (drvr_opts->qdepth[tgt_id] == 1) {
3642                                 unsigned short tgt_bit = 1 << tgt_id;
3643                                 drvr_opts->tagq_ok &= ~tgt_bit;
3644                                 drvr_opts->tagq_ok_mask |= tgt_bit;
3645                         }
3646                 if (*options == ';')
3647                         options++;
3648                 if (*options == '\0')
3649                         return 0;
3650         }
3651         return 1;
3652 }
3653
3654 /*
3655   Get it all started
3656 */
3657
3658 static struct scsi_host_template blogic_template = {
3659         .module = THIS_MODULE,
3660         .proc_name = "BusLogic",
3661         .write_info = blogic_write_info,
3662         .show_info = blogic_show_info,
3663         .name = "BusLogic",
3664         .info = blogic_drvr_info,
3665         .queuecommand = blogic_qcmd,
3666         .slave_configure = blogic_slaveconfig,
3667         .bios_param = blogic_diskparam,
3668         .eh_host_reset_handler = blogic_hostreset,
3669 #if 0
3670         .eh_abort_handler = blogic_abort,
3671 #endif
3672         .max_sectors = 128,
3673 };
3674
3675 /*
3676   blogic_setup handles processing of Kernel Command Line Arguments.
3677 */
3678
3679 static int __init blogic_setup(char *str)
3680 {
3681         int ints[3];
3682
3683         (void) get_options(str, ARRAY_SIZE(ints), ints);
3684
3685         if (ints[0] != 0) {
3686                 blogic_err("BusLogic: Obsolete Command Line Entry Format Ignored\n", NULL);
3687                 return 0;
3688         }
3689         if (str == NULL || *str == '\0')
3690                 return 0;
3691         return blogic_parseopts(str);
3692 }
3693
3694 /*
3695  * Exit function.  Deletes all hosts associated with this driver.
3696  */
3697
3698 static void __exit blogic_exit(void)
3699 {
3700         struct blogic_adapter *ha, *next;
3701
3702         list_for_each_entry_safe(ha, next, &blogic_host_list, host_list)
3703                 blogic_deladapter(ha);
3704 }
3705
3706 __setup("BusLogic=", blogic_setup);
3707
3708 #ifdef MODULE
3709 /*static struct pci_device_id blogic_pci_tbl[] = {
3710         { PCI_VENDOR_ID_BUSLOGIC, PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER,
3711           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3712         { PCI_VENDOR_ID_BUSLOGIC, PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER_NC,
3713           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3714         { PCI_VENDOR_ID_BUSLOGIC, PCI_DEVICE_ID_BUSLOGIC_FLASHPOINT,
3715           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3716         { }
3717 };*/
3718 static const struct pci_device_id blogic_pci_tbl[] = {
3719         {PCI_DEVICE(PCI_VENDOR_ID_BUSLOGIC, PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER)},
3720         {PCI_DEVICE(PCI_VENDOR_ID_BUSLOGIC, PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER_NC)},
3721         {PCI_DEVICE(PCI_VENDOR_ID_BUSLOGIC, PCI_DEVICE_ID_BUSLOGIC_FLASHPOINT)},
3722         {0, },
3723 };
3724 #endif
3725 MODULE_DEVICE_TABLE(pci, blogic_pci_tbl);
3726
3727 module_init(blogic_init);
3728 module_exit(blogic_exit);