MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / scsi / cpqfcTSinit.c
blob93b940a32e22b0034dd8bdf3a210f4ec818d1744
1 /* Copyright(c) 2000, Compaq Computer Corporation
2 * Fibre Channel Host Bus Adapter
3 * 64-bit, 66MHz PCI
4 * Originally developed and tested on:
5 * (front): [chip] Tachyon TS HPFC-5166A/1.2 L2C1090 ...
6 * SP# P225CXCBFIEL6T, Rev XC
7 * SP# 161290-001, Rev XD
8 * (back): Board No. 010008-001 A/W Rev X5, FAB REV X5
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
13 * later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 * Written by Don Zimmerman
20 * IOCTL and procfs added by Jouke Numan
21 * SMP testing by Chel Van Gennip
23 * portions copied from:
24 * QLogic CPQFCTS SCSI-FCP
25 * Written by Erik H. Moe, ehm@cris.com
26 * Copyright 1995, Erik H. Moe
27 * Renamed and updated to 1.3.x by Michael Griffith <grif@cs.ucr.edu>
28 * Chris Loveland <cwl@iol.unh.edu> to support the isp2100 and isp2200
32 #define LinuxVersionCode(v, p, s) (((v)<<16)+((p)<<8)+(s))
34 #include <linux/config.h>
35 #include <linux/interrupt.h>
36 #include <linux/module.h>
37 #include <linux/version.h>
38 #include <linux/blkdev.h>
39 #include <linux/kernel.h>
40 #include <linux/string.h>
41 #include <linux/types.h>
42 #include <linux/pci.h>
43 #include <linux/delay.h>
44 #include <linux/timer.h>
45 #include <linux/init.h>
46 #include <linux/ioport.h> // request_region() prototype
47 #include <linux/completion.h>
49 #include <asm/io.h>
50 #include <asm/uaccess.h> // ioctl related
51 #include <asm/irq.h>
52 #include <linux/spinlock.h>
53 #include "scsi.h"
54 #include <scsi/scsi_host.h>
55 #include <scsi/scsi_ioctl.h>
56 #include "cpqfcTSchip.h"
57 #include "cpqfcTSstructs.h"
58 #include "cpqfcTStrigger.h"
60 #include "cpqfcTS.h"
62 /* Embedded module documentation macros - see module.h */
63 MODULE_AUTHOR("Compaq Computer Corporation");
64 MODULE_DESCRIPTION("Driver for Compaq 64-bit/66Mhz PCI Fibre Channel HBA v. 2.5.4");
65 MODULE_LICENSE("GPL");
67 int cpqfcTS_TargetDeviceReset( Scsi_Device *ScsiDev, unsigned int reset_flags);
69 // This struct was originally defined in
70 // /usr/src/linux/include/linux/proc_fs.h
71 // since it's only partially implemented, we only use first
72 // few fields...
73 // NOTE: proc_fs changes in 2.4 kernel
75 #if LINUX_VERSION_CODE < LinuxVersionCode(2,3,27)
76 static struct proc_dir_entry proc_scsi_cpqfcTS =
78 PROC_SCSI_CPQFCTS, // ushort low_ino (enumerated list)
79 7, // ushort namelen
80 DEV_NAME, // const char* name
81 S_IFDIR | S_IRUGO | S_IXUGO, // mode_t mode
82 2 // nlink_t nlink
83 // etc. ...
87 #endif
89 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,7)
90 # define CPQFC_DECLARE_COMPLETION(x) DECLARE_COMPLETION(x)
91 # define CPQFC_WAITING waiting
92 # define CPQFC_COMPLETE(x) complete(x)
93 # define CPQFC_WAIT_FOR_COMPLETION(x) wait_for_completion(x);
94 #else
95 # define CPQFC_DECLARE_COMPLETION(x) DECLARE_MUTEX_LOCKED(x)
96 # define CPQFC_WAITING sem
97 # define CPQFC_COMPLETE(x) up(x)
98 # define CPQFC_WAIT_FOR_COMPLETION(x) down(x)
99 #endif
101 static int cpqfc_alloc_private_data_pool(CPQFCHBA *hba);
103 /* local function to load our per-HBA (local) data for chip
104 registers, FC link state, all FC exchanges, etc.
106 We allocate space and compute address offsets for the
107 most frequently accessed addresses; others (like World Wide
108 Name) are not necessary.
110 static void Cpqfc_initHBAdata(CPQFCHBA *cpqfcHBAdata, struct pci_dev *PciDev )
113 cpqfcHBAdata->PciDev = PciDev; // copy PCI info ptr
115 // since x86 port space is 64k, we only need the lower 16 bits
116 cpqfcHBAdata->fcChip.Registers.IOBaseL =
117 PciDev->resource[1].start & PCI_BASE_ADDRESS_IO_MASK;
119 cpqfcHBAdata->fcChip.Registers.IOBaseU =
120 PciDev->resource[2].start & PCI_BASE_ADDRESS_IO_MASK;
122 // 32-bit memory addresses
123 cpqfcHBAdata->fcChip.Registers.MemBase =
124 PciDev->resource[3].start & PCI_BASE_ADDRESS_MEM_MASK;
126 cpqfcHBAdata->fcChip.Registers.ReMapMemBase =
127 ioremap( PciDev->resource[3].start & PCI_BASE_ADDRESS_MEM_MASK,
128 0x200);
130 cpqfcHBAdata->fcChip.Registers.RAMBase =
131 PciDev->resource[4].start;
133 cpqfcHBAdata->fcChip.Registers.SROMBase = // NULL for HP TS adapter
134 PciDev->resource[5].start;
136 // now the Tachlite chip registers
137 // the REGISTER struct holds both the physical address & last
138 // written value (some TL registers are WRITE ONLY)
140 cpqfcHBAdata->fcChip.Registers.SFQconsumerIndex.address =
141 cpqfcHBAdata->fcChip.Registers.ReMapMemBase + TL_MEM_SFQ_CONSUMER_INDEX;
143 cpqfcHBAdata->fcChip.Registers.ERQproducerIndex.address =
144 cpqfcHBAdata->fcChip.Registers.ReMapMemBase + TL_MEM_ERQ_PRODUCER_INDEX;
146 // TL Frame Manager
147 cpqfcHBAdata->fcChip.Registers.FMconfig.address =
148 cpqfcHBAdata->fcChip.Registers.ReMapMemBase + TL_MEM_FM_CONFIG;
149 cpqfcHBAdata->fcChip.Registers.FMcontrol.address =
150 cpqfcHBAdata->fcChip.Registers.ReMapMemBase + TL_MEM_FM_CONTROL;
151 cpqfcHBAdata->fcChip.Registers.FMstatus.address =
152 cpqfcHBAdata->fcChip.Registers.ReMapMemBase + TL_MEM_FM_STATUS;
153 cpqfcHBAdata->fcChip.Registers.FMLinkStatus1.address =
154 cpqfcHBAdata->fcChip.Registers.ReMapMemBase + TL_MEM_FM_LINK_STAT1;
155 cpqfcHBAdata->fcChip.Registers.FMLinkStatus2.address =
156 cpqfcHBAdata->fcChip.Registers.ReMapMemBase + TL_MEM_FM_LINK_STAT2;
157 cpqfcHBAdata->fcChip.Registers.FMBB_CreditZero.address =
158 cpqfcHBAdata->fcChip.Registers.ReMapMemBase + TL_MEM_FM_BB_CREDIT0;
160 // TL Control Regs
161 cpqfcHBAdata->fcChip.Registers.TYconfig.address =
162 cpqfcHBAdata->fcChip.Registers.ReMapMemBase + TL_MEM_TACH_CONFIG;
163 cpqfcHBAdata->fcChip.Registers.TYcontrol.address =
164 cpqfcHBAdata->fcChip.Registers.ReMapMemBase + TL_MEM_TACH_CONTROL;
165 cpqfcHBAdata->fcChip.Registers.TYstatus.address =
166 cpqfcHBAdata->fcChip.Registers.ReMapMemBase + TL_MEM_TACH_STATUS;
167 cpqfcHBAdata->fcChip.Registers.rcv_al_pa.address =
168 cpqfcHBAdata->fcChip.Registers.ReMapMemBase + TL_MEM_FM_RCV_AL_PA;
169 cpqfcHBAdata->fcChip.Registers.ed_tov.address =
170 cpqfcHBAdata->fcChip.Registers.ReMapMemBase + TL_MEM_FM_ED_TOV;
173 cpqfcHBAdata->fcChip.Registers.INTEN.address =
174 cpqfcHBAdata->fcChip.Registers.ReMapMemBase + IINTEN;
175 cpqfcHBAdata->fcChip.Registers.INTPEND.address =
176 cpqfcHBAdata->fcChip.Registers.ReMapMemBase + IINTPEND;
177 cpqfcHBAdata->fcChip.Registers.INTSTAT.address =
178 cpqfcHBAdata->fcChip.Registers.ReMapMemBase + IINTSTAT;
180 DEBUG_PCI(printk(" cpqfcHBAdata->fcChip.Registers. :\n"));
181 DEBUG_PCI(printk(" IOBaseL = %x\n",
182 cpqfcHBAdata->fcChip.Registers.IOBaseL));
183 DEBUG_PCI(printk(" IOBaseU = %x\n",
184 cpqfcHBAdata->fcChip.Registers.IOBaseU));
186 /* printk(" ioremap'd Membase: %p\n", cpqfcHBAdata->fcChip.Registers.ReMapMemBase); */
188 DEBUG_PCI(printk(" SFQconsumerIndex.address = %p\n",
189 cpqfcHBAdata->fcChip.Registers.SFQconsumerIndex.address));
190 DEBUG_PCI(printk(" ERQproducerIndex.address = %p\n",
191 cpqfcHBAdata->fcChip.Registers.ERQproducerIndex.address));
192 DEBUG_PCI(printk(" TYconfig.address = %p\n",
193 cpqfcHBAdata->fcChip.Registers.TYconfig.address));
194 DEBUG_PCI(printk(" FMconfig.address = %p\n",
195 cpqfcHBAdata->fcChip.Registers.FMconfig.address));
196 DEBUG_PCI(printk(" FMcontrol.address = %p\n",
197 cpqfcHBAdata->fcChip.Registers.FMcontrol.address));
199 // set default options for FC controller (chip)
200 cpqfcHBAdata->fcChip.Options.initiator = 1; // default: SCSI initiator
201 cpqfcHBAdata->fcChip.Options.target = 0; // default: SCSI target
202 cpqfcHBAdata->fcChip.Options.extLoopback = 0;// default: no loopback @GBIC
203 cpqfcHBAdata->fcChip.Options.intLoopback = 0;// default: no loopback inside chip
205 // set highest and lowest FC-PH version the adapter/driver supports
206 // (NOT strict compliance)
207 cpqfcHBAdata->fcChip.highest_FCPH_ver = FC_PH3;
208 cpqfcHBAdata->fcChip.lowest_FCPH_ver = FC_PH43;
210 // set function points for this controller / adapter
211 cpqfcHBAdata->fcChip.ResetTachyon = CpqTsResetTachLite;
212 cpqfcHBAdata->fcChip.FreezeTachyon = CpqTsFreezeTachlite;
213 cpqfcHBAdata->fcChip.UnFreezeTachyon = CpqTsUnFreezeTachlite;
214 cpqfcHBAdata->fcChip.CreateTachyonQues = CpqTsCreateTachLiteQues;
215 cpqfcHBAdata->fcChip.DestroyTachyonQues = CpqTsDestroyTachLiteQues;
216 cpqfcHBAdata->fcChip.InitializeTachyon = CpqTsInitializeTachLite;
217 cpqfcHBAdata->fcChip.LaserControl = CpqTsLaserControl;
218 cpqfcHBAdata->fcChip.ProcessIMQEntry = CpqTsProcessIMQEntry;
219 cpqfcHBAdata->fcChip.InitializeFrameManager = CpqTsInitializeFrameManager;
220 cpqfcHBAdata->fcChip.ReadWriteWWN = CpqTsReadWriteWWN;
221 cpqfcHBAdata->fcChip.ReadWriteNVRAM = CpqTsReadWriteNVRAM;
223 if (cpqfc_alloc_private_data_pool(cpqfcHBAdata) != 0) {
224 printk(KERN_WARNING
225 "cpqfc: unable to allocate pool for passthru ioctls. "
226 "Passthru ioctls disabled.\n");
231 /* (borrowed from linux/drivers/scsi/hosts.c) */
232 static void launch_FCworker_thread(struct Scsi_Host *HostAdapter)
234 DECLARE_MUTEX_LOCKED(sem);
236 CPQFCHBA *cpqfcHBAdata = (CPQFCHBA *)HostAdapter->hostdata;
238 ENTER("launch_FC_worker_thread");
240 cpqfcHBAdata->notify_wt = &sem;
242 /* must unlock before kernel_thread(), for it may cause a reschedule. */
243 spin_unlock_irq(HostAdapter->host_lock);
244 kernel_thread((int (*)(void *))cpqfcTSWorkerThread,
245 (void *) HostAdapter, 0);
247 * Now wait for the kernel error thread to initialize itself
250 down (&sem);
251 spin_lock_irq(HostAdapter->host_lock);
252 cpqfcHBAdata->notify_wt = NULL;
254 LEAVE("launch_FC_worker_thread");
259 /* "Entry" point to discover if any supported PCI
260 bus adapter can be found
262 /* We're supporting:
263 * Compaq 64-bit, 66MHz HBA with Tachyon TS
264 * Agilent XL2
265 * HP Tachyon
267 #define HBA_TYPES 3
269 #ifndef PCI_DEVICE_ID_COMPAQ_
270 #define PCI_DEVICE_ID_COMPAQ_TACHYON 0xa0fc
271 #endif
273 static struct SupportedPCIcards cpqfc_boards[] __initdata = {
274 {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_TACHYON},
275 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_TACHLITE},
276 {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_TACHYON},
280 int cpqfcTS_detect(Scsi_Host_Template *ScsiHostTemplate)
282 int NumberOfAdapters=0; // how many of our PCI adapters are found?
283 struct pci_dev *PciDev = NULL;
284 struct Scsi_Host *HostAdapter = NULL;
285 CPQFCHBA *cpqfcHBAdata = NULL;
286 struct timer_list *cpqfcTStimer = NULL;
287 int i;
289 ENTER("cpqfcTS_detect");
291 #if LINUX_VERSION_CODE < LinuxVersionCode(2,3,27)
292 ScsiHostTemplate->proc_dir = &proc_scsi_cpqfcTS;
293 #else
294 ScsiHostTemplate->proc_name = "cpqfcTS";
295 #endif
297 for( i=0; i < HBA_TYPES; i++)
299 // look for all HBAs of each type
301 while((PciDev = pci_find_device(cpqfc_boards[i].vendor_id,
302 cpqfc_boards[i].device_id, PciDev)))
305 if (pci_enable_device(PciDev)) {
306 printk(KERN_ERR
307 "cpqfc: can't enable PCI device at %s\n", pci_name(PciDev));
308 goto err_continue;
311 if (pci_set_dma_mask(PciDev, CPQFCTS_DMA_MASK) != 0) {
312 printk(KERN_WARNING
313 "cpqfc: HBA cannot support required DMA mask, skipping.\n");
314 goto err_disable_dev;
317 // NOTE: (kernel 2.2.12-32) limits allocation to 128k bytes...
318 /* printk(" scsi_register allocating %d bytes for FC HBA\n",
319 (ULONG)sizeof(CPQFCHBA)); */
321 HostAdapter = scsi_register( ScsiHostTemplate, sizeof( CPQFCHBA ) );
323 if(HostAdapter == NULL) {
324 printk(KERN_WARNING
325 "cpqfc: can't register SCSI HBA, skipping.\n");
326 goto err_disable_dev;
328 DEBUG_PCI( printk(" HBA found!\n"));
329 DEBUG_PCI( printk(" HostAdapter->PciDev->irq = %u\n", PciDev->irq) );
330 DEBUG_PCI(printk(" PciDev->baseaddress[0]= %lx\n",
331 PciDev->resource[0].start));
332 DEBUG_PCI(printk(" PciDev->baseaddress[1]= %lx\n",
333 PciDev->resource[1].start));
334 DEBUG_PCI(printk(" PciDev->baseaddress[2]= %lx\n",
335 PciDev->resource[2].start));
336 DEBUG_PCI(printk(" PciDev->baseaddress[3]= %lx\n",
337 PciDev->resource[3].start));
339 scsi_set_device(HostAdapter, &PciDev->dev);
340 HostAdapter->irq = PciDev->irq; // copy for Scsi layers
342 // HP Tachlite uses two (255-byte) ranges of Port I/O (lower & upper),
343 // for a total I/O port address space of 512 bytes.
344 // mask out the I/O port address (lower) & record
345 HostAdapter->io_port = (unsigned int)
346 PciDev->resource[1].start & PCI_BASE_ADDRESS_IO_MASK;
347 HostAdapter->n_io_port = 0xff;
349 // i.e., expect 128 targets (arbitrary number), while the
350 // RA-4000 supports 32 LUNs
351 HostAdapter->max_id = 0; // incremented as devices log in
352 HostAdapter->max_lun = CPQFCTS_MAX_LUN; // LUNs per FC device
353 HostAdapter->max_channel = CPQFCTS_MAX_CHANNEL; // multiple busses?
355 // get the pointer to our HBA specific data... (one for
356 // each HBA on the PCI bus(ses)).
357 cpqfcHBAdata = (CPQFCHBA *)HostAdapter->hostdata;
359 // make certain our data struct is clear
360 memset( cpqfcHBAdata, 0, sizeof( CPQFCHBA ) );
363 // initialize our HBA info
364 cpqfcHBAdata->HBAnum = NumberOfAdapters;
366 cpqfcHBAdata->HostAdapter = HostAdapter; // back ptr
367 Cpqfc_initHBAdata( cpqfcHBAdata, PciDev ); // fill MOST fields
369 cpqfcHBAdata->HBAnum = NumberOfAdapters;
370 cpqfcHBAdata->hba_spinlock = SPIN_LOCK_UNLOCKED;
372 // request necessary resources and check for conflicts
373 if( request_irq( HostAdapter->irq,
374 cpqfcTS_intr_handler,
375 SA_INTERRUPT | SA_SHIRQ,
376 DEV_NAME,
377 HostAdapter) )
379 printk(KERN_WARNING "cpqfc: IRQ %u already used\n", HostAdapter->irq);
380 goto err_unregister;
383 // Since we have two 256-byte I/O port ranges (upper
384 // and lower), check them both
385 if( !request_region( cpqfcHBAdata->fcChip.Registers.IOBaseU,
386 0xff, DEV_NAME ) )
388 printk(KERN_WARNING "cpqfc: address in use: %x\n",
389 cpqfcHBAdata->fcChip.Registers.IOBaseU);
390 goto err_free_irq;
393 if( !request_region( cpqfcHBAdata->fcChip.Registers.IOBaseL,
394 0xff, DEV_NAME ) )
396 printk(KERN_WARNING "cpqfc: address in use: %x\n",
397 cpqfcHBAdata->fcChip.Registers.IOBaseL);
398 goto err_release_region_U;
401 // OK, we have grabbed everything we need now.
402 DEBUG_PCI(printk(" Reserved 255 I/O addresses @ %x\n",
403 cpqfcHBAdata->fcChip.Registers.IOBaseL ));
404 DEBUG_PCI(printk(" Reserved 255 I/O addresses @ %x\n",
405 cpqfcHBAdata->fcChip.Registers.IOBaseU ));
409 // start our kernel worker thread
411 spin_lock_irq(HostAdapter->host_lock);
412 launch_FCworker_thread(HostAdapter);
415 // start our TimerTask...
417 cpqfcTStimer = &cpqfcHBAdata->cpqfcTStimer;
419 init_timer( cpqfcTStimer); // Linux clears next/prev values
420 cpqfcTStimer->expires = jiffies + HZ; // one second
421 cpqfcTStimer->data = (unsigned long)cpqfcHBAdata; // this adapter
422 cpqfcTStimer->function = cpqfcTSheartbeat; // handles timeouts, housekeeping
424 add_timer( cpqfcTStimer); // give it to Linux
427 // now initialize our hardware...
428 if (cpqfcHBAdata->fcChip.InitializeTachyon( cpqfcHBAdata, 1,1)) {
429 printk(KERN_WARNING "cpqfc: initialization of HBA hardware failed.\n");
430 goto err_release_region_L;
433 cpqfcHBAdata->fcStatsTime = jiffies; // (for FC Statistics delta)
435 // give our HBA time to initialize and login current devices...
437 // The Brocade switch (e.g. 2400, 2010, etc.) as of March 2000,
438 // has the following algorithm for FL_Port startup:
439 // Time(sec) Action
440 // 0: Device Plugin and LIP(F7,F7) transmission
441 // 1.0 LIP incoming
442 // 1.027 LISA incoming, no CLS! (link not up)
443 // 1.028 NOS incoming (switch test for N_Port)
444 // 1.577 ED_TOV expired, transmit LIPs again
445 // 3.0 LIP(F8,F7) incoming (switch passes Tach Prim.Sig)
446 // 3.028 LILP received, link up, FLOGI starts
447 // slowest(worst) case, measured on 1Gb Finisar GT analyzer
449 unsigned long stop_time;
451 spin_unlock_irq(HostAdapter->host_lock);
452 stop_time = jiffies + 4*HZ;
453 while ( time_before(jiffies, stop_time) )
454 schedule(); // (our worker task needs to run)
458 spin_lock_irq(HostAdapter->host_lock);
459 NumberOfAdapters++;
460 spin_unlock_irq(HostAdapter->host_lock);
462 continue;
464 err_release_region_L:
465 release_region( cpqfcHBAdata->fcChip.Registers.IOBaseL, 0xff );
466 err_release_region_U:
467 release_region( cpqfcHBAdata->fcChip.Registers.IOBaseU, 0xff );
468 err_free_irq:
469 free_irq( HostAdapter->irq, HostAdapter);
470 err_unregister:
471 scsi_unregister( HostAdapter);
472 err_disable_dev:
473 pci_disable_device( PciDev );
474 err_continue:
475 continue;
476 } // end of while()
479 LEAVE("cpqfcTS_detect");
481 return NumberOfAdapters;
484 #ifdef SUPPORT_RESET
485 static void my_ioctl_done (Scsi_Cmnd * SCpnt)
487 struct request * req;
489 req = SCpnt->request;
490 req->rq_status = RQ_SCSI_DONE; /* Busy, but indicate request done */
492 if (req->CPQFC_WAITING != NULL)
493 CPQFC_COMPLETE(req->CPQFC_WAITING);
495 #endif
497 static int cpqfc_alloc_private_data_pool(CPQFCHBA *hba)
499 hba->private_data_bits = NULL;
500 hba->private_data_pool = NULL;
501 hba->private_data_bits =
502 kmalloc(((CPQFC_MAX_PASSTHRU_CMDS+BITS_PER_LONG-1) /
503 BITS_PER_LONG)*sizeof(unsigned long),
504 GFP_KERNEL);
505 if (hba->private_data_bits == NULL)
506 return -1;
507 memset(hba->private_data_bits, 0,
508 ((CPQFC_MAX_PASSTHRU_CMDS+BITS_PER_LONG-1) /
509 BITS_PER_LONG)*sizeof(unsigned long));
510 hba->private_data_pool = kmalloc(sizeof(cpqfc_passthru_private_t) *
511 CPQFC_MAX_PASSTHRU_CMDS, GFP_KERNEL);
512 if (hba->private_data_pool == NULL) {
513 kfree(hba->private_data_bits);
514 hba->private_data_bits = NULL;
515 return -1;
517 return 0;
520 static void cpqfc_free_private_data_pool(CPQFCHBA *hba)
522 kfree(hba->private_data_bits);
523 kfree(hba->private_data_pool);
526 int is_private_data_of_cpqfc(CPQFCHBA *hba, void *pointer)
528 /* Is pointer within our private data pool?
529 We use Scsi_Request->upper_private_data (normally
530 reserved for upper layer drivers, e.g. the sg driver)
531 We check to see if the pointer is ours by looking at
532 its address. Is this ok? Hmm, it occurs to me that
533 a user app might do something bad by using sg to send
534 a cpqfc passthrough ioctl with upper_data_private
535 forged to be somewhere in our pool..., though they'd
536 normally have to be root already to do this. */
538 return (pointer != NULL &&
539 pointer >= (void *) hba->private_data_pool &&
540 pointer < (void *) hba->private_data_pool +
541 sizeof(*hba->private_data_pool) *
542 CPQFC_MAX_PASSTHRU_CMDS);
545 cpqfc_passthru_private_t *cpqfc_alloc_private_data(CPQFCHBA *hba)
547 int i;
549 do {
550 i = find_first_zero_bit(hba->private_data_bits,
551 CPQFC_MAX_PASSTHRU_CMDS);
552 if (i == CPQFC_MAX_PASSTHRU_CMDS)
553 return NULL;
554 } while ( test_and_set_bit(i & (BITS_PER_LONG - 1),
555 hba->private_data_bits+(i/BITS_PER_LONG)) != 0);
556 return &hba->private_data_pool[i];
559 void cpqfc_free_private_data(CPQFCHBA *hba, cpqfc_passthru_private_t *data)
561 int i;
562 i = data - hba->private_data_pool;
563 clear_bit(i&(BITS_PER_LONG-1),
564 hba->private_data_bits+(i/BITS_PER_LONG));
567 int cpqfcTS_ioctl( struct scsi_device *ScsiDev, int Cmnd, void *arg)
569 int result = 0;
570 struct Scsi_Host *HostAdapter = ScsiDev->host;
571 CPQFCHBA *cpqfcHBAdata = (CPQFCHBA *)HostAdapter->hostdata;
572 PTACHYON fcChip = &cpqfcHBAdata->fcChip;
573 PFC_LOGGEDIN_PORT pLoggedInPort = NULL;
574 struct scsi_cmnd *DumCmnd;
575 int i, j;
576 VENDOR_IOCTL_REQ ioc;
577 cpqfc_passthru_t *vendor_cmd;
578 Scsi_Device *SDpnt;
579 Scsi_Request *ScsiPassThruReq;
580 cpqfc_passthru_private_t *privatedata;
582 ENTER("cpqfcTS_ioctl ");
584 // printk("ioctl CMND %d", Cmnd);
585 switch (Cmnd) {
586 // Passthrough provides a mechanism to bypass the RAID
587 // or other controller and talk directly to the devices
588 // (e.g. physical disk drive)
589 // Passthrough commands, unfortunately, tend to be vendor
590 // specific; this is tailored to COMPAQ's RAID (RA4x00)
591 case CPQFCTS_SCSI_PASSTHRU:
593 void *buf = NULL; // for kernel space buffer for user data
595 /* Check that our pool got allocated ok. */
596 if (cpqfcHBAdata->private_data_pool == NULL)
597 return -ENOMEM;
599 if( !arg)
600 return -EINVAL;
602 // must be super user to send stuff directly to the
603 // controller and/or physical drives...
604 if( !capable(CAP_SYS_RAWIO) )
605 return -EPERM;
607 // copy the caller's struct to our space.
608 if( copy_from_user( &ioc, arg, sizeof( VENDOR_IOCTL_REQ)))
609 return( -EFAULT);
611 vendor_cmd = ioc.argp; // i.e., CPQ specific command struct
613 // If necessary, grab a kernel/DMA buffer
614 if( vendor_cmd->len)
616 buf = kmalloc( vendor_cmd->len, GFP_KERNEL);
617 if( !buf)
618 return -ENOMEM;
620 // Now build a Scsi_Request to pass down...
621 ScsiPassThruReq = scsi_allocate_request(ScsiDev, GFP_KERNEL);
622 if (ScsiPassThruReq == NULL) {
623 kfree(buf);
624 return -ENOMEM;
626 ScsiPassThruReq->upper_private_data =
627 cpqfc_alloc_private_data(cpqfcHBAdata);
628 if (ScsiPassThruReq->upper_private_data == NULL) {
629 kfree(buf);
630 scsi_release_request(ScsiPassThruReq); // "de-allocate"
631 return -ENOMEM;
634 if (vendor_cmd->rw_flag == VENDOR_WRITE_OPCODE) {
635 if (vendor_cmd->len) { // Need data from user?
636 if (copy_from_user(buf, vendor_cmd->bufp,
637 vendor_cmd->len)) {
638 kfree(buf);
639 cpqfc_free_private_data(cpqfcHBAdata,
640 ScsiPassThruReq->upper_private_data);
641 scsi_release_request(ScsiPassThruReq);
642 return( -EFAULT);
645 ScsiPassThruReq->sr_data_direction = SCSI_DATA_WRITE;
646 } else if (vendor_cmd->rw_flag == VENDOR_READ_OPCODE) {
647 ScsiPassThruReq->sr_data_direction = SCSI_DATA_READ;
648 } else
649 // maybe this means a bug in the user app
650 ScsiPassThruReq->sr_data_direction = SCSI_DATA_NONE;
652 ScsiPassThruReq->sr_cmd_len = 0; // set correctly by scsi_do_req()
653 ScsiPassThruReq->sr_sense_buffer[0] = 0;
654 ScsiPassThruReq->sr_sense_buffer[2] = 0;
656 // We copy the scheme used by sd.c:spinup_disk() to submit commands
657 // to our own HBA. We do this in order to stall the
658 // thread calling the IOCTL until it completes, and use
659 // the same "_quecommand" function for synchronizing
660 // FC Link events with our "worker thread".
662 privatedata = ScsiPassThruReq->upper_private_data;
663 privatedata->bus = vendor_cmd->bus;
664 privatedata->pdrive = vendor_cmd->pdrive;
666 // eventually gets us to our own _quecommand routine
667 scsi_wait_req(ScsiPassThruReq,
668 &vendor_cmd->cdb[0], buf, vendor_cmd->len,
669 10*HZ, // timeout
670 1); // retries
671 result = ScsiPassThruReq->sr_result;
673 // copy any sense data back to caller
674 if( result != 0 )
676 memcpy( vendor_cmd->sense_data, // see struct def - size=40
677 ScsiPassThruReq->sr_sense_buffer,
678 sizeof(ScsiPassThruReq->sr_sense_buffer) <
679 sizeof(vendor_cmd->sense_data) ?
680 sizeof(ScsiPassThruReq->sr_sense_buffer) :
681 sizeof(vendor_cmd->sense_data)
684 SDpnt = ScsiPassThruReq->sr_device;
685 /* upper_private_data is already freed in call_scsi_done() */
686 scsi_release_request(ScsiPassThruReq); // "de-allocate"
687 ScsiPassThruReq = NULL;
689 // need to pass data back to user (space)?
690 if( (vendor_cmd->rw_flag == VENDOR_READ_OPCODE) &&
691 vendor_cmd->len )
692 if( copy_to_user( vendor_cmd->bufp, buf, vendor_cmd->len))
693 result = -EFAULT;
695 if( buf)
696 kfree( buf);
698 return result;
701 case CPQFCTS_GETPCIINFO:
703 cpqfc_pci_info_struct pciinfo;
705 if( !arg)
706 return -EINVAL;
710 pciinfo.bus = cpqfcHBAdata->PciDev->bus->number;
711 pciinfo.dev_fn = cpqfcHBAdata->PciDev->devfn;
712 pciinfo.board_id = cpqfcHBAdata->PciDev->device |
713 (cpqfcHBAdata->PciDev->vendor <<16);
715 if(copy_to_user( arg, &pciinfo, sizeof(cpqfc_pci_info_struct)))
716 return( -EFAULT);
717 return 0;
720 case CPQFCTS_GETDRIVVER:
722 DriverVer_type DriverVer =
723 CPQFCTS_DRIVER_VER( VER_MAJOR,VER_MINOR,VER_SUBMINOR);
725 if( !arg)
726 return -EINVAL;
728 if(copy_to_user( arg, &DriverVer, sizeof(DriverVer)))
729 return( -EFAULT);
730 return 0;
735 case CPQFC_IOCTL_FC_TARGET_ADDRESS:
736 // can we find an FC device mapping to this SCSI target?
737 /* DumCmnd.channel = ScsiDev->channel; */ // For searching
738 /* DumCmnd.target = ScsiDev->id; */
739 /* DumCmnd.lun = ScsiDev->lun; */
741 DumCmnd = scsi_get_command (ScsiDev, GFP_KERNEL);
742 if (!DumCmnd)
743 return -ENOMEM;
745 pLoggedInPort = fcFindLoggedInPort( fcChip,
746 DumCmnd, // search Scsi Nexus
747 0, // DON'T search linked list for FC port id
748 NULL, // DON'T search linked list for FC WWN
749 NULL); // DON'T care about end of list
750 scsi_put_command (DumCmnd);
751 if (pLoggedInPort == NULL) {
752 result = -ENXIO;
753 break;
755 result = verify_area(VERIFY_WRITE, arg, sizeof(Scsi_FCTargAddress));
756 if (result) break;
758 put_user(pLoggedInPort->port_id,
759 &((Scsi_FCTargAddress *) arg)->host_port_id);
761 for( i=3,j=0; i>=0; i--) // copy the LOGIN port's WWN
762 put_user(pLoggedInPort->u.ucWWN[i],
763 &((Scsi_FCTargAddress *) arg)->host_wwn[j++]);
764 for( i=7; i>3; i--) // copy the LOGIN port's WWN
765 put_user(pLoggedInPort->u.ucWWN[i],
766 &((Scsi_FCTargAddress *) arg)->host_wwn[j++]);
767 break;
770 case CPQFC_IOCTL_FC_TDR:
772 result = cpqfcTS_TargetDeviceReset( ScsiDev, 0);
774 break;
779 default:
780 result = -EINVAL;
781 break;
784 LEAVE("cpqfcTS_ioctl");
785 return result;
789 /* "Release" the Host Bus Adapter...
790 disable interrupts, stop the HBA, release the interrupt,
791 and free all resources */
793 int cpqfcTS_release(struct Scsi_Host *HostAdapter)
795 CPQFCHBA *cpqfcHBAdata = (CPQFCHBA *)HostAdapter->hostdata;
798 ENTER("cpqfcTS_release");
800 DEBUG_PCI( printk(" cpqfcTS: delete timer...\n"));
801 del_timer( &cpqfcHBAdata->cpqfcTStimer);
803 // disable the hardware...
804 DEBUG_PCI( printk(" disable hardware, destroy queues, free mem\n"));
805 cpqfcHBAdata->fcChip.ResetTachyon( cpqfcHBAdata, CLEAR_FCPORTS);
807 // kill kernel thread
808 if( cpqfcHBAdata->worker_thread ) // (only if exists)
810 DECLARE_MUTEX_LOCKED(sem); // synchronize thread kill
812 cpqfcHBAdata->notify_wt = &sem;
813 DEBUG_PCI( printk(" killing kernel thread\n"));
814 send_sig( SIGKILL, cpqfcHBAdata->worker_thread, 1);
815 down( &sem);
816 cpqfcHBAdata->notify_wt = NULL;
820 cpqfc_free_private_data_pool(cpqfcHBAdata);
821 // free Linux resources
822 DEBUG_PCI( printk(" cpqfcTS: freeing resources...\n"));
823 free_irq( HostAdapter->irq, HostAdapter);
824 scsi_unregister( HostAdapter);
825 release_region( cpqfcHBAdata->fcChip.Registers.IOBaseL, 0xff);
826 release_region( cpqfcHBAdata->fcChip.Registers.IOBaseU, 0xff);
827 /* we get "vfree: bad address" executing this - need to investigate...
828 if( (void*)((unsigned long)cpqfcHBAdata->fcChip.Registers.MemBase) !=
829 cpqfcHBAdata->fcChip.Registers.ReMapMemBase)
830 vfree( cpqfcHBAdata->fcChip.Registers.ReMapMemBase);
832 pci_disable_device( cpqfcHBAdata->PciDev);
834 LEAVE("cpqfcTS_release");
835 return 0;
839 const char * cpqfcTS_info(struct Scsi_Host *HostAdapter)
841 static char buf[300];
842 CPQFCHBA *cpqfcHBA;
843 int BusSpeed, BusWidth;
845 // get the pointer to our Scsi layer HBA buffer
846 cpqfcHBA = (CPQFCHBA *)HostAdapter->hostdata;
848 BusWidth = (cpqfcHBA->fcChip.Registers.PCIMCTR &0x4) > 0 ?
849 64 : 32;
851 if( cpqfcHBA->fcChip.Registers.TYconfig.value & 0x80000000)
852 BusSpeed = 66;
853 else
854 BusSpeed = 33;
856 sprintf(buf,
857 "%s: WWN %08X%08X\n on PCI bus %d device 0x%02x irq %d IObaseL 0x%x, MEMBASE 0x%x\nPCI bus width %d bits, bus speed %d MHz\nFCP-SCSI Driver v%d.%d.%d",
858 cpqfcHBA->fcChip.Name,
859 cpqfcHBA->fcChip.Registers.wwn_hi,
860 cpqfcHBA->fcChip.Registers.wwn_lo,
861 cpqfcHBA->PciDev->bus->number,
862 cpqfcHBA->PciDev->device,
863 HostAdapter->irq,
864 cpqfcHBA->fcChip.Registers.IOBaseL,
865 cpqfcHBA->fcChip.Registers.MemBase,
866 BusWidth,
867 BusSpeed,
868 VER_MAJOR, VER_MINOR, VER_SUBMINOR
872 cpqfcTSDecodeGBICtype( &cpqfcHBA->fcChip, &buf[ strlen(buf)]);
873 cpqfcTSGetLPSM( &cpqfcHBA->fcChip, &buf[ strlen(buf)]);
874 return buf;
878 // /proc/scsi support. The following routines allow us to do 'normal'
879 // sprintf like calls to return the currently requested piece (buflenght
880 // chars, starting at bufoffset) of the file. Although procfs allows for
881 // a 1 Kb bytes overflow after te supplied buffer, I consider it bad
882 // programming to use it to make programming a little simpler. This piece
883 // of coding is borrowed from ncr53c8xx.c with some modifications
885 struct info_str
887 char *buffer; // Pointer to output buffer
888 int buflength; // It's length
889 int bufoffset; // File offset corresponding with buf[0]
890 int buffillen; // Current filled length
891 int filpos; // Current file offset
894 static void copy_mem_info(struct info_str *info, char *data, int datalen)
897 if (info->filpos < info->bufoffset) { // Current offset before buffer offset
898 if (info->filpos + datalen <= info->bufoffset) {
899 info->filpos += datalen; // Discard if completely before buffer
900 return;
901 } else { // Partial copy, set to begin
902 data += (info->bufoffset - info->filpos);
903 datalen -= (info->bufoffset - info->filpos);
904 info->filpos = info->bufoffset;
908 info->filpos += datalen; // Update current offset
910 if (info->buffillen == info->buflength) // Buffer full, discard
911 return;
913 if (info->buflength - info->buffillen < datalen) // Overflows buffer ?
914 datalen = info->buflength - info->buffillen;
916 memcpy(info->buffer + info->buffillen, data, datalen);
917 info->buffillen += datalen;
920 static int copy_info(struct info_str *info, char *fmt, ...)
922 va_list args;
923 char buf[400];
924 int len;
926 va_start(args, fmt);
927 len = vsprintf(buf, fmt, args);
928 va_end(args);
930 copy_mem_info(info, buf, len);
931 return len;
935 // Routine to get data for /proc RAM filesystem
937 int cpqfcTS_proc_info (struct Scsi_Host *host, char *buffer, char **start, off_t offset, int length,
938 int inout)
940 struct scsi_cmnd *DumCmnd;
941 struct scsi_device *ScsiDev;
942 int Chan, Targ, i;
943 struct info_str info;
944 CPQFCHBA *cpqfcHBA;
945 PTACHYON fcChip;
946 PFC_LOGGEDIN_PORT pLoggedInPort;
947 char buf[81];
949 if (inout) return -EINVAL;
951 // get the pointer to our Scsi layer HBA buffer
952 cpqfcHBA = (CPQFCHBA *)host->hostdata;
953 fcChip = &cpqfcHBA->fcChip;
955 *start = buffer;
957 info.buffer = buffer;
958 info.buflength = length;
959 info.bufoffset = offset;
960 info.filpos = 0;
961 info.buffillen = 0;
962 copy_info(&info, "Driver version = %d.%d.%d", VER_MAJOR, VER_MINOR, VER_SUBMINOR);
963 cpqfcTSDecodeGBICtype( &cpqfcHBA->fcChip, &buf[0]);
964 cpqfcTSGetLPSM( &cpqfcHBA->fcChip, &buf[ strlen(buf)]);
965 copy_info(&info, "%s\n", buf);
967 #define DISPLAY_WWN_INFO
968 #ifdef DISPLAY_WWN_INFO
969 ScsiDev = scsi_get_host_dev (host);
970 if (!ScsiDev)
971 return -ENOMEM;
972 DumCmnd = scsi_get_command (ScsiDev, GFP_KERNEL);
973 if (!DumCmnd) {
974 scsi_free_host_dev (ScsiDev);
975 return -ENOMEM;
977 copy_info(&info, "WWN database: (\"port_id: 000000\" means disconnected)\n");
978 for ( Chan=0; Chan <= host->max_channel; Chan++) {
979 DumCmnd->device->channel = Chan;
980 for (Targ=0; Targ <= host->max_id; Targ++) {
981 DumCmnd->device->id = Targ;
982 if ((pLoggedInPort = fcFindLoggedInPort( fcChip,
983 DumCmnd, // search Scsi Nexus
984 0, // DON'T search list for FC port id
985 NULL, // DON'T search list for FC WWN
986 NULL))){ // DON'T care about end of list
987 copy_info(&info, "Host: scsi%d Channel: %02d TargetId: %02d -> WWN: ",
988 host->host_no, Chan, Targ);
989 for( i=3; i>=0; i--) // copy the LOGIN port's WWN
990 copy_info(&info, "%02X", pLoggedInPort->u.ucWWN[i]);
991 for( i=7; i>3; i--) // copy the LOGIN port's WWN
992 copy_info(&info, "%02X", pLoggedInPort->u.ucWWN[i]);
993 copy_info(&info, " port_id: %06X\n", pLoggedInPort->port_id);
998 scsi_put_command (DumCmnd);
999 scsi_free_host_dev (ScsiDev);
1000 #endif
1006 // Unfortunately, the proc_info buffer isn't big enough
1007 // for everything we would like...
1008 // For FC stats, compile this and turn off WWN stuff above
1009 //#define DISPLAY_FC_STATS
1010 #ifdef DISPLAY_FC_STATS
1011 // get the Fibre Channel statistics
1013 int DeltaSecs = (jiffies - cpqfcHBA->fcStatsTime) / HZ;
1014 int days,hours,minutes,secs;
1016 days = DeltaSecs / (3600*24); // days
1017 hours = (DeltaSecs% (3600*24)) / 3600; // hours
1018 minutes = (DeltaSecs%3600 /60); // minutes
1019 secs = DeltaSecs%60; // secs
1020 copy_info( &info, "Fibre Channel Stats (time dd:hh:mm:ss %02u:%02u:%02u:%02u\n",
1021 days, hours, minutes, secs);
1024 cpqfcHBA->fcStatsTime = jiffies; // (for next delta)
1026 copy_info( &info, " LinkUp %9u LinkDown %u\n",
1027 fcChip->fcStats.linkUp, fcChip->fcStats.linkDown);
1029 copy_info( &info, " Loss of Signal %9u Loss of Sync %u\n",
1030 fcChip->fcStats.LossofSignal, fcChip->fcStats.LossofSync);
1032 copy_info( &info, " Discarded Frames %9u Bad CRC Frame %u\n",
1033 fcChip->fcStats.Dis_Frm, fcChip->fcStats.Bad_CRC);
1035 copy_info( &info, " TACH LinkFailTX %9u TACH LinkFailRX %u\n",
1036 fcChip->fcStats.linkFailTX, fcChip->fcStats.linkFailRX);
1038 copy_info( &info, " TACH RxEOFa %9u TACH Elastic Store %u\n",
1039 fcChip->fcStats.Rx_EOFa, fcChip->fcStats.e_stores);
1041 copy_info( &info, " BufferCreditWait %9uus TACH FM Inits %u\n",
1042 fcChip->fcStats.BB0_Timer*10, fcChip->fcStats.FMinits );
1044 copy_info( &info, " FC-2 Timeouts %9u FC-2 Logouts %u\n",
1045 fcChip->fcStats.timeouts, fcChip->fcStats.logouts);
1047 copy_info( &info, " FC-2 Aborts %9u FC-4 Aborts %u\n",
1048 fcChip->fcStats.FC2aborted, fcChip->fcStats.FC4aborted);
1050 // clear the counters
1051 cpqfcTSClearLinkStatusCounters( fcChip);
1052 #endif
1054 return info.buffillen;
1058 #if DEBUG_CMND
1060 UCHAR *ScsiToAscii( UCHAR ScsiCommand)
1063 /*++
1065 Routine Description:
1067 Converts a SCSI command to a text string for debugging purposes.
1070 Arguments:
1072 ScsiCommand -- hex value SCSI Command
1075 Return Value:
1077 An ASCII, null-terminated string if found, else returns NULL.
1079 Original code from M. McGowen, Compaq
1080 --*/
1083 switch (ScsiCommand)
1085 case 0x00:
1086 return( "Test Unit Ready" );
1088 case 0x01:
1089 return( "Rezero Unit or Rewind" );
1091 case 0x02:
1092 return( "Request Block Address" );
1094 case 0x03:
1095 return( "Requese Sense" );
1097 case 0x04:
1098 return( "Format Unit" );
1100 case 0x05:
1101 return( "Read Block Limits" );
1103 case 0x07:
1104 return( "Reassign Blocks" );
1106 case 0x08:
1107 return( "Read (6)" );
1109 case 0x0a:
1110 return( "Write (6)" );
1112 case 0x0b:
1113 return( "Seek (6)" );
1115 case 0x12:
1116 return( "Inquiry" );
1118 case 0x15:
1119 return( "Mode Select (6)" );
1121 case 0x16:
1122 return( "Reserve" );
1124 case 0x17:
1125 return( "Release" );
1127 case 0x1a:
1128 return( "ModeSen(6)" );
1130 case 0x1b:
1131 return( "Start/Stop Unit" );
1133 case 0x1c:
1134 return( "Receive Diagnostic Results" );
1136 case 0x1d:
1137 return( "Send Diagnostic" );
1139 case 0x25:
1140 return( "Read Capacity" );
1142 case 0x28:
1143 return( "Read (10)" );
1145 case 0x2a:
1146 return( "Write (10)" );
1148 case 0x2b:
1149 return( "Seek (10)" );
1151 case 0x2e:
1152 return( "Write and Verify" );
1154 case 0x2f:
1155 return( "Verify" );
1157 case 0x34:
1158 return( "Pre-Fetch" );
1160 case 0x35:
1161 return( "Synchronize Cache" );
1163 case 0x37:
1164 return( "Read Defect Data (10)" );
1166 case 0x3b:
1167 return( "Write Buffer" );
1169 case 0x3c:
1170 return( "Read Buffer" );
1172 case 0x3e:
1173 return( "Read Long" );
1175 case 0x3f:
1176 return( "Write Long" );
1178 case 0x41:
1179 return( "Write Same" );
1181 case 0x4c:
1182 return( "Log Select" );
1184 case 0x4d:
1185 return( "Log Sense" );
1187 case 0x56:
1188 return( "Reserve (10)" );
1190 case 0x57:
1191 return( "Release (10)" );
1193 case 0xa0:
1194 return( "ReportLuns" );
1196 case 0xb7:
1197 return( "Read Defect Data (12)" );
1199 case 0xca:
1200 return( "Peripheral Device Addressing SCSI Passthrough" );
1202 case 0xcb:
1203 return( "Compaq Array Firmware Passthrough" );
1205 default:
1206 return( NULL );
1209 } // end ScsiToAscii()
1211 void cpqfcTS_print_scsi_cmd(Scsi_Cmnd * cmd)
1214 printk("cpqfcTS: (%s) chnl 0x%02x, trgt = 0x%02x, lun = 0x%02x, cmd_len = 0x%02x\n",
1215 ScsiToAscii( cmd->cmnd[0]), cmd->channel, cmd->target, cmd->lun, cmd->cmd_len);
1217 if( cmd->cmnd[0] == 0) // Test Unit Ready?
1219 int i;
1221 printk("Cmnd->request_bufflen = 0x%X, ->use_sg = %d, ->bufflen = %d\n",
1222 cmd->request_bufflen, cmd->use_sg, cmd->bufflen);
1223 printk("Cmnd->request_buffer = %p, ->sglist_len = %d, ->buffer = %p\n",
1224 cmd->request_buffer, cmd->sglist_len, cmd->buffer);
1225 for (i = 0; i < cmd->cmd_len; i++)
1226 printk("0x%02x ", cmd->cmnd[i]);
1227 printk("\n");
1232 #endif /* DEBUG_CMND */
1237 static void QueCmndOnBoardLock( CPQFCHBA *cpqfcHBAdata, Scsi_Cmnd *Cmnd)
1239 int i;
1241 for( i=0; i< CPQFCTS_REQ_QUEUE_LEN; i++)
1242 { // find spare slot
1243 if( cpqfcHBAdata->BoardLockCmnd[i] == NULL )
1245 cpqfcHBAdata->BoardLockCmnd[i] = Cmnd;
1246 // printk(" BoardLockCmnd[%d] %p Queued, chnl/target/lun %d/%d/%d\n",
1247 // i,Cmnd, Cmnd->channel, Cmnd->target, Cmnd->lun);
1248 break;
1251 if( i >= CPQFCTS_REQ_QUEUE_LEN)
1253 printk(" cpqfcTS WARNING: Lost Cmnd %p on BoardLock Q full!", Cmnd);
1259 static void QueLinkDownCmnd( CPQFCHBA *cpqfcHBAdata, Scsi_Cmnd *Cmnd)
1261 int indx;
1263 // Remember the command ptr so we can return; we'll complete when
1264 // the device comes back, causing immediate retry
1265 for( indx=0; indx < CPQFCTS_REQ_QUEUE_LEN; indx++)//, SCptr++)
1267 if( cpqfcHBAdata->LinkDnCmnd[indx] == NULL ) // available?
1269 #ifdef DUMMYCMND_DBG
1270 printk(" @add Cmnd %p to LnkDnCmnd[%d]@ ", Cmnd,indx);
1271 #endif
1272 cpqfcHBAdata->LinkDnCmnd[indx] = Cmnd;
1273 break;
1277 if( indx >= CPQFCTS_REQ_QUEUE_LEN ) // no space for Cmnd??
1279 // this will result in an _abort call later (with possible trouble)
1280 printk("no buffer for LinkDnCmnd!! %p\n", Cmnd);
1288 // The file <scsi/scsi_host.h> says not to call scsi_done from
1289 // inside _queuecommand, so we'll do it from the heartbeat timer
1290 // (clarification: Turns out it's ok to call scsi_done from queuecommand
1291 // for cases that don't go to the hardware like scsi cmds destined
1292 // for LUNs we know don't exist, so this code might be simplified...)
1294 static void QueBadTargetCmnd( CPQFCHBA *cpqfcHBAdata, Scsi_Cmnd *Cmnd)
1296 int i;
1297 // printk(" can't find target %d\n", Cmnd->target);
1299 for( i=0; i< CPQFCTS_MAX_TARGET_ID; i++)
1300 { // find spare slot
1301 if( cpqfcHBAdata->BadTargetCmnd[i] == NULL )
1303 cpqfcHBAdata->BadTargetCmnd[i] = Cmnd;
1304 // printk(" BadTargetCmnd[%d] %p Queued, chnl/target/lun %d/%d/%d\n",
1305 // i,Cmnd, Cmnd->channel, Cmnd->target, Cmnd->lun);
1306 break;
1312 // This is the "main" entry point for Linux Scsi commands --
1313 // it all starts here.
1315 int cpqfcTS_queuecommand(Scsi_Cmnd *Cmnd, void (* done)(Scsi_Cmnd *))
1317 struct Scsi_Host *HostAdapter = Cmnd->device->host;
1318 CPQFCHBA *cpqfcHBAdata = (CPQFCHBA *)HostAdapter->hostdata;
1319 PTACHYON fcChip = &cpqfcHBAdata->fcChip;
1320 TachFCHDR_GCMND fchs; // only use for FC destination id field
1321 PFC_LOGGEDIN_PORT pLoggedInPort;
1322 ULONG ulStatus, SESTtype;
1323 LONG ExchangeID;
1328 ENTER("cpqfcTS_queuecommand");
1330 PCI_TRACEO( (ULONG)Cmnd, 0x98)
1333 Cmnd->scsi_done = done;
1334 #ifdef DEBUG_CMND
1335 cpqfcTS_print_scsi_cmd( Cmnd);
1336 #endif
1338 // prevent board contention with kernel thread...
1340 if( cpqfcHBAdata->BoardLock )
1342 // printk(" @BrdLck Hld@ ");
1343 QueCmndOnBoardLock( cpqfcHBAdata, Cmnd);
1346 else
1349 // in the current system (2.2.12), this routine is called
1350 // after spin_lock_irqsave(), so INTs are disabled. However,
1351 // we might have something pending in the LinkQ, which
1352 // might cause the WorkerTask to run. In case that
1353 // happens, make sure we lock it out.
1357 PCI_TRACE( 0x98)
1358 CPQ_SPINLOCK_HBA( cpqfcHBAdata)
1359 PCI_TRACE( 0x98)
1361 // can we find an FC device mapping to this SCSI target?
1362 pLoggedInPort = fcFindLoggedInPort( fcChip,
1363 Cmnd, // search Scsi Nexus
1364 0, // DON'T search linked list for FC port id
1365 NULL, // DON'T search linked list for FC WWN
1366 NULL); // DON'T care about end of list
1368 if( pLoggedInPort == NULL ) // not found!
1370 // printk(" @Q bad targ cmnd %p@ ", Cmnd);
1371 QueBadTargetCmnd( cpqfcHBAdata, Cmnd);
1373 else if (Cmnd->device->lun >= CPQFCTS_MAX_LUN)
1375 printk(KERN_WARNING "cpqfc: Invalid LUN: %d\n", Cmnd->device->lun);
1376 QueBadTargetCmnd( cpqfcHBAdata, Cmnd);
1379 else // we know what FC device to send to...
1382 // does this device support FCP target functions?
1383 // (determined by PRLI field)
1385 if( !(pLoggedInPort->fcp_info & TARGET_FUNCTION) )
1387 printk(" Doesn't support TARGET functions port_id %Xh\n",
1388 pLoggedInPort->port_id );
1389 QueBadTargetCmnd( cpqfcHBAdata, Cmnd);
1392 // In this case (previous login OK), the device is temporarily
1393 // unavailable waiting for re-login, in which case we expect it
1394 // to be back in between 25 - 500ms.
1395 // If the FC port doesn't log back in within several seconds
1396 // (i.e. implicit "logout"), or we get an explicit logout,
1397 // we set "device_blocked" in Scsi_Device struct; in this
1398 // case 30 seconds will elapse before Linux/Scsi sends another
1399 // command to the device.
1400 else if( pLoggedInPort->prli != TRUE )
1402 // printk("Device (Chnl/Target %d/%d) invalid PRLI, port_id %06lXh\n",
1403 // Cmnd->channel, Cmnd->target, pLoggedInPort->port_id);
1404 QueLinkDownCmnd( cpqfcHBAdata, Cmnd);
1405 // Need to use "blocked" flag??
1406 // Cmnd->device->device_blocked = TRUE; // just let it timeout
1408 else // device supports TARGET functions, and is logged in...
1410 // (context of fchs is to "reply" to...)
1411 fchs.s_id = pLoggedInPort->port_id; // destination FC address
1413 // what is the data direction? For data TO the device,
1414 // we need IWE (Intiator Write Entry). Otherwise, IRE.
1416 if( Cmnd->cmnd[0] == WRITE_10 ||
1417 Cmnd->cmnd[0] == WRITE_6 ||
1418 Cmnd->cmnd[0] == WRITE_BUFFER ||
1419 Cmnd->cmnd[0] == VENDOR_WRITE_OPCODE || // CPQ specific
1420 Cmnd->cmnd[0] == MODE_SELECT )
1422 SESTtype = SCSI_IWE; // data from HBA to Device
1424 else
1425 SESTtype = SCSI_IRE; // data from Device to HBA
1427 ulStatus = cpqfcTSBuildExchange(
1428 cpqfcHBAdata,
1429 SESTtype, // e.g. Initiator Read Entry (IRE)
1430 &fchs, // we are originator; only use d_id
1431 Cmnd, // Linux SCSI command (with scatter/gather list)
1432 &ExchangeID );// fcController->fcExchanges index, -1 if failed
1434 if( !ulStatus ) // Exchange setup?
1437 if( cpqfcHBAdata->BoardLock )
1439 TriggerHBA( fcChip->Registers.ReMapMemBase, 0);
1440 printk(" @bl! %d, xID %Xh@ ", current->pid, ExchangeID);
1443 ulStatus = cpqfcTSStartExchange( cpqfcHBAdata, ExchangeID );
1444 if( !ulStatus )
1446 PCI_TRACEO( ExchangeID, 0xB8)
1447 // submitted to Tach's Outbound Que (ERQ PI incremented)
1448 // waited for completion for ELS type (Login frames issued
1449 // synchronously)
1451 else
1452 // check reason for Exchange not being started - we might
1453 // want to Queue and start later, or fail with error
1455 printk("quecommand: cpqfcTSStartExchange failed: %Xh\n", ulStatus );
1457 } // end good BuildExchange status
1459 else // SEST table probably full -- why? hardware hang?
1461 printk("quecommand: cpqfcTSBuildExchange faild: %Xh\n", ulStatus);
1463 } // end can't do FCP-SCSI target functions
1464 } // end can't find target (FC device)
1466 CPQ_SPINUNLOCK_HBA( cpqfcHBAdata)
1469 PCI_TRACEO( (ULONG)Cmnd, 0x9C)
1470 LEAVE("cpqfcTS_queuecommand");
1471 return 0;
1475 // Entry point for upper Scsi layer intiated abort. Typically
1476 // this is called if the command (for hard disk) fails to complete
1477 // in 30 seconds. This driver intends to complete all disk commands
1478 // within Exchange ".timeOut" seconds (now 7) with target status, or
1479 // in case of ".timeOut" expiration, a DID_SOFT_ERROR which causes
1480 // immediate retry.
1481 // If any disk commands get the _abort call, except for the case that
1482 // the physical device was removed or unavailable due to hardware
1483 // errors, it should be considered a driver error and reported to
1484 // the author.
1486 int cpqfcTS_abort(Scsi_Cmnd *Cmnd)
1488 // printk(" cpqfcTS_abort called?? \n");
1489 return 0;
1492 int cpqfcTS_eh_abort(Scsi_Cmnd *Cmnd)
1495 struct Scsi_Host *HostAdapter = Cmnd->device->host;
1496 // get the pointer to our Scsi layer HBA buffer
1497 CPQFCHBA *cpqfcHBAdata = (CPQFCHBA *)HostAdapter->hostdata;
1498 PTACHYON fcChip = &cpqfcHBAdata->fcChip;
1499 FC_EXCHANGES *Exchanges = fcChip->Exchanges;
1500 int i;
1501 ENTER("cpqfcTS_eh_abort");
1503 Cmnd->result = DID_ABORT <<16; // assume we'll find it
1505 printk(" @Linux _abort Scsi_Cmnd %p ", Cmnd);
1506 // See if we can find a Cmnd pointer that matches...
1507 // The most likely case is we accepted the command
1508 // from Linux Scsi (e.g. ceated a SEST entry) and it
1509 // got lost somehow. If we can't find any reference
1510 // to the passed pointer, we can only presume it
1511 // got completed as far as our driver is concerned.
1512 // If we found it, we will try to abort it through
1513 // common mechanism. If FC ABTS is successful (ACC)
1514 // or is rejected (RJT) by target, we will call
1515 // Scsi "done" quickly. Otherwise, the ABTS will timeout
1516 // and we'll call "done" later.
1518 // Search the SEST exchanges for a matching Cmnd ptr.
1519 for( i=0; i< TACH_SEST_LEN; i++)
1521 if( Exchanges->fcExchange[i].Cmnd == Cmnd )
1524 // found it!
1525 printk(" x_ID %Xh, type %Xh\n", i, Exchanges->fcExchange[i].type);
1527 Exchanges->fcExchange[i].status = INITIATOR_ABORT; // seconds default
1528 Exchanges->fcExchange[i].timeOut = 10; // seconds default (changed later)
1530 // Since we need to immediately return the aborted Cmnd to Scsi
1531 // upper layers, we can't make future reference to any of its
1532 // fields (e.g the Nexus).
1534 cpqfcTSPutLinkQue( cpqfcHBAdata, BLS_ABTS, &i);
1536 break;
1540 if( i >= TACH_SEST_LEN ) // didn't find Cmnd ptr in chip's SEST?
1542 // now search our non-SEST buffers (i.e. Cmnd waiting to
1543 // start on the HBA or waiting to complete with error for retry).
1545 // first check BadTargetCmnd
1546 for( i=0; i< CPQFCTS_MAX_TARGET_ID; i++)
1548 if( cpqfcHBAdata->BadTargetCmnd[i] == Cmnd )
1550 cpqfcHBAdata->BadTargetCmnd[i] = NULL;
1551 printk("in BadTargetCmnd Q\n");
1552 goto Done; // exit
1556 // if not found above...
1558 for( i=0; i < CPQFCTS_REQ_QUEUE_LEN; i++)
1560 if( cpqfcHBAdata->LinkDnCmnd[i] == Cmnd )
1562 cpqfcHBAdata->LinkDnCmnd[i] = NULL;
1563 printk("in LinkDnCmnd Q\n");
1564 goto Done;
1569 for( i=0; i< CPQFCTS_REQ_QUEUE_LEN; i++)
1570 { // find spare slot
1571 if( cpqfcHBAdata->BoardLockCmnd[i] == Cmnd )
1573 cpqfcHBAdata->BoardLockCmnd[i] = NULL;
1574 printk("in BoardLockCmnd Q\n");
1575 goto Done;
1579 Cmnd->result = DID_ERROR <<16; // Hmmm...
1580 printk("Not found! ");
1581 // panic("_abort");
1584 Done:
1586 // panic("_abort");
1587 LEAVE("cpqfcTS_eh_abort");
1588 return 0; // (see scsi.h)
1592 // FCP-SCSI Target Device Reset
1593 // See dpANS Fibre Channel Protocol for SCSI
1594 // X3.269-199X revision 12, pg 25
1596 #ifdef SUPPORT_RESET
1598 int cpqfcTS_TargetDeviceReset( Scsi_Device *ScsiDev,
1599 unsigned int reset_flags)
1601 int timeout = 10*HZ;
1602 int retries = 1;
1603 char scsi_cdb[12];
1604 int result;
1605 Scsi_Cmnd * SCpnt;
1606 Scsi_Device * SDpnt;
1608 // FIXME, cpqfcTS_TargetDeviceReset needs to be fixed
1609 // similarly to how the passthrough ioctl was fixed
1610 // around the 2.5.30 kernel. Scsi_Cmnd replaced with
1611 // Scsi_Request, etc.
1612 // For now, so people don't fall into a hole...
1614 // printk(" ENTERING cpqfcTS_TargetDeviceReset() - flag=%d \n",reset_flags);
1616 if (ScsiDev->host->eh_active) return FAILED;
1618 memset( scsi_cdb, 0, sizeof( scsi_cdb));
1620 scsi_cdb[0] = RELEASE;
1622 SCpnt = scsi_get_command(ScsiDev, GFP_KERNEL);
1624 CPQFC_DECLARE_COMPLETION(wait);
1626 SCpnt->SCp.buffers_residual = FCP_TARGET_RESET;
1628 // FIXME: this would panic, SCpnt->request would be NULL.
1629 SCpnt->request->CPQFC_WAITING = &wait;
1630 scsi_do_cmd(SCpnt, scsi_cdb, NULL, 0, my_ioctl_done, timeout, retries);
1631 CPQFC_WAIT_FOR_COMPLETION(&wait);
1632 SCpnt->request->CPQFC_WAITING = NULL;
1636 if(driver_byte(SCpnt->result) != 0)
1637 switch(SCpnt->sense_buffer[2] & 0xf) {
1638 case ILLEGAL_REQUEST:
1639 if(cmd[0] == ALLOW_MEDIUM_REMOVAL) dev->lockable = 0;
1640 else printk("SCSI device (ioctl) reports ILLEGAL REQUEST.\n");
1641 break;
1642 case NOT_READY: // This happens if there is no disc in drive
1643 if(dev->removable && (cmd[0] != TEST_UNIT_READY)){
1644 printk(KERN_INFO "Device not ready. Make sure there is a disc in the drive.\n");
1645 break;
1647 case UNIT_ATTENTION:
1648 if (dev->removable){
1649 dev->changed = 1;
1650 SCpnt->result = 0; // This is no longer considered an error
1651 // gag this error, VFS will log it anyway /axboe
1652 // printk(KERN_INFO "Disc change detected.\n");
1653 break;
1655 default: // Fall through for non-removable media
1656 #if 0 //johnson remove 2008-1-10
1657 printk("SCSI error: host %d id %d lun %d return code = %x\n",
1658 dev->host->host_no,
1659 dev->id,
1660 dev->lun,
1661 SCpnt->result);
1662 printk("\tSense class %x, sense error %x, extended sense %x\n",
1663 sense_class(SCpnt->sense_buffer[0]),
1664 sense_error(SCpnt->sense_buffer[0]),
1665 SCpnt->sense_buffer[2] & 0xf);
1666 #endif
1668 result = SCpnt->result;
1670 SDpnt = SCpnt->device;
1671 scsi_put_command(SCpnt);
1672 SCpnt = NULL;
1674 // printk(" LEAVING cpqfcTS_TargetDeviceReset() - return SUCCESS \n");
1675 return SUCCESS;
1678 #else
1679 int cpqfcTS_TargetDeviceReset( Scsi_Device *ScsiDev,
1680 unsigned int reset_flags)
1682 return -ENOTSUPP;
1685 #endif /* SUPPORT_RESET */
1687 int cpqfcTS_eh_device_reset(Scsi_Cmnd *Cmnd)
1689 int retval;
1690 Scsi_Device *SDpnt = Cmnd->device;
1691 // printk(" ENTERING cpqfcTS_eh_device_reset() \n");
1692 spin_unlock_irq(Cmnd->device->host->host_lock);
1693 retval = cpqfcTS_TargetDeviceReset( SDpnt, 0);
1694 spin_lock_irq(Cmnd->device->host->host_lock);
1695 return retval;
1699 int cpqfcTS_reset(Scsi_Cmnd *Cmnd, unsigned int reset_flags)
1702 ENTER("cpqfcTS_reset");
1704 LEAVE("cpqfcTS_reset");
1705 return SCSI_RESET_ERROR; /* Bus Reset Not supported */
1708 /* This function determines the bios parameters for a given
1709 harddisk. These tend to be numbers that are made up by the
1710 host adapter. Parameters:
1711 size, device number, list (heads, sectors,cylinders).
1712 (from hosts.h)
1715 int cpqfcTS_biosparam(struct scsi_device *sdev, struct block_device *n,
1716 sector_t capacity, int ip[])
1718 int size = capacity;
1720 ENTER("cpqfcTS_biosparam");
1721 ip[0] = 64;
1722 ip[1] = 32;
1723 ip[2] = size >> 11;
1725 if( ip[2] > 1024 )
1727 ip[0] = 255;
1728 ip[1] = 63;
1729 ip[2] = size / (ip[0] * ip[1]);
1732 LEAVE("cpqfcTS_biosparam");
1733 return 0;
1738 irqreturn_t cpqfcTS_intr_handler( int irq,
1739 void *dev_id,
1740 struct pt_regs *regs)
1743 unsigned long flags, InfLoopBrk=0;
1744 struct Scsi_Host *HostAdapter = dev_id;
1745 CPQFCHBA *cpqfcHBA = (CPQFCHBA *)HostAdapter->hostdata;
1746 int MoreMessages = 1; // assume we have something to do
1747 UCHAR IntPending;
1748 int handled = 0;
1750 ENTER("intr_handler");
1751 spin_lock_irqsave( HostAdapter->host_lock, flags);
1752 // is this our INT?
1753 IntPending = readb( cpqfcHBA->fcChip.Registers.INTPEND.address);
1755 // broken boards can generate messages forever, so
1756 // prevent the infinite loop
1757 #define INFINITE_IMQ_BREAK 10000
1758 if( IntPending )
1760 handled = 1;
1761 // mask our HBA interrupts until we handle it...
1762 writeb( 0, cpqfcHBA->fcChip.Registers.INTEN.address);
1764 if( IntPending & 0x4) // "INT" - Tach wrote to IMQ
1766 while( (++InfLoopBrk < INFINITE_IMQ_BREAK) && (MoreMessages ==1) )
1768 MoreMessages = CpqTsProcessIMQEntry( HostAdapter); // ret 0 when done
1770 if( InfLoopBrk >= INFINITE_IMQ_BREAK )
1772 printk("WARNING: Compaq FC adapter generating excessive INTs -REPLACE\n");
1773 printk("or investigate alternate causes (e.g. physical FC layer)\n");
1776 else // working normally - re-enable INTs and continue
1777 writeb( 0x1F, cpqfcHBA->fcChip.Registers.INTEN.address);
1779 } // (...ProcessIMQEntry() clears INT by writing IMQ consumer)
1780 else // indications of errors or problems...
1781 // these usually indicate critical system hardware problems.
1783 if( IntPending & 0x10 )
1784 printk(" cpqfcTS adapter external memory parity error detected\n");
1785 if( IntPending & 0x8 )
1786 printk(" cpqfcTS adapter PCI master address crossed 45-bit boundary\n");
1787 if( IntPending & 0x2 )
1788 printk(" cpqfcTS adapter DMA error detected\n");
1789 if( IntPending & 0x1 ) {
1790 UCHAR IntStat;
1791 printk(" cpqfcTS adapter PCI error detected\n");
1792 IntStat = readb( cpqfcHBA->fcChip.Registers.INTSTAT.address);
1793 printk("cpqfc: ISR = 0x%02x\n", IntStat);
1794 if (IntStat & 0x1) {
1795 __u16 pcistat;
1796 /* read the pci status register */
1797 pci_read_config_word(cpqfcHBA->PciDev, 0x06, &pcistat);
1798 printk("PCI status register is 0x%04x\n", pcistat);
1799 if (pcistat & 0x8000) printk("Parity Error Detected.\n");
1800 if (pcistat & 0x4000) printk("Signalled System Error\n");
1801 if (pcistat & 0x2000) printk("Received Master Abort\n");
1802 if (pcistat & 0x1000) printk("Received Target Abort\n");
1803 if (pcistat & 0x0800) printk("Signalled Target Abort\n");
1805 if (IntStat & 0x4) printk("(INT)\n");
1806 if (IntStat & 0x8)
1807 printk("CRS: PCI master address crossed 46 bit bouandary\n");
1808 if (IntStat & 0x10) printk("MRE: external memory parity error.\n");
1812 spin_unlock_irqrestore( HostAdapter->host_lock, flags);
1813 LEAVE("intr_handler");
1814 return IRQ_RETVAL(handled);
1820 int cpqfcTSDecodeGBICtype( PTACHYON fcChip, char cErrorString[])
1822 // Verify GBIC type (if any) and correct Tachyon Port State Machine
1823 // (GBIC) module definition is:
1824 // GPIO1, GPIO0, GPIO4 for MD2, MD1, MD0. The input states appear
1825 // to be inverted -- i.e., a setting of 111 is read when there is NO
1826 // GBIC present. The Module Def (MD) spec says 000 is "no GBIC"
1827 // Hard code the bit states to detect Copper,
1828 // Long wave (single mode), Short wave (multi-mode), and absent GBIC
1830 ULONG ulBuff;
1832 sprintf( cErrorString, "\nGBIC detected: ");
1834 ulBuff = fcChip->Registers.TYstatus.value & 0x13;
1835 switch( ulBuff )
1837 case 0x13: // GPIO4, GPIO1, GPIO0 = 111; no GBIC!
1838 sprintf( &cErrorString[ strlen( cErrorString)],
1839 "NONE! ");
1840 return FALSE;
1843 case 0x11: // Copper GBIC detected
1844 sprintf( &cErrorString[ strlen( cErrorString)],
1845 "Copper. ");
1846 break;
1848 case 0x10: // Long-wave (single mode) GBIC detected
1849 sprintf( &cErrorString[ strlen( cErrorString)],
1850 "Long-wave. ");
1851 break;
1852 case 0x1: // Short-wave (multi mode) GBIC detected
1853 sprintf( &cErrorString[ strlen( cErrorString)],
1854 "Short-wave. ");
1855 break;
1856 default: // unknown GBIC - presumably it will work (?)
1857 sprintf( &cErrorString[ strlen( cErrorString)],
1858 "Unknown. ");
1860 break;
1861 } // end switch GBIC detection
1863 return TRUE;
1871 int cpqfcTSGetLPSM( PTACHYON fcChip, char cErrorString[])
1873 // Tachyon's Frame Manager LPSM in LinkDown state?
1874 // (For non-loop port, check PSM instead.)
1875 // return string with state and FALSE is Link Down
1877 int LinkUp;
1879 if( fcChip->Registers.FMstatus.value & 0x80 )
1880 LinkUp = FALSE;
1881 else
1882 LinkUp = TRUE;
1884 sprintf( &cErrorString[ strlen( cErrorString)],
1885 " LPSM %Xh ",
1886 (fcChip->Registers.FMstatus.value >>4) & 0xf );
1889 switch( fcChip->Registers.FMstatus.value & 0xF0)
1891 // bits set in LPSM
1892 case 0x10:
1893 sprintf( &cErrorString[ strlen( cErrorString)], "ARB");
1894 break;
1895 case 0x20:
1896 sprintf( &cErrorString[ strlen( cErrorString)], "ARBwon");
1897 break;
1898 case 0x30:
1899 sprintf( &cErrorString[ strlen( cErrorString)], "OPEN");
1900 break;
1901 case 0x40:
1902 sprintf( &cErrorString[ strlen( cErrorString)], "OPENed");
1903 break;
1904 case 0x50:
1905 sprintf( &cErrorString[ strlen( cErrorString)], "XmitCLS");
1906 break;
1907 case 0x60:
1908 sprintf( &cErrorString[ strlen( cErrorString)], "RxCLS");
1909 break;
1910 case 0x70:
1911 sprintf( &cErrorString[ strlen( cErrorString)], "Xfer");
1912 break;
1913 case 0x80:
1914 sprintf( &cErrorString[ strlen( cErrorString)], "Init");
1915 break;
1916 case 0x90:
1917 sprintf( &cErrorString[ strlen( cErrorString)], "O-IInitFin");
1918 break;
1919 case 0xa0:
1920 sprintf( &cErrorString[ strlen( cErrorString)], "O-IProtocol");
1921 break;
1922 case 0xb0:
1923 sprintf( &cErrorString[ strlen( cErrorString)], "O-ILipRcvd");
1924 break;
1925 case 0xc0:
1926 sprintf( &cErrorString[ strlen( cErrorString)], "HostControl");
1927 break;
1928 case 0xd0:
1929 sprintf( &cErrorString[ strlen( cErrorString)], "LoopFail");
1930 break;
1931 case 0xe0:
1932 sprintf( &cErrorString[ strlen( cErrorString)], "Offline");
1933 break;
1934 case 0xf0:
1935 sprintf( &cErrorString[ strlen( cErrorString)], "OldPort");
1936 break;
1937 case 0:
1938 default:
1939 sprintf( &cErrorString[ strlen( cErrorString)], "Monitor");
1940 break;
1944 return LinkUp;
1950 #include "linux/slab.h"
1952 // Dynamic memory allocation alignment routines
1953 // HP's Tachyon Fibre Channel Controller chips require
1954 // certain memory queues and register pointers to be aligned
1955 // on various boundaries, usually the size of the Queue in question.
1956 // Alignment might be on 2, 4, 8, ... or even 512 byte boundaries.
1957 // Since most O/Ss don't allow this (usually only Cache aligned -
1958 // 32-byte boundary), these routines provide generic alignment (after
1959 // O/S allocation) at any boundary, and store the original allocated
1960 // pointer for deletion (O/S free function). Typically, we expect
1961 // these functions to only be called at HBA initialization and
1962 // removal time (load and unload times)
1963 // ALGORITHM notes:
1964 // Memory allocation varies by compiler and platform. In the worst case,
1965 // we are only assured BYTE alignment, but in the best case, we can
1966 // request allocation on any desired boundary. Our strategy: pad the
1967 // allocation request size (i.e. waste memory) so that we are assured
1968 // of passing desired boundary near beginning of contiguous space, then
1969 // mask out lower address bits.
1970 // We define the following algorithm:
1971 // allocBoundary - compiler/platform specific address alignment
1972 // in number of bytes (default is single byte; i.e. 1)
1973 // n_alloc - number of bytes application wants @ aligned address
1974 // ab - alignment boundary, in bytes (e.g. 4, 32, ...)
1975 // t_alloc - total allocation needed to ensure desired boundary
1976 // mask - to clear least significant address bits for boundary
1977 // Compute:
1978 // t_alloc = n_alloc + (ab - allocBoundary)
1979 // allocate t_alloc bytes @ alloc_address
1980 // mask = NOT (ab - 1)
1981 // (e.g. if ab=32 _0001 1111 -> _1110 0000
1982 // aligned_address = alloc_address & mask
1983 // set n_alloc bytes to 0
1984 // return aligned_address (NULL if failed)
1986 // If u32_AlignedAddress is non-zero, then search for BaseAddress (stored
1987 // from previous allocation). If found, invoke call to FREE the memory.
1988 // Return NULL if BaseAddress not found
1990 // we need about 8 allocations per HBA. Figuring at most 10 HBAs per server
1991 // size the dynamic_mem array at 80.
1993 void* fcMemManager( struct pci_dev *pdev, ALIGNED_MEM *dynamic_mem,
1994 ULONG n_alloc, ULONG ab, ULONG u32_AlignedAddress,
1995 dma_addr_t *dma_handle)
1997 USHORT allocBoundary=1; // compiler specific - worst case 1
1998 // best case - replace malloc() call
1999 // with function that allocates exactly
2000 // at desired boundary
2002 unsigned long ulAddress;
2003 ULONG t_alloc, i;
2004 void *alloc_address = 0; // def. error code / address not found
2005 LONG mask; // must be 32-bits wide!
2007 ENTER("fcMemManager");
2008 if( u32_AlignedAddress ) // are we freeing existing memory?
2010 // printk(" freeing AlignedAddress %Xh\n", u32_AlignedAddress);
2011 for( i=0; i<DYNAMIC_ALLOCATIONS; i++) // look for the base address
2013 // printk("dynamic_mem[%u].AlignedAddress %lX\n", i, dynamic_mem[i].AlignedAddress);
2014 if( dynamic_mem[i].AlignedAddress == u32_AlignedAddress )
2016 alloc_address = dynamic_mem[i].BaseAllocated; // 'success' status
2017 pci_free_consistent(pdev,dynamic_mem[i].size,
2018 alloc_address,
2019 dynamic_mem[i].dma_handle);
2020 dynamic_mem[i].BaseAllocated = 0; // clear for next use
2021 dynamic_mem[i].AlignedAddress = 0;
2022 dynamic_mem[i].size = 0;
2023 break; // quit for loop; done
2027 else if( n_alloc ) // want new memory?
2029 dma_addr_t handle;
2030 t_alloc = n_alloc + (ab - allocBoundary); // pad bytes for alignment
2031 // printk("pci_alloc_consistent() for Tach alignment: %ld bytes\n", t_alloc);
2033 // (would like to) allow thread block to free pages
2034 alloc_address = // total bytes (NumberOfBytes)
2035 pci_alloc_consistent(pdev, t_alloc, &handle);
2037 // now mask off least sig. bits of address
2038 if( alloc_address ) // (only if non-NULL)
2040 // find place to store ptr, so we
2041 // can free it later...
2043 mask = (LONG)(ab - 1); // mask all low-order bits
2044 mask = ~mask; // invert bits
2045 for( i=0; i<DYNAMIC_ALLOCATIONS; i++) // look for free slot
2047 if( dynamic_mem[i].BaseAllocated == 0) // take 1st available
2049 dynamic_mem[i].BaseAllocated = alloc_address;// address from O/S
2050 dynamic_mem[i].dma_handle = handle;
2051 if (dma_handle != NULL)
2053 // printk("handle = %p, ab=%d, boundary = %d, mask=0x%08x\n",
2054 // handle, ab, allocBoundary, mask);
2055 *dma_handle = (dma_addr_t)
2056 ((((ULONG)handle) + (ab - allocBoundary)) & mask);
2058 dynamic_mem[i].size = t_alloc;
2059 break;
2062 ulAddress = (unsigned long)alloc_address;
2064 ulAddress += (ab - allocBoundary); // add the alignment bytes-
2065 // then truncate address...
2066 alloc_address = (void*)(ulAddress & mask);
2068 dynamic_mem[i].AlignedAddress =
2069 (ULONG)(ulAddress & mask); // 32bit Tach address
2070 memset( alloc_address, 0, n_alloc ); // clear new memory
2072 else // O/S dynamic mem alloc failed!
2073 alloc_address = 0; // (for debugging breakpt)
2077 LEAVE("fcMemManager");
2078 return alloc_address; // good (or NULL) address
2082 static Scsi_Host_Template driver_template = {
2083 .detect = cpqfcTS_detect,
2084 .release = cpqfcTS_release,
2085 .info = cpqfcTS_info,
2086 .proc_info = cpqfcTS_proc_info,
2087 .ioctl = cpqfcTS_ioctl,
2088 .queuecommand = cpqfcTS_queuecommand,
2089 .eh_device_reset_handler = cpqfcTS_eh_device_reset,
2090 .eh_abort_handler = cpqfcTS_eh_abort,
2091 .bios_param = cpqfcTS_biosparam,
2092 .can_queue = CPQFCTS_REQ_QUEUE_LEN,
2093 .this_id = -1,
2094 .sg_tablesize = SG_ALL,
2095 .cmd_per_lun = CPQFCTS_CMD_PER_LUN,
2096 .use_clustering = ENABLE_CLUSTERING,
2098 #include "scsi_module.c"