1/********************************************************************************
2 *                  QLOGIC LINUX SOFTWARE
3 *
4 * QLogic ISP1x80/1x160 device driver for Linux 2.3.x (redhat 6.X).
5 *
6 * COPYRIGHT (C) 1999-2000 QLOGIC CORPORATION
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the Qlogic's Linux Software License. See below.
10 *
11 * This program is WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *
18 * 1. Redistribution's or source code must retain the above copyright
19 *    notice, this list of conditions, and the following disclaimer,
20 *    without modification, immediately at the beginning of the file.
21 * 2. The name of the author may not be used to endorse or promote products
22 *    derived from this software without specific prior written permission.
23 *
24 ********************************************************************************/
25
26/*****************************************************************************************
27			QLOGIC CORPORATION SOFTWARE
28           "GNU" GENERAL PUBLIC LICENSE
29    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION
30                 AND MODIFICATION
31
32This GNU General Public License ("License") applies solely to QLogic Linux
33Software ("Software") and may be distributed under the terms of this License.
34
351. You may copy and distribute verbatim copies of the Software's source code as
36you receive it, in any medium, provided that you conspicuously and appropriately
37publish on each copy an appropriate copyright notice and disclaimer of warranty;
38keep intact all the notices that refer to this License and to the absence of any
39warranty; and give any other recipients of the Software a copy of this License along
40with the Software.
41
42You may charge a fee for the physical act of transferring a copy, and you may at your
43option offer warranty protection in exchange for a fee.
44
452. You may modify your copy or copies of the Software or any portion of it, thus forming
46a work based on the Software, and copy and distribute such modifications or work under
47the terms of Section 1 above, provided that you also meet all of these conditions:
48
49* a) You must cause the modified files to carry prominent notices stating that you
50changed the files and the date of any change.
51
52* b) You must cause any work that you distribute or publish that in whole or in part
53contains or is derived from the Software or any part thereof, to be licensed as a
54whole at no charge to all third parties under the terms of this License.
55
56* c) If the modified Software normally reads commands interactively when run, you
57must cause it, when started running for such interactive use in the most ordinary way,
58to print or display an announcement including an appropriate copyright notice and a
59notice that there is no warranty (or else, saying that you provide a warranty) and that
60users may redistribute the Software under these conditions, and telling the user how to
61view a copy of this License. (Exception:if the Software itself is interactive but does
62not normally print such an announcement, your work based on the Software is not required
63to print an announcement.)
64
65These requirements apply to the modified work as a whole. If identifiable sections of
66that work are not derived from the Software, and can be reasonably considered independent
67and separate works in themselves, then this License, and its terms, do not apply to those
68sections when you distribute them as separate works. But when you distribute the same
69sections as part of a whole which is a work based on the Software, the distribution of the
70whole must be on the terms of this License, whose permissions for other licensees extend
71to the entire whole, and thus to each and every part regardless of who wrote it.
72
733. You may copy and distribute the Software (or a work based on it, under Section 2) in
74object code or executable form under the terms of Sections 1 and 2 above provided that
75you also do one of the following:
76
77* a) Accompany it with the complete corresponding machine-readable source code, which must
78be distributed under the terms of Sections 1 and 2 above on a medium customarily used for
79software interchange; or,
80
81* b) Accompany it with a written offer, valid for at least three years, to give any third
82party, for a charge no more than your cost of physically performing source distribution,
83a complete machine-readable copy of the corresponding source code, to be distributed under
84the terms of Sections 1 and 2 above on a medium customarily used for software interchange;
85or,
86
87* c) Accompany it with the information you received as to the offer to distribute
88corresponding source code. (This alternative is allowed only for noncommercial distribution
89and only if you received the Software in object code or executable form with such an offer,
90in accord with Subsection b above.)
91
92The source code for a work means the preferred form of the work for making modifications
93to it. For an executable work, complete source code means all the source code for all
94modules it contains, plus any associated interface definition files, plus the scripts used
95to control compilation and installation of the executable.
96
97If distribution of executable or object code is made by offering access to copy from a
98designated place, then offering equivalent access to copy the source code from the same
99place counts as distribution of the source code, even though third parties are not
100compelled to copy the source along with the object code.
101
1024. You may not copy, modify, sublicense, or distribute the Software except as expressly
103provided under this License. Any attempt otherwise to copy, modify, sublicense or
104distribute the Software is void, and will automatically terminate your rights under this
105License. However, parties who have received copies, or rights, from you under this License
106will not have their licenses terminated so long as such parties remain in full compliance.
107
1085. This license grants you world wide, royalty free non-exclusive rights to modify or
109distribute the Software or its derivative works. These actions are prohibited by law
110if you do not accept this License. Therefore, by modifying or distributing the Software
111(or any work based on the Software), you indicate your acceptance of this License to do
112so, and all its terms and conditions for copying, distributing or modifying the Software
113or works based on it.
114
1156. Each time you redistribute the Software (or any work based on the Software), the
116recipient automatically receives a license from the original licensor to copy, distribute
117or modify the Software subject to these terms and conditions. You may not impose any
118further restrictions on the recipients' exercise of the rights granted herein. You are
119not responsible for enforcing compliance by third parties to this License.
120
1217. If, as a consequence of a court judgment or allegation of patent infringement or for
122any other reason (not limited to patent issues), conditions are imposed on you
123(whether by court order, agreement or otherwise) that contradict the conditions of this
124License, they do not excuse you from the conditions of this License. If you cannot
125distribute so as to satisfy simultaneously your obligations under this License
126and any other pertinent obligations, then as a consequence you may not distribute the
127Software at all.
128
129If any portion of this section is held invalid or unenforceable under any particular
130circumstance, the balance of the section is intended to apply and the section as a whole
131is intended to apply in other circumstances.
132NO WARRANTY
133
13411. THE SOFTWARE IS PROVIDED WITHOUT A WARRANTY OF ANY KIND. THERE IS NO
135WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
136EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
137OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND,
138EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
139WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
140ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU.
141SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
142NECESSARY SERVICING, REPAIR OR CORRECTION.
143
14412. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
145WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
146REDISTRIBUTE THE SOFTWARE AS PERMITTED ABOVE, BE LIABLE TO YOU FOR
147DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL
148DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING
149BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR
150LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO
151OPERATE WITH ANY OTHER SOFTWARES), EVEN IF SUCH HOLDER OR OTHER PARTY HAS
152BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
153END OF TERMS AND CONDITIONS
154
155*******************************************************************************************/
156
157/****************************************************************************
158    Revision History:
159    Rev. 3.00       Jan 17, 1999    DG  Qlogic
160	   - Added 64-bit support.
161    Rev. 2.07       Nov 9, 1999     DG  Qlogic
162	   - Added new routine to set target parameters for ISP12160.
163    Rev. 2.06       Sept 10, 1999     DG  Qlogic
164       - Added support for ISP12160 Ultra 3 chip.
165    Rev. 2.03       August 3, 1999    Fred Lewis, Intel DuPont
166	- Modified code to remove errors generated when compiling with
167	  Cygnus IA64 Compiler.
168        - Changed conversion of pointers to unsigned longs instead of integers.
169        - Changed type of I/O port variables from uint32_t to unsigned long.
170        - Modified OFFSET macro to work with 64-bit as well as 32-bit.
171        - Changed sprintf and printk format specifiers for pointers to %p.
172        - Changed some int to long type casts where needed in sprintf & printk.
173        - Added l modifiers to sprintf and printk format specifiers for longs.
174        - Removed unused local variables.
175    Rev. 1.20       June 8, 1999      DG,  Qlogic
176         Changes to support RedHat release 6.0 (kernel 2.2.5).
177       - Added SCSI exclusive access lock (io_request_lock) when accessing
178         the adapter.
179       - Added changes for the new LINUX interface template. Some new error
180         handling routines have been added to the template, but for now we
181         will use the old ones.
182    -   Initial Beta Release.
183*****************************************************************************/
184
185
186#ifdef MODULE
187#include <linux/module.h>
188#endif
189
190#define QLA1280_VERSION      " 3.00-Beta"
191
192#include <stdarg.h>
193#include <asm/io.h>
194#include <asm/irq.h>
195#include <asm/segment.h>
196#include <asm/byteorder.h>
197#include <linux/version.h>
198#include <linux/types.h>
199#include <linux/string.h>
200#include <linux/errno.h>
201#include <linux/kernel.h>
202#include <linux/ioport.h>
203#include <linux/delay.h>
204#include <linux/timer.h>
205#include <linux/sched.h>
206#include <linux/pci.h>
207#include <linux/proc_fs.h>
208#include <linux/blk.h>
209#include <linux/tqueue.h>
210/* MRS #include <linux/tasks.h> */
211#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
212# include <linux/bios32.h>
213#endif
214#include "sd.h"
215#include "scsi.h"
216#include "hosts.h"
217#define UNIQUE_FW_NAME
218#include "qla1280.h"
219#include "ql12160_fw.h"                     /* ISP RISC code */
220#include "ql1280_fw.h"
221
222#include <linux/stat.h>
223#include <linux/slab.h>        /* for kmalloc() */
224
225
226#ifndef KERNEL_VERSION
227#  define KERNEL_VERSION(x,y,z) (((x)<<16)+((y)<<8)+(z))
228#endif
229
230
231/*
232 * Compile time Options:
233 *            0 - Disable and 1 - Enable
234 */
235#define  QLA1280_64BIT_SUPPORT         1   /* 64-bit Support */
236#define  QL1280_TARGET_MODE_SUPPORT    0   /* Target mode support */
237#define  WATCHDOGTIMER                 0
238#define  MEMORY_MAPPED_IO              0
239#define  DEBUG_QLA1280_INTR            0
240#define  USE_NVRAM_DEFAULTS	       0
241#define  DEBUG_PRINT_NVRAM             0
242#define  LOADING_RISC_ACTIVITY         0
243#define  AUTO_ESCALATE_RESET           0   /* Automatically escalate resets */
244#define  AUTO_ESCALATE_ABORT           0   /* Automatically escalate aborts */
245#define  STOP_ON_ERROR                 0   /* Stop on aborts and resets  */
246#define  STOP_ON_RESET                 0
247#define  STOP_ON_ABORT                 0
248#undef   DYNAMIC_MEM_ALLOC
249
250#define  DEBUG_QLA1280                 0    /* Debugging  */
251/* #define CHECKSRBSIZE */
252
253/*
254 * These macros to assist programming
255 */
256
257#define	BZERO(ptr, amt)		memset(ptr, 0, amt)
258#define	BCOPY(src, dst, amt)	memcpy(dst, src, amt)
259#define	KMALLOC(siz)	kmalloc((siz), GFP_ATOMIC)
260#define	KMFREE(ip,siz)	kfree((ip))
261#define	SYS_DELAY(x)		udelay(x);barrier()
262#define QLA1280_DELAY(sec)  mdelay(sec * 1000)
263#define VIRT_TO_BUS(a) virt_to_bus((a))
264#if  QLA1280_64BIT_SUPPORT
265#if  BITS_PER_LONG <= 32
266#define  VIRT_TO_BUS_LOW(a) (uint32_t)virt_to_bus((a))
267#define  VIRT_TO_BUS_HIGH(a) (uint32_t)(0x0)
268#else
269#define  VIRT_TO_BUS_LOW(a) (uint32_t)(0xffffffff & virt_to_bus((a)))
270#define  VIRT_TO_BUS_HIGH(a) (uint32_t)(0xffffffff & (virt_to_bus((a))>>32))
271#endif
272#endif  /* QLA1280_64BIT_SUPPORT */
273
274#define STATIC
275
276#define NVRAM_DELAY() udelay(500) /* 2 microsecond delay */
277void qla1280_device_queue_depth(scsi_qla_host_t *, Scsi_Device *);
278
279#define  CACHE_FLUSH(a) (RD_REG_WORD(a))
280#define  INVALID_HANDLE    (MAX_OUTSTANDING_COMMANDS+1)
281
282#define  MSW(x)          (uint16_t)((uint32_t)(x) >> 16)
283#define  LSW(x)          (uint16_t)(x)
284#define  MSB(x)          (uint8_t)((uint16_t)(x) >> 8)
285#define  LSB(x)          (uint8_t)(x)
286
287#if  BITS_PER_LONG <= 32
288#define  LS_64BITS(x) (uint32_t)(x)
289#define  MS_64BITS(x) (uint32_t)(0x0)
290#else
291#define  LS_64BITS(x) (uint32_t)(0xffffffff & (x))
292#define  MS_64BITS(x) (uint32_t)(0xffffffff & ((x)>>32) )
293#endif
294
295/*
296 *  QLogic Driver Support Function Prototypes.
297 */
298STATIC void   qla1280_done(scsi_qla_host_t *, srb_t **, srb_t **);
299STATIC void   qla1280_next(scsi_qla_host_t *, scsi_lu_t *, uint8_t);
300STATIC void   qla1280_putq_t(scsi_lu_t *, srb_t *);
301STATIC void   qla1280_done_q_put(srb_t *, srb_t **, srb_t **);
302STATIC void qla1280_select_queue_depth(struct Scsi_Host *, Scsi_Device *);
303#ifdef  QLA1280_UNUSED
304static void qla1280_dump_regs(struct Scsi_Host *host);
305#endif
306#if  STOP_ON_ERROR
307static void qla1280_panic(char *, struct Scsi_Host *host);
308#endif
309void qla1280_print_scsi_cmd(Scsi_Cmnd *cmd);
310STATIC void qla1280_abort_queue_single(scsi_qla_host_t *,uint32_t,uint32_t,uint32_t,uint32_t);
311
312STATIC int qla1280_return_status( sts_entry_t *sts, Scsi_Cmnd       *cp);
313STATIC void qla1280_removeq(scsi_lu_t *q, srb_t *sp);
314STATIC void qla1280_mem_free(scsi_qla_host_t *ha);
315static void qla1280_do_dpc(void *p);
316#ifdef  QLA1280_UNUSED
317static void qla1280_set_flags(char * s);
318#endif
319static char	*qla1280_get_token(char *, char *);
320#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
321STATIC inline void mdelay(int);
322#endif
323static inline void qla1280_enable_intrs(scsi_qla_host_t *);
324static inline void qla1280_disable_intrs(scsi_qla_host_t *);
325
326/*
327 *  QLogic ISP1280 Hardware Support Function Prototypes.
328 */
329STATIC uint8_t   qla1280_initialize_adapter(struct scsi_qla_host   *ha);
330STATIC uint8_t   qla1280_enable_tgt(scsi_qla_host_t *, uint8_t);
331STATIC uint8_t   qla1280_isp_firmware(scsi_qla_host_t *);
332STATIC uint8_t   qla1280_pci_config(scsi_qla_host_t *);
333STATIC uint8_t   qla1280_chip_diag(scsi_qla_host_t *);
334STATIC uint8_t   qla1280_setup_chip(scsi_qla_host_t *);
335STATIC uint8_t   qla1280_init_rings(scsi_qla_host_t *);
336STATIC uint8_t   qla1280_nvram_config(scsi_qla_host_t *);
337STATIC uint8_t   qla1280_mailbox_command(scsi_qla_host_t *, uint8_t, uint16_t *);
338STATIC uint8_t   qla1280_bus_reset(scsi_qla_host_t *, uint8_t);
339STATIC uint8_t   qla1280_device_reset(scsi_qla_host_t *, uint8_t, uint32_t);
340STATIC uint8_t   qla1280_abort_device(scsi_qla_host_t *, uint8_t, uint32_t, uint32_t);
341STATIC uint8_t   qla1280_abort_command(scsi_qla_host_t *, srb_t *),
342#if  QLA1280_64BIT_SUPPORT
343                 qla1280_64bit_start_scsi(scsi_qla_host_t *, srb_t *),
344#endif
345                 qla1280_32bit_start_scsi(scsi_qla_host_t *, srb_t *),
346                 qla1280_abort_isp(scsi_qla_host_t *);
347STATIC void      qla1280_nv_write(scsi_qla_host_t *, uint16_t),
348                 qla1280_nv_delay(scsi_qla_host_t *),
349                 qla1280_poll(scsi_qla_host_t *),
350                 qla1280_reset_adapter(scsi_qla_host_t *),
351                 qla1280_marker(scsi_qla_host_t *, uint8_t, uint32_t, uint32_t, uint8_t),
352                 qla1280_isp_cmd(scsi_qla_host_t *),
353                 qla1280_isr(scsi_qla_host_t *, srb_t **, srb_t **),
354                 qla1280_rst_aen(scsi_qla_host_t *),
355                 qla1280_status_entry(scsi_qla_host_t *, sts_entry_t *, srb_t **,
356                                      srb_t **),
357                 qla1280_error_entry(scsi_qla_host_t *, response_t *, srb_t **,
358                                     srb_t **),
359                 qla1280_restart_queues(scsi_qla_host_t *),
360                 qla1280_abort_queues(scsi_qla_host_t *);
361STATIC uint16_t  qla1280_get_nvram_word(scsi_qla_host_t *, uint32_t),
362                 qla1280_nvram_request(scsi_qla_host_t *, uint32_t),
363                 qla1280_debounce_register(volatile uint16_t *);
364STATIC request_t *qla1280_req_pkt(scsi_qla_host_t *);
365int  qla1280_check_for_dead_scsi_bus(scsi_qla_host_t *ha, srb_t *sp);
366STATIC uint8_t qla1280_mem_alloc(scsi_qla_host_t *ha);
367STATIC uint8_t  qla1280_register_with_Linux(scsi_qla_host_t *ha, uint8_t maxchannels);
368
369STATIC uint8_t qla12160_set_target_parameters(scsi_qla_host_t *, uint32_t, uint32_t, uint32_t, nvram160_t *);
370STATIC void qla12160_get_target_parameters(scsi_qla_host_t *, uint32_t, uint32_t, uint32_t);
371
372#if QL1280_TARGET_MODE_SUPPORT
373STATIC void      qla1280_enable_lun(scsi_qla_host_t *, uint8_t, uint32_t),
374                 qla1280_notify_ack(scsi_qla_host_t *, notify_entry_t *),
375                 qla1280_immed_notify(scsi_qla_host_t *, notify_entry_t *),
376                 qla1280_accept_io(scsi_qla_host_t *, ctio_ret_entry_t *),
377#if  QLA1280_64BIT_SUPPORT
378                 qla1280_64bit_continue_io(scsi_qla_host_t *, atio_entry_t *, uint32_t,
379                                     paddr32_t *),
380#endif
381                 qla1280_32bit_continue_io(scsi_qla_host_t *, atio_entry_t *, uint32_t,
382                                     paddr32_t *),
383                 qla1280_atio_entry(scsi_qla_host_t *, atio_entry_t *),
384                 qla1280_notify_entry(scsi_qla_host_t *, notify_entry_t *);
385#endif  /* QLA1280_TARGET_MODE_SUPPORT */
386
387#ifdef QL_DEBUG_ROUTINES
388/*
389 *  Driver Debug Function Prototypes.
390 */
391STATIC uint8_t  qla1280_getbyte(uint8_t *);
392STATIC uint16_t qla1280_getword(uint16_t *);
393STATIC uint32_t qla1280_getdword(uint32_t *);
394STATIC void     qla1280_putbyte(uint8_t *, uint8_t),
395                qla1280_putword(uint16_t *, uint16_t),
396                qla1280_putdword(uint32_t *, uint32_t),
397                qla1280_print(caddr_t),
398                qla1280_output_number(uint32_t, uint8_t),
399                qla1280_putc(uint8_t),
400                qla1280_dump_buffer(caddr_t, uint32_t);
401
402char          debug_buff[80];
403#if DEBUG_QLA1280
404STATIC uint8_t ql_debug_print = 1;
405#else
406STATIC uint8_t ql_debug_print = 0;
407#endif
408#endif
409
410/*
411 * insmod needs to find the variable and make it point to something
412 */
413#ifdef MODULE
414static char *options = NULL;
415#if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,18)
416
417/* insmod qla1280 options=verbose" */
418MODULE_PARM(options, "s");
419#endif
420/*
421 * Just in case someone uses commas to separate items on the insmod
422 * command line, we define a dummy buffer here to avoid having insmod
423 * write wild stuff into our code segment
424 */
425static char dummy_buffer[60] = "Please don't add commas in your insmod command!!\n";
426
427#endif
428
429/* We use the Scsi_Pointer structure that's included with each command
430 * SCSI_Cmnd as a scratchpad for our SRB.
431 *
432 * SCp will always point to the SRB structure (defined in qla1280.h).
433 * It is define as follows:
434 *  - SCp.ptr  -- > pointer back to the cmd
435 *  - SCp.this_residual --> used as forward pointer to next srb
436 *  - SCp.buffer --> used as backward pointer to next srb
437 *  - SCp.buffers_residual --> used as flags field
438 *  - SCp.have_data_in --> not used
439 *  - SCp.sent_command --> not used
440 *  - SCp.phase --> not used
441 */
442
443#define	CMD_SP(Cmnd)		(&(Cmnd)->SCp)
444#define	CMD_XFRLEN(Cmnd)	(Cmnd)->request_bufflen
445#define	CMD_CDBLEN(Cmnd)	(Cmnd)->cmd_len
446#define	CMD_CDBP(Cmnd)		(Cmnd)->cmnd
447#define	CMD_SNSP(Cmnd)		(Cmnd)->sense_buffer
448#define	CMD_SNSLEN(Cmnd)	(sizeof (Cmnd)->sense_buffer)
449#define	CMD_RESULT(Cmnd)	((Cmnd)->result)
450#define	CMD_HANDLE(Cmnd)	((Cmnd)->host_scribble)
451
452/*****************************************/
453/*   ISP Boards supported by this driver */
454/*****************************************/
455#define QLA1280_VENDOR_ID   0x1077
456#define QLA1080_DEVICE_ID   0x1080
457#define QLA1240_DEVICE_ID   0x1240
458#define QLA1280_DEVICE_ID   0x1280
459#define QLA12160_DEVICE_ID  0x1216
460#define QLA10160_DEVICE_ID  0x1016
461#define NUM_OF_ISP_DEVICES        6
462
463typedef struct _qlaboards
464{
465   unsigned char   bdName[9];       /* Board ID String */
466   unsigned long   device_id;       /* Device PCI ID   */
467   int   numPorts;                  /* Number of SCSI ports */
468   unsigned short   *fwcode;        /* pointer to FW array         */
469   unsigned long    *fwlen;         /* number of words in array    */
470   unsigned short   *fwstart;       /* start address for F/W       */
471   unsigned char   *fwver;          /* Ptr to F/W version array    */
472} qla_boards_t;
473
474struct _qlaboards   QLBoardTbl[NUM_OF_ISP_DEVICES] =
475{
476   /* Name ,  Board PCI Device ID,         Number of ports */
477  {"QLA1080 ", QLA1080_DEVICE_ID,           1,
478               &fw1280ei_code01[0],  (unsigned long *)&fw1280ei_length01,&fw1280ei_addr01, &fw1280ei_version_str[0] },
479  {"QLA1240 ", QLA1240_DEVICE_ID,           2,
480               &fw1280ei_code01[0],  (unsigned long *)&fw1280ei_length01,&fw1280ei_addr01, &fw1280ei_version_str[0] },
481  {"QLA1280 ", QLA1280_DEVICE_ID,           2,
482               &fw1280ei_code01[0],  (unsigned long *)&fw1280ei_length01,&fw1280ei_addr01, &fw1280ei_version_str[0] },
483  {"QLA12160 ", QLA12160_DEVICE_ID,          2,
484               &fw12160i_code01[0],  (unsigned long *)&fw12160i_length01,&fw12160i_addr01, &fw12160i_version_str[0] },
485  {"QLA10160 ", QLA10160_DEVICE_ID,          1,
486               &fw12160i_code01[0],  (unsigned long *)&fw12160i_length01,&fw12160i_addr01, &fw12160i_version_str[0] },
487  {"        ",                 0,           0}
488};
489
490static unsigned long qla1280_verbose = 1L;
491static scsi_qla_host_t *qla1280_hostlist = NULL;
492#ifdef QLA1280_PROFILE
493static int qla1280_buffer_size = 0;
494static char *qla1280_buffer = NULL;
495#endif
496
497#ifdef QL_DEBUG_LEVEL_3
498#define ENTER(x)	sprintf(debug_buff,"qla1280 : Entering %s()\n\r", x); \
499                        qla1280_print(debug_buff);
500#define LEAVE(x)	sprintf(debug_buff,"qla1280 : Leaving %s()\n\r", x); \
501                        qla1280_print(debug_buff);
502#define ENTER_INTR(x)	sprintf(debug_buff,"qla1280 : Entering %s()\n\r", x); \
503                        qla1280_print(debug_buff);
504#define LEAVE_INTR(x)	sprintf(debug_buff,"qla1280 : Leaving %s()\n\r", x); \
505                        qla1280_print(debug_buff);
506#define DEBUG3(x)	x
507#else
508#define ENTER(x)
509#define LEAVE(x)
510#define ENTER_INTR(x)
511#define LEAVE_INTR(x)
512#define DEBUG3(x)
513#endif
514
515#if  DEBUG_QLA1280
516#define COMTRACE(x)
517/* #define COMTRACE(x)     qla1280_putc(x); */
518#define DEBUG(x)	x
519#else
520#define DEBUG(x)
521#define COMTRACE(x)
522#endif
523
524#ifdef QL_DEBUG_LEVEL_2
525#define DEBUG2(x)	x
526#else
527#define DEBUG2(x)
528#endif
529#define DEBUG5(x)
530
531#if BITS_PER_LONG==64
532#   define OFFSET(w)   (((uint64_t) &w) & 0xFF)   /* 256 byte offsets */
533#else
534#   define OFFSET(w)   (((uint32_t) &w) & 0xFF)   /* 256 byte offsets */
535#endif
536
537#define SCSI_BUS_32(scp)   ((scp)->channel)
538#define SCSI_TCN_32(scp)    ((scp)->target)
539#define SCSI_LUN_32(scp)    ((scp)->lun)
540
541/****************************************************************************/
542/*  LINUX -  Loadable Module Functions.                                     */
543/****************************************************************************/
544
545
546/*************************************************************************
547 *   qla1280_set_info
548 *
549 * Description:
550 *   Set parameters for the driver from the /proc filesystem.
551 *
552 * Returns:
553 *************************************************************************/
554int
555qla1280_set_info(char *buffer, int length, struct Scsi_Host *HBAptr)
556{
557  return (-ENOSYS);  /* Currently this is a no-op */
558}
559
560/*************************************************************************
561 * qla1280_proc_info
562 *
563 * Description:
564 *   Return information to handle /proc support for the driver.
565 *
566 * buffer - ptrs to a page buffer
567 *
568 * Returns:
569 *************************************************************************/
570#ifdef QLA1280_PROFILE
571#define	PROC_BUF	(&qla1280_buffer[size])
572#define LUN_ID       (targ_lun>>(MAX_T_BITS+MAX_L_BITS)),((targ_lun>>MAX_L_BITS)&0xf), targ_lun&0x7
573#endif
574int
575qla1280_proc_info ( char *buffer, char **start, off_t offset, int length,
576                    int hostno, int inout)
577{
578#ifdef QLA1280_PROFILE
579  struct Scsi_Host *host;
580  scsi_qla_host_t *ha;
581  int    size = 0;
582  int  targ_lun;
583  scsi_lu_t  *up;
584  int   no_devices;
585
586  printk("Entering proc_info 0x%p,0x%lx,0x%x,0x%x\n",buffer,offset,length,hostno);
587  host = NULL;
588  /* find the host they want to look at */
589  for(ha=qla1280_hostlist; (ha != NULL) && ha->host->host_no != hostno; ha=ha->next)
590    ;
591
592  if (!ha)
593  {
594    size += sprintf(buffer, "Can't find adapter for host number %d\n", hostno);
595    if (size > length)
596    {
597      return (size);
598    }
599    else
600    {
601      return (length);
602    }
603  }
604
605  host = ha->host;
606  if (inout == TRUE) /* Has data been written to the file? */
607  {
608    return (qla1280_set_info(buffer, length, host));
609  }
610
611  /* compute number of active devices */
612  no_devices = 0;
613  for (targ_lun = 0; targ_lun < MAX_EQ; targ_lun++)
614  {
615          if( (up = ha->dev[targ_lun]) == NULL )
616              continue;
617          no_devices++;
618  }
619  /* size = 112 * no_devices; */
620  size = 4096;
621  /* round up to the next page */
622
623  /*
624   * if our old buffer is the right size use it otherwise
625   * allocate a new one.
626   */
627  if (qla1280_buffer_size != size)
628  {
629    /* deallocate this buffer and get a new one */
630    if (qla1280_buffer != NULL)
631    {
632      kfree(qla1280_buffer);
633      qla1280_buffer_size = 0;
634    }
635    qla1280_buffer = kmalloc(size, GFP_KERNEL);
636  }
637  if (qla1280_buffer == NULL)
638  {
639    size = sprintf(buffer, "qla1280 - kmalloc error at line %d\n",
640        __LINE__);
641    return size;
642  }
643  qla1280_buffer_size = size;
644
645  size = 0;
646  size += sprintf(PROC_BUF, "Qlogic 1280/1080 SCSI driver version: ");   /* 43 bytes */
647  size += sprintf(PROC_BUF, "%5s, ", QLA1280_VERSION);                         /* 5        */
648  size += sprintf(PROC_BUF, "Qlogic Firmware version: ");                     /* 25       */
649  size += sprintf(PROC_BUF, "%2d.%2d.%2d",_firmware_version[0],           /* 8        */
650                                          ql12_firmware_version[1],
651                                          ql12_firmware_version[2]);
652  size += sprintf(PROC_BUF, "\n");                                             /* 1       */
653
654  size += sprintf(PROC_BUF, "SCSI Host Adapter Information: %s\n", QLBoardTbl[ha->devnum].bdName);
655  size += sprintf(PROC_BUF, "Request Queue = 0x%lx, Response Queue = 0x%lx\n",
656                        ha->request_dma,
657                        ha->response_dma);
658  size += sprintf(PROC_BUF, "Request Queue count= 0x%x, Response Queue count= 0x%x\n",
659                        REQUEST_ENTRY_CNT,
660                        RESPONSE_ENTRY_CNT);
661  size += sprintf(PROC_BUF,"Number of pending commands = 0x%lx\n", ha->actthreads);
662  size += sprintf(PROC_BUF,"Number of queued commands = 0x%lx\n", ha->qthreads);
663  size += sprintf(PROC_BUF,"Number of free request entries = %d\n",ha->req_q_cnt);
664  size += sprintf(PROC_BUF, "\n");                                             /* 1       */
665
666  size += sprintf(PROC_BUF, "Attached devices:\n");
667  /* scan for all equipment stats */
668  for (targ_lun = 0; targ_lun < MAX_EQ; targ_lun++)
669  {
670      if( (up = ha->dev[targ_lun]) == NULL )
671           continue;
672      if( up->io_cnt == 0 )
673      {
674          size += sprintf(PROC_BUF,"(%2d:%2d:%2d) No stats\n",LUN_ID);
675           continue;
676      }
677      /* total reads since boot */
678      /* total writes since boot */
679      /* total requests since boot  */
680      size += sprintf(PROC_BUF, "Total requests %ld,",up->io_cnt);
681      /* current number of pending requests */
682      size += sprintf(PROC_BUF, "(%2d:%2d:%2d) pending requests %d,",LUN_ID,up->q_outcnt);
683      /* avg response time */
684      size += sprintf(PROC_BUF, "Avg response time %ld%%,",(up->resp_time/up->io_cnt)*100);
685
686      /* avg active time */
687      size += sprintf(PROC_BUF, "Avg active time %ld%%\n",(up->act_time/up->io_cnt)*100);
688  }
689
690  if (size >= qla1280_buffer_size)
691  {
692    printk(KERN_WARNING "qla1280: Overflow buffer in qla1280_proc.c\n");
693  }
694
695  if (offset > size - 1)
696  {
697    kfree(qla1280_buffer);
698    qla1280_buffer = NULL;
699    qla1280_buffer_size = length = 0;
700    *start = NULL;
701  }
702  else
703  {
704    *start = &qla1280_buffer[offset];   /* Start of wanted data */
705    if (size - offset < length)
706    {
707      length = size - offset;
708    }
709  }
710#endif
711
712  return (length);
713}
714
715
716/**************************************************************************
717 * qla1280_detect
718 *    This routine will probe for Qlogic 1280 SCSI host adapters.
719 *    It returns the number of host adapters of a particular
720 *    type that were found.	 It also initialize all data necessary for
721 *    the driver.  It is passed-in the host number, so that it
722 *    knows where its first entry is in the scsi_hosts[] array.
723 *
724 * Input:
725 *     template - pointer to SCSI template
726 *
727 * Returns:
728 *  num - number of host adapters found.
729 **************************************************************************/
730int
731qla1280_detect(Scsi_Host_Template *template)
732{
733    int num_hosts = 0;
734    struct Scsi_Host *host;
735    scsi_qla_host_t *ha, *cur_ha;
736    struct _qlaboards  *bdp;
737    int i, j;
738#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,1,95)
739    unsigned int piobase;
740    unsigned char pci_bus, pci_devfn, pci_irq;
741    config_reg_t   *cfgp = 0;
742#endif
743    device_reg_t   *reg;
744    char   *cp;
745#if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,95)
746    struct pci_dev *pdev = NULL;
747#else
748    int index;
749#endif
750
751    ENTER("qla1280_detect");
752
753#ifdef CHECKSRBSIZE
754    if (sizeof(srb_t) > sizeof(Scsi_Pointer) )
755    {
756      printk("Redefine SRB - its too big");
757      return 0;
758    }
759#endif
760
761#ifdef MODULE
762	DEBUG(sprintf(debug_buff,"DEBUG: qla1280_detect starts at address = %p\n",qla1280_detect);)
763	DEBUG(qla1280_print(debug_buff);)
764    /*
765    * If we are called as a module, the qla1280 pointer may not be null
766    * and it would point to our bootup string, just like on the lilo
767    * command line.  IF not NULL, then process this config string with
768    * qla1280_setup
769    *
770    * Boot time Options
771    * To add options at boot time add a line to your lilo.conf file like:
772    * append="qla1280=verbose,max_tags:{{255,255,255,255},{255,255,255,255}}"
773    * which will result in the first four devices on the first two
774    * controllers being set to a tagged queue depth of 32.
775    */
776    if(options)
777        qla1280_setup(options, NULL);
778    if(dummy_buffer[0] != 'P')
779        printk(KERN_WARNING "qla1280: Please read the file /usr/src/linux/drivers"
780                "/scsi/README.qla1280\n"
781                "qla1280: to see the proper way to specify options to the qla1280 "
782                "module\n"
783                "qla1280: Specifically, don't use any commas when passing arguments to\n"
784                "qla1280: insmod or else it might trash certain memory areas.\n");
785#endif
786
787    if ((int) !pcibios_present())
788    {
789		printk("scsi: PCI not present\n");
790		return 0;
791    } /* end of IF */
792    bdp = &QLBoardTbl[0];
793    qla1280_hostlist = NULL;
794    template->proc_name = "qla1280";
795
796	/* Try and find each different type of adapter we support */
797	for( i=0; bdp->device_id != 0 && i < NUM_OF_ISP_DEVICES; i++, bdp++ ) {
798#if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,95)
799		while ((pdev = pci_find_device(QLA1280_VENDOR_ID,
800			bdp->device_id, pdev ) ))  {
801		if (pci_enable_device(pdev)) continue;
802#else
803		while (!(pcibios_find_device(QLA1280_VENDOR_ID,
804			bdp->device_id,
805			index++, &pci_bus, &pci_devfn)) )  {
806#endif
807                /* found a adapter */
808		host = scsi_register(template, sizeof(scsi_qla_host_t));
809                if (!host) {
810			printk(KERN_WARNING "qla1280: Failed to register host, aborting.\n");
811                        return 0;
812                }
813		scsi_set_pci_device(host, pdev);
814		ha = (scsi_qla_host_t *) host->hostdata;
815		/* Clear our data area */
816		for( j =0, cp = (char *)ha;  j < sizeof(scsi_qla_host_t); j++)
817			*cp = 0;
818		/* Sanitize the information from PCI BIOS.  */
819#if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,95)
820		host->irq = pdev->irq;
821		host->io_port = pci_resource_start(pdev, 0);
822		ha->pci_bus = pdev->bus->number;
823		ha->pci_device_fn = pdev->devfn;
824		ha->pdev = pdev;
825#else
826		pcibios_read_config_byte(pci_bus, pci_devfn, OFFSET(cfgp->interrupt_line), &pci_irq);
827		pcibios_read_config_dword(pci_bus, pci_devfn, OFFSET(cfgp->base_port), &piobase);
828		host->irq = pci_irq;
829		host->io_port = (unsigned int) piobase;
830		host->io_port &= PCI_BASE_ADDRESS_IO_MASK;
831		ha->pci_bus = pci_bus;
832		ha->pci_device_fn = pci_devfn;
833#endif
834		ha->device_id = bdp->device_id;
835
836                ha->devnum = i;
837		if( qla1280_mem_alloc(ha) ) {
838			printk(KERN_INFO "qla1280: Failed to allocate memory for adapter\n");
839		}
840
841                ha->ports = bdp->numPorts;
842                ha->iobase = (device_reg_t *) host->io_port;
843                ha->host = host;
844                ha->host_no = host->host_no;
845
846                /* load the F/W, read paramaters, and init the H/W */
847                if (qla1280_initialize_adapter(ha))
848                {
849
850                    printk(KERN_INFO "qla1280: Failed to initialized adapter\n");
851                    qla1280_mem_free(ha);
852                    scsi_unregister(host);
853                    continue;
854                }
855
856                host->max_channel = bdp->numPorts-1;
857                ha->instance = num_hosts;
858		/* Register our resources with Linux */
859		if( qla1280_register_with_Linux(ha, bdp->numPorts-1) ) {
860			printk(KERN_INFO "qla1280: Failed to register our resources\n");
861			qla1280_mem_free(ha);
862			scsi_unregister(host);
863			continue;
864		}
865
866
867                reg = ha->iobase;
868                /* Disable ISP interrupts. */
869		qla1280_disable_intrs(ha);
870
871                /* Insure mailbox registers are free. */
872                WRT_REG_WORD(&reg->semaphore, 0);
873                WRT_REG_WORD(&reg->host_cmd, HC_CLR_RISC_INT);
874                WRT_REG_WORD(&reg->host_cmd, HC_CLR_HOST_INT);
875
876                /* Enable chip interrupts. */
877		qla1280_enable_intrs(ha);
878
879                /* Insert new entry into the list of adapters */
880                ha->next = NULL;
881                if( qla1280_hostlist == NULL )
882                {
883                    cur_ha = qla1280_hostlist = ha;
884                }
885                else
886                {
887                    cur_ha = qla1280_hostlist;
888                    while( cur_ha->next != NULL )
889                        cur_ha = cur_ha->next;
890                    cur_ha->next = ha;
891                }
892                num_hosts++;
893            }  /* end of WHILE */
894        } /* end of FOR */
895
896    LEAVE("qla1280_detect");
897    return num_hosts;
898}
899
900/**************************************************************************
901*   qla1280_register_with_Linux
902*
903* Description:
904*   Free the passed in Scsi_Host memory structures prior to unloading the
905*   module.
906*
907* Input:
908*     ha - pointer to host adapter structure
909*     maxchannels - MAX number of channels.
910*
911* Returns:
912*  0 - Sucessfully reserved resources.
913*  1 - Failed to reserved a resource.
914**************************************************************************/
915STATIC uint8_t  qla1280_register_with_Linux(scsi_qla_host_t *ha, uint8_t maxchannels)
916{
917
918	struct Scsi_Host *host = ha->host;
919
920	host->can_queue = 0xfffff;  /* unlimited  */
921	host->cmd_per_lun = 1;
922       host->select_queue_depths = qla1280_select_queue_depth;
923	host->n_io_port = 0xFF;
924	host->base = (unsigned long) ha->mmpbase;
925	host->max_channel = maxchannels;
926       host->max_lun = MAX_LUNS-1;
927	host->unique_id = ha->instance;
928       host->max_id = MAX_TARGETS;
929       host->unique_id = ha->instance;
930
931	/* set our host ID  (need to do something about our two IDs) */
932       host->this_id = ha->bus_settings[0].id;
933       /* Register the IRQ with Linux (sharable) */
934       if ( request_irq(host->irq, qla1280_intr_handler, SA_INTERRUPT| SA_SHIRQ, "qla1280", ha))
935       {
936          printk("qla1280 : Failed to reserved interrupt %d already in use\n", host->irq);
937          qla1280_mem_free(ha);
938          scsi_unregister(host);
939	     return 1;
940       }
941
942       /* Register the I/O space with Linux */
943       if (check_region(host->io_port, 0xff))
944       {
945           printk("qla1280 : Failed to reserved i/o region 0x%04lx-0x%04lx already in use\n",
946              host->io_port, host->io_port + 0xff);
947           free_irq(host->irq, NULL);
948           qla1280_mem_free(ha);
949           scsi_unregister(host);
950	     return 1;
951       }
952
953       request_region(host->io_port, 0xff, "qla1280");
954
955	return 0;
956}
957
958
959/**************************************************************************
960 *   qla1280_release
961 *   Free the passed in Scsi_Host memory structures prior to unloading the
962 *   module.
963 **************************************************************************/
964int
965qla1280_release(struct Scsi_Host *host)
966{
967    scsi_qla_host_t *ha = (scsi_qla_host_t *) host->hostdata;
968
969    ENTER("qla1280_release");
970
971    if( !ha->flags.online )
972        return(0);
973
974    /* turn-off interrupts on the card */
975    WRT_REG_WORD(&ha->iobase->ictrl, 0);
976
977    /* Detach interrupts */
978    if(host->irq)
979        free_irq(host->irq, ha);
980
981    /* release io space registers  */
982    if( host->io_port )
983        release_region(host->io_port, 0xff);
984
985#if MEMORY_MAPPED_IO
986    if(ha->mmpbase)
987    {
988#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
989        vfree((void *) (((unsigned long) ha->mmpbase) & PAGE_MASK));
990#else
991        iounmap((void *) (((unsigned long) ha->mmpbase) & PAGE_MASK));
992#endif
993    }
994#endif /* MEMORY_MAPPED_IO */
995    qla1280_mem_free(ha);
996
997    ENTER("qla1280_release");
998    return(0);
999}
1000
1001/**************************************************************************
1002 *   qla1280_info
1003 *     Return a string describing the driver.
1004 **************************************************************************/
1005const char *
1006qla1280_info(struct Scsi_Host *host)
1007{
1008    static char qla1280_buffer[125];
1009    char *bp;
1010    scsi_qla_host_t *ha;
1011    qla_boards_t   *bdp;
1012
1013    bp = &qla1280_buffer[0];
1014    ha = (scsi_qla_host_t *)host->hostdata;
1015    bdp = &QLBoardTbl[ha->devnum];
1016    memset(bp, 0, sizeof(qla1280_buffer));
1017    sprintf(bp,
1018            "QLogic %sPCI to SCSI Host Adapter: bus %d device %d irq %d\n"
1019            "       Firmware version: %2d.%02d.%02d, Driver version %s",
1020            (char *)&bdp->bdName[0], ha->pci_bus, (ha->pci_device_fn & 0xf8) >> 3, host->irq,
1021            bdp->fwver[0],bdp->fwver[1],bdp->fwver[2],
1022            QLA1280_VERSION);
1023    return(bp);
1024}
1025
1026/**************************************************************************
1027 *   qla1200_queuecommand
1028 *     Queue a command to the controller.
1029 *
1030 * Note:
1031 * The mid-level driver tries to ensures that queuecommand never gets invoked
1032 * concurrently with itself or the interrupt handler (although the
1033 * interrupt handler may call this routine as part of request-completion
1034 * handling).   Unfortunely, it sometimes calls the scheduler in interrupt
1035 * context which is a big NO! NO!.
1036 **************************************************************************/
1037int
1038qla1280_queuecommand(Scsi_Cmnd *cmd, void (*fn)(Scsi_Cmnd *))
1039{
1040    scsi_qla_host_t *ha;
1041    srb_t  *sp;
1042#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
1043    unsigned long cpu_flags = 0;
1044#endif
1045    struct Scsi_Host *host;
1046    uint32_t        b, t, l;
1047    scsi_lu_t       *q;
1048    u_long          handle;
1049
1050    ENTER("qla1280_queuecommand");
1051    COMTRACE('C')
1052
1053    host = cmd->host;
1054    ha = (scsi_qla_host_t *) host->hostdata;
1055
1056    /* send command to adapter */
1057    sp = (srb_t *) CMD_SP(cmd);
1058    sp->cmd =  cmd;
1059    cmd->scsi_done = fn;
1060    if (cmd->flags == 0)  /* new command */
1061    {
1062        sp->flags = 0;
1063    }
1064
1065    DEBUG5(qla1280_print_scsi_cmd(cmd));
1066
1067    /* Generate LU queue on bus, target, LUN */
1068    b = SCSI_BUS_32(cmd);
1069    t = SCSI_TCN_32(cmd);
1070    l = SCSI_LUN_32(cmd);
1071    if((q = LU_Q(ha, b, t, l)) == NULL )
1072    {
1073        DRIVER_LOCK
1074        if( (q = (scsi_lu_t *)KMALLOC(sizeof(struct scsi_lu))) )
1075        {
1076           LU_Q(ha, b, t, l) = q;
1077           BZERO(q,sizeof(struct scsi_lu));
1078           DEBUG(sprintf(debug_buff,"Allocate new device queue 0x%x\n",q));
1079           DEBUG(qla1280_print(debug_buff));
1080           DRIVER_UNLOCK
1081        }
1082        else
1083        {
1084            CMD_RESULT(cmd) = (int) (DID_BUS_BUSY << 16);
1085            qla1280_done_q_put(sp, &ha->done_q_first, &ha->done_q_last);
1086
1087            schedule_task(&ha->run_qla_bh);
1088            ha->flags.dpc_sched = TRUE;
1089            DRIVER_UNLOCK
1090            return(0);
1091        }
1092    }
1093    /* Set an invalid handle until we issue the command to ISP */
1094    /* then we will set the real handle value.                 */
1095    handle = INVALID_HANDLE;
1096    CMD_HANDLE(cmd) = (unsigned char *)handle;
1097
1098    /* Bookkeeping information */
1099    sp->r_start = jiffies;       /* time the request was received */
1100    sp->u_start = 0;
1101
1102    /* add the command to our queue */
1103    ha->qthreads++;
1104    qla1280_putq_t(q,sp);
1105
1106    DEBUG(sprintf(debug_buff,"qla1280_queuecmd: queue pid=%d, hndl=0x%x\n\r",cmd->pid,handle));
1107    DEBUG(qla1280_print(debug_buff));
1108
1109    /* send command to adapter */
1110    DRIVER_LOCK
1111        if (q->q_outcnt == 0)
1112            qla1280_restart_queues(ha);
1113    DRIVER_UNLOCK
1114
1115
1116    LEAVE("qla1280_queuecommand");
1117    return (0);
1118}
1119
1120/**************************************************************************
1121 *   qla1200_abort
1122 *     Abort the speciifed SCSI command(s).
1123 **************************************************************************/
1124int
1125qla1280_abort(Scsi_Cmnd *cmd)
1126{
1127    scsi_qla_host_t *ha;
1128    srb_t  *sp;
1129#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
1130    unsigned long cpu_flags = 0;
1131#endif
1132    struct Scsi_Host *host;
1133    uint32_t        b, t, l;
1134    scsi_lu_t       *q;
1135    int return_status = SCSI_ABORT_SUCCESS;
1136    int found = 0;
1137    int i;
1138    u_long     handle;
1139    u_short    data;
1140
1141    ENTER("qla1280_abort");
1142    COMTRACE('A')
1143    ha = (scsi_qla_host_t *) cmd->host->hostdata;
1144    host = cmd->host;
1145    DRIVER_LOCK
1146
1147    /* Get the SCSI request ptr */
1148    sp = (srb_t *) CMD_SP(cmd);
1149    handle = (u_long) CMD_HANDLE(cmd);
1150    if (qla1280_verbose)
1151        printk("scsi(%d): ABORT Command=0x%lx, handle=0x%lx\n",(int)ha->host_no,(long)cmd,handle);
1152
1153    /* Check for pending interrupts. */
1154    if( handle == 0L )
1155    {
1156    COMTRACE('a')
1157        /* we never got this command */
1158        printk(KERN_INFO "qla1280: Aborting a NULL handle\n");
1159        DRIVER_UNLOCK
1160                return(SCSI_ABORT_NOT_RUNNING);  /* no action - we don't have command */
1161    }
1162    data = qla1280_debounce_register(&ha->iobase->istatus);
1163    if( !(ha->flags.in_isr) && (data & RISC_INT) )
1164    {
1165        /* put any pending command in done queue */
1166        qla1280_isr(ha, (srb_t **)&ha->done_q_first, (srb_t **)&ha->done_q_last);
1167    }
1168
1169    handle = (u_long) CMD_HANDLE(cmd);
1170
1171    /* Generate LU queue on bus, target, LUN */
1172    b = SCSI_BUS_32(cmd);
1173    t = SCSI_TCN_32(cmd);
1174    l = SCSI_LUN_32(cmd);
1175    if((q = LU_Q(ha, b, t, l)) == NULL )
1176    {
1177    COMTRACE('a')
1178        /* No lun queue -- command must not be active */
1179        DRIVER_UNLOCK
1180        printk(KERN_WARNING "qla1280 (%d:%d:%d): No LUN queue for the specified device\n",(int)b,(int)t,(int)l);
1181        return(SCSI_ABORT_NOT_RUNNING);  /* no action - we don't have command */
1182    }
1183
1184#if AUTO_ESCALATE_ABORT
1185    if ( (sp->flags & SRB_ABORTED) )
1186    {
1187        DRIVER_UNLOCK
1188        DEBUG(qla1280_print("qla1280_abort: Abort escalayted - returning SCSI_ABORT_SNOOZE.\n\r"));
1189        return(SCSI_ABORT_SNOOZE);
1190    }
1191#endif
1192
1193    if ( (sp->flags & SRB_ABORT_PENDING) )
1194    {
1195    COMTRACE('a')
1196        DRIVER_UNLOCK
1197        if( qla1280_verbose  )
1198            printk("scsi(): Command has a pending abort message - ABORT_PENDING.\n");
1199        DEBUG(qla1280_print("qla1280: Command has a pending abort message - ABORT_PENDING.\n\r"));
1200        return(SCSI_ABORT_PENDING);
1201    }
1202
1203#if  STOP_ON_ABORT
1204    printk("Scsi layer issued a ABORT command= 0x%x\n",(int)cmd);
1205    DEBUG2(qla1280_print_scsi_cmd(cmd));
1206#endif
1207
1208    ha->flags.in_abort = TRUE;
1209    /*
1210    * Normally, would would need to search our queue for the specified command
1211    * but; since our sp contains the cmd ptr, we can just remove it from our
1212    * LUN queue.
1213    */
1214    if( !(sp->flags&SRB_SENT) )
1215    {
1216        found++;
1217        if( qla1280_verbose  )
1218            printk("scsi(): Command returned from queue aborted.\n");
1219        DEBUG(qla1280_print("qla1280: Command returned from queue aborted.\n\r"));
1220        /* Remove srb from SCSI LU queue. */
1221        qla1280_removeq(q, sp);
1222        sp->flags |=  SRB_ABORTED;
1223        CMD_RESULT(cmd) = DID_ABORT << 16;
1224        qla1280_done_q_put(sp, &ha->done_q_first, &ha->done_q_last);
1225        return_status = SCSI_ABORT_SUCCESS;
1226    }
1227    else
1228    {  /* find the command in our active list */
1229        for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++)
1230        {
1231            if( sp == ha->outstanding_cmds[i] )
1232            {
1233                found++;
1234                DEBUG(qla1280_print("qla1280: RISC aborting command.\n\r"));
1235                qla1280_abort_command(ha,sp);
1236                return_status = SCSI_ABORT_PENDING;
1237                break;
1238            }
1239        }
1240    }
1241
1242#if  STOP_ON_ABORT
1243    qla1280_panic("qla1280_abort",ha->host);
1244#endif
1245    if ( found == 0 )
1246        return_status = SCSI_ABORT_NOT_RUNNING;  /* no action - we don't have command */
1247
1248    DEBUG(sprintf(debug_buff, "qla1280_abort: Aborted status returned = 0x%x.\n\r",return_status));
1249    DEBUG(qla1280_print(debug_buff));
1250
1251    if( ha->done_q_first )
1252       qla1280_done(ha, (srb_t **)&ha->done_q_first, (srb_t **)&ha->done_q_last);
1253    if ( found )
1254    {
1255        qla1280_restart_queues(ha);
1256    }
1257    ha->flags.in_abort = FALSE;
1258    DRIVER_UNLOCK
1259
1260    LEAVE("qla1280_abort");
1261    COMTRACE('a')
1262    return(return_status);
1263}
1264
1265/**************************************************************************
1266 * qla1200_reset
1267 *    The reset function will reset the SCSI bus and abort any executing
1268 *    commands.
1269 *
1270 * Input:
1271 *      cmd = Linux SCSI command packet of the command that cause the
1272 *            bus reset.
1273 *      flags = SCSI bus reset option flags (see scsi.h)
1274 *
1275 * Returns:
1276 *      DID_RESET in cmd.host_byte of aborted command(s)
1277 *
1278 * Note:
1279 *      Resetting the bus always succeeds - is has to, otherwise the
1280 *      kernel will panic! Try a surgical technique - sending a BUS
1281 *      DEVICE RESET message - on the offending target before pulling
1282 *      the SCSI bus reset line.
1283 **************************************************************************/
1284int
1285qla1280_reset(Scsi_Cmnd *cmd, unsigned int flags)
1286{
1287    scsi_qla_host_t *ha;
1288#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
1289    unsigned long cpu_flags = 0;
1290#endif
1291    uint32_t        b, t, l;
1292    srb_t  *sp;
1293    typedef enum
1294    {
1295        ABORT_DEVICE = 1,
1296                DEVICE_RESET = 2,
1297                BUS_RESET    = 3,
1298                ADAPTER_RESET= 4,
1299                RESET_DELAYED= 5,
1300                FAIL         = 6
1301    } action_t;
1302    action_t     action = ADAPTER_RESET;
1303    u_short    data;
1304    scsi_lu_t       *q;
1305    int result;
1306
1307
1308    ENTER("qla1280_reset");
1309    COMTRACE('R')
1310    if (qla1280_verbose)
1311        printk("scsi(): Resetting Cmnd=0x%lx, Handle=0x%lx, flags=0x%x\n",(long)cmd,(long)CMD_HANDLE(cmd),flags);
1312    if ( cmd == NULL )
1313    {
1314        printk(KERN_WARNING "(scsi?:?:?:?) Reset called with NULL Scsi_Cmnd "
1315                "pointer, failing.\n");
1316        return(SCSI_RESET_SNOOZE);
1317    }
1318    ha = (scsi_qla_host_t *) cmd->host->hostdata;
1319    sp = (srb_t *) CMD_SP(cmd);
1320
1321#if  STOP_ON_RESET
1322    qla1280_panic("qla1280_reset",ha->host);
1323#endif
1324
1325    DRIVER_LOCK
1326    /* Check for pending interrupts. */
1327    data = qla1280_debounce_register(&ha->iobase->istatus);
1328    if( !(ha->flags.in_isr) && (data & RISC_INT) )
1329    {
1330        qla1280_isr(ha, (srb_t **)&ha->done_q_first, (srb_t **)&ha->done_q_last);
1331    }
1332    DRIVER_UNLOCK
1333
1334    /*
1335    * Determine the suggested action that the mid-level driver wants
1336    * us to perform.
1337    */
1338    if( CMD_HANDLE(cmd) == (unsigned char *) 0  )
1339    {
1340        /*
1341        * if mid-level driver called reset with a orphan SCSI_Cmnd
1342        * (i.e. a command that's not pending ), so perform the
1343        * function specified.
1344        */
1345        if( (flags & SCSI_RESET_SUGGEST_HOST_RESET) )
1346            action = ADAPTER_RESET;
1347        else
1348            action = BUS_RESET;
1349    }
1350    else
1351    { /*
1352        * Mid-level driver has called reset with this SCSI_Cmnd and
1353        * its pending.
1354        */
1355        if( flags & SCSI_RESET_SUGGEST_HOST_RESET )
1356            action = ADAPTER_RESET;
1357        else if( flags & SCSI_RESET_SUGGEST_BUS_RESET )
1358            action = BUS_RESET;
1359        else
1360            action = DEVICE_RESET;
1361    }
1362
1363    b = SCSI_BUS_32(cmd);
1364    t = SCSI_TCN_32(cmd);
1365    l = SCSI_LUN_32(cmd);
1366    q = LU_Q(ha, b, t, l);
1367
1368#if AUTO_ESCALATE_RESET
1369    if ( (action & DEVICE_RESET) && (q->q_flag & QLA1280_QRESET) )
1370    {
1371        printk(KERN_INFO "qla1280(%d): Bus device reset already sent to " "device, escalating.\n", (int)ha->host_no);
1372        action = BUS_RESET;
1373    }
1374    if ( (action & DEVICE_RESET) && (sp->flags & SRB_ABORT_PENDING) )
1375    {
1376        printk(KERN_INFO "qla1280(%d):Have already attempted to reach " "device with abort device\n", (int)ha->host_no);
1377        printk(KERN_INFO "qla1280(%d):message, will escalate to BUS " "RESET.\n",(int) ha->host_no);
1378        action = BUS_RESET;
1379    }
1380#endif
1381
1382    /*
1383    *  By this point, we want to already know what we are going to do,
1384    *  so we only need to perform the course of action.
1385    */
1386    DRIVER_LOCK
1387    result = SCSI_RESET_ERROR;
1388    switch (action)
1389    {
1390        case FAIL:
1391            break;
1392
1393        case RESET_DELAYED:
1394            result = SCSI_RESET_PENDING;
1395            break;
1396
1397        case ABORT_DEVICE:
1398            ha->flags.in_reset = TRUE;
1399            if (qla1280_verbose)
1400                printk(KERN_INFO "scsi(%d:%d:%d:%d): Queueing abort device command.\n", (int)ha->host_no,(int)b,(int)t,(int)l);
1401            qla1280_abort_queue_single(ha,b,t,l,DID_ABORT);
1402            if( qla1280_abort_device(ha, b, t, l) == 0)
1403                result = SCSI_RESET_PENDING;
1404            break;
1405
1406        case DEVICE_RESET:
1407            if (qla1280_verbose)
1408                printk(KERN_INFO "scsi(%d:%d:%d:%d): Queueing device reset command.\n",(int) ha->host_no,(int)b,(int)t,(int)l);
1409            ha->flags.in_reset = TRUE;
1410            for (l = 0; l < MAX_LUNS; l++)
1411                qla1280_abort_queue_single(ha,b,t,l,DID_ABORT);
1412            if( qla1280_device_reset(ha, b, t) == 0 )
1413                result = SCSI_RESET_PENDING;
1414            q->q_flag |= QLA1280_QRESET;
1415            break;
1416
1417        case BUS_RESET:
1418                if (qla1280_verbose)
1419                    printk(KERN_INFO "qla1280(%d:%d:%d:%d): Issuing BUS DEVICE RESET.\n",(int) ha->host_no,(int)b,(int)t,(int)l);
1420            ha->flags.in_reset = TRUE;
1421            for (t = 0; t < MAX_TARGETS; t++)
1422                for (l = 0; l < MAX_LUNS; l++)
1423                    qla1280_abort_queue_single(ha,b,t,l,DID_RESET);
1424                qla1280_bus_reset(ha, b);
1425                /*
1426                * The bus reset routine returns all the outstanding commands back
1427                * with "DID_RESET" in the status field after a short delay
1428                * by the firmware. If the mid-level time out the SCSI reset before
1429                * our delay we may need to ignore it.
1430                */
1431                /* result = SCSI_RESET_PENDING | SCSI_RESET_BUS_RESET; */
1432                result = SCSI_RESET_SUCCESS | SCSI_RESET_BUS_RESET;
1433                mdelay(4 * 1000); barrier();
1434                if( flags & SCSI_RESET_SYNCHRONOUS )
1435                {
1436                  CMD_RESULT(cmd) = (int) (DID_BUS_BUSY << 16);
1437                  (*(cmd)->scsi_done)(cmd);
1438                }
1439                /* ha->reset_start = jiffies; */
1440                break;
1441
1442            case ADAPTER_RESET:
1443            default:
1444                if (qla1280_verbose)
1445                {
1446                    printk(KERN_INFO "scsi(%d:%d:%d:%d): Issued an ADAPTER RESET.\n",(int) ha->host_no,(int)b,(int)t,(int)l);
1447                    printk(KERN_INFO "scsi(%d:%d:%d:%d): I/O processing will continue automatically.\n",(int) ha->host_no,(int)b,(int)t,(int)l);
1448                }
1449                ha->flags.reset_active = TRUE;
1450                /*
1451                * We restarted all of the commands automatically, so the mid-level code can expect
1452                * completions momentitarily.
1453                */
1454                if( qla1280_abort_isp(ha) == 0 )
1455                    result = SCSI_RESET_SUCCESS | SCSI_RESET_HOST_RESET;
1456
1457                        ha->flags.reset_active = FALSE;
1458    }
1459
1460    if( ha->done_q_first )
1461        qla1280_done(ha, (srb_t **)&ha->done_q_first, (srb_t **)&ha->done_q_last);
1462    qla1280_restart_queues(ha);
1463    ha->flags.in_reset = FALSE;
1464
1465DRIVER_UNLOCK
1466    DEBUG(printk("RESET returning %d\n", result));
1467
1468    COMTRACE('r')
1469    LEAVE("qla1280_reset");
1470    return( result );
1471}
1472
1473/**************************************************************************
1474 * qla1200_biosparam
1475 *   Return the disk geometry for the given SCSI device.
1476 **************************************************************************/
1477int
1478qla1280_biosparam(Disk *disk, kdev_t dev, int geom[])
1479{
1480    int heads, sectors, cylinders;
1481
1482            heads = 64;
1483    sectors = 32;
1484    cylinders = disk->capacity / (heads * sectors);
1485    if (cylinders > 1024)
1486    {
1487        heads = 255;
1488        sectors = 63;
1489        cylinders = disk->capacity / (heads * sectors);
1490        /* if (cylinders > 1023)
1491        cylinders = 1023; */
1492    }
1493
1494    geom[0] = heads;
1495    geom[1] = sectors;
1496    geom[2] = cylinders;
1497
1498    return (0);
1499}
1500/**************************************************************************
1501 * qla1280_intr_handler
1502 *   Handles the H/W interrupt
1503 **************************************************************************/
1504void qla1280_intr_handler(int irq, void *dev_id, struct pt_regs *regs)
1505{
1506#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,95)
1507    unsigned long cpu_flags = 0;
1508#endif
1509    scsi_qla_host_t *ha;
1510    u_short    data;
1511    device_reg_t *reg;
1512
1513    ENTER_INTR("qla1280_intr_handler");
1514    COMTRACE('I')
1515    ha = (scsi_qla_host_t *) dev_id;
1516    if(!ha)
1517    {
1518        printk(KERN_INFO "scsi(): Interrupt with NULL host ptr\n");
1519        COMTRACE('X')
1520        return;
1521    }
1522#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,95)
1523    spin_lock_irqsave(&io_request_lock, cpu_flags);
1524    if(test_and_set_bit(QLA1280_IN_ISR_BIT, &ha->flags))
1525    {
1526        COMTRACE('X')
1527        spin_unlock_irqrestore(&io_request_lock, cpu_flags);
1528        return;
1529    }
1530    ha->isr_count++;
1531    reg = ha->iobase;
1532     /* disable our interrupt. */
1533    WRT_REG_WORD(&reg->ictrl, 0);
1534    data = qla1280_debounce_register(&reg->istatus);
1535    /* Check for pending interrupts. */
1536    if ( !(data & RISC_INT) )
1537    {
1538        /* spurious interrupts can happen legally */
1539        DEBUG(printk("scsi(%d): Spurious interrupt - ignoring\n",(int)ha->host_no));
1540        COMTRACE('X')
1541    }
1542    else
1543      qla1280_isr(ha, (srb_t **)&ha->done_q_first, (srb_t **)&ha->done_q_last);
1544    if (ha->done_q_first)
1545        qla1280_done(ha, (srb_t **)&ha->done_q_first, (srb_t **)&ha->done_q_last);
1546
1547    clear_bit(QLA1280_IN_ISR_BIT, &ha->flags);
1548    spin_unlock_irqrestore(&io_request_lock, cpu_flags);
1549#else  /* LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95) */
1550
1551    if( test_bit(QLA1280_IN_ISR_BIT, (int *)&ha->flags) )
1552    {
1553          COMTRACE('X')
1554          printk(KERN_INFO "scsi(%d): Already in interrupt - returning \n", (int)ha->host_no);
1555          return;
1556    }
1557    set_bit(QLA1280_IN_ISR_BIT, (int *)&ha->flags);
1558    ha->isr_count++;
1559    reg = ha->iobase;
1560     /* disable our interrupt. */
1561    WRT_REG_WORD(&reg->ictrl, 0);
1562
1563    data = qla1280_debounce_register(&reg->istatus);
1564    /* Check for pending interrupts. */
1565    if ( !(data & RISC_INT) )
1566    {
1567        /* spurious interrupts can happen legally */
1568        DEBUG(printk("scsi(%d): Spurious interrupt - ignoring\n",(int)ha->host_no));
1569        COMTRACE('X')
1570    }
1571    else
1572     qla1280_isr(ha, (srb_t **)&ha->done_q_first, (srb_t **)&ha->done_q_last);
1573
1574    /* if no work to do then call the SCSI mid-level right away */
1575    if( ha->done_q_first )
1576        qla1280_done(ha, (srb_t **)&ha->done_q_first, (srb_t **)&ha->done_q_last);
1577
1578    /* Schedule the DPC routine */
1579    if (ha->flags.isp_abort_needed || ha->flags.reset_marker ||
1580            ha->done_q_first        )
1581        {
1582            ha->run_qla_bh.data = (void *) ha;
1583            ha->run_qla_bh.routine = qla1280_do_dpc;
1584
1585             COMTRACE('P')
1586            schedule_task(&ha->run_qla_bh);
1587            ha->flags.dpc_sched = TRUE;
1588        }
1589        clear_bit(QLA1280_IN_ISR_BIT, (int *)&ha->flags);
1590#endif
1591     /* enable our interrupt. */
1592        WRT_REG_WORD(&reg->ictrl, ISP_EN_INT + ISP_EN_RISC);
1593
1594        COMTRACE('i')
1595        LEAVE_INTR("qla1280_intr_handler");
1596}
1597
1598/**************************************************************************
1599 *   qla1280_do_dpc
1600 *
1601 * Description:
1602 * This routine is a task that is schedule by the interrupt handler
1603 * to perform the background processing for interrupts.  We put it
1604 * on a task queue that is consumed whenever the scheduler runs; that's
1605 * so you can do anything (i.e. put the process to sleep etc).  In fact, the
1606 * mid-level tries to sleep when it reaches the driver threshold
1607 * "host->can_queue". This can cause a panic if we were in our interrupt
1608 * code .
1609 **************************************************************************/
1610static void qla1280_do_dpc(void *p)
1611{
1612    scsi_qla_host_t *ha = (scsi_qla_host_t *) p;
1613#if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,95)
1614    unsigned long cpu_flags = 0;
1615#endif
1616
1617    COMTRACE('p')
1618#if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,95)
1619    spin_lock_irqsave(&io_request_lock, cpu_flags);
1620#endif
1621    if (ha->flags.isp_abort_needed)
1622        qla1280_abort_isp(ha);
1623
1624    if (ha->flags.reset_marker)
1625        qla1280_rst_aen(ha);
1626
1627    if (ha->done_q_first)
1628        qla1280_done(ha, (srb_t **)&ha->done_q_first, (srb_t **)&ha->done_q_last);
1629    ha->flags.dpc_sched = FALSE;
1630#if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,95)
1631    spin_unlock_irqrestore(&io_request_lock, cpu_flags);
1632#endif
1633}
1634
1635/**************************************************************************
1636 *   qla1280_device_queue_depth
1637 *
1638 * Description:
1639 *   Determines the queue depth for a given device.  There are two ways
1640 *   a queue depth can be obtained for a tagged queueing device.  One
1641 *   way is the default queue depth which is determined by whether
1642 *   If it is defined, then it is used
1643 *   as the default queue depth.  Otherwise, we use either 4 or 8 as the
1644 *   default queue depth (dependent on the number of hardware SCBs).
1645 **************************************************************************/
1646STATIC void qla1280_device_queue_depth(scsi_qla_host_t *p, Scsi_Device *device)
1647{
1648    int default_depth = 3;
1649    int bus = device->channel;
1650    int target = device->id;
1651
1652    device->queue_depth = default_depth;
1653
1654    if (device->tagged_supported &&
1655        (p->bus_settings[bus].qtag_enables & (BIT_0 << target)) )
1656    {
1657        device->tagged_queue = 1;
1658        device->current_tag = 0;
1659        device->queue_depth = p->bus_settings[bus].hiwat;
1660        /* device->queue_depth = 20; */
1661        printk(KERN_INFO "scsi(%d:%d:%d:%d): Enabled tagged queuing, queue depth %d.\n",
1662                (int)p->host_no, device->channel, device->id,
1663                device->lun, device->queue_depth);
1664    }
1665    qla12160_get_target_parameters(p, bus, target, device->lun);
1666
1667}
1668
1669/**************************************************************************
1670 *   qla1280_select_queue_depth
1671 *
1672 *   Sets the queue depth for each SCSI device hanging off the input
1673 *   host adapter.  We use a queue depth of 2 for devices that do not
1674 *   support tagged queueing.
1675 **************************************************************************/
1676STATIC void
1677qla1280_select_queue_depth(struct Scsi_Host *host, Scsi_Device *scsi_devs)
1678{
1679    Scsi_Device *device;
1680    scsi_qla_host_t  *p = (scsi_qla_host_t *) host->hostdata;
1681
1682    ENTER("qla1280_select_queue_depth");
1683    for (device = scsi_devs; device != NULL; device = device->next)
1684    {
1685        if (device->host == host)
1686            qla1280_device_queue_depth(p, device);
1687    }
1688    LEAVE("qla1280_select_queue_depth");
1689}
1690
1691/*--------------------------**
1692** Driver Support Routines  **
1693**--------------------------*/
1694
1695#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
1696/*
1697 * mdelay
1698 *      Delay in milliseconds
1699 *
1700 * Input:
1701 *      milliseconds  = delay
1702 */
1703STATIC inline void mdelay(int milliseconds)
1704{
1705    int i;
1706
1707    for(i=0; i<milliseconds; i++)
1708        udelay(1000);
1709}
1710#endif
1711
1712/*
1713 * qla1280_done
1714 *      Process completed commands.
1715 *
1716 * Input:
1717 *      ha           = adapter block pointer.
1718 *      done_q_first = done queue first pointer.
1719 *      done_q_last  = done queue last pointer.
1720 */
1721STATIC void
1722qla1280_done(scsi_qla_host_t *ha, srb_t **done_q_first, srb_t **done_q_last)
1723{
1724    srb_t           *sp;
1725    scsi_lu_t       *q;
1726    uint32_t        b, t, l;
1727    Scsi_Cmnd  *cmd;
1728#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
1729    unsigned long cpu_flags = 0;
1730#endif
1731
1732    ENTER("qla1280_done");
1733    COMTRACE('D')
1734
1735    DRIVER_LOCK
1736    while (*done_q_first !=  NULL)
1737    {
1738        /* remove command from done list */
1739                sp = *done_q_first;
1740        if (!(*done_q_first = sp->s_next))
1741            *done_q_last = NULL;
1742        else
1743            (*done_q_first)->s_prev = NULL;
1744                cmd = sp->cmd;
1745        b = SCSI_BUS_32(cmd);
1746        t = SCSI_TCN_32(cmd);
1747        l = SCSI_LUN_32(cmd);
1748        q = LU_Q(ha, b, t, l);
1749
1750        /* Decrement outstanding commands on device. */
1751        if (q->q_outcnt)
1752            q->q_outcnt--;
1753        if (q->q_outcnt < ha->bus_settings[b].hiwat)
1754        {
1755            q->q_flag &= ~QLA1280_QBUSY;
1756        }
1757
1758        q->resp_time += jiffies - sp->r_start;                /* Lun bookkeeping information */
1759        q->act_time += jiffies - sp->u_start;
1760        q->io_cnt++;
1761        if( sp->dir & BIT_5 )
1762         q->r_cnt++;
1763        else
1764         q->w_cnt++;
1765
1766        switch ( (CMD_RESULT(cmd)>>16))
1767        {
1768            case DID_RESET:
1769                q->q_flag &= ~QLA1280_QRESET;
1770                /* Issue marker command. */
1771                qla1280_marker(ha, b, t, 0, MK_SYNC_ID);
1772                break;
1773            case DID_ABORT:
1774                sp->flags &= ~SRB_ABORT_PENDING;
1775                sp->flags |= SRB_ABORTED;
1776                if (sp->flags & SRB_TIMEOUT)
1777                    CMD_RESULT(sp->cmd)= DID_TIME_OUT << 16;
1778                break;
1779            default:
1780                break;
1781        }
1782
1783        /* Call the mid-level driver interrupt handler */
1784        CMD_HANDLE(sp->cmd) = (unsigned char *) 0;
1785        ha->actthreads--;
1786#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
1787        sti();
1788        (*(cmd)->scsi_done)(cmd);
1789        cli();
1790#else
1791        (*(cmd)->scsi_done)(cmd);
1792#endif
1793        qla1280_next(ha, q, b);
1794    }
1795    DRIVER_UNLOCK
1796
1797
1798    COMTRACE('d')
1799    LEAVE("qla1280_done");
1800}
1801
1802/*
1803 * Translates a ISP error to a Linux SCSI error
1804 */
1805STATIC int qla1280_return_status( sts_entry_t *sts, Scsi_Cmnd       *cp)
1806{
1807    int host_status = DID_ERROR;
1808#if DEBUG_QLA1280_INTR
1809    STATIC char *reason[] =
1810    {
1811        "DID_OK",
1812                "DID_NO_CONNECT",
1813                "DID_BUS_BUSY",
1814                "DID_TIME_OUT",
1815                "DID_BAD_TARGET",
1816                "DID_ABORT",
1817                "DID_PARITY",
1818                "DID_ERROR",
1819                "DID_RESET",
1820                "DID_BAD_INTR"
1821    };
1822#endif /* DEBUG_QLA1280_INTR */
1823
1824    ENTER("qla1280_return_status");
1825
1826#if DEBUG_QLA1280_INTR
1827    /*
1828    DEBUG(printk("qla1280_return_status: compl status = 0x%04x\n", sts->comp_status));
1829    */
1830#endif
1831    switch(sts->comp_status)
1832    {
1833        case CS_COMPLETE:
1834            host_status = DID_OK;
1835            break;
1836        case CS_INCOMPLETE:
1837            if (!(sts->state_flags & SF_GOT_BUS))
1838                host_status = DID_NO_CONNECT;
1839            else if (!(sts->state_flags & SF_GOT_TARGET))
1840                host_status = DID_BAD_TARGET;
1841            else if (!(sts->state_flags & SF_SENT_CDB))
1842                host_status = DID_ERROR;
1843            else if (!(sts->state_flags & SF_TRANSFERRED_DATA))
1844                host_status = DID_ERROR;
1845            else if (!(sts->state_flags & SF_GOT_STATUS))
1846                host_status = DID_ERROR;
1847            else if (!(sts->state_flags & SF_GOT_SENSE))
1848                host_status = DID_ERROR;
1849            break;
1850        case CS_RESET:
1851            host_status = DID_RESET;
1852            break;
1853        case CS_ABORTED:
1854            host_status = DID_ABORT;
1855            break;
1856        case CS_TIMEOUT:
1857            host_status = DID_TIME_OUT;
1858            break;
1859        case CS_DATA_OVERRUN:
1860#ifdef QL_DEBUG_LEVEL_2
1861            printk("Data overrun 0x%x\n",(int)sts->residual_length);
1862            qla1280_print(
1863                        "\n\rqla1280_isr: response packet data\n\r");
1864                        qla1280_dump_buffer((caddr_t)sts,
1865                        RESPONSE_ENTRY_SIZE);
1866#endif
1867            host_status = DID_ERROR;
1868            break;
1869        case CS_DATA_UNDERRUN:
1870            if ( (CMD_XFRLEN(cp) - sts->residual_length) < cp->underflow)
1871            {
1872              printk("scsi: Underflow detected - retrying command.\n");
1873              host_status = DID_ERROR;
1874            }
1875            else
1876                host_status = DID_OK;
1877            break;
1878        default:
1879            host_status = DID_ERROR;
1880            break;
1881    }
1882
1883#if DEBUG_QLA1280_INTR
1884    sprintf(debug_buff, "qla1280 ISP status: host status (%s) scsi status %x\n\r", reason[host_status], sts->scsi_status);
1885    qla1280_print(debug_buff);
1886#endif
1887
1888    LEAVE("qla1280_return_status");
1889
1890    return (sts->scsi_status & 0xff) | (host_status << 16);
1891}
1892
1893/*
1894 * qla1280_done_q_put
1895 *      Place SRB command on done queue.
1896 *
1897 * Input:
1898 *      sp           = srb pointer.
1899 *      done_q_first = done queue first pointer.
1900 *      done_q_last  = done queue last pointer.
1901 */
1902STATIC void
1903qla1280_done_q_put(srb_t *sp, srb_t **done_q_first, srb_t **done_q_last)
1904{
1905#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
1906    unsigned long cpu_flags = 0;
1907#endif
1908#ifdef QL_DEBUG_LEVEL_3
1909    ENTER("qla1280_put_done_q");
1910#endif
1911    /* Place block on done queue */
1912    DRIVER_LOCK
1913            sp->s_next = NULL;
1914    sp->s_prev = *done_q_last;
1915    if (!*done_q_first)
1916        *done_q_first = sp;
1917    else
1918        (*done_q_last)->s_next = sp;
1919    *done_q_last = sp;
1920
1921    DRIVER_UNLOCK
1922#ifdef QL_DEBUG_LEVEL_3
1923            LEAVE("qla1280_put_done_q");
1924#endif
1925}
1926
1927/*
1928 * qla1280_next
1929 *      Retrieve and process next job in the queue.
1930 *
1931 * Input:
1932 *      ha = adapter block pointer.
1933 *      q  = SCSI LU pointer.
1934 *      b  = SCSI bus number.
1935 *      SCSI_LU_Q lock must be already obtained and no other locks.
1936 *
1937 * Output:
1938 *      Releases SCSI_LU_Q upon exit.
1939 */
1940STATIC void
1941qla1280_next(scsi_qla_host_t *ha, scsi_lu_t *q, uint8_t b)
1942{
1943    srb_t   *sp;
1944    uint32_t cnt;
1945    uint8_t status;
1946#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
1947    unsigned long cpu_flags = 0;
1948#endif
1949
1950    ENTER("qla1280_next");
1951
1952    DRIVER_LOCK
1953    while ( ((sp = q->q_first) != NULL) &&     /* we have a queue pending */
1954        !(q->q_flag &  QLA1280_QBUSY) &&      /* device not busy */
1955        !ha->flags.abort_isp_active &&      /* not resetting the adapter */
1956        !(q->q_flag & QLA1280_QSUSP) )      /* device not suspended */
1957    {
1958        /* Remove srb from SCSI LU queue. */
1959        qla1280_removeq(q, sp);
1960
1961        DEBUG(sprintf(debug_buff,"starting request 0x%p<-(0x%p)\n\r",q,sp));
1962        DEBUG(qla1280_print(debug_buff));
1963        {
1964            /* Set busy flag if reached high water mark. */
1965            q->q_outcnt++;
1966            if (q->q_outcnt >= ha->bus_settings[b].hiwat)
1967                q->q_flag |= QLA1280_QBUSY;
1968
1969#if  QLA1280_64BIT_SUPPORT
1970            if (ha->flags.enable_64bit_addressing)
1971                status = qla1280_64bit_start_scsi(ha, sp);
1972            else
1973#endif
1974                status = qla1280_32bit_start_scsi(ha, sp);
1975
1976            if (status)  /* if couldn't start the request */
1977            {
1978                if (q->q_outcnt == 1)
1979                {
1980                    /* Release SCSI LU queue specific lock */
1981                    QLA1280_SCSILU_UNLOCK(q);
1982
1983                    /* Wait for 30 sec for command to be accepted. */
1984                    for (cnt = 6000000; cnt; cnt--)
1985                    {
1986#if  QLA1280_64BIT_SUPPORT
1987                        if (ha->flags.enable_64bit_addressing)
1988                            status = qla1280_64bit_start_scsi(ha, sp);
1989                        else
1990#endif
1991                            status = qla1280_32bit_start_scsi(ha, sp);
1992
1993                        if (!status)
1994                        {
1995                            break;
1996                        }
1997
1998                        /* Go check for pending interrupts. */
1999                        qla1280_poll(ha);
2000
2001                        SYS_DELAY(5);  /* 10 */
2002                    }
2003                    if (!cnt)
2004                    {
2005                        /* Set timeout status */
2006                        CMD_RESULT(sp->cmd) = DID_TIME_OUT << 16;
2007
2008#if WATCHDOGTIMER
2009                        /* Remove command from watchdog queue. */
2010                        if (sp->flags & SRB_WATCHDOG)
2011                            qla1280_timeout_remove(ha, sp);
2012#endif
2013                        COMTRACE('M')
2014                        CMD_HANDLE(sp->cmd) = (unsigned char *) 0;
2015
2016                        /* Call the mid-level driver interrupt handler */
2017#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
2018                        sti();
2019                        (*(sp->cmd)->scsi_done)(sp->cmd);
2020                        cli();
2021#else
2022                        (*(sp->cmd)->scsi_done)(sp->cmd);
2023#endif
2024
2025                        /* Acquire LU queue specific lock */
2026                        QLA1280_SCSILU_LOCK(q);
2027
2028                        if (q->q_outcnt)
2029                            q->q_outcnt--;
2030                    }
2031                    else
2032                        /* Acquire LU queue specific lock */
2033                        QLA1280_SCSILU_LOCK(q);
2034                }
2035                else
2036                {   /* Place request back on top of device queue. */
2037                    qla1280_putq_t(q, sp);
2038
2039                    if (q->q_outcnt)
2040                        q->q_outcnt--;
2041                    if (q->q_outcnt < ha->bus_settings[b].hiwat)
2042                        q->q_flag &= ~QLA1280_QBUSY;
2043                    break;
2044                }
2045            }
2046        }
2047    }
2048   DRIVER_UNLOCK
2049
2050    /* Release SCSI LU queue specific lock */
2051    QLA1280_SCSILU_UNLOCK(q);
2052
2053    LEAVE("qla1280_next");
2054}
2055
2056/*
2057 * qla1280_putq_t
2058 *      Add the standard SCB job to the top of standard SCB commands.
2059 *
2060 * Input:
2061 *      q  = SCSI LU pointer.
2062 *      sp = srb pointer.
2063 *      SCSI_LU_Q lock must be already obtained.
2064 */
2065STATIC void
2066qla1280_putq_t(scsi_lu_t *q, srb_t *sp)
2067{
2068    srb_t *srb_p;
2069#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
2070    unsigned long cpu_flags = 0;
2071#endif
2072
2073#ifdef QL_DEBUG_LEVEL_3
2074    ENTER("qla1280_putq_t");
2075#endif
2076    DRIVER_LOCK
2077    DEBUG(sprintf(debug_buff,"Adding to device 0x%p<-(0x%p)\n\r",q,sp));
2078    DEBUG(qla1280_print(debug_buff));
2079    sp->s_next = NULL;
2080    if (!q->q_first)                  /* If queue empty */
2081    {
2082        sp->s_prev = NULL;
2083        q->q_first = sp;
2084        q->q_last = sp;
2085    }
2086    else
2087    {
2088        srb_p = q->q_first;
2089        while (srb_p )
2090            srb_p = srb_p->s_next;
2091
2092        if (srb_p)
2093        {
2094            sp->s_prev = srb_p->s_prev;
2095            if (srb_p->s_prev)
2096                srb_p->s_prev->s_next = sp;
2097            else
2098                q->q_first = sp;
2099            srb_p->s_prev = sp;
2100            sp->s_next = srb_p;
2101        }
2102        else
2103        {
2104            sp->s_prev = q->q_last;
2105            q->q_last->s_next = sp;
2106            q->q_last = sp;
2107        }
2108    }
2109
2110    DRIVER_UNLOCK
2111#ifdef QL_DEBUG_LEVEL_3
2112            LEAVE("qla1280_putq_t");
2113#endif
2114}
2115
2116/*
2117 * qla1280_removeq
2118 *      Function used to remove a command block from the
2119 *      LU queue.
2120 *
2121 * Input:
2122 *      q  = SCSI LU pointer.
2123 *      sp = srb pointer.
2124 *      SCSI_LU_Q lock must be already obtained.
2125 */
2126STATIC void
2127qla1280_removeq(scsi_lu_t *q, srb_t *sp)
2128{
2129#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
2130    unsigned long cpu_flags = 0;
2131#endif
2132    DEBUG(sprintf(debug_buff,"Removing from device_q (0x%p)->(0x%p)\n\r",q,sp));
2133    DEBUG(qla1280_print(debug_buff));
2134    DRIVER_LOCK
2135            if (sp->s_prev)
2136    {
2137        if ((sp->s_prev->s_next = sp->s_next) != NULL)
2138            sp->s_next->s_prev = sp->s_prev;
2139        else
2140            q->q_last = sp->s_prev;
2141    }
2142    else if (!(q->q_first = sp->s_next))
2143        q->q_last = NULL;
2144    else
2145        q->q_first->s_prev = NULL;
2146    DRIVER_UNLOCK
2147}
2148
2149/*
2150* qla1280_mem_alloc
2151*      Allocates adapter memory.
2152*
2153* Returns:
2154*      0  = success.
2155*      1  = failure.
2156*/
2157STATIC uint8_t
2158qla1280_mem_alloc(scsi_qla_host_t *ha)
2159{
2160
2161	uint8_t   status = 1;
2162
2163#ifdef QL_DEBUG_LEVEL_3
2164	ENTER("qla1280_mem_alloc");
2165#endif
2166
2167#ifdef DYNAMIC_MEM_ALLOC
2168	ha->request_ring = qla1280_alloc_phys(REQUEST_ENTRY_SIZE * REQUEST_ENTRY_CNT,
2169	&ha->request_dma);
2170	if(ha->request_ring) {
2171		ha->response_ring = qla1280_alloc_phys(RESPONSE_ENTRY_SIZE * RESPONSE_ENTRY_CNT,
2172		&ha->response_dma);
2173		if(ha->response_ring) {
2174			status = 0;
2175		}
2176	}
2177#else
2178	ha->request_ring = &ha->req[0];
2179	ha->request_dma = VIRT_TO_BUS(&ha->req[0]);
2180	ha->response_ring = &ha->res[0];
2181	ha->response_dma = VIRT_TO_BUS(&ha->res[0]);
2182	status = 0;
2183#endif
2184
2185	if(status) {
2186#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
2187		qla1280_print("qla1280_mem_alloc: **** FAILED ****\n");
2188#endif
2189	}
2190#ifdef QL_DEBUG_LEVEL_3
2191	else
2192		LEAVE("qla1280_mem_alloc");
2193#endif
2194	return(status);
2195}
2196
2197/*
2198 * qla1280_mem_free
2199 *      Frees adapter allocated memory.
2200 *
2201 * Input:
2202 *      ha = adapter block pointer.
2203 */
2204STATIC void
2205qla1280_mem_free(scsi_qla_host_t *ha)
2206{
2207    scsi_lu_t *q;
2208    uint32_t  b, t, l;
2209
2210    ENTER("qlc1280_mem_free");
2211    if (ha)
2212    {
2213        /* Free device queues. */
2214        for (b = 0; b < MAX_BUSES; b++)
2215        {
2216            q = LU_Q(ha, b, ha->bus_settings[b].id, 0);
2217            for (t = 0; t < MAX_TARGETS; t++)
2218                for (l = 0; l < MAX_LUNS; l++)
2219                    if (LU_Q(ha, b, t, l) != NULL && LU_Q(ha, b, t, l) != q)
2220                        KMFREE(LU_Q(ha, b, t, l),sizeof(struct scsi_lu));
2221                    KMFREE(q, sizeof(struct scsi_lu));
2222        }
2223        for( b =0; b < MAX_EQ; b++ )
2224            ha->dev[b] =  (scsi_lu_t  *)NULL;
2225    }
2226
2227    LEAVE("qlc1280_mem_free");
2228}
2229
2230
2231
2232
2233/****************************************************************************/
2234/*                QLogic ISP1280 Hardware Support Functions.                */
2235/****************************************************************************/
2236
2237 /*
2238 * qla2100_enable_intrs
2239 * qla2100_disable_intrs
2240 *
2241 * Input:
2242 *      ha = adapter block pointer.
2243 *
2244 * Returns:
2245 *      None
2246 */
2247    static inline void qla1280_enable_intrs(scsi_qla_host_t *ha) {
2248        device_reg_t *reg;
2249
2250        reg = ha->iobase;
2251        ha->flags.interrupts_on = 1;
2252        /* enable risc and host interrupts */
2253        WRT_REG_WORD(&reg->ictrl, (ISP_EN_INT+ ISP_EN_RISC));
2254    }
2255
2256    static inline void qla1280_disable_intrs(scsi_qla_host_t *ha) {
2257        device_reg_t *reg;
2258
2259        reg = ha->iobase;
2260        ha->flags.interrupts_on = 0;
2261        /* disable risc and host interrupts */
2262        WRT_REG_WORD(&reg->ictrl, 0);
2263    }
2264
2265/*
2266 * qla1280_initialize_adapter
2267 *      Initialize board.
2268 *
2269 * Input:
2270 *      ha = adapter block pointer.
2271 *
2272 * Returns:
2273 *      0 = success
2274 */
2275STATIC uint8_t
2276qla1280_initialize_adapter(scsi_qla_host_t *ha)
2277{
2278    device_reg_t *reg;
2279    uint8_t      status;
2280    /* uint8_t      cnt; */
2281    uint8_t      b;
2282
2283#ifdef QL_DEBUG_LEVEL_3
2284    ENTER("qla1280_initialize_adapter");
2285#endif
2286
2287    /* Clear adapter flags. */
2288    ha->flags.online = FALSE;
2289    ha->flags.isp_abort_needed = FALSE;
2290    ha->flags.disable_host_adapter = FALSE;
2291    ha->flags.reset_active = FALSE;
2292    ha->flags.abort_isp_active = FALSE;
2293    ha->flags.watchdog_enabled = FALSE;
2294
2295    DEBUG(printk("Configure PCI space for adapter...\n"));
2296    if (!(status = qla1280_pci_config(ha)))
2297    {
2298        reg = ha->iobase;
2299
2300        /* Disable ISP interrupts. */
2301        WRT_REG_WORD(&reg->ictrl, 0);
2302
2303        /* Insure mailbox registers are free. */
2304        WRT_REG_WORD(&reg->semaphore, 0);
2305        WRT_REG_WORD(&reg->host_cmd, HC_CLR_RISC_INT);
2306        WRT_REG_WORD(&reg->host_cmd, HC_CLR_HOST_INT);
2307
2308        /* If firmware needs to be loaded */
2309        if (qla1280_verbose)
2310         printk("scsi(%d): Determining if RISC is loaded...\n",(int)ha->host_no);
2311        if (qla1280_isp_firmware(ha))
2312        {
2313            if (qla1280_verbose)
2314              printk("scsi(%d): Verifying chip...\n",(int)ha->host_no);
2315            if (!(status = qla1280_chip_diag(ha)))
2316            {
2317                if (qla1280_verbose)
2318                  printk("scsi(%d): Setup chip...\n",(int)ha->host_no);
2319                status = qla1280_setup_chip(ha);
2320            }
2321        }
2322
2323        if (!status)
2324        {
2325            /* Setup adapter based on NVRAM parameters. */
2326            if (qla1280_verbose)
2327              printk("scsi(%d): Configure NVRAM parameters...\n",(int)ha->host_no);
2328            qla1280_nvram_config(ha);
2329
2330            if (!ha->flags.disable_host_adapter &&
2331                !qla1280_init_rings(ha))
2332            {
2333                /* Issue SCSI reset. */
2334                for (b = 0; b < ha->ports; b++)
2335                    if (!ha->bus_settings[b].disable_scsi_reset)
2336                    {
2337                      /* dg 03/13 if we can't reset twice then bus is dead */
2338                        if( qla1280_bus_reset(ha, b) )
2339                           if( qla1280_bus_reset(ha, b) )
2340                           {
2341                               ha->bus_settings[b].scsi_bus_dead = TRUE;
2342                            }
2343                    }
2344
2345                    do
2346                    {
2347                        /* Issue marker command. */
2348                        ha->flags.reset_marker = FALSE;
2349                        for (b = 0; b < ha->ports; b++)
2350                        {
2351                            ha->bus_settings[b].reset_marker = FALSE;
2352                            qla1280_marker(ha, b, 0, 0, MK_SYNC_ALL);
2353                        }
2354                    }while (ha->flags.reset_marker);
2355
2356                    ha->flags.online = TRUE;
2357
2358                    /* Enable host adapter target mode. */
2359                    for (b = 0; b < ha->ports; b++)
2360                    {
2361                        if (!(status = qla1280_enable_tgt(ha, b)))
2362                        {
2363                            /* for (cnt = 0; cnt < MAX_LUNS; cnt++)
2364                            {
2365                                qla1280_enable_lun(ha, b, cnt);
2366                                 qla1280_poll(ha);
2367                            }*/
2368                        }
2369                        else
2370                            break;
2371                    }
2372            }
2373            else
2374                status = 1;
2375        }
2376    }
2377
2378#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
2379    if (status)
2380        qla1280_print("qla1280_initialize_adapter: **** FAILED ****\n\r");
2381#endif
2382#ifdef QL_DEBUG_LEVEL_3
2383    else
2384        LEAVE("qla1280_initialize_adapter");
2385#endif
2386    return(status);
2387}
2388
2389/*
2390 * qla1280_enable_tgt
2391 *      Enable target mode.
2392 *
2393 * Input:
2394 *      ha = adapter block pointer.
2395 *      b  = SCSI bus number.
2396 *
2397 * Returns:
2398 *      0 = success.
2399 */
2400STATIC uint8_t
2401qla1280_enable_tgt(scsi_qla_host_t *ha, uint8_t b)
2402{
2403    uint8_t     status = 0;
2404    /*  uint16_t    mb[MAILBOX_REGISTER_COUNT]; */
2405
2406#ifdef QL_DEBUG_LEVEL_3
2407    qla1280_print("qla1280_enable_tgt: entered\n\r");
2408#endif
2409
2410    /* Enable target mode. */
2411    /*
2412    mb[0] = MBC_ENABLE_TARGET_MODE;
2413    mb[1] = BIT_15;
2414    mb[2] = (uint16_t)(b << 15);
2415    status = qla1280_mailbox_command(ha, BIT_2|BIT_1|BIT_0, &mb[0]);
2416    */
2417#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
2418    if (status)
2419        qla1280_print("qla1280_enable_tgt: **** FAILED ****\n\r");
2420#endif
2421#ifdef QL_DEBUG_LEVEL_3
2422    else
2423        qla1280_print("qla1280_enable_tgt: exiting normally\n\r");
2424#endif
2425    return(status);
2426}
2427
2428/*
2429 * ISP Firmware Test
2430 *      Checks if present version of RISC firmware is older than
2431 *      driver firmware.
2432 *
2433 * Input:
2434 *      ha = adapter block pointer.
2435 *
2436 * Returns:
2437 *      0 = firmware does not need to be loaded.
2438 */
2439STATIC uint8_t
2440qla1280_isp_firmware(scsi_qla_host_t *ha)
2441{
2442    nvram_t     *nv    = (nvram_t *)ha->response_ring;
2443    uint16_t    *wptr;
2444    uint8_t     chksum;
2445    uint8_t     cnt;
2446    uint8_t     status = 0;    /* dg 2/27 always loads RISC */
2447    uint16_t    mb[MAILBOX_REGISTER_COUNT];
2448
2449    ENTER("qla1280_isp_firmware");
2450
2451    /* Verify valid NVRAM checksum. */
2452    wptr = (uint16_t *)ha->response_ring;
2453    DEBUG(printk("qla1280_isp_firmware: Reading NVRAM\n"));
2454    chksum = 0;
2455    for (cnt = 0; cnt < sizeof(nvram_t)/2; cnt++)
2456    {
2457        *wptr = qla1280_get_nvram_word(ha, cnt);
2458        chksum += (uint8_t)*wptr;
2459        chksum += (uint8_t)(*wptr >> 8);
2460        wptr++;
2461    }
2462    DEBUG(printk("qla1280_isp_firmware: Completed Reading NVRAM\n"));
2463
2464#if defined(QL_DEBUG_LEVEL_3)
2465    sprintf(debug_buff,"qla1280_isp_firmware: NVRAM Magic ID= %c %c %c\n\r",(char *) nv->id[0],nv->id[1],nv->id[2]);
2466    qla1280_print(debug_buff);
2467#endif
2468
2469    /* Bad NVRAM data, load RISC code. */
2470    if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
2471        nv->id[2] != 'P' || nv->id[3] != ' ' || nv->version < 1)
2472    {
2473        printk(KERN_INFO "qla1280_isp_firmware: Bad checksum or magic number or version in NVRAM.\n");
2474        ha->flags.disable_risc_code_load = FALSE;
2475    }
2476    else
2477        ha->flags.disable_risc_code_load = nv->cntr_flags_1.disable_loading_risc_code;
2478
2479    if (ha->flags.disable_risc_code_load)
2480    {
2481#if defined(QL_DEBUG_LEVEL_3)
2482        qla1280_print("qla1280_isp_firmware: Telling RISC to verify checksum of loaded BIOS code.\n\r");
2483#endif
2484        /* Verify checksum of loaded RISC code. */
2485        mb[0] = MBC_VERIFY_CHECKSUM;
2486        /* mb[1] = ql12_risc_code_addr01; */
2487        mb[1] = *QLBoardTbl[ha->devnum].fwstart;
2488
2489        if (!(status = qla1280_mailbox_command(ha, BIT_1|BIT_0, &mb[0])))
2490        {
2491            /* Start firmware execution. */
2492#if defined(QL_DEBUG_LEVEL_3)
2493            qla1280_print("qla1280_isp_firmware: Startng F/W execution.\n\r");
2494#endif
2495            mb[0] = MBC_EXECUTE_FIRMWARE;
2496            /* mb[1] = ql12_risc_code_addr01; */
2497            mb[1] = *QLBoardTbl[ha->devnum].fwstart;
2498            qla1280_mailbox_command(ha, BIT_1|BIT_0, &mb[0]);
2499        }
2500        else
2501            printk(KERN_INFO "qla1280: RISC checksum failed.\n");
2502    }
2503    else
2504    {
2505        DEBUG(printk("qla1280: NVRAM configured to load RISC load.\n"));
2506        status = 1;
2507     }
2508
2509#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
2510    if (status)
2511        qla1280_print(
2512                "qla1280_isp_firmware: **** Load RISC code ****\n\r");
2513#endif
2514    LEAVE("qla1280_isp_firmware");
2515    return(status);
2516}
2517
2518/*
2519 * PCI configuration
2520 *      Setup device PCI configuration registers.
2521 *
2522 * Input:
2523 *      ha = adapter block pointer.
2524 *
2525 * Returns:
2526 *      0 = success.
2527 */
2528STATIC uint8_t
2529qla1280_pci_config(scsi_qla_host_t *ha)
2530{
2531    uint8_t      status = 1;
2532    uint32_t     command;
2533#if MEMORY_MAPPED_IO
2534    uint32_t  page_offset, base;
2535    uint32_t   mmapbase;
2536#endif
2537    config_reg_t *creg = 0;
2538    uint16_t     buf_wd;
2539
2540    ENTER("qla1280_pci_config");
2541
2542    /* Get command register. */
2543    if (pci_read_config_word(ha->pdev,OFFSET(creg->command), &buf_wd) == PCIBIOS_SUCCESSFUL)
2544    {
2545        command = buf_wd;
2546        /*
2547        * Set Bus Master Enable, Memory Address Space Enable and
2548        * reset any error bits.
2549        */
2550        buf_wd &= ~0x7;
2551#if MEMORY_MAPPED_IO
2552        DEBUG(printk("qla1280: MEMORY MAPPED IO is enabled.\n"));
2553        buf_wd |= BIT_2 + BIT_1 + BIT_0;
2554#else
2555        buf_wd |= BIT_2 + BIT_0;
2556#endif
2557        if( pci_write_config_word(ha->pdev,OFFSET(creg->command), buf_wd) )
2558        {
2559            printk(KERN_WARNING "qla1280: Could not write config word.\n");
2560        }
2561        /* Get expansion ROM address. */
2562        if (pci_read_config_word(ha->pdev,OFFSET(creg->expansion_rom), &buf_wd) == PCIBIOS_SUCCESSFUL)
2563        {
2564            /* Reset expansion ROM address decode enable. */
2565            buf_wd &= ~BIT_0;
2566            if (pci_write_config_word(ha->pdev,OFFSET(creg->expansion_rom), buf_wd) == PCIBIOS_SUCCESSFUL)
2567            {
2568#if MEMORY_MAPPED_IO
2569                /* Get memory mapped I/O address. */
2570                pci_read_config_dword(ha->pdev,OFFSET(cfgp->mem_base_addr), &mmapbase);
2571                mmapbase &= PCI_BASE_ADDRESS_MEM_MASK;
2572
2573                /* Find proper memory chunk for memory map I/O reg. */
2574                base = mmapbase & PAGE_MASK;
2575                page_offset = mmapbase - base;
2576                /* Get virtual address for I/O registers. */
2577#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
2578                ha->mmpbase = ioremap_nocache(base, page_offset + 256);
2579#else
2580                ha->mmpbase = vremap(base,page_offset + 256);
2581#endif
2582                if( ha->mmpbase )
2583                {
2584                    ha->mmpbase += page_offset;
2585                    /* ha->iobase = ha->mmpbase; */
2586                    status = 0;
2587                }
2588#else /* MEMORY_MAPPED_IO */
2589                status = 0;
2590#endif /* MEMORY_MAPPED_IO */
2591            }
2592        }
2593    }
2594
2595    LEAVE("qla1280_pci_config");
2596    return(status);
2597}
2598
2599/*
2600 * Chip diagnostics
2601 *      Test chip for proper operation.
2602 *
2603 * Input:
2604 *      ha = adapter block pointer.
2605 *
2606 * Returns:
2607 *      0 = success.
2608 */
2609STATIC uint8_t
2610qla1280_chip_diag(scsi_qla_host_t *ha)
2611{
2612    device_reg_t *reg   = ha->iobase;
2613    uint8_t      status = 0;
2614    uint16_t     data;
2615    uint32_t     cnt;
2616    uint16_t     mb[MAILBOX_REGISTER_COUNT];
2617
2618#ifdef QL_DEBUG_LEVEL_3
2619    sprintf(debug_buff, "qla1280_chip_diag: testing device at 0x%p \n\r",&reg->id_l);
2620    qla1280_print(debug_buff);
2621#endif
2622
2623    /* Soft reset chip and wait for it to finish. */
2624    WRT_REG_WORD(&reg->ictrl, ISP_RESET);
2625    data = qla1280_debounce_register(&reg->ictrl);
2626    for (cnt = 6000000; cnt && data & ISP_RESET; cnt--)
2627    {
2628        SYS_DELAY(5);
2629        data = RD_REG_WORD(&reg->ictrl);
2630    }
2631    if (cnt)
2632    {
2633        /* Reset register not cleared by chip reset. */
2634#if defined(QL_DEBUG_LEVEL_3)
2635        qla1280_print("qla1280_chip_diag: reset register cleared by chip reset\n\r");
2636#endif
2637        WRT_REG_WORD(&reg->cfg_1, 0);
2638
2639        /* Reset RISC and disable BIOS which
2640        allows RISC to execute out of RAM. */
2641        WRT_REG_WORD(&reg->host_cmd, HC_RESET_RISC);
2642        WRT_REG_WORD(&reg->host_cmd, HC_RELEASE_RISC);
2643        WRT_REG_WORD(&reg->host_cmd, HC_DISABLE_BIOS);
2644        data = qla1280_debounce_register(&reg->mailbox0);
2645        for (cnt = 6000000; cnt && data == MBS_BUSY; cnt--)
2646        {
2647            SYS_DELAY(5);
2648            data = RD_REG_WORD(&reg->mailbox0);
2649        }
2650
2651        if (cnt)
2652        {
2653            /* Check product ID of chip */
2654#if defined(QL_DEBUG_LEVEL_3)
2655            qla1280_print("qla1280_chip_diag: Checking product ID of chip\n\r");
2656#endif
2657            if (RD_REG_WORD(&reg->mailbox1) != PROD_ID_1 ||
2658                (RD_REG_WORD(&reg->mailbox2) != PROD_ID_2 &&
2659                RD_REG_WORD(&reg->mailbox2) != PROD_ID_2a) ||
2660                RD_REG_WORD(&reg->mailbox3) != PROD_ID_3 ||
2661                RD_REG_WORD(&reg->mailbox4) != PROD_ID_4)
2662            {
2663                printk(KERN_INFO "qla1280: Wrong product ID = 0x%x,0x%x,0x%x,0x%x\n",
2664                        RD_REG_WORD(&reg->mailbox1),
2665                        RD_REG_WORD(&reg->mailbox2),
2666                        RD_REG_WORD(&reg->mailbox3),
2667                        RD_REG_WORD(&reg->mailbox4) );
2668                status = 1;
2669            }
2670            else
2671            {
2672                DEBUG(printk("qla1280_chip_diag: Checking mailboxes of chip\n"));
2673                /* Wrap Incoming Mailboxes Test. */
2674                mb[0] = MBC_MAILBOX_REGISTER_TEST;
2675                mb[1] = 0xAAAA;
2676                mb[2] = 0x5555;
2677                mb[3] = 0xAA55;
2678                mb[4] = 0x55AA;
2679                mb[5] = 0xA5A5;
2680                mb[6] = 0x5A5A;
2681                mb[7] = 0x2525;
2682                if (!(status = qla1280_mailbox_command(ha,
2683                    (BIT_7|BIT_6|BIT_5|BIT_4|BIT_3|BIT_2|BIT_1|BIT_0),
2684                    &mb[0])))
2685                {
2686                    if (mb[1] != 0xAAAA || mb[2] != 0x5555 ||
2687                        mb[3] != 0xAA55 || mb[4] != 0x55AA)
2688                        status = 1;
2689                    if (mb[5] != 0xA5A5 || mb[6] != 0x5A5A ||
2690                        mb[7] != 0x2525)
2691                        status = 1;
2692                    if( status == 1 )
2693                        printk(KERN_INFO "qla1280: Failed mailbox check\n");
2694                }
2695            }
2696        }
2697        else
2698            status = 1;
2699    }
2700    else
2701        status = 1;
2702
2703#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
2704    if (status)
2705        qla1280_print("qla1280_chip_diag: **** FAILED ****\n\r");
2706#endif
2707#ifdef QL_DEBUG_LEVEL_3
2708    else
2709        qla1280_print("qla1280_chip_diag: exiting normally\n\r");
2710#endif
2711    return(status);
2712}
2713
2714/*
2715 * Setup chip
2716 *      Load and start RISC firmware.
2717 *
2718 * Input:
2719 *      ha = adapter block pointer.
2720 *
2721 * Returns:
2722 *      0 = success.
2723 */
2724STATIC uint8_t
2725qla1280_setup_chip(scsi_qla_host_t *ha)
2726{
2727    uint8_t      status = 0;
2728    uint16_t     risc_address;
2729    uint16_t     *risc_code_address;
2730    long         risc_code_size;
2731    uint16_t     mb[MAILBOX_REGISTER_COUNT];
2732#ifdef QLA1280_UNUSED
2733    uint8_t	*sp;
2734    int i;
2735#endif
2736    uint16_t     cnt;
2737    int          num;
2738    uint8_t    *tbuf;
2739    u_long     p_tbuf;
2740
2741#ifdef QL_DEBUG_LEVEL_3
2742    ENTER("qla1280_setup_chip");
2743#endif
2744
2745	if( (tbuf = (uint8_t *)KMALLOC(8000) ) == NULL )
2746        {
2747            printk("setup_chip: couldn't alloacte memory\n");
2748            return(1);
2749        }
2750        p_tbuf =  VIRT_TO_BUS(tbuf);
2751    /* Load RISC code. */
2752    /*
2753    risc_address      = ql12_risc_code_addr01;
2754    risc_code_address = &ql12_risc_code01[0];
2755    risc_code_size    = ql12_risc_code_length01;
2756    */
2757    risc_address = *QLBoardTbl[ha->devnum].fwstart;
2758    risc_code_address = QLBoardTbl[ha->devnum].fwcode;
2759    risc_code_size    = (long)(*QLBoardTbl[ha->devnum].fwlen & 0xffff);
2760
2761    DEBUG(printk("qla1280: DMAing RISC code (%d) words.\n",(int)risc_code_size));
2762    DEBUG(sprintf(debug_buff,"qla1280_setup_chip:  Loading RISC code size =(%ld).\n\r",risc_code_size);)
2763    DEBUG(qla1280_print(debug_buff));
2764    num =0;
2765    while (risc_code_size > 0 && !status)
2766    {
2767        cnt = 2000 >> 1;
2768
2769        if ( cnt > risc_code_size )
2770            cnt = risc_code_size;
2771
2772        DEBUG(sprintf(debug_buff,"qla1280_setup_chip:  loading risc @ =(0x%p),%d,%d(0x%x).\n\r",risc_code_address,cnt,num,risc_address);)
2773        DEBUG(qla1280_print(debug_buff));
2774        DEBUG(printk("qla1280_setup_chip:  loading risc @ =code=(0x%p),cnt=%d,seg=%d,addr=0x%x\n\r",risc_code_address,cnt,num,risc_address));
2775        BCOPY((caddr_t) risc_code_address,(caddr_t) ha->request_ring, (cnt <<1));
2776        mb[0] = MBC_LOAD_RAM;
2777        /* mb[0] = MBC_LOAD_RAM_A64; */
2778        mb[1] = risc_address;
2779        mb[4] = cnt;
2780        mb[3] = (uint16_t)  ha->request_dma & 0xffff;
2781        mb[2] = (uint16_t) (ha->request_dma >> 16) & 0xffff;
2782        mb[7] = (uint16_t) (MS_64BITS(ha->request_dma) & 0xffff);
2783        mb[6] = (uint16_t) (MS_64BITS(ha->request_dma) >> 16) & 0xffff;
2784        DEBUG(printk("qla1280_setup_chip: op=%d  0x%lx = 0x%4x,0x%4x,0x%4x,0x%4x\n",mb[0],ha->request_dma,mb[6],mb[7],mb[2],mb[3]));
2785        if( (status = qla1280_mailbox_command(ha, BIT_4|BIT_3|BIT_2|BIT_1|BIT_0,
2786            &mb[0]))  )
2787        {
2788            printk("Failed to load partial segment of f/w\n");
2789            break;
2790        }
2791        /* dump it back */
2792
2793        risc_address += cnt;
2794        risc_code_size = risc_code_size - cnt;
2795        risc_code_address = risc_code_address + cnt;
2796        num++;
2797    }
2798#ifdef QLA1280_UNUSED
2799    DEBUG(ql_debug_print = 0;)
2800    {
2801        for (i = 0; i < ql12_risc_code_length01; i++)
2802        {
2803            mb[0] = 0x4;
2804            mb[1] = ql12_risc_code_addr01 + i;
2805            mb[2] = ql12_risc_code01[i];
2806
2807            status = qla1280_mailbox_command(ha, BIT_2|BIT_1|BIT_0,
2808                    &mb[0]);
2809            if (status)
2810            {
2811                printk("qla1280 : firmware load failure\n");
2812                break;
2813            }
2814
2815            mb[0] = 0x5;
2816            mb[1] = ql12_risc_code_addr01 + i;
2817            mb[2] = 0;
2818
2819            status = qla1280_mailbox_command(ha, BIT_2|BIT_1|BIT_0,
2820                    &mb[0]);
2821            if (status)
2822            {
2823                printk("qla1280 : firmware dump failure\n");
2824                break;
2825            }
2826            if( mb[2] != ql12_risc_code01[i] )
2827                printk("qla1280 : firmware compare error @ (0x%x)\n",ql12_risc_code_addr01+i);
2828        }
2829    }
2830    DEBUG(ql_debug_print = 1;)
2831#endif
2832
2833    /* Verify checksum of loaded RISC code. */
2834    if (!status)
2835    {
2836        DEBUG(printk("qla1280_setup_chip: Verifying checksum of loaded RISC code.\n");)
2837        mb[0] = MBC_VERIFY_CHECKSUM;
2838        /* mb[1] = ql12_risc_code_addr01; */
2839        mb[1] = *QLBoardTbl[ha->devnum].fwstart;
2840
2841        if (!(status = qla1280_mailbox_command(ha, BIT_1|BIT_0, &mb[0])))
2842        {
2843            /* Start firmware execution. */
2844            DEBUG(qla1280_print("qla1280_setup_chip: start firmware running.\n\r");)
2845            mb[0] = MBC_EXECUTE_FIRMWARE;
2846            /* mb[1] = ql12_risc_code_addr01; */
2847            mb[1] = *QLBoardTbl[ha->devnum].fwstart;
2848            qla1280_mailbox_command(ha, BIT_1|BIT_0, &mb[0]);
2849        }
2850        else
2851            printk("qla1280_setup_chip: Failed checksum.\n");
2852    }
2853
2854	KMFREE(tbuf,8000);
2855
2856#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
2857    if (status)
2858        qla1280_print("qla1280_setup_chip: **** FAILED ****\n\r");
2859#endif
2860#ifdef QL_DEBUG_LEVEL_3
2861    else
2862        LEAVE("qla1280_setup_chip");
2863#endif
2864    return(status);
2865}
2866
2867/*
2868 * Initialize rings
2869 *
2870 * Input:
2871 *      ha                = adapter block pointer.
2872 *      ha->request_ring  = request ring virtual address
2873 *      ha->response_ring = response ring virtual address
2874 *      ha->request_dma   = request ring physical address
2875 *      ha->response_dma  = response ring physical address
2876 *
2877 * Returns:
2878 *      0 = success.
2879 */
2880STATIC uint8_t
2881qla1280_init_rings(scsi_qla_host_t *ha)
2882{
2883    uint8_t     status = 0;
2884    uint16_t    cnt;
2885    uint16_t    mb[MAILBOX_REGISTER_COUNT];
2886
2887#ifdef QL_DEBUG_LEVEL_3
2888    ENTER("qla1280_init_rings");
2889#endif
2890    /* Clear outstanding commands array. */
2891    for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
2892        ha->outstanding_cmds[cnt] = 0;
2893
2894    /* Initialize request queue. */
2895    ha->request_ring_ptr = ha->request_ring;
2896    ha->req_ring_index   = 0;
2897    ha->req_q_cnt        = REQUEST_ENTRY_CNT;
2898    /* mb[0] = MBC_INIT_REQUEST_QUEUE; */
2899    mb[0] = MBC_INIT_REQUEST_QUEUE_A64;
2900    mb[1] = REQUEST_ENTRY_CNT;
2901    mb[3] = (uint16_t)LS_64BITS(ha->request_dma);
2902    mb[2] = (uint16_t)( LS_64BITS(ha->request_dma) >> 16);
2903    mb[4] = 0;
2904    mb[7] = (uint16_t)MS_64BITS(ha->request_dma);
2905    mb[6] = (uint16_t)( MS_64BITS(ha->request_dma) >> 16);
2906    if (!(status = qla1280_mailbox_command(ha,
2907        BIT_7|BIT_6|BIT_4|BIT_3|BIT_2|BIT_1|BIT_0,
2908        &mb[0])))
2909    {
2910        /* Initialize response queue. */
2911        ha->response_ring_ptr = ha->response_ring;
2912        ha->rsp_ring_index    = 0;
2913        /* mb[0] = MBC_INIT_RESPONSE_QUEUE; */
2914        mb[0] = MBC_INIT_RESPONSE_QUEUE_A64;
2915        mb[1] = RESPONSE_ENTRY_CNT;
2916        mb[3] = (uint16_t)LS_64BITS(ha->response_dma);
2917        mb[2] = (uint16_t)(LS_64BITS(ha->response_dma) >> 16);
2918        mb[5] = 0;
2919        mb[7] = (uint16_t)MS_64BITS(ha->response_dma);
2920        mb[6] = (uint16_t)(MS_64BITS(ha->response_dma) >> 16);
2921        status = qla1280_mailbox_command(ha,
2922                BIT_7|BIT_6|BIT_5|BIT_3|BIT_2|BIT_1|BIT_0,
2923                &mb[0]);
2924    }
2925
2926#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
2927    if (status)
2928        qla1280_print("qla1280_init_rings: **** FAILED ****\n\r");
2929#endif
2930#ifdef QL_DEBUG_LEVEL_3
2931    else
2932        LEAVE("qla1280_init_rings");
2933#endif
2934    return(status);
2935}
2936
2937/*
2938 * NVRAM configuration.
2939 *
2940 * Input:
2941 *      ha                = adapter block pointer.
2942 *      ha->request_ring  = request ring virtual address
2943 *
2944 * Output:
2945 *      host adapters parameters in host adapter block
2946 *
2947 * Returns:
2948 *      0 = success.
2949 */
2950STATIC uint8_t
2951qla1280_nvram_config(scsi_qla_host_t *ha)
2952{
2953    device_reg_t *reg   = ha->iobase;
2954    nvram_t      *nv    = (nvram_t *)ha->response_ring;
2955    uint8_t      status = 0;
2956    uint32_t     b, t, l;
2957    uint16_t     *wptr;
2958    uint16_t     mb[MAILBOX_REGISTER_COUNT];
2959    uint8_t      cnt;
2960    uint8_t      chksum;
2961    uint32_t     nvsize;
2962
2963#if defined(QL_DEBUG_ROUTINES) && !defined(QL_DEBUG_LEVEL_4)
2964    uint8_t      saved_print_status = ql_debug_print;
2965#endif
2966    ENTER("qla1280_nvram_config");
2967#if defined(QL_DEBUG_ROUTINES) && !defined(QL_DEBUG_LEVEL_4)
2968    ql_debug_print = FALSE;
2969#endif
2970
2971    /* Verify valid NVRAM checksum. */
2972#if  USE_NVRAM_DEFAULTS
2973    chksum = 1;
2974#else
2975    wptr = (uint16_t *)ha->response_ring;
2976    chksum = 0;
2977    if( ha->device_id == QLA12160_DEVICE_ID ||
2978        ha->device_id == QLA10160_DEVICE_ID )
2979    nvsize = sizeof(nvram160_t)/2;
2980    else
2981    nvsize = sizeof(nvram_t)/2;
2982    for( cnt = 0; cnt < nvsize; cnt++ )
2983    {
2984        *wptr = qla1280_get_nvram_word(ha, cnt);
2985        chksum += (uint8_t)*wptr;
2986        chksum += (uint8_t)(*wptr >> 8);
2987        wptr++;
2988    }
2989#endif
2990
2991
2992    /* Bad NVRAM data, set defaults parameters. */
2993    if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
2994        nv->id[2] != 'P' || nv->id[3] != ' ' || nv->version < 1)
2995    {
2996#if  USE_NVRAM_DEFAULTS
2997        DEBUG(printk("Using defaults for NVRAM\n"));
2998#else
2999        DEBUG(printk("Using defaults for NVRAM: \n"));
3000        DEBUG(printk("checksum=0x%x, Id=%c, version=0x%x\n",chksum,nv->id[0],nv->version));
3001#if defined(QL_DEBUG_LEVEL_3)
3002        /* ql_debug_print = 1;
3003        qla1280_dump_buffer((caddr_t)ha->response_ring, REQUEST_ENTRY_SIZE);
3004        ql_debug_print = 0; */
3005#endif
3006                wptr = (uint16_t *)ha->response_ring;
3007        for (cnt = 0; cnt < sizeof(nvram_t)/2; cnt++)
3008            *wptr++ = 0;
3009#endif
3010
3011
3012        /* nv->cntr_flags_1.disable_loading_risc_code = 1; */
3013        nv->firmware_feature.w = BIT_0;
3014        nv->termination.f.scsi_bus_0_control = 3;
3015        nv->termination.f.scsi_bus_1_control = 3;
3016        nv->termination.f.auto_term_support = 1;
3017
3018        for (b = 0; b < MAX_BUSES; b++)
3019        {
3020            nv->bus[b].config_1.initiator_id = 7;
3021            nv->bus[b].bus_reset_delay = 5;
3022            nv->bus[b].config_2.async_data_setup_time = 9;
3023            nv->bus[b].config_2.req_ack_active_negation = 1;
3024            nv->bus[b].config_2.data_line_active_negation = 1;
3025            nv->bus[b].selection_timeout = 250;
3026            nv->bus[b].max_queue_depth = 256;
3027
3028            for (t = 0; t < MAX_TARGETS; t++)
3029            {
3030                nv->bus[b].target[t].parameter.f.auto_request_sense = 1;
3031                nv->bus[b].target[t].parameter.f.disconnect_allowed = 1;
3032                nv->bus[b].target[t].parameter.f.tag_queuing = 1;
3033                nv->bus[b].target[t].flags.device_enable = 1;
3034            }
3035        }
3036
3037#if  USE_NVRAM_DEFAULTS
3038        status = 0;
3039#else
3040        status = 1;
3041#endif
3042    }
3043    else
3044    {
3045        /* Always force AUTO sense for LINUX SCSI */
3046        for (b = 0; b < MAX_BUSES; b++)
3047            for (t = 0; t < MAX_TARGETS; t++)
3048            {
3049                nv->bus[b].target[t].parameter.f.auto_request_sense = 1;
3050            }
3051    }
3052#if  DEBUG_PRINT_NVRAM
3053    ql_debug_print = 1;
3054    sprintf(debug_buff,"qla1280 : initiator scsi id bus[0]=%d\n\r",
3055            nv->bus[0].config_1.initiator_id);
3056    qla1280_print(debug_buff);
3057    sprintf(debug_buff,"qla1280 : initiator scsi id bus[1]=%d\n\r",
3058            nv->bus[1].config_1.initiator_id);
3059    qla1280_print(debug_buff);
3060
3061    sprintf(debug_buff,"qla1280 : bus reset delay[0]=%d\n\r",
3062            nv->bus[0].bus_reset_delay);
3063    qla1280_print(debug_buff);
3064    sprintf(debug_buff,"qla1280 : bus reset delay[1]=%d\n\r",
3065            nv->bus[1].bus_reset_delay);
3066    qla1280_print(debug_buff);
3067
3068    sprintf(debug_buff,"qla1280 : retry count[0]=%d\n\r",
3069            nv->bus[0].retry_count);
3070    qla1280_print(debug_buff);
3071    sprintf(debug_buff,"qla1280 : retry delay[0]=%d\n\r",
3072            nv->bus[0].retry_delay);
3073    qla1280_print(debug_buff);
3074    sprintf(debug_buff,"qla1280 : retry count[1]=%d\n\r",
3075            nv->bus[1].retry_count);
3076    qla1280_print(debug_buff);
3077    sprintf(debug_buff,"qla1280 : retry delay[1]=%d\n\r",
3078            nv->bus[1].retry_delay);
3079    qla1280_print(debug_buff);
3080
3081    sprintf(debug_buff,"qla1280 : async data setup time[0]=%d\n\r",
3082            nv->bus[0].config_2.async_data_setup_time);
3083    qla1280_print(debug_buff);
3084    sprintf(debug_buff,"qla1280 : async data setup time[1]=%d\n\r",
3085            nv->bus[1].config_2.async_data_setup_time);
3086    qla1280_print(debug_buff);
3087
3088    sprintf(debug_buff,"qla1280 : req/ack active negation[0]=%d\n\r",
3089            nv->bus[0].config_2.req_ack_active_negation);
3090    qla1280_print(debug_buff);
3091    sprintf(debug_buff,"qla1280 : req/ack active negation[1]=%d\n\r",
3092            nv->bus[1].config_2.req_ack_active_negation);
3093    qla1280_print(debug_buff);
3094
3095    sprintf(debug_buff,"qla1280 : data line active negation[0]=%d\n\r",
3096            nv->bus[0].config_2.data_line_active_negation);
3097    qla1280_print(debug_buff);
3098    sprintf(debug_buff,"qla1280 : data line active negation[1]=%d\n\r",
3099            nv->bus[1].config_2.data_line_active_negation);
3100    qla1280_print(debug_buff);
3101
3102
3103    sprintf(debug_buff,"qla1280 : disable loading risc code=%d\n\r",
3104            nv->cntr_flags_1.disable_loading_risc_code);
3105    qla1280_print(debug_buff);
3106
3107    sprintf(debug_buff,"qla1280 : enable 64bit addressing=%d\n\r",
3108            nv->cntr_flags_1.enable_64bit_addressing);
3109    qla1280_print(debug_buff);
3110
3111    sprintf(debug_buff,"qla1280 : selection timeout limit[0]=%d\n\r",
3112            nv->bus[0].selection_timeout);
3113    qla1280_print(debug_buff);
3114    sprintf(debug_buff,"qla1280 : selection timeout limit[1]=%d\n\r",
3115            nv->bus[1].selection_timeout);
3116
3117    qla1280_print(debug_buff);
3118    sprintf(debug_buff,"qla1280 : max queue depth[0]=%d\n\r",
3119            nv->bus[0].max_queue_depth);
3120    qla1280_print(debug_buff);
3121    sprintf(debug_buff,"qla1280 : max queue depth[1]=%d\n\r",
3122            nv->bus[1].max_queue_depth);
3123    qla1280_print(debug_buff);
3124#endif
3125
3126    DEBUG(ql_debug_print = 0;)
3127
3128    /* Disable RISC load of firmware. */
3129    ha->flags.disable_risc_code_load =
3130            nv->cntr_flags_1.disable_loading_risc_code;
3131    /* Enable 64bit addressing. */
3132    ha->flags.enable_64bit_addressing =
3133            nv->cntr_flags_1.enable_64bit_addressing;
3134
3135    /* Set ISP hardware DMA burst */
3136    mb[0] = nv->isp_config.c;
3137    WRT_REG_WORD(&reg->cfg_1, mb[0]);
3138
3139    /* Set SCSI termination. */
3140    WRT_REG_WORD(&reg->gpio_enable, (BIT_3 + BIT_2 + BIT_1 + BIT_0));
3141    mb[0] = nv->termination.c & (BIT_3 + BIT_2 + BIT_1 + BIT_0);
3142    WRT_REG_WORD(&reg->gpio_data, mb[0]);
3143
3144    /* ISP parameter word. */
3145    mb[0] = MBC_SET_SYSTEM_PARAMETER;
3146    mb[1] = nv->isp_parameter;
3147    status |= qla1280_mailbox_command(ha, BIT_1|BIT_0, &mb[0]);
3148
3149    /* Firmware feature word. */
3150    mb[0] = MBC_SET_FIRMWARE_FEATURES;
3151    mb[1] = nv->firmware_feature.w & (BIT_1|BIT_0);
3152    status |= qla1280_mailbox_command(ha, BIT_1|BIT_0, &mb[0]);
3153
3154    /* Retry count and delay. */
3155    mb[0] = MBC_SET_RETRY_COUNT;
3156    mb[1] = nv->bus[0].retry_count;
3157    mb[2] = nv->bus[0].retry_delay;
3158    mb[6] = nv->bus[1].retry_count;
3159    mb[7] = nv->bus[1].retry_delay;
3160    status |= qla1280_mailbox_command(ha, BIT_7|BIT_6|BIT_2|BIT_1|BIT_0, &mb[0]);
3161
3162    /* ASYNC data setup time. */
3163    mb[0] = MBC_SET_ASYNC_DATA_SETUP;
3164    mb[1] = nv->bus[0].config_2.async_data_setup_time;
3165    mb[2] = nv->bus[1].config_2.async_data_setup_time;
3166    status |= qla1280_mailbox_command(ha, BIT_2|BIT_1|BIT_0, &mb[0]);
3167
3168    /* Active negation states. */
3169    mb[0] = MBC_SET_ACTIVE_NEGATION;
3170    mb[1] = 0;
3171    if (nv->bus[0].config_2.req_ack_active_negation)
3172        mb[1] |= BIT_5;
3173    if (nv->bus[0].config_2.data_line_active_negation)
3174        mb[1] |= BIT_4;
3175    mb[2] = 0;
3176    if (nv->bus[1].config_2.req_ack_active_negation)
3177        mb[2] |= BIT_5;
3178    if (nv->bus[1].config_2.data_line_active_negation)
3179        mb[2] |= BIT_4;
3180    status |= qla1280_mailbox_command(ha, BIT_2|BIT_1|BIT_0, &mb[0]);
3181
3182    /* Selection timeout. */
3183    mb[0] = MBC_SET_SELECTION_TIMEOUT;
3184    mb[1] = nv->bus[0].selection_timeout;
3185    mb[2] = nv->bus[1].selection_timeout;
3186    status |= qla1280_mailbox_command(ha, BIT_2|BIT_1|BIT_0, &mb[0]);
3187
3188    for (b = 0; b < ha->ports; b++)
3189    {
3190        /* SCSI Reset Disable. */
3191        ha->bus_settings[b].disable_scsi_reset = nv->bus[b].config_1.scsi_reset_disable;
3192
3193        /* Initiator ID. */
3194        ha->bus_settings[b].id = nv->bus[b].config_1.initiator_id;
3195        mb[0] = MBC_SET_INITIATOR_ID;
3196        mb[1] = b ? ha->bus_settings[b].id | BIT_7 : ha->bus_settings[b].id;
3197        status |= qla1280_mailbox_command(ha, BIT_1|BIT_0, &mb[0]);
3198
3199        /* Reset Delay. */
3200        ha->bus_settings[b].bus_reset_delay = nv->bus[b].bus_reset_delay;
3201
3202        /* Command queue depth per device. */
3203        ha->bus_settings[b].hiwat = nv->bus[b].max_queue_depth - 1;
3204
3205        /* Set target parameters. */
3206        for (t = 0; t < MAX_TARGETS; t++)
3207        {
3208            if( ha->device_id == QLA12160_DEVICE_ID ||
3209                ha->device_id == QLA10160_DEVICE_ID )
3210            {
3211                status = qla12160_set_target_parameters(ha,b,t,0,(nvram160_t *)nv);
3212            }
3213			else
3214            {
3215                /* Set Target Parameters. */
3216                mb[0] = MBC_SET_TARGET_PARAMETERS;
3217                mb[1] = (uint16_t)(b ? t | BIT_7 :t);
3218                mb[1] <<= 8;
3219                mb[2] = nv->bus[b].target[t].parameter.c << 8;
3220                mb[2] |= TP_AUTO_REQUEST_SENSE;
3221                mb[2] &= ~TP_STOP_QUEUE;
3222                mb[3] = nv->bus[b].target[t].flags.sync_offset << 8;
3223                mb[3] |= nv->bus[b].target[t].sync_period;
3224                status |= qla1280_mailbox_command(ha, BIT_3|BIT_2|BIT_1|BIT_0,
3225                    &mb[0]);
3226            }
3227
3228            /* Save Tag queuing enable flag. */
3229            mb[0] = BIT_0 << t;
3230            if (nv->bus[b].target[t].parameter.f.tag_queuing)
3231                ha->bus_settings[b].qtag_enables |= mb[0];
3232
3233            /* Save Device enable flag. */
3234            if (nv->bus[b].target[t].flags.device_enable)
3235                ha->bus_settings[b].device_enables |= mb[0];
3236
3237            /* Save LUN disable flag. */
3238            if (nv->bus[b].target[t].flags.lun_disable)
3239                ha->bus_settings[b].lun_disables |= mb[0];
3240
3241            /* Set Device Queue Parameters. */
3242            for (l = 0; l < MAX_LUNS; l++)
3243            {
3244                mb[0] = MBC_SET_DEVICE_QUEUE;
3245                mb[1] = (uint16_t)(b ? t | BIT_7 :t);
3246                mb[1] = mb[1] << 8 | l;
3247                mb[2] = nv->bus[b].max_queue_depth;
3248                mb[3] = nv->bus[b].target[t].execution_throttle;
3249                status |= qla1280_mailbox_command(ha, BIT_3|BIT_2|BIT_1|BIT_0,
3250                        &mb[0]);
3251            }
3252        }
3253    }
3254    DEBUG(ql_debug_print = 0;)
3255
3256#if defined(QL_DEBUG_ROUTINES) && !defined(QL_DEBUG_LEVEL_4)
3257    ql_debug_print = saved_print_status;
3258#endif
3259
3260#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
3261    DEBUG(if (status))
3262        DEBUG(qla1280_print("qla1280_nvram_config: **** FAILED ****\n\r");)
3263#endif
3264    LEAVE("qla1280_nvram_config");
3265    return(status);
3266}
3267
3268/*
3269 * Get NVRAM data word
3270 *      Calculates word position in NVRAM and calls request routine to
3271 *      get the word from NVRAM.
3272 *
3273 * Input:
3274 *      ha      = adapter block pointer.
3275 *      address = NVRAM word address.
3276 *
3277 * Returns:
3278 *      data word.
3279 */
3280STATIC uint16_t
3281qla1280_get_nvram_word(scsi_qla_host_t *ha, uint32_t address)
3282{
3283    uint32_t nv_cmd;
3284    uint16_t data;
3285
3286#ifdef QL_DEBUG_ROUTINES
3287    uint8_t  saved_print_status = ql_debug_print;
3288#endif
3289#ifdef QL_DEBUG_LEVEL_4
3290    ENTER("qla1280_get_nvram_word");
3291#endif
3292
3293    nv_cmd = address << 16;
3294    nv_cmd |= NV_READ_OP;
3295
3296#ifdef QL_DEBUG_ROUTINES
3297    ql_debug_print = FALSE;
3298#endif
3299    data = qla1280_nvram_request(ha, nv_cmd);
3300#ifdef QL_DEBUG_ROUTINES
3301    ql_debug_print = saved_print_status;
3302#endif
3303
3304#ifdef QL_DEBUG_LEVEL_4
3305    qla1280_print("qla1280_get_nvram_word: exiting normally NVRAM data = ");
3306    qla1280_output_number((uint32_t)data, 16);
3307    qla1280_print("\n\r");
3308#endif
3309    return(data);
3310}
3311
3312/*
3313 * NVRAM request
3314 *      Sends read command to NVRAM and gets data from NVRAM.
3315 *
3316 * Input:
3317 *      ha     = adapter block pointer.
3318 *      nv_cmd = Bit 26     = start bit
3319 *               Bit 25, 24 = opcode
3320 *               Bit 23-16  = address
3321 *               Bit 15-0   = write data
3322 *
3323 * Returns:
3324 *      data word.
3325 */
3326STATIC uint16_t
3327qla1280_nvram_request(scsi_qla_host_t *ha, uint32_t nv_cmd)
3328{
3329    uint8_t      cnt;
3330    device_reg_t *reg = ha->iobase;
3331    uint16_t     data = 0;
3332    uint16_t     reg_data;
3333
3334    /* Send command to NVRAM. */
3335
3336    nv_cmd <<= 5;
3337    for (cnt = 0; cnt < 11; cnt++)
3338    {
3339        if (nv_cmd & BIT_31)
3340            qla1280_nv_write(ha, NV_DATA_OUT);
3341        else
3342            qla1280_nv_write(ha, 0);
3343        nv_cmd <<= 1;
3344    }
3345
3346    /* Read data from NVRAM. */
3347
3348    for (cnt = 0; cnt < 16; cnt++)
3349    {
3350        WRT_REG_WORD(&reg->nvram, NV_SELECT+NV_CLOCK);
3351        /* qla1280_nv_delay(ha); */
3352        NVRAM_DELAY();
3353        data <<= 1;
3354        reg_data = RD_REG_WORD(&reg->nvram);
3355        if (reg_data & NV_DATA_IN)
3356            data |= BIT_0;
3357        WRT_REG_WORD(&reg->nvram, NV_SELECT);
3358        /* qla1280_nv_delay(ha); */
3359        NVRAM_DELAY();
3360    }
3361
3362    /* Deselect chip. */
3363
3364    WRT_REG_WORD(&reg->nvram, NV_DESELECT);
3365    /* qla1280_nv_delay(ha); */
3366    NVRAM_DELAY();
3367
3368    return(data);
3369}
3370
3371STATIC void
3372qla1280_nv_write(scsi_qla_host_t *ha, uint16_t data)
3373{
3374    device_reg_t *reg = ha->iobase;
3375
3376    WRT_REG_WORD(&reg->nvram, data | NV_SELECT);
3377    NVRAM_DELAY();
3378    /* qla1280_nv_delay(ha); */
3379    WRT_REG_WORD(&reg->nvram, data | NV_SELECT | NV_CLOCK);
3380    /* qla1280_nv_delay(ha); */
3381    NVRAM_DELAY();
3382    WRT_REG_WORD(&reg->nvram, data | NV_SELECT);
3383    /* qla1280_nv_delay(ha); */
3384    NVRAM_DELAY();
3385}
3386
3387STATIC void
3388qla1280_nv_delay(scsi_qla_host_t *ha)
3389{
3390    device_reg_t *reg = ha->iobase;
3391    int          cnt  = NV_DELAY_COUNT;
3392    uint16_t     data = 0;
3393
3394    while (cnt--)
3395        data |= RD_REG_WORD(&reg->nvram);
3396}
3397
3398/*
3399 * Mailbox Command
3400 *      Issue mailbox command and waits for completion.
3401 *
3402 * Input:
3403 *      ha = adapter block pointer.
3404 *      mr = mailbox registers to load.
3405 *      mb = data pointer for mailbox registers.
3406 *
3407 * Output:
3408 *      mb[MAILBOX_REGISTER_COUNT] = returned mailbox data.
3409 *
3410 * Returns:
3411 *      0 = success
3412 */
3413STATIC uint8_t
3414qla1280_mailbox_command(scsi_qla_host_t *ha, uint8_t mr, uint16_t *mb)
3415{
3416    device_reg_t *reg   = ha->iobase;
3417    uint8_t      status = 0;
3418    uint32_t     cnt;
3419    uint16_t     *optr, *iptr;
3420    uint16_t     data;
3421    srb_t        *done_q_first = 0;
3422    srb_t        *done_q_last = 0;
3423#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
3424    unsigned long cpu_flags = 0;
3425#endif
3426
3427#ifdef QL_DEBUG_LEVEL_3
3428    ENTER("qla1280_mailbox_command");
3429#endif
3430
3431    /* Acquire interrupt specific lock */
3432    QLA1280_INTR_LOCK(ha);
3433    DRIVER_LOCK
3434            ha->flags.mbox_busy = TRUE;
3435
3436    /* Load mailbox registers. */
3437    optr = (uint16_t *)&reg->mailbox0;
3438    iptr = mb;
3439    for (cnt = 0; cnt < MAILBOX_REGISTER_COUNT; cnt++)
3440    {
3441        if (mr & BIT_0)
3442        {
3443            WRT_REG_WORD(optr, (*iptr));
3444        }
3445
3446        mr >>= 1;
3447        optr++;
3448        iptr++;
3449    }
3450    /* Issue set host interrupt command. */
3451    ha->flags.mbox_int = FALSE;
3452    WRT_REG_WORD(&reg->host_cmd, HC_SET_HOST_INT);
3453    data = qla1280_debounce_register(&reg->istatus);
3454
3455    /* Wait for 30 seconds for command to finish. */
3456    for (cnt = 30000000; cnt > 0 && !ha->flags.mbox_int; cnt--)
3457    {
3458        /* Check for pending interrupts. */
3459        if (data & RISC_INT)
3460        {
3461            qla1280_isr(ha, (srb_t **)&done_q_first, (srb_t **)&done_q_last);
3462        }
3463        SYS_DELAY(1);
3464        data = RD_REG_WORD(&reg->istatus);
3465    }
3466
3467    /* Check for mailbox command timeout. */
3468    if ( !cnt )
3469    {
3470#ifdef QL_DEBUG_LEVEL_2
3471        qla1280_print(
3472                "qla1280_mailbox_command: **** Command Timeout, mailbox0 = ");
3473        qla1280_output_number((uint32_t)mb[0], 16);
3474        qla1280_print(" ****\n\r");
3475#endif
3476        ha->flags.isp_abort_needed = TRUE;
3477        status = 1;
3478    }
3479    else if (ha->mailbox_out[0] != MBS_CMD_CMP)
3480        status = 1;
3481
3482    /* Load return mailbox registers. */
3483    optr = mb;
3484    iptr = (uint16_t *)&ha->mailbox_out[0];
3485    mr = MAILBOX_REGISTER_COUNT;
3486    while (mr--)
3487        *optr++ = *iptr++;
3488
3489    /* Go check for any response interrupts pending. */
3490    ha->flags.mbox_busy = FALSE;
3491    qla1280_isr(ha, (srb_t **)&done_q_first, (srb_t **)&done_q_last);
3492
3493    /* Release interrupt specific lock */
3494    QLA1280_INTR_UNLOCK(ha);
3495    DRIVER_UNLOCK
3496
3497            if (ha->flags.isp_abort_needed)
3498        qla1280_abort_isp(ha);
3499
3500    if (ha->flags.reset_marker)
3501        qla1280_rst_aen(ha);
3502
3503    if (done_q_first)
3504        qla1280_done(ha, (srb_t **)&done_q_first, (srb_t **)&done_q_last);
3505
3506#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
3507    if (status)
3508    {
3509        qla1280_print("qla1280_mailbox_command: **** FAILED, mailbox0 = ");
3510        qla1280_output_number((uint32_t)mb[0], 16);
3511        qla1280_print(" ****\n\r");
3512    }
3513#endif
3514#ifdef QL_DEBUG_LEVEL_3
3515    else
3516        LEAVE("qla1280_mailbox_command");
3517#endif
3518    return(status);
3519}
3520
3521/*
3522 * qla1280_poll
3523 *      Polls ISP for interrupts.
3524 *
3525 * Input:
3526 *      ha = adapter block pointer.
3527 */
3528STATIC void
3529qla1280_poll(scsi_qla_host_t *ha)
3530{
3531    device_reg_t    *reg   = ha->iobase;
3532    uint16_t        data;
3533    srb_t           *done_q_first = 0;
3534    srb_t           *done_q_last = 0;
3535
3536#ifdef QL_DEBUG_LEVEL_3
3537    /* ENTER("qla1280_poll"); */
3538#endif
3539
3540    /* Acquire interrupt specific lock */
3541    QLA1280_INTR_LOCK(ha);
3542
3543    /* Check for pending interrupts. */
3544    data = RD_REG_WORD(&reg->istatus);
3545    if (data & RISC_INT)
3546        qla1280_isr(ha, (srb_t **)&done_q_first, (srb_t **)&done_q_last);
3547
3548    /* Release interrupt specific lock */
3549    QLA1280_INTR_UNLOCK(ha);
3550
3551    if (!ha->flags.mbox_busy)
3552    {
3553        if (ha->flags.isp_abort_needed)
3554            qla1280_abort_isp(ha);
3555        if (ha->flags.reset_marker)
3556            qla1280_rst_aen(ha);
3557    }
3558
3559    if (done_q_first)
3560        qla1280_done(ha, (srb_t **)&done_q_first, (srb_t **)&done_q_last);
3561
3562#ifdef QL_DEBUG_LEVEL_3
3563    /* LEAVE("qla1280_poll"); */
3564#endif
3565}
3566
3567/*
3568 * qla1280_bus_reset
3569 *      Issue SCSI bus reset.
3570 *
3571 * Input:
3572 *      ha = adapter block pointer.
3573 *      b  = SCSI bus number.
3574 *
3575 * Returns:
3576 *      0 = success
3577 */
3578STATIC uint8_t
3579qla1280_bus_reset(scsi_qla_host_t *ha, uint8_t b)
3580{
3581    uint8_t     status;
3582    uint16_t    mb[MAILBOX_REGISTER_COUNT];
3583
3584#ifdef QL_DEBUG_LEVEL_3
3585    qla1280_print("qla1280_bus_reset: entered\n\r");
3586#endif
3587   if( qla1280_verbose )
3588   {
3589    printk("scsi(%d): Resetting SCSI BUS (%d)\n",(int)ha->host_no,b);
3590   }
3591
3592    mb[0] = MBC_BUS_RESET;
3593    mb[1] = ha->bus_settings[b].bus_reset_delay;
3594    mb[2] = (uint16_t)b;
3595    status = qla1280_mailbox_command(ha, BIT_2|BIT_1|BIT_0, &mb[0]);
3596
3597    if (status)
3598    {
3599        if (ha->bus_settings[b].failed_reset_count > 2)                  /* dg - 03/13/99 */
3600            ha->bus_settings[b].scsi_bus_dead = TRUE;
3601        ha->bus_settings[b].failed_reset_count++;
3602    }
3603	else
3604    {
3605       QLA1280_DELAY(4);
3606       ha->bus_settings[b].scsi_bus_dead = FALSE;                         /* dg - 03/13/99 */
3607       ha->bus_settings[b].failed_reset_count = 0;
3608       /* Issue marker command. */
3609       qla1280_marker(ha, b, 0, 0, MK_SYNC_ALL);
3610    }
3611#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
3612    if (status)
3613        qla1280_print("qla1280_bus_reset: **** FAILED ****\n\r");
3614#endif
3615#ifdef QL_DEBUG_LEVEL_3
3616    else
3617        qla1280_print("qla1280_bus_reset: exiting normally\n\r");
3618#endif
3619    return(status);
3620}
3621
3622/*
3623 * qla1280_device_reset
3624 *      Issue bus device reset message to the target.
3625 *
3626 * Input:
3627 *      ha = adapter block pointer.
3628 *      b  = SCSI BUS number.
3629 *      t  = SCSI ID.
3630 *
3631 * Returns:
3632 *      0 = success
3633 */
3634STATIC uint8_t
3635qla1280_device_reset(scsi_qla_host_t *ha, uint8_t b, uint32_t t)
3636{
3637    uint8_t     status;
3638    uint16_t    mb[MAILBOX_REGISTER_COUNT];
3639
3640#ifdef QL_DEBUG_LEVEL_3
3641    ENTER("qla1280_device_reset");
3642#endif
3643
3644    mb[0] = MBC_ABORT_TARGET;
3645    mb[1] = (b ? (t | BIT_7) : t) << 8;
3646    mb[2] = 1;
3647    status = qla1280_mailbox_command(ha, BIT_2|BIT_1|BIT_0, &mb[0]);
3648
3649    /* Issue marker command. */
3650    qla1280_marker(ha, b, t, 0, MK_SYNC_ID);
3651
3652#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
3653    if (status)
3654        qla1280_print("qla1280_device_reset: **** FAILED ****\n\r");
3655#endif
3656#ifdef QL_DEBUG_LEVEL_3
3657    else
3658        LEAVE("qla1280_device_reset");
3659#endif
3660    return(status);
3661}
3662
3663/*
3664 * qla1280_abort_device
3665 *      Issue an abort message to the device
3666 *
3667 * Input:
3668 *      ha = adapter block pointer.
3669 *      b  = SCSI BUS.
3670 *      t  = SCSI ID.
3671 *      l  = SCSI LUN.
3672 *
3673 * Returns:
3674 *      0 = success
3675 */
3676STATIC uint8_t
3677qla1280_abort_device(scsi_qla_host_t *ha, uint8_t b, uint32_t t, uint32_t l)
3678{
3679    uint8_t     status;
3680    uint16_t    mb[MAILBOX_REGISTER_COUNT];
3681
3682#ifdef QL_DEBUG_LEVEL_3
3683    ENTER("qla1280_abort_device");
3684#endif
3685
3686    mb[0] = MBC_ABORT_DEVICE;
3687    mb[1] = (b ? t | BIT_7 : t) << 8 | l;
3688    status = qla1280_mailbox_command(ha, BIT_1|BIT_0, &mb[0]);
3689
3690    /* Issue marker command. */
3691    qla1280_marker(ha, b, t, l, MK_SYNC_ID_LUN);
3692
3693#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
3694    if (status)
3695        qla1280_print("qla1280_abort_device: **** FAILED ****\n\r");
3696#endif
3697#ifdef QL_DEBUG_LEVEL_3
3698    else
3699        LEAVE("qla1280_abort_device");
3700#endif
3701    return(status);
3702}
3703
3704/*
3705 * qla1280_abort_command
3706 *      Abort command aborts a specified IOCB.
3707 *
3708 * Input:
3709 *      ha = adapter block pointer.
3710 *      sp = SB structure pointer.
3711 *
3712 * Returns:
3713 *      0 = success
3714 */
3715STATIC uint8_t
3716qla1280_abort_command(scsi_qla_host_t *ha, srb_t *sp)
3717{
3718    uint8_t         status;
3719    uint16_t        mb[MAILBOX_REGISTER_COUNT];
3720    uint32_t        b, t, l;
3721    uint32_t        handle;
3722
3723#ifdef QL_DEBUG_LEVEL_3
3724    ENTER("qla1280_abort_command");
3725#endif
3726
3727    /* Locate handle number. */
3728    for (handle = 0; handle < MAX_OUTSTANDING_COMMANDS; handle++)
3729        if (ha->outstanding_cmds[handle] == sp)
3730            break;
3731
3732                b  = SCSI_BUS_32(sp->cmd);
3733        t  = SCSI_TCN_32(sp->cmd);
3734        l  = SCSI_LUN_32(sp->cmd);
3735
3736        mb[0] = MBC_ABORT_COMMAND;
3737        mb[1] = (b ? t | BIT_7 : t) << 8 | l;
3738        mb[2] = handle >> 16;
3739        mb[3] = (uint16_t)handle;
3740        status = qla1280_mailbox_command(ha, BIT_3|BIT_2|BIT_1|BIT_0, &mb[0]);
3741
3742#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
3743        if (status)
3744            qla1280_print("qla1280_abort_command: **** FAILED ****\n\r");
3745#endif
3746        sp->flags |= SRB_ABORT_PENDING;
3747
3748        LEAVE("qla1280_abort_command");
3749        return(status);
3750}
3751
3752/*
3753 * qla1280_reset_adapter
3754 *      Reset adapter.
3755 *
3756 * Input:
3757 *      ha = adapter block pointer.
3758 */
3759STATIC void
3760qla1280_reset_adapter(scsi_qla_host_t *ha)
3761{
3762    device_reg_t *reg = ha->iobase;
3763
3764#ifdef QL_DEBUG_LEVEL_3
3765    ENTER("qla1280_reset_adapter");
3766#endif
3767
3768    /* Disable ISP chip */
3769    ha->flags.online = FALSE;
3770    WRT_REG_WORD(&reg->ictrl, ISP_RESET);
3771    WRT_REG_WORD(&reg->host_cmd, HC_RESET_RISC);
3772    WRT_REG_WORD(&reg->host_cmd, HC_RELEASE_RISC);
3773    WRT_REG_WORD(&reg->host_cmd, HC_DISABLE_BIOS);
3774
3775#ifdef QL_DEBUG_LEVEL_3
3776    LEAVE("qla1280_reset_adapter");
3777#endif
3778}
3779
3780/*
3781 *  Issue marker command.
3782 *      Function issues marker IOCB.
3783 *
3784 * Input:
3785 *      ha   = adapter block pointer.
3786 *      b    = SCSI BUS number
3787 *      t    = SCSI ID
3788 *      l    = SCSI LUN
3789 *      type = marker modifier
3790 */
3791STATIC void
3792qla1280_marker(scsi_qla_host_t *ha, uint8_t b, uint32_t t, uint32_t l, uint8_t type)
3793{
3794    mrk_entry_t     *pkt;
3795
3796#ifdef QL_DEBUG_LEVEL_3
3797    ENTER("qla1280_marker");
3798#endif
3799
3800    /* Get request packet. */
3801    if ( (pkt = (mrk_entry_t *)qla1280_req_pkt(ha)) )
3802    {
3803        pkt->entry_type = MARKER_TYPE;
3804        pkt->lun = (uint8_t)l;
3805        pkt->target = (uint8_t)(b ? (t | BIT_7) : t);
3806        pkt->modifier = type;
3807
3808        /* Issue command to ISP */
3809        qla1280_isp_cmd(ha);
3810    }
3811
3812#ifdef QL_DEBUG_LEVEL_3
3813    LEAVE("qla1280_marker");
3814#endif
3815}
3816
3817#if  QLA1280_64BIT_SUPPORT
3818/*
3819 * qla1280_64bit_start_scsi
3820 *      The start SCSI is responsible for building request packets on
3821 *      request ring and modifying ISP input pointer.
3822 *
3823 * Input:
3824 *      ha = adapter block pointer.
3825 *      sp = SB structure pointer.
3826 *
3827 * Returns:
3828 *      0 = success, was able to issue command.
3829 */
3830STATIC uint8_t
3831qla1280_64bit_start_scsi(scsi_qla_host_t *ha, srb_t *sp)
3832{
3833    device_reg_t    *reg   = ha->iobase;
3834    uint8_t         status = 0;
3835    Scsi_Cmnd       *cmd = sp->cmd;
3836    uint32_t        cnt;
3837    cmd_a64_entry_t     *pkt;
3838    uint16_t        req_cnt;
3839    uint16_t        seg_cnt;
3840    struct scatterlist    *sg = (struct scatterlist *) NULL;
3841    uint32_t        *dword_ptr;
3842
3843#ifdef QL_DEBUG_LEVEL_3
3844    ENTER("qla1280_64bit_start_scsi:");
3845#endif
3846
3847    if( qla1280_check_for_dead_scsi_bus(ha, sp) )
3848    {
3849        return(0);
3850    }
3851
3852    /* Calculate number of entries and segments required. */
3853    seg_cnt = 0;
3854    req_cnt = 1;
3855    if (cmd->use_sg)
3856    {
3857        seg_cnt =  cmd->use_sg;
3858        sg = (struct scatterlist *) cmd->request_buffer;
3859
3860        if (seg_cnt > 2)
3861        {
3862           req_cnt += (uint16_t)(seg_cnt - 2) / 5;
3863           if ((uint16_t)(seg_cnt - 2) % 5)
3864               req_cnt++;
3865        }
3866    }
3867    else if (cmd->request_bufflen)  /* If data transfer. */
3868    {
3869        DEBUG(printk("Single data transfer (0x%x)\n",cmd->request_bufflen));
3870        seg_cnt = 1;
3871    }
3872
3873    /* Acquire ring specific lock */
3874    QLA1280_RING_LOCK(ha);
3875
3876    if ((uint16_t)(req_cnt + 2) >= ha->req_q_cnt)
3877    {
3878        /* Calculate number of free request entries. */
3879        cnt = RD_REG_WORD(&reg->mailbox4);
3880        if (ha->req_ring_index < cnt)
3881            ha->req_q_cnt = cnt - ha->req_ring_index;
3882        else
3883            ha->req_q_cnt = REQUEST_ENTRY_CNT - (ha->req_ring_index - cnt);
3884    }
3885
3886    /* If room for request in request ring. */
3887    if ((uint16_t)(req_cnt + 2) < ha->req_q_cnt)
3888    {
3889        /* Check for room in outstanding command list. */
3890        for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS &&
3891            ha->outstanding_cmds[cnt] != 0; cnt++)
3892            ;
3893
3894        if (cnt < MAX_OUTSTANDING_COMMANDS)
3895        {
3896            ha->outstanding_cmds[cnt] = sp;
3897            ha->req_q_cnt -= req_cnt;
3898            CMD_HANDLE(sp->cmd) = (unsigned char *) (u_long) cnt;
3899
3900            /*
3901            * Build command packet.
3902            */
3903            pkt = (cmd_a64_entry_t *)ha->request_ring_ptr;
3904
3905            pkt->entry_type = COMMAND_A64_TYPE;
3906            pkt->entry_count = (uint8_t)req_cnt;
3907            pkt->sys_define = (uint8_t)ha->req_ring_index;
3908            pkt->handle = (uint32_t)cnt;
3909
3910            /* Zero out remaining portion of packet. */
3911            dword_ptr = (uint32_t *)pkt + 2;
3912            for (cnt = 2; cnt < REQUEST_ENTRY_SIZE/4; cnt++)
3913                *dword_ptr++ = 0;
3914
3915            /* Set ISP command timeout. */
3916            pkt->timeout = (uint16_t)30;
3917
3918            /* Set device target ID and LUN */
3919            pkt->lun = SCSI_LUN_32(cmd);
3920            pkt->target = SCSI_BUS_32(cmd) ?
3921                    (SCSI_TCN_32(cmd) | BIT_7) : SCSI_TCN_32(cmd);
3922
3923            /* Enable simple tag queuing if device supports it. */
3924            if (cmd->device->tagged_queue )
3925                pkt->control_flags |= BIT_3;
3926
3927            /* Load SCSI command packet. */
3928            pkt->cdb_len = (uint16_t)CMD_CDBLEN(cmd);
3929            BCOPY(&(CMD_CDBP(cmd)), pkt->scsi_cdb, pkt->cdb_len);
3930            DEBUG(printk("Build packet for command[0]=0x%x\n",pkt->scsi_cdb[0]));
3931
3932            /*
3933            * Load data segments.
3934            */
3935            if (seg_cnt)                /* If data transfer. */
3936            {
3937                /* Set transfer direction. */
3938                if ( (cmd->data_cmnd[0] == WRITE_6) )
3939                    pkt->control_flags |= BIT_6;
3940                else
3941                    pkt->control_flags |= (BIT_5|BIT_6);
3942
3943                sp->dir = pkt->control_flags & (BIT_5|BIT_6);
3944
3945                /* Set total data segment count. */
3946                pkt->dseg_count = seg_cnt;
3947
3948                /* Setup packet address segment pointer. */
3949                dword_ptr = (uint32_t *)&pkt->dseg_0_address;
3950
3951                if (cmd->use_sg)              /* If scatter gather */
3952                {
3953                    /* Load command entry data segments. */
3954                    for (cnt = 0; cnt < 2 && seg_cnt; cnt++, seg_cnt--)
3955                    {
3956                        DEBUG(sprintf(debug_buff,"SG Segment ap=0x%p, len=0x%x\n\r",sg->address,sg->length));
3957                        DEBUG(qla1280_print(debug_buff));
3958                        *dword_ptr++ = cpu_to_le32(VIRT_TO_BUS_LOW(sg->address));
3959                        *dword_ptr++ = cpu_to_le32(VIRT_TO_BUS_HIGH(sg->address));
3960                        *dword_ptr++ = sg->length;
3961                        sg++;
3962                    }
3963#ifdef QL_DEBUG_LEVEL_5
3964                    qla1280_print(
3965                            "qla1280_64bit_start_scsi: Scatter/gather command packet data - ");
3966                    qla1280_print("b ");
3967                    qla1280_output_number((uint32_t)SCSI_BUS_32(cmd), 10);
3968                    qla1280_print(" t ");
3969                    qla1280_output_number((uint32_t)SCSI_TCN_32(cmd), 10);
3970                    qla1280_print(" d ");
3971                    qla1280_output_number((uint32_t)SCSI_LUN_32(cmd), 10);
3972                    qla1280_print("\n\r");
3973                    qla1280_dump_buffer((caddr_t)pkt, REQUEST_ENTRY_SIZE);
3974#endif
3975                    /*
3976                    * Build continuation packets.
3977                    */
3978                    while (seg_cnt > 0)
3979                    {
3980                        /* Adjust ring index. */
3981                        ha->req_ring_index++;
3982                        if (ha->req_ring_index == REQUEST_ENTRY_CNT)
3983                        {
3984                            ha->req_ring_index = 0;
3985                            ha->request_ring_ptr = ha->request_ring;
3986                        }
3987                        else
3988                            ha->request_ring_ptr++;
3989
3990                        pkt = (cmd_a64_entry_t *)ha->request_ring_ptr;
3991
3992                        /* Zero out packet. */
3993                        dword_ptr = (uint32_t *)pkt;
3994                        for (cnt = 0;cnt < REQUEST_ENTRY_SIZE/4; cnt++)
3995                            *dword_ptr++ = 0;
3996
3997                        /* Load packet defaults. */
3998                        ((cont_a64_entry_t *)pkt)->entry_type =
3999                                CONTINUE_A64_TYPE;
4000                        ((cont_a64_entry_t *)pkt)->entry_count = 1;
4001                        ((cont_a64_entry_t *)pkt)->sys_define = (uint8_t)
4002                                ha->req_ring_index;
4003
4004                        /* Setup packet address segment pointer. */
4005                        dword_ptr = (uint32_t *)
4006                                &((cont_a64_entry_t *)pkt)->dseg_0_address;
4007
4008                        /* Load continuation entry data segments. */
4009                        for (cnt = 0; cnt < 5 && seg_cnt; cnt++, seg_cnt--)
4010                        {
4011                            *dword_ptr++ = cpu_to_le32(VIRT_TO_BUS_LOW(sg->address));
4012                            *dword_ptr++ = cpu_to_le32(VIRT_TO_BUS_HIGH(sg->address));
4013                            *dword_ptr++ = sg->length;
4014                            sg++;
4015                        }
4016#ifdef QL_DEBUG_LEVEL_5
4017                        qla1280_print(
4018                                "qla1280_64bit_start_scsi: continuation packet data - c");
4019                        qla1280_print(" b ");
4020                        qla1280_output_number((uint32_t)SCSI_BUS_32(cmd), 10);
4021
4022                        qla1280_print(" t ");
4023                        qla1280_output_number((uint32_t)SCSI_TCN_32(cmd), 10);
4024                        qla1280_print(" d ");
4025                        qla1280_output_number((uint32_t)SCSI_LUN_32(cmd), 10);
4026                        qla1280_print("\n\r");
4027                        qla1280_dump_buffer((caddr_t)pkt, REQUEST_ENTRY_SIZE);
4028#endif
4029                    }
4030                }
4031                else                    /* No scatter gather data transfer */
4032                {
4033                    *dword_ptr++ = cpu_to_le32(VIRT_TO_BUS_LOW(cmd->request_buffer));
4034                    *dword_ptr++ = cpu_to_le32(VIRT_TO_BUS_HIGH(cmd->request_buffer));
4035                    *dword_ptr = (uint32_t) cmd->request_bufflen;
4036#ifdef QL_DEBUG_LEVEL_5
4037                    qla1280_print(
4038                            "qla1280_64bit_start_scsi: No scatter/gather command packet data - c");
4039                    qla1280_print(" b ");
4040                    qla1280_output_number((uint32_t)SCSI_BUS_32(cmd), 10);
4041                    qla1280_print(" t ");
4042                    qla1280_output_number((uint32_t)SCSI_TCN_32(cmd), 10);
4043                    qla1280_print(" d ");
4044                    qla1280_output_number((uint32_t)SCSI_LUN_32(cmd), 10);
4045                    qla1280_print("\n\r");
4046                    qla1280_dump_buffer((caddr_t)pkt, REQUEST_ENTRY_SIZE);
4047#endif
4048                }
4049            }
4050#ifdef QL_DEBUG_LEVEL_5
4051            else                            /* No data transfer */
4052            {
4053                *dword_ptr++ = (uint32_t) 0;
4054                *dword_ptr++ = (uint32_t) 0;
4055                *dword_ptr = (uint32_t)  0;
4056                qla1280_print(
4057                        "qla1280_64bit_start_scsi: No data, command packet data - c");
4058                qla1280_print(" b ");
4059                qla1280_output_number((uint32_t)SCSI_BUS_32(cmd), 10);
4060                qla1280_print(" t ");
4061                qla1280_output_number((uint32_t)SCSI_TCN_32(cmd), 10);
4062                qla1280_print(" d ");
4063                qla1280_output_number((uint32_t)SCSI_LUN_32(cmd), 10);
4064                qla1280_print("\n\r");
4065                qla1280_dump_buffer((caddr_t)pkt, REQUEST_ENTRY_SIZE);
4066            }
4067#endif
4068            /* Adjust ring index. */
4069            ha->req_ring_index++;
4070            if (ha->req_ring_index == REQUEST_ENTRY_CNT)
4071            {
4072                ha->req_ring_index = 0;
4073                ha->request_ring_ptr = ha->request_ring;
4074            }
4075            else
4076                ha->request_ring_ptr++;
4077
4078            /* Set chip new ring index. */
4079            WRT_REG_WORD(&reg->mailbox4, ha->req_ring_index);
4080        }
4081        else
4082        {
4083            status = 1;
4084#ifdef QL_DEBUG_LEVEL_2
4085            qla1280_print(
4086                    "qla1280_64bit_start_scsi: NO ROOM IN OUTSTANDING ARRAY\n\r");
4087            qla1280_print(" req_q_cnt=");
4088            qla1280_output_number((uint32_t)ha->req_q_cnt, 16);
4089#endif
4090        }
4091    }
4092    else
4093    {
4094        status = 1;
4095#ifdef QL_DEBUG_LEVEL_2
4096        qla1280_print("qla1280_64bit_start_scsi: in-ptr=");
4097        qla1280_output_number((uint32_t)ha->req_ring_index, 16);
4098        qla1280_print(" req_q_cnt=");
4099        qla1280_output_number((uint32_t)ha->req_q_cnt, 16);
4100        qla1280_print(" req_cnt=");
4101        qla1280_output_number((uint32_t)req_cnt, 16);
4102        qla1280_print("\n\r");
4103#endif
4104    }
4105
4106    /* Release ring specific lock */
4107    QLA1280_RING_UNLOCK(ha);
4108
4109#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
4110    if (status)
4111        qla1280_print("qla1280_64bit_start_scsi: **** FAILED ****\n\r");
4112#endif
4113#ifdef QL_DEBUG_LEVEL_3
4114    else
4115        qla1280_print("qla1280_64bit_start_scsi: exiting normally\n\r");
4116#endif
4117    return(status);
4118}
4119#endif  /* QLA1280_64BIT_SUPPORT */
4120
4121/*
4122 * qla1280_32bit_start_scsi
4123 *      The start SCSI is responsible for building request packets on
4124 *      request ring and modifying ISP input pointer.
4125 *
4126 *      The Qlogic firmware interface allows every queue slot to have a SCSI
4127 *      command and up to 4 scatter/gather (SG) entries.  If we need more
4128 *      than 4 SG entries, then continuation entries are used that can
4129 *      hold another 7 entries each.  The start routine determines if there
4130 *      is eought empty slots then build the combination of requests to
4131 *      fulfill the OS request.
4132 *
4133 * Input:
4134 *      ha = adapter block pointer.
4135 *      sp = SCSI Request Block structure pointer.
4136 *
4137 * Returns:
4138 *      0 = success, was able to issue command.
4139 */
4140STATIC uint8_t
4141qla1280_32bit_start_scsi(scsi_qla_host_t *ha, srb_t *sp)
4142{
4143    device_reg_t    *reg   = ha->iobase;
4144    uint8_t         status = 0;
4145    Scsi_Cmnd       *cmd = sp->cmd;
4146    uint32_t        cnt;
4147    cmd_entry_t     *pkt;
4148    uint16_t        req_cnt;
4149    uint16_t        seg_cnt;
4150    struct scatterlist    *sg = (struct scatterlist *) NULL;
4151    uint8_t        *data_ptr;
4152    uint32_t        *dword_ptr;
4153
4154    ENTER("qla1280_32bit_start_scsi");
4155
4156
4157    if( qla1280_check_for_dead_scsi_bus(ha, sp) )
4158    {
4159        return(0);
4160    }
4161
4162    /* Calculate number of entries and segments required. */
4163    req_cnt = 1;
4164    if (cmd->use_sg)
4165    {
4166        /*
4167        * We must build an SG list in adapter format, as the kernel's SG list
4168        * cannot be used directly because of data field size (__alpha__)
4169        * differences and the kernel SG list uses virtual addresses where
4170        * we need physical addresses.
4171        */
4172        seg_cnt =  cmd->use_sg;
4173        sg = (struct scatterlist *) cmd->request_buffer;
4174        /*
4175        * if greater than four sg entries then we need to allocate
4176        * continuation entries
4177        */
4178        if (seg_cnt > 4)
4179        {
4180            req_cnt += (uint16_t)(seg_cnt - 4) / 7;
4181            if ((uint16_t)(seg_cnt - 4) % 7)
4182                req_cnt++;
4183        }
4184        DEBUG(sprintf(debug_buff,"S/G for data transfer -num segs(%d), req blk cnt(%d)\n\r",seg_cnt,req_cnt));
4185        DEBUG(qla1280_print(debug_buff));
4186    }
4187    else if (cmd->request_bufflen)  /* If data transfer. */
4188    {
4189        DEBUG(printk("Single data transfer (0x%x)\n",cmd->request_bufflen));
4190        seg_cnt = 1;
4191    }
4192    else
4193    {
4194        DEBUG(printk("No data transfer \n"));
4195        seg_cnt = 0;
4196    }
4197
4198    /* Acquire ring specific lock */
4199    QLA1280_RING_LOCK(ha);
4200
4201    if ((uint16_t)(req_cnt + 2) >= ha->req_q_cnt)
4202    {
4203        /* Calculate number of free request entries. */
4204        cnt = RD_REG_WORD(&reg->mailbox4);
4205        if (ha->req_ring_index < cnt)
4206            ha->req_q_cnt = cnt - ha->req_ring_index;
4207        else
4208            ha->req_q_cnt = REQUEST_ENTRY_CNT - (ha->req_ring_index - cnt);
4209    }
4210
4211    DEBUG(sprintf(debug_buff,"Number of free entries = (%d)\n\r",ha->req_q_cnt));
4212    DEBUG(qla1280_print(debug_buff));
4213    /* If room for request in request ring. */
4214    if ((uint16_t)(req_cnt + 2) < ha->req_q_cnt)
4215    {
4216        /* Check for empty slot in outstanding command list. */
4217        for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS &&
4218            (ha->outstanding_cmds[cnt] != 0); cnt++)
4219            ;
4220
4221        if (cnt < MAX_OUTSTANDING_COMMANDS)
4222        {
4223            CMD_HANDLE(sp->cmd) = (unsigned char *)(unsigned long)cnt;
4224            ha->outstanding_cmds[cnt] = sp;
4225            ha->req_q_cnt -= req_cnt;
4226
4227            /*
4228            * Build command packet.
4229            */
4230            pkt = (cmd_entry_t *)ha->request_ring_ptr;
4231
4232            pkt->entry_type = COMMAND_TYPE;
4233            pkt->entry_count = (uint8_t)req_cnt;
4234            pkt->sys_define = (uint8_t)ha->req_ring_index;
4235            pkt->handle = (uint32_t)cnt;
4236
4237            /* Zero out remaining portion of packet. */
4238            dword_ptr = (uint32_t *)pkt + 2;
4239            for (cnt = 2; cnt < REQUEST_ENTRY_SIZE/4; cnt++)
4240                *dword_ptr++ = 0;
4241
4242            /* Set ISP command timeout. */
4243            pkt->timeout = (uint16_t)30;
4244
4245            /* Set device target ID and LUN */
4246            pkt->lun = SCSI_LUN_32(cmd);
4247            pkt->target = SCSI_BUS_32(cmd) ?
4248                    (SCSI_TCN_32(cmd) | BIT_7) : SCSI_TCN_32(cmd);
4249
4250            /* Enable simple tag queuing if device supports it. */
4251            if (cmd->device->tagged_queue )
4252                pkt->control_flags |= BIT_3;
4253
4254            /* Load SCSI command packet. */
4255            pkt->cdb_len = (uint16_t)CMD_CDBLEN(cmd);
4256            data_ptr = (uint8_t *) &(CMD_CDBP(cmd));
4257            for (cnt = 0; cnt < pkt->cdb_len; cnt++)
4258                pkt->scsi_cdb[cnt] = *data_ptr++;
4259            DEBUG(printk("Build packet for command[0]=0x%x\n",pkt->scsi_cdb[0]));
4260            /*
4261            * Load data segments.
4262            */
4263            if (seg_cnt)
4264            {
4265                DEBUG(printk("loading data segments..\n"));
4266                /* Set transfer direction (READ and WRITE) */
4267                /* Linux doesn't tell us                   */
4268
4269                /*
4270                * 3/10 dg - Normally, we should need this check with our F/W
4271                * but because of a small issue with it we do.
4272                *
4273                * For block devices, cmd->request.cmd has the operation
4274                * For character devices, this isn't always set properly, so
4275                * we need to check data_cmnd[0].  This catches the conditions
4276                * for st.c, but not sg. Generic commands are pass down to us.
4277                */
4278                if ( (cmd->data_cmnd[0] == WRITE_6) )
4279                    pkt->control_flags |= BIT_6;
4280                else
4281                    pkt->control_flags |= (BIT_5|BIT_6);
4282
4283                sp->dir = pkt->control_flags & (BIT_5|BIT_6);
4284
4285                /* Set total data segment count. */
4286                pkt->dseg_count = seg_cnt;
4287
4288                /* Setup packet address segment pointer. */
4289                dword_ptr = (uint32_t *)&pkt->dseg_0_address;
4290
4291                if (cmd->use_sg)     /* If scatter gather */
4292                {
4293                    DEBUG(qla1280_print("Building S/G data segments..\n\r"));
4294                    DEBUG(qla1280_dump_buffer((caddr_t)sg, 4*16 ));
4295                    /* Load command entry data segments. */
4296                    for (cnt = 0; cnt < 4 && seg_cnt; cnt++, seg_cnt--)
4297                    {
4298                        *dword_ptr++ = (uint32_t) cpu_to_le32(VIRT_TO_BUS(sg->address));
4299                        *dword_ptr++ = sg->length;
4300                        DEBUG(sprintf(debug_buff,"SG Segment ap=0x%p, len=0x%x\n\r",sg->address,sg->length));
4301                        DEBUG(qla1280_print(debug_buff));
4302                        sg++;
4303                    }
4304                    /*
4305                    * Build continuation packets.
4306                    */
4307                    while (seg_cnt > 0)
4308                    {
4309                        /* Adjust ring index. */
4310                        ha->req_ring_index++;
4311                        if (ha->req_ring_index == REQUEST_ENTRY_CNT)
4312                        {
4313                            ha->req_ring_index = 0;
4314                            ha->request_ring_ptr = ha->request_ring;
4315                        }
4316                        else
4317                            ha->request_ring_ptr++;
4318
4319                        pkt = (cmd_entry_t *)ha->request_ring_ptr;
4320
4321                        /* Zero out packet. */
4322                        dword_ptr = (uint32_t *)pkt;
4323                        for (cnt = 0;cnt < REQUEST_ENTRY_SIZE/4; cnt++)
4324                            *dword_ptr++ = 0;
4325
4326                        /* Load packet defaults. */
4327                        ((cont_entry_t *)pkt)->entry_type =
4328                                CONTINUE_TYPE;
4329                        ((cont_entry_t *)pkt)->entry_count = 1;
4330
4331                        ((cont_entry_t *)pkt)->sys_define = (uint8_t)
4332                                ha->req_ring_index;
4333
4334                        /* Setup packet address segment pointer. */
4335                        dword_ptr = (uint32_t *)
4336                                &((cont_entry_t *)pkt)->dseg_0_address;
4337
4338                        /* Load continuation entry data segments. */
4339                        for (cnt = 0; cnt < 7 && seg_cnt; cnt++, seg_cnt--)
4340                        {
4341                            *dword_ptr++ = (u_int) cpu_to_le32(VIRT_TO_BUS(sg->address));
4342                            *dword_ptr++ = sg->length;
4343                            sg++;
4344                        }
4345#ifdef QL_DEBUG_LEVEL_5
4346                        qla1280_print(
4347                                "qla1280_32bit_start_scsi: continuation packet data - scsi(");
4348                        qla1280_output_number((uint32_t)SCSI_BUS_32(cmd), 10);
4349                        qla1280_print(":");
4350                        qla1280_output_number((uint32_t)SCSI_TCN_32(cmd), 10);
4351                        qla1280_print(":");
4352                        qla1280_output_number((uint32_t)SCSI_LUN_32(cmd), 10);
4353                        qla1280_print(")\n\r");
4354                        qla1280_dump_buffer((caddr_t)pkt, REQUEST_ENTRY_SIZE);
4355#endif
4356                    }
4357                }
4358                else                    /* No scatter gather data transfer */
4359                {
4360                    *dword_ptr++ = (uint32_t) cpu_to_le32(VIRT_TO_BUS(cmd->request_buffer));
4361                    *dword_ptr = (uint32_t) cmd->request_bufflen;
4362                    DEBUG(printk("Single Segment ap=0x%p, len=0x%x\n",cmd->request_buffer,cmd->request_bufflen));
4363                }
4364            }
4365            else                            /* No data transfer */
4366            {
4367                *dword_ptr++ = (uint32_t) 0;
4368                *dword_ptr = (uint32_t)  0;
4369#ifdef QL_DEBUG_LEVEL_5
4370                qla1280_print(
4371                        "qla1280_32bit_start_scsi: No data, command packet data - ");
4372                qla1280_print("\n\r");
4373                qla1280_dump_buffer((caddr_t)pkt, REQUEST_ENTRY_SIZE);
4374#endif
4375            }
4376#ifdef QL_DEBUG_LEVEL_5
4377            qla1280_print("qla1280_32bit_start_scsi: First IOCB block:\n\r");
4378            qla1280_dump_buffer((caddr_t)ha->request_ring_ptr, REQUEST_ENTRY_SIZE);
4379#endif
4380            /* Adjust ring index. */
4381            ha->req_ring_index++;
4382            if (ha->req_ring_index == REQUEST_ENTRY_CNT)
4383            {
4384                ha->req_ring_index = 0;
4385                ha->request_ring_ptr = ha->request_ring;
4386            }
4387            else
4388                ha->request_ring_ptr++;
4389
4390            /* Set chip new ring index. */
4391            DEBUG(qla1280_print("qla1280_32bit_start_scsi: Wakeup RISC for pending command\n\r"));
4392            ha->qthreads--;
4393            sp->u_start = jiffies;
4394            sp->flags |= SRB_SENT;
4395            ha->actthreads++;
4396            /* qla1280_output_number((uint32_t)ha->actthreads++, 16); */
4397            WRT_REG_WORD(&reg->mailbox4, ha->req_ring_index);
4398        }
4399        else
4400        {
4401            status = 1;
4402#ifdef QL_DEBUG_LEVEL_2
4403            qla1280_print(
4404                    "qla1280_32bit_start_scsi: NO ROOM IN OUTSTANDING ARRAY\n\r");
4405            qla1280_print(" req_q_cnt=");
4406            qla1280_output_number((uint32_t)ha->req_q_cnt, 16);
4407            qla1280_print("\n\r");
4408#endif
4409        }
4410    }
4411    else
4412    {
4413        status = 1;
4414#ifdef QL_DEBUG_LEVEL_2
4415        /*  qla1280_print("qla1280_32bit_start_scsi: in-ptr=");
4416        qla1280_output_number((uint32_t)ha->req_ring_index, 16);
4417        qla1280_print(" req_q_cnt=");
4418        qla1280_output_number((uint32_t)ha->req_q_cnt, 16);
4419        qla1280_print(" req_cnt=");
4420        qla1280_output_number((uint32_t)req_cnt, 16);
4421        qla1280_print("\n\r"); */
4422#endif
4423    }
4424
4425    /* Release ring specific lock */
4426    QLA1280_RING_UNLOCK(ha);
4427
4428#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
4429    /* if (status)
4430    qla1280_print("qla1280_32bit_start_scsi: **** FAILED ****\n\r"); */
4431#endif
4432#ifdef QL_DEBUG_LEVEL_3
4433            LEAVE("qla1280_32bit_start_scsi");
4434#endif
4435    return(status);
4436}
4437
4438/*
4439 * qla1280_req_pkt
4440 *      Function is responsible for locking ring and
4441 *      getting a zeroed out request packet.
4442 *
4443 * Input:
4444 *      ha  = adapter block pointer.
4445 *
4446 * Returns:
4447 *      0 = failed to get slot.
4448 */
4449STATIC request_t *
4450qla1280_req_pkt(scsi_qla_host_t *ha)
4451{
4452    device_reg_t    *reg = ha->iobase;
4453    request_t       *pkt = 0;
4454    uint16_t        cnt;
4455    uint32_t        *dword_ptr;
4456    uint32_t        timer;
4457
4458#ifdef QL_DEBUG_LEVEL_3
4459    ENTER("qla1280_req_pkt");
4460#endif
4461
4462    /* Wait for 30 seconds for slot. */
4463    for (timer = 15000000; timer; timer--)
4464    {
4465        /* Acquire ring specific lock */
4466        QLA1280_RING_LOCK(ha);
4467
4468        if (ha->req_q_cnt > 0)
4469        {
4470            /* Calculate number of free request entries. */
4471            cnt = RD_REG_WORD(&reg->mailbox4);
4472            if (ha->req_ring_index < cnt)
4473                ha->req_q_cnt = cnt - ha->req_ring_index;
4474            else
4475                ha->req_q_cnt = REQUEST_ENTRY_CNT - (ha->req_ring_index - cnt);
4476        }
4477
4478        /* Found empty request ring slot? */
4479        if (ha->req_q_cnt > 0)
4480        {
4481            ha->req_q_cnt--;
4482            pkt = ha->request_ring_ptr;
4483
4484            /* Zero out packet. */
4485            dword_ptr = (uint32_t *)pkt;
4486            for (cnt = 0; cnt < REQUEST_ENTRY_SIZE/4; cnt++)
4487                *dword_ptr++ = 0;
4488
4489            /* Set system defined field. */
4490            pkt->sys_define = (uint8_t)ha->req_ring_index;
4491
4492            /* Set entry count. */
4493            pkt->entry_count = 1;
4494
4495            break;
4496        }
4497
4498        /* Release ring specific lock */
4499        QLA1280_RING_UNLOCK(ha);
4500
4501        SYS_DELAY(2);   /* 10 */
4502
4503        /* Check for pending interrupts. */
4504        qla1280_poll(ha);
4505    }
4506
4507#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
4508    if (!pkt)
4509        qla1280_print("qla1280_req_pkt: **** FAILED ****\n\r");
4510#endif
4511#ifdef QL_DEBUG_LEVEL_3
4512    else
4513        qla1280_print("qla1280_req_pkt: exiting normally\n\r");
4514#endif
4515    return(pkt);
4516}
4517
4518/*
4519 * qla1280_isp_cmd
4520 *      Function is responsible for modifying ISP input pointer.
4521 *      Releases ring lock.
4522 *
4523 * Input:
4524 *      ha  = adapter block pointer.
4525 */
4526STATIC void
4527qla1280_isp_cmd(scsi_qla_host_t *ha)
4528{
4529    device_reg_t    *reg = ha->iobase;
4530
4531#ifdef QL_DEBUG_LEVEL_3
4532    ENTER("qla1280_isp_cmd");
4533#endif
4534
4535#ifdef QL_DEBUG_LEVEL_5
4536    qla1280_print("qla1280_isp_cmd: IOCB data:\n\r");
4537    qla1280_dump_buffer((caddr_t)ha->request_ring_ptr, REQUEST_ENTRY_SIZE);
4538#endif
4539
4540    /* Adjust ring index. */
4541    ha->req_ring_index++;
4542    if (ha->req_ring_index == REQUEST_ENTRY_CNT)
4543    {
4544        ha->req_ring_index = 0;
4545        ha->request_ring_ptr = ha->request_ring;
4546    }
4547    else
4548        ha->request_ring_ptr++;
4549
4550    /* Set chip new ring index. */
4551    WRT_REG_WORD(&reg->mailbox4, ha->req_ring_index);
4552
4553    /* Release ring specific lock */
4554    QLA1280_RING_UNLOCK(ha);
4555
4556#ifdef QL_DEBUG_LEVEL_3
4557    LEAVE("qla1280_isp_cmd");
4558#endif
4559}
4560
4561/*
4562 * qla1280_enable_lun
4563 *      Issue enable LUN entry IOCB.
4564 *
4565 * Input:
4566 *      ha = adapter block pointer.
4567 *      b  = SCSI BUS number.
4568 *      l  = LUN number.
4569 */
4570STATIC void
4571qla1280_enable_lun(scsi_qla_host_t *ha, uint8_t b, uint32_t l)
4572{
4573    elun_entry_t    *pkt;
4574
4575#ifdef QL_DEBUG_LEVEL_3
4576    qla1280_print("qla1280_enable_lun: entered\n\r");
4577#endif
4578
4579    /* Get request packet. */
4580    /*
4581    if (pkt = (elun_entry_t *)qla1280_req_pkt(ha))
4582    {
4583    pkt->entry_type = ENABLE_LUN_TYPE;
4584    pkt->lun = (uint16_t)(b ? l | BIT_15 : l);
4585    pkt->command_count = 32;
4586    pkt->immed_notify_count = 1;
4587    pkt->group_6_length = MAX_CMDSZ;
4588    pkt->group_7_length = MAX_CMDSZ;
4589    pkt->timeout = 0x30;
4590
4591    qla1280_isp_cmd(ha);
4592    }
4593    */
4594    pkt = (elun_entry_t *)1;
4595
4596#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
4597    if (!pkt)
4598        qla1280_print("qla1280_enable_lun: **** FAILED ****\n\r");
4599#endif
4600#ifdef QL_DEBUG_LEVEL_3
4601    else
4602        qla1280_print("qla1280_enable_lun: exiting normally\n\r");
4603#endif
4604}
4605
4606#if QL1280_TARGET_MODE_SUPPORT
4607/****************************************************************************/
4608/*                      Target Mode Support Functions.                      */
4609/****************************************************************************/
4610
4611/*
4612 * qla1280_notify_ack
4613 *      Issue notify acknowledge IOCB.
4614 *      If sequence ID is zero, acknowledgement of
4615 *      SCSI bus reset or bus device reset is assumed.
4616 *
4617 * Input:
4618 *      ha      = adapter block pointer.
4619 *      inotify = immediate notify entry pointer.
4620 */
4621STATIC void
4622qla1280_notify_ack(scsi_qla_host_t *ha, notify_entry_t *inotify)
4623{
4624    nack_entry_t    *pkt;
4625
4626#ifdef QL_DEBUG_LEVEL_3
4627    qla1280_print("qla1280_notify_ack: entered\n\r");
4628#endif
4629
4630    /* Get request packet. */
4631    if (pkt = (nack_entry_t *)qla1280_req_pkt(ha))
4632    {
4633        pkt->entry_type = NOTIFY_ACK_TYPE;
4634        pkt->lun = inotify->lun;
4635        pkt->initiator_id = inotify->initiator_id;
4636        pkt->target_id = inotify->target_id;
4637        if (inotify->seq_id == 0)
4638            pkt->event = BIT_7;
4639        else
4640            pkt->seq_id = inotify->seq_id;
4641
4642        /* Issue command to ISP */
4643        qla1280_isp_cmd(ha);
4644    }
4645
4646#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
4647    if (!pkt)
4648        qla1280_print("qla1280_notify_ack: **** FAILED ****\n\r");
4649#endif
4650#ifdef QL_DEBUG_LEVEL_3
4651    else
4652        qla1280_print("qla1280_notify_ack: exiting normally\n\r");
4653#endif
4654}
4655
4656/*
4657 * qla1280_immed_notify
4658 *      Issue immediate notify IOCB for LUN 0.
4659 *
4660 * Input:
4661 *      ha      = adapter block pointer.
4662 *      inotify = immediate notify entry pointer.
4663 */
4664STATIC void
4665qla1280_immed_notify(scsi_qla_host_t *ha, notify_entry_t *inotify)
4666{
4667    notify_entry_t    *pkt;
4668
4669#ifdef QL_DEBUG_LEVEL_3
4670    qla1280_print("qla1280_immed_notify: entered\n\r");
4671#endif
4672
4673    /* Get request packet. */
4674    if (pkt = (notify_entry_t *)qla1280_req_pkt(ha))
4675    {
4676        pkt->entry_type = IMMED_NOTIFY_TYPE;
4677        pkt->lun = inotify->lun;
4678        pkt->initiator_id = inotify->initiator_id;
4679        pkt->target_id = inotify->target_id;
4680        pkt->status = 1;
4681
4682        /* Issue command to ISP */
4683        qla1280_isp_cmd(ha);
4684    }
4685
4686#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
4687    if (!pkt)
4688        qla1280_print("qla1280_immed_notify: **** FAILED ****\n\r");
4689#endif
4690#ifdef QL_DEBUG_LEVEL_3
4691    else
4692        qla1280_print("qla1280_immed_notify: exiting normally\n\r");
4693#endif
4694}
4695
4696/*
4697 * qla1280_accept_io
4698 *      Issue accept target I/O IOCB for LUN 0.
4699 *
4700 * Input:
4701 *      ha = adapter block pointer.
4702 *      ctio = ctio returned entry pointer.
4703 */
4704STATIC void
4705qla1280_accept_io(scsi_qla_host_t *ha, ctio_ret_entry_t *ctio)
4706{
4707    atio_entry_t    *pkt;
4708
4709#ifdef QL_DEBUG_LEVEL_3
4710    qla1280_print("qla1280_accept_io: entered\n\r");
4711#endif
4712
4713    /* Get request packet. */
4714    if (pkt = (atio_entry_t *)qla1280_req_pkt(ha))
4715    {
4716        pkt->entry_type = ACCEPT_TGT_IO_TYPE;
4717        pkt->lun = ctio->lun;
4718        pkt->initiator_id = ctio->initiator_id;
4719        pkt->target_id = ctio->target_id;
4720        pkt->tag_value = ctio->tag_value;
4721        pkt->status = 1;
4722
4723        /* Issue command to ISP */
4724        qla1280_isp_cmd(ha);
4725    }
4726
4727#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
4728    if (!pkt)
4729        qla1280_print("qla1280_accept_io: **** FAILED ****\n\r");
4730#endif
4731#ifdef QL_DEBUG_LEVEL_3
4732    else
4733        qla1280_print("qla1280_accept_io: exiting normally\n\r");
4734#endif
4735}
4736
4737/*
4738 * qla1280_64bit_continue_io
4739 *      Issue continue target I/O IOCB.
4740 *
4741 * Input:
4742 *      ha   = adapter block pointer.
4743 *      atio = atio pointer.
4744 *      len  = total bytecount.
4745 *      addr = physical address pointer.
4746 */
4747STATIC void
4748qla1280_64bit_continue_io(scsi_qla_host_t *ha, atio_entry_t *atio, uint32_t len,
4749                    paddr32_t *addr)
4750{
4751    ctio_a64_entry_t *pkt;
4752    uint32_t         *dword_ptr;
4753
4754#ifdef QL_DEBUG_LEVEL_3
4755    qla1280_print("qla1280_64bit_continue_io: entered\n\r");
4756#endif
4757
4758    /* Get request packet. */
4759    if (pkt = (ctio_a64_entry_t *)qla1280_req_pkt(ha))
4760    {
4761        pkt->entry_type = CTIO_A64_TYPE;
4762        pkt->lun = atio->lun;
4763        pkt->initiator_id = atio->initiator_id;
4764        pkt->target_id = atio->target_id;
4765        pkt->option_flags = atio->option_flags;
4766        pkt->tag_value = atio->tag_value;
4767        pkt->scsi_status = atio->scsi_status;
4768
4769        if (len)
4770        {
4771            pkt->dseg_count = 1;
4772            pkt->transfer_length = len;
4773            pkt->dseg_0_length = len;
4774            dword_ptr = (uint32_t *)addr;
4775            pkt->dseg_0_address[0] = *dword_ptr++;
4776            pkt->dseg_0_address[1] = *dword_ptr;
4777        }
4778
4779        /* Issue command to ISP */
4780        qla1280_isp_cmd(ha);
4781    }
4782
4783#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
4784    if (!pkt)
4785        qla1280_print("qla1280_64bit_continue_io: **** FAILED ****\n\r");
4786#endif
4787#ifdef QL_DEBUG_LEVEL_3
4788    else
4789        qla1280_print("qla1280_64bit_continue_io: exiting normally\n\r");
4790#endif
4791}
4792
4793/*
4794 * qla1280_32bit_continue_io
4795 *      Issue continue target I/O IOCB.
4796 *
4797 * Input:
4798 *      ha   = adapter block pointer.
4799 *      atio = atio pointer.
4800 *      len  = total bytecount.
4801 *      addr = physical address pointer.
4802 */
4803STATIC void
4804qla1280_32bit_continue_io(scsi_qla_host_t *ha, atio_entry_t *atio, uint32_t len,
4805                    paddr32_t *addr)
4806{
4807    ctio_entry_t *pkt;
4808    uint32_t     *dword_ptr;
4809
4810#ifdef QL_DEBUG_LEVEL_3
4811    qla1280_print("qla1280_32bit_continue_io: entered\n\r");
4812#endif
4813
4814    /* Get request packet. */
4815    if (pkt = (ctio_entry_t *)qla1280_req_pkt(ha))
4816    {
4817        pkt->entry_type = CONTINUE_TGT_IO_TYPE;
4818        pkt->lun = atio->lun;
4819        pkt->initiator_id = atio->initiator_id;
4820        pkt->target_id = atio->target_id;
4821        pkt->option_flags = atio->option_flags;
4822        pkt->tag_value = atio->tag_value;
4823        pkt->scsi_status = atio->scsi_status;
4824
4825        if (len)
4826        {
4827            pkt->dseg_count = 1;
4828            pkt->transfer_length = len;
4829            pkt->dseg_0_length = len;
4830            dword_ptr = (uint32_t *)addr;
4831            pkt->dseg_0_address = *dword_ptr;
4832        }
4833
4834        /* Issue command to ISP */
4835        qla1280_isp_cmd(ha);
4836    }
4837
4838#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
4839    if (!pkt)
4840        qla1280_print("qla1280_32bit_continue_io: **** FAILED ****\n\r");
4841#endif
4842#ifdef QL_DEBUG_LEVEL_3
4843    else
4844        qla1280_print("qla1280_32bit_continue_io: exiting normally\n\r");
4845#endif
4846}
4847#endif /* QL1280_TARGET_MODE_SUPPORT */
4848
4849/****************************************************************************/
4850/*                        Interrupt Service Routine.                        */
4851/****************************************************************************/
4852
4853/****************************************************************************
4854 *  qla1280_isr
4855 *      Calls I/O done on command completion.
4856 *
4857 * Input:
4858 *      ha           = adapter block pointer.
4859 *      done_q_first = done queue first pointer.
4860 *      done_q_last  = done queue last pointer.
4861 *      INTR_LOCK must be already obtained.
4862 ****************************************************************************/
4863STATIC void
4864qla1280_isr(scsi_qla_host_t *ha, srb_t **done_q_first, srb_t **done_q_last)
4865{
4866    device_reg_t    *reg = ha->iobase;
4867    response_t      *pkt;
4868    srb_t           *sp;
4869    uint16_t        mailbox[MAILBOX_REGISTER_COUNT];
4870    uint16_t        *wptr;
4871    uint32_t        index;
4872
4873    ENTER("qla1280_isr");
4874
4875
4876    /* Save mailbox register 5 */
4877    mailbox[5] = RD_REG_WORD(&reg->mailbox5);
4878
4879    /* Check for mailbox interrupt. */
4880
4881    mailbox[0] = RD_REG_WORD(&reg->semaphore);
4882    if (mailbox[0] & BIT_0)
4883    {
4884        /* Get mailbox data. */
4885
4886        wptr = &mailbox[0];
4887        *wptr++ = RD_REG_WORD(&reg->mailbox0);
4888        *wptr++ = RD_REG_WORD(&reg->mailbox1);
4889        *wptr = RD_REG_WORD(&reg->mailbox2);
4890        if (mailbox[0] != MBA_SCSI_COMPLETION)
4891        {
4892            wptr++;
4893            *wptr++ = RD_REG_WORD(&reg->mailbox3);
4894            *wptr++ = RD_REG_WORD(&reg->mailbox4);
4895            wptr++;
4896            *wptr++ = RD_REG_WORD(&reg->mailbox6);
4897            *wptr   = RD_REG_WORD(&reg->mailbox7);
4898        }
4899
4900        /* Release mailbox registers. */
4901
4902        WRT_REG_WORD(&reg->semaphore, 0);
4903        WRT_REG_WORD(&reg->host_cmd, HC_CLR_RISC_INT);
4904
4905#ifdef QL_DEBUG_LEVEL_5
4906        qla1280_print("qla1280_isr: mailbox interrupt mailbox[0] = ");
4907        qla1280_output_number((uint32_t)mailbox[0], 16);
4908        qla1280_print("\n\r");
4909#endif
4910
4911        /* Handle asynchronous event */
4912
4913        switch (mailbox[0])
4914        {
4915            case MBA_SCSI_COMPLETION:   /* Response completion */
4916#ifdef QL_DEBUG_LEVEL_5
4917                qla1280_print("qla1280_isr: mailbox response completion\n\r");
4918#endif
4919                if (ha->flags.online)
4920                {
4921                    /* Get outstanding command index. */
4922                    index = (uint32_t)(mailbox[2] << 16 | mailbox[1]);
4923
4924                    /* Validate handle. */
4925                    if (index < MAX_OUTSTANDING_COMMANDS)
4926                        sp = ha->outstanding_cmds[index];
4927                    else
4928                        sp = 0;
4929
4930                    if (sp)
4931                    {
4932                        /* Free outstanding command slot. */
4933                        ha->outstanding_cmds[index] = 0;
4934
4935                        /* Save ISP completion status */
4936                        CMD_RESULT(sp->cmd) = 0;
4937
4938                        /* Place block on done queue */
4939                        sp->s_next = NULL;
4940                        sp->s_prev = *done_q_last;
4941                        if (!*done_q_first)
4942                            *done_q_first = sp;
4943                        else
4944                            (*done_q_last)->s_next = sp;
4945                        *done_q_last = sp;
4946                    }
4947                    else
4948                    {
4949#ifdef QL_DEBUG_LEVEL_2
4950                            qla1280_print("qla1280_isr: ISP invalid handle\n\r");
4951#endif
4952                            printk(KERN_WARNING "qla1280: ISP invalid handle");
4953                            ha->flags.isp_abort_needed = TRUE;
4954                    }
4955                }
4956                break;
4957            case MBA_BUS_RESET:         /* SCSI Bus Reset */
4958#ifdef QL_DEBUG_LEVEL_2
4959                qla1280_print("qla1280_isr: asynchronous BUS_RESET\n\r");
4960#endif
4961                ha->flags.reset_marker = TRUE;
4962                index = mailbox[6] & BIT_0;
4963                ha->bus_settings[index].reset_marker = TRUE;
4964                break;
4965            case MBA_SYSTEM_ERR:        /* System Error */
4966#ifdef QL_DEBUG_LEVEL_2
4967                qla1280_print("qla1280_isr: ISP System Error - mbx1=");
4968                qla1280_output_number((uint32_t)mailbox[1], 16);
4969                qla1280_print(", mbx2=");
4970                qla1280_output_number((uint32_t)mailbox[2], 16);
4971                qla1280_print(", mbx3=");
4972                qla1280_output_number((uint32_t)mailbox[3], 16);
4973                qla1280_print("\n\r");
4974#endif
4975                printk(KERN_WARNING
4976                        "qla1280: ISP System Error - mbx1=%xh, mbx2=%xh, mbx3=%xh\n",
4977                        mailbox[1], mailbox[2], mailbox[3]);
4978                ha->flags.isp_abort_needed = TRUE;
4979                break;
4980            case MBA_REQ_TRANSFER_ERR:  /* Request Transfer Error */
4981#ifdef QL_DEBUG_LEVEL_2
4982                qla1280_print("qla1280_isr: ISP Request Transfer Error\n\r");
4983#endif
4984                printk(KERN_WARNING "qla1280: ISP Request Transfer Error\n");
4985                ha->flags.isp_abort_needed = TRUE;
4986                break;
4987            case MBA_RSP_TRANSFER_ERR:  /* Response Transfer Error */
4988#ifdef QL_DEBUG_LEVEL_2
4989                qla1280_print("qla1280_isr: ISP Response Transfer Error\n\r");
4990#endif
4991                printk(KERN_WARNING "qla1280: ISP Response Transfer Error\n");
4992                ha->flags.isp_abort_needed = TRUE;
4993                break;
4994            case MBA_WAKEUP_THRES:      /* Request Queue Wake-up */
4995#ifdef QL_DEBUG_LEVEL_2
4996                qla1280_print("qla1280_isr: asynchronous WAKEUP_THRES\n\r");
4997#endif
4998                break;
4999            case MBA_TIMEOUT_RESET:     /* Execution Timeout Reset */
5000#ifdef QL_DEBUG_LEVEL_2
5001                qla1280_print("qla1280_isr: asynchronous TIMEOUT_RESET\n\r");
5002#endif
5003                break;
5004            case MBA_DEVICE_RESET:         /* Bus Device Reset */
5005#ifdef QL_DEBUG_LEVEL_2
5006                qla1280_print(
5007                        "qla1280_isr: asynchronous BUS_DEVICE_RESET\n\r");
5008#endif
5009                ha->flags.reset_marker = TRUE;
5010                index = mailbox[6] & BIT_0;
5011                ha->bus_settings[index].reset_marker = TRUE;
5012                break;
5013            case MBA_BUS_MODE_CHANGE:
5014#ifdef QL_DEBUG_LEVEL_2
5015                qla1280_print(
5016                        "qla1280_isr: asynchronous BUS_MODE_CHANGE\n\r");
5017#endif
5018                break;
5019            default:
5020                if (mailbox[0] < MBA_ASYNC_EVENT)
5021                {
5022                        wptr = &mailbox[0];
5023                        ha->mailbox_out[0] = *wptr++;
5024                        ha->mailbox_out[1] = *wptr++;
5025                        ha->mailbox_out[2] = *wptr++;
5026                        ha->mailbox_out[3] = *wptr++;
5027                        ha->mailbox_out[4] = *wptr++;
5028                        ha->mailbox_out[5] = *wptr++;
5029                        ha->mailbox_out[6] = *wptr++;
5030                        ha->mailbox_out[7] = *wptr;
5031                        ha->flags.mbox_int = TRUE;
5032                }
5033                break;
5034        }
5035    }
5036    else
5037        WRT_REG_WORD(&reg->host_cmd, HC_CLR_RISC_INT);
5038
5039    /*
5040    * Response ring
5041    */
5042    if (ha->flags.online && !ha->flags.mbox_busy)
5043    {
5044        if (mailbox[5] < RESPONSE_ENTRY_CNT)
5045        {
5046            while (ha->rsp_ring_index != mailbox[5])
5047            {
5048                pkt = ha->response_ring_ptr;
5049
5050#ifdef QL_DEBUG_LEVEL_5
5051                qla1280_print("qla1280_isr: ha->rsp_ring_index = ");
5052                qla1280_output_number((uint32_t)ha->rsp_ring_index, 16);
5053                qla1280_print(" mailbox[5] = ");
5054                qla1280_output_number((uint32_t)mailbox[5], 16);
5055                qla1280_print("\n\rqla1280_isr: response packet data\n\r");
5056                qla1280_dump_buffer((caddr_t)pkt, RESPONSE_ENTRY_SIZE);
5057#endif
5058
5059#if defined(QL_DEBUG_LEVEL_2) && !defined(QL_DEBUG_LEVEL_5)
5060                if (pkt->entry_type == STATUS_TYPE)
5061                {
5062                    if ((uint8_t)(pkt->scsi_status) || pkt->comp_status ||
5063                        pkt->entry_status)
5064                    {
5065                        DEBUG(qla1280_print("qla1280_isr: ha->rsp_ring_index = ");)
5066                        DEBUG(qla1280_output_number((uint32_t)ha->rsp_ring_index,
5067                                16);)
5068                        DEBUG(qla1280_print(" mailbox[5] = ");)
5069                        DEBUG(qla1280_output_number((uint32_t)mailbox[5], 16);)
5070                        DEBUG(qla1280_print( "\n\r comp_status = ");)
5071                        DEBUG(qla1280_output_number((uint32_t)pkt->comp_status,16);)
5072                        DEBUG(qla1280_print( ", ");)
5073                        DEBUG(qla1280_print( " scsi_status = ");)
5074                        DEBUG(qla1280_output_number((uint32_t)pkt->scsi_status,16);)
5075                        DEBUG(qla1280_print( "\n\r");)
5076                        /* qla1280_print(
5077                        "\n\rqla1280_isr: response packet data\n\r");
5078                        qla1280_dump_buffer((caddr_t)pkt,
5079                        RESPONSE_ENTRY_SIZE); */
5080                    }
5081                }
5082                else
5083                {
5084                    qla1280_print("qla1280_isr: ha->rsp_ring_index = ");
5085                    qla1280_output_number((uint32_t)ha->rsp_ring_index, 16);
5086                    qla1280_print(" mailbox[5] = ");
5087                    qla1280_output_number((uint32_t)mailbox[5], 16);
5088                    qla1280_print(
5089                            "\n\rqla1280_isr: response packet data\n\r");
5090                    qla1280_dump_buffer((caddr_t)pkt, RESPONSE_ENTRY_SIZE);
5091                }
5092#endif
5093                if (pkt->entry_type == STATUS_TYPE || pkt->entry_status)
5094                {
5095                    if (pkt->entry_type == STATUS_TYPE)
5096                        qla1280_status_entry(ha, (sts_entry_t *)pkt,
5097                                done_q_first, done_q_last);
5098                    else
5099                        qla1280_error_entry(ha, pkt,
5100                                done_q_first, done_q_last);
5101
5102                    /* Adjust ring index. */
5103                    ha->rsp_ring_index++;
5104                    if (ha->rsp_ring_index == RESPONSE_ENTRY_CNT)
5105                    {
5106                        ha->rsp_ring_index = 0;
5107                        ha->response_ring_ptr = ha->response_ring;
5108                    }
5109                    else
5110                        ha->response_ring_ptr++;
5111                    WRT_REG_WORD(&reg->mailbox5, ha->rsp_ring_index);
5112                }
5113#if QLA1280_TARGET_MODE_SUPPORT
5114                else
5115                {
5116                    pkt = &response_entry;
5117
5118                    /* Copy packet. */
5119                    dptr1 = (uint32_t *)ha->response_ring_ptr;
5120                    dptr2 = (uint32_t *)pkt;
5121                    for (index = 0; index < RESPONSE_ENTRY_SIZE/4; index++)
5122                        *dptr2++ = *dptr1++;
5123
5124                    /* Adjust ring index. */
5125                    ha->rsp_ring_index++;
5126                    if (ha->rsp_ring_index == RESPONSE_ENTRY_CNT)
5127                    {
5128                        ha->rsp_ring_index = 0;
5129                        ha->response_ring_ptr = ha->response_ring;
5130                    }
5131                    else
5132                        ha->response_ring_ptr++;
5133                    WRT_REG_WORD(&reg->mailbox5, ha->rsp_ring_index);
5134
5135                    /* Release interrupt specific lock */
5136                    QLA1280_INTR_UNLOCK(ha);
5137
5138                    switch (pkt->entry_type)
5139                    {
5140                        case ACCEPT_TGT_IO_TYPE:
5141                            qla1280_atio_entry(ha, (atio_entry_t *)pkt);
5142                            break;
5143                        case IMMED_NOTIFY_TYPE:
5144                            qla1280_notify_entry(ha, (notify_entry_t *)pkt);
5145                            break;
5146                        case CTIO_RET_TYPE:
5147                            qla1280_accept_io(ha, (ctio_ret_entry_t *)pkt);
5148                            break;
5149                        default:
5150                            break;
5151                    }
5152
5153                    /* Acquire interrupt specific lock */
5154                    QLA1280_INTR_LOCK(ha);
5155                }
5156#endif
5157            }
5158        }
5159        else
5160        {
5161            ha->flags.isp_abort_needed = TRUE;
5162#ifdef QL_DEBUG_LEVEL_2
5163            qla1280_print("qla1280_isr: Response pointer Error\n");
5164#endif
5165        }
5166    }
5167
5168    LEAVE("qla1280_isr");
5169}
5170
5171/*
5172 *  qla1280_rst_aen
5173 *      Processes asynchronous reset.
5174 *
5175 * Input:
5176 *      ha  = adapter block pointer.
5177 */
5178STATIC void
5179qla1280_rst_aen(scsi_qla_host_t *ha)
5180{
5181#if QL1280_TARGET_MODE_SUPPORT
5182    notify_entry_t  nentry;
5183#endif
5184    uint8_t         b;
5185
5186#ifdef QL_DEBUG_LEVEL_3
5187    ENTER("qla1280_rst_aen");
5188#endif
5189
5190    if (ha->flags.online && !ha->flags.reset_active &&
5191        !ha->flags.abort_isp_active)
5192    {
5193        ha->flags.reset_active = TRUE;
5194        while (ha->flags.reset_marker)
5195        {
5196            /* Issue marker command. */
5197            ha->flags.reset_marker = FALSE;
5198            for (b = 0; b < ha->ports && !ha->flags.reset_marker; b++)
5199            {
5200                if (ha->bus_settings[b].reset_marker)
5201                {
5202                    ha->bus_settings[b].reset_marker = FALSE;
5203                    qla1280_marker(ha, b, 0, 0, MK_SYNC_ALL);
5204
5205                    if (!ha->flags.reset_marker)
5206                    {
5207#if QL1280_TARGET_MODE_SUPPORT
5208                        /* Issue notify acknowledgement command. */
5209                        bzero((caddr_t)&nentry, sizeof(notify_entry_t));
5210
5211                        nentry.initiator_id = nentry.target_id = b ?
5212                                ha->bus_settings[b].id | BIT_7 :
5213                        ha->bus_settings[b].id;
5214                        qla1280_notify_entry(ha, &nentry);
5215#endif
5216
5217                        /* Asynchronous event notification */
5218                    }
5219                }
5220            }
5221        }
5222    }
5223
5224#ifdef QL_DEBUG_LEVEL_3
5225    LEAVE("qla1280_rst_aen");
5226#endif
5227}
5228
5229#if QL1280_TARGET_MODE_SUPPORT
5230/*
5231 *  qla1280_atio_entry
5232 *      Processes received ISP accept target I/O entry.
5233 *
5234 * Input:
5235 *      ha  = adapter block pointer.
5236 *      pkt = entry pointer.
5237 */
5238STATIC void
5239qla1280_atio_entry(scsi_qla_host_t *ha, atio_entry_t *pkt)
5240{
5241    uint64_t    *a64;
5242    uint64_t    *end_a64;
5243    paddr32_t   phy_addr[2];
5244    paddr32_t   end_addr[2];
5245    uint32_t    len;
5246    uint32_t    offset;
5247    uint8_t     t;
5248    uint8_t     *sense_ptr;
5249
5250#ifdef QL_DEBUG_LEVEL_3
5251    qla1280_print("qla1280_atio_entry: entered\n\r");
5252#endif
5253
5254    t = pkt->initiator_id;
5255    sense_ptr = ha->tsense + t * TARGET_SENSE_SIZE;
5256    a64 = (uint64_t *)&phy_addr[0];
5257    end_a64 = (uint64_t *)&end_addr[0];
5258
5259    switch (pkt->status & ~BIT_7)
5260    {
5261        case 7:                         /* Path invalid */
5262#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
5263            qla1280_print("qla1280_atio_entry: Path invalid\n\r");
5264#endif
5265            break;
5266        case 0x14:                  /* Target Bus Phase Sequence Failure */
5267#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
5268            qla1280_print(
5269                    "qla1280_atio_entry: Target Bus Phase Sequence Failure\n\r");
5270#endif
5271            if (pkt->status & BIT_7)
5272            {
5273                BCOPY((caddr_t)&pkt->sense_data, sense_ptr,TARGET_SENSE_SIZE);
5274            }
5275            else
5276            {
5277                    bzero(sense_ptr, TARGET_SENSE_SIZE);
5278                    *sense_ptr = 0x70;
5279                    *(sense_ptr+2) = SD_HARDERR;
5280                    *(sense_ptr+7) = TARGET_SENSE_SIZE-8;
5281                    *(sense_ptr+12) = SC_SELFAIL;
5282            }
5283            pkt->scsi_status = S_CKCON;
5284            pkt->option_flags |= (uint32_t)OF_SSTS | (uint32_t)OF_NO_DATA;
5285            if (ha->flags.enable_64bit_addressing)
5286                qla1280_64bit_continue_io(ha, pkt, 0, 0);
5287            else
5288                qla1280_32bit_continue_io(ha, pkt, 0, 0);
5289            break;
5290        case 0x16:                  /* Requested Capability Not Available */
5291#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
5292            qla1280_print(
5293                    "qla1280_atio_entry: Target Bus Phase Sequence Failure\n\r");
5294#endif
5295            break;
5296        case 0x17:                  /* Bus Device Reset Message Received */
5297#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
5298            qla1280_print(
5299                    "qla1280_atio_entry: Target Bus Phase Sequence Failure\n\r");
5300#endif
5301            break;
5302        case 0x3D:                  /* CDB Received */
5303
5304            /* Check for invalid LUN */
5305            if (pkt->lun && pkt->cdb[0] != SS_INQUIR &&
5306                pkt->cdb[0] != SS_REQSEN)
5307                pkt->cdb[0] = SS_TEST;
5308
5309            switch (pkt->cdb[0])
5310            {
5311                case SS_TEST:
5312#ifdef QL_DEBUG_LEVEL_3
5313                    qla1280_print("qla1280_atio_entry: SS_TEST\n\r");
5314#endif
5315                    bzero(sense_ptr, TARGET_SENSE_SIZE);
5316                    len = 0;
5317                    if (pkt->lun == 0)
5318                        pkt->scsi_status = S_GOOD;
5319                    else
5320                    {
5321                        *sense_ptr = 0x70;
5322                        *(sense_ptr+2) = SD_ILLREQ;
5323                        *(sense_ptr+7) = TARGET_SENSE_SIZE-8;
5324                        *(sense_ptr+12) = SC_INVLUN;
5325                        pkt->scsi_status = S_CKCON;
5326                    }
5327
5328                    pkt->option_flags |= (uint32_t)OF_SSTS |
5329                            (uint32_t)OF_NO_DATA;
5330                    break;
5331                case SS_REQSEN:
5332#ifdef QL_DEBUG_LEVEL_3
5333                    qla1280_print("qla1280_atio_entry: SS_REQSEN\n\r");
5334#endif
5335                    phy_addr[0] = ha->tsense_dma;
5336                    phy_addr[1] = 0;
5337                    *a64 += t * TARGET_SENSE_SIZE;
5338                    if (pkt->cdb[4] > TARGET_SENSE_SIZE)
5339                        len = TARGET_SENSE_SIZE;
5340                    else
5341                        len = pkt->cdb[4];
5342                    pkt->scsi_status = S_GOOD;
5343                    pkt->option_flags |= (uint32_t)OF_SSTS |
5344                            (uint32_t)OF_DATA_IN;
5345                    break;
5346                case SS_INQUIR:
5347#ifdef QL_DEBUG_LEVEL_3
5348                    qla1280_print("qla1280_atio_entry: SS_INQUIR\n\r");
5349#endif
5350                    bzero(sense_ptr, TARGET_SENSE_SIZE);
5351                    phy_addr[0] = ha->tbuf_dma;
5352                    phy_addr[1] = 0;
5353                    *a64 += TARGET_INQ_OFFSET;
5354
5355                    if (pkt->lun == 0)
5356                    {
5357                            ha->tbuf->inq.id_type = ID_PROCESOR;
5358                            ha->tbuf->inq.id_pqual = ID_QOK;
5359                    }
5360                    else
5361                    {
5362                            ha->tbuf->inq.id_type = ID_NODEV;
5363                            ha->tbuf->inq.id_pqual = ID_QNOLU;
5364                    }
5365
5366                    if (pkt->cdb[4] > sizeof(struct ident))
5367                        len = sizeof(struct ident);
5368                    else
5369                        len = pkt->cdb[4];
5370                    pkt->scsi_status = S_GOOD;
5371                    pkt->option_flags |= (uint32_t)OF_SSTS |
5372                            (uint32_t)OF_DATA_IN;
5373                    break;
5374                case SM_WRDB:
5375                    bzero(sense_ptr, TARGET_SENSE_SIZE);
5376                    offset = pkt->cdb[5];
5377                    offset |= pkt->cdb[4] << 8;
5378                    offset |= pkt->cdb[3] << 16;
5379                    len = pkt->cdb[8];
5380                    len |= pkt->cdb[7] << 8;
5381                    len |= pkt->cdb[6] << 16;
5382                    end_addr[0] = phy_addr[0] = ha->tbuf_dma;
5383                    end_addr[1] = phy_addr[1] = 0;
5384                    *end_a64 += TARGET_DATA_OFFSET + TARGET_DATA_SIZE;
5385                    switch (pkt->cdb[1] & 7)
5386                    {
5387                        case RW_BUF_HDATA:
5388#ifdef QL_DEBUG_LEVEL_3
5389                            qla1280_print("qla1280_atio_entry: SM_WRDB, RW_BUF_HDATA\n\r");
5390#endif
5391                            if (len > TARGET_DATA_SIZE + 4)
5392                            {
5393#ifdef QL_DEBUG_LEVEL_2
5394                                qla1280_print("qla1280_atio_entry: SM_WRDB, length > buffer size\n\r");
5395#endif
5396                                *sense_ptr = 0x70;
5397                                *(sense_ptr+2) = SD_ILLREQ;
5398                                *(sense_ptr+7) = TARGET_SENSE_SIZE-8;
5399                                *(sense_ptr+12) = SC_ILLCDB;
5400                                pkt->scsi_status = S_CKCON;
5401                                pkt->option_flags |= (uint32_t)OF_SSTS |
5402                                        (uint32_t)OF_NO_DATA;
5403                                len = 0;
5404                            }
5405                            else if (len)
5406                            {
5407                                    pkt->scsi_status = S_GOOD;
5408                                    pkt->option_flags |= (uint32_t)OF_SSTS |
5409                                            (uint32_t)OF_DATA_OUT;
5410#ifdef QL_DEBUG_LEVEL_3
5411                                    qla1280_print("qla1280_atio_entry: Issuing SDI_TARMOD_WRCOMP\n\r");
5412#endif
5413                                    sdi_xaen(SDI_TARMOD_WRCOMP, ha->cntlr,
5414                                            pkt->target_id, pkt->lun, 0, offset);
5415                            }
5416                            else
5417                            {
5418#ifdef QL_DEBUG_LEVEL_2
5419                                    qla1280_print("qla1280_atio_entry: SM_WRDB, zero length\n\r");
5420#endif
5421                                    pkt->scsi_status = S_GOOD;
5422                                    pkt->option_flags |= (uint32_t)OF_SSTS |
5423                                            (uint32_t)OF_NO_DATA;
5424                            }
5425
5426                            break;
5427                        case RW_BUF_DATA:
5428#ifdef QL_DEBUG_LEVEL_3
5429                            qla1280_print("qla1280_atio_entry: SM_WRDB, RW_BUF_DATA\n\r");
5430#endif
5431                            *a64 += offset + TARGET_DATA_OFFSET;
5432                            if (pkt->cdb[2] != 0 || *a64 >= *end_a64 ||
5433                                *a64 + len > *end_a64)
5434                            {
5435#ifdef QL_DEBUG_LEVEL_2
5436                                    qla1280_print("qla1280_atio_entry: SM_WRDB, RW_BUF_DATA BAD\n\r");
5437                                    qla1280_print("buf_id=");
5438                                    qla1280_output_number((uint32_t)pkt->cdb[2], 16);
5439                                    qla1280_print(", offset=");
5440                                    qla1280_output_number((uint32_t)offset, 16);
5441                                    qla1280_print(", length=");
5442                                    qla1280_output_number((uint32_t)len, 16);
5443                                    qla1280_print("\n\r");
5444#endif
5445                                    *sense_ptr = 0x70;
5446                                    *(sense_ptr+2) = SD_ILLREQ;
5447                                    *(sense_ptr+7) = TARGET_SENSE_SIZE-8;
5448                                    *(sense_ptr+12) = SC_ILLCDB;
5449                                    len = 0;
5450                                    pkt->scsi_status = S_CKCON;
5451                                    pkt->option_flags |= (uint32_t)OF_SSTS |
5452                                            (uint32_t)OF_NO_DATA;
5453                            }
5454                            else if (len)
5455                            {
5456                                    pkt->scsi_status = S_GOOD;
5457                                    pkt->option_flags |= (uint32_t)OF_SSTS |
5458                                            (uint32_t)OF_DATA_OUT;
5459#ifdef QL_DEBUG_LEVEL_3
5460                                    qla1280_print("qla1280_atio_entry: Issuing SDI_TARMOD_WRCOMP\n\r");
5461#endif
5462                                    sdi_xaen(SDI_TARMOD_WRCOMP, ha->cntlr,
5463                                            pkt->target_id, pkt->lun, 0, offset);
5464                            }
5465                            else
5466                            {
5467#ifdef QL_DEBUG_LEVEL_2
5468                                    qla1280_print("qla1280_atio_entry: SM_WRDB, zero length\n\r");
5469#endif
5470                                    pkt->scsi_status = S_GOOD;
5471                                    pkt->option_flags |= (uint32_t)OF_SSTS |
5472                                            (uint32_t)OF_NO_DATA;
5473                            }
5474                            break;
5475                        default:
5476#ifdef QL_DEBUG_LEVEL_2
5477                            qla1280_print("qla1280_atio_entry: SM_WRDB unknown mode\n\r");
5478#endif
5479                            *sense_ptr = 0x70;
5480                            *(sense_ptr+2) = SD_ILLREQ;
5481                            *(sense_ptr+7) = TARGET_SENSE_SIZE-8;
5482                            *(sense_ptr+12) = SC_ILLCDB;
5483                            len = 0;
5484                            pkt->scsi_status = S_CKCON;
5485                            pkt->option_flags |= (uint32_t)OF_SSTS |
5486                                    (uint32_t)OF_NO_DATA;
5487                            break;
5488                    }
5489                    break;
5490                case SM_RDDB:
5491                    bzero(sense_ptr, TARGET_SENSE_SIZE);
5492                    offset = pkt->cdb[5];
5493                    offset |= pkt->cdb[4] << 8;
5494                    offset |= pkt->cdb[3] << 16;
5495                    len = pkt->cdb[8];
5496                    len |= pkt->cdb[7] << 8;
5497                    len |= pkt->cdb[6] << 16;
5498                    end_addr[0] = phy_addr[0] = ha->tbuf_dma;
5499                    end_addr[1] = phy_addr[1] = 0;
5500                    *end_a64 += TARGET_DATA_OFFSET + TARGET_DATA_SIZE;
5501                    switch (pkt->cdb[1] & 7)
5502                    {
5503                        case RW_BUF_HDATA:
5504#ifdef QL_DEBUG_LEVEL_3
5505                            qla1280_print("qla1280_atio_entry: SM_RDDB, RW_BUF_HDATA\n\r");
5506#endif
5507                            if (len)
5508                            {
5509                                ha->tbuf->hdr[0] = 0;
5510                                ha->tbuf->hdr[1] =
5511                                        (uint8_t)(TARGET_DATA_SIZE >> 16);
5512                                ha->tbuf->hdr[2] =
5513                                        (uint8_t)(TARGET_DATA_SIZE >> 8);
5514                                ha->tbuf->hdr[3] = (uint8_t)TARGET_DATA_SIZE;
5515                                if (len > TARGET_DATA_SIZE + 4)
5516                                    len = TARGET_DATA_SIZE + 4;
5517                                pkt->scsi_status = S_GOOD;
5518                                pkt->option_flags |= (uint32_t)OF_SSTS |
5519                                        (uint32_t)OF_DATA_IN;
5520                            }
5521                            else
5522                            {
5523#ifdef QL_DEBUG_LEVEL_2
5524                                    qla1280_print("qla1280_atio_entry: SM_RDDB, zero length\n\r");
5525#endif
5526                                    pkt->scsi_status = S_GOOD;
5527                                    pkt->option_flags |= (uint32_t)OF_SSTS |
5528                                            (uint32_t)OF_NO_DATA;
5529                            }
5530                            break;
5531                        case RW_BUF_DATA:
5532#ifdef QL_DEBUG_LEVEL_3
5533                            qla1280_print("qla1280_atio_entry: SM_RDDB, RW_BUF_DATA\n\r");
5534#endif
5535                            *a64 += offset + TARGET_DATA_OFFSET;
5536                            if (pkt->cdb[2] != 0 || *a64 >= *end_a64)
5537                            {
5538#ifdef QL_DEBUG_LEVEL_2
5539                                    qla1280_print("qla1280_atio_entry: SM_RDDB, RW_BUF_DATA BAD\n\r");
5540                                    qla1280_print("buf_id=");
5541                                    qla1280_output_number((uint32_t)pkt->cdb[2], 16);
5542                                    qla1280_print(", offset=");
5543                                    qla1280_output_number((uint32_t)offset, 16);
5544                                    qla1280_print("\n\r");
5545#endif
5546                                    *sense_ptr = 0x70;
5547                                    *(sense_ptr+2) = SD_ILLREQ;
5548                                    *(sense_ptr+7) = TARGET_SENSE_SIZE-8;
5549                                    *(sense_ptr+12) = SC_ILLCDB;
5550                                    len = 0;
5551                                    pkt->scsi_status = S_CKCON;
5552                                    pkt->option_flags |= (uint32_t)OF_SSTS |
5553                                            (uint32_t)OF_NO_DATA;
5554                            }
5555                            else
5556                            {
5557                                    if (*a64 + len > *end_a64)
5558                                        len = *end_a64 - *a64;
5559                                    if (len)
5560                                    {
5561                                        pkt->scsi_status = S_GOOD;
5562                                        pkt->option_flags |= (uint32_t)OF_SSTS |
5563                                                (uint32_t)OF_DATA_IN;
5564                                    }
5565                                    else
5566                                    {
5567#ifdef QL_DEBUG_LEVEL_2
5568                                            qla1280_print("qla1280_atio_entry: SM_RDDB, zero length\n\r");
5569#endif
5570                                            pkt->scsi_status = S_GOOD;
5571                                            pkt->option_flags |= (uint32_t)OF_SSTS |
5572                                                    (uint32_t)OF_NO_DATA;
5573                                    }
5574                            }
5575                            break;
5576                        case RW_BUF_DESC:
5577#ifdef QL_DEBUG_LEVEL_3
5578                            qla1280_print("qla1280_atio_entry: SM_RDDB, RW_BUF_DESC\n\r");
5579#endif
5580                            if (len)
5581                            {
5582                                    if (len > 4)
5583                                        len = 4;
5584
5585                                    ha->tbuf->hdr[0] = 0;
5586                                    if (pkt->cdb[2] != 0)
5587                                    {
5588                                        ha->tbuf->hdr[1] = 0;
5589                                        ha->tbuf->hdr[2] = 0;
5590                                        ha->tbuf->hdr[3] = 0;
5591                                    }
5592                                    else
5593                                    {
5594                                            ha->tbuf->hdr[1] =
5595                                                    (uint8_t)(TARGET_DATA_SIZE >> 16);
5596                                            ha->tbuf->hdr[2] =
5597                                                    (uint8_t)(TARGET_DATA_SIZE >> 8);
5598                                            ha->tbuf->hdr[3] =
5599                                                    (uint8_t)TARGET_DATA_SIZE;
5600                                    }
5601                                    pkt->scsi_status = S_GOOD;
5602                                    pkt->option_flags |= (uint32_t)OF_SSTS |
5603                                            (uint32_t)OF_DATA_IN;
5604                            }
5605                            else
5606                            {
5607#ifdef QL_DEBUG_LEVEL_2
5608                                    qla1280_print("qla1280_atio_entry: SM_RDDB, zero length\n\r");
5609#endif
5610                                    pkt->scsi_status = S_GOOD;
5611                                    pkt->option_flags |= (uint32_t)OF_SSTS |
5612                                            (uint32_t)OF_NO_DATA;
5613                            }
5614                            break;
5615                        default:
5616#ifdef QL_DEBUG_LEVEL_2
5617                            qla1280_print("qla1280_atio_entry: SM_RDDB unknown mode\n\r");
5618#endif
5619                            *sense_ptr = 0x70;
5620                            *(sense_ptr+2) = SD_ILLREQ;
5621                            *(sense_ptr+7) = TARGET_SENSE_SIZE-8;
5622                            *(sense_ptr+12) = SC_ILLCDB;
5623                            len = 0;
5624                            pkt->scsi_status = S_CKCON;
5625                            pkt->option_flags |= (uint32_t)OF_SSTS |
5626                                    (uint32_t)OF_NO_DATA;
5627                            break;
5628                    }
5629                    break;
5630                default:
5631#ifdef QL_DEBUG_LEVEL_2
5632                    qla1280_print("qla1280_atio_entry: Unknown SCSI command\n\r");
5633                    qla1280_dump_buffer((caddr_t)&pkt->cdb[0], pkt->cdb_len);
5634#endif
5635                    bzero(sense_ptr, TARGET_SENSE_SIZE);
5636                    *sense_ptr = 0x70;
5637                    *(sense_ptr+2) = SD_ILLREQ;
5638                    *(sense_ptr+7) = TARGET_SENSE_SIZE-8;
5639                    *(sense_ptr+12) = SC_INVOPCODE;
5640                    len = 0;
5641                    pkt->scsi_status = S_CKCON;
5642                    pkt->option_flags |= (uint32_t)OF_SSTS |
5643                            (uint32_t)OF_NO_DATA;
5644                    break;
5645            }
5646            if (ha->flags.enable_64bit_addressing)
5647                qla1280_64bit_continue_io(ha, pkt, len, (paddr32_t *)&phy_addr);
5648            else
5649                qla1280_32bit_continue_io(ha, pkt, len, (paddr32_t *)&phy_addr);
5650            break;
5651        default:
5652            break;
5653    }
5654
5655#ifdef QL_DEBUG_LEVEL_3
5656    qla1280_print("qla1280_atio_entry: exiting normally\n\r");
5657#endif
5658}
5659
5660/*
5661 *  qla1280_notify_entry
5662 *      Processes received ISP immediate notify entry.
5663 *
5664 * Input:
5665 *      ha  = adapter block pointer.
5666 *      pkt = entry pointer.
5667 */
5668STATIC void
5669qla1280_notify_entry(scsi_qla_host_t *ha, notify_entry_t *pkt)
5670{
5671#ifdef QL_DEBUG_LEVEL_3
5672    qla1280_print("qla1280_notify_entry: entered\n\r");
5673#endif
5674
5675    /* Acknowledge immediate notify */
5676    qla1280_notify_ack(ha, pkt);
5677
5678    /* Issue notify entry to increment resource count */
5679    qla1280_immed_notify(ha, pkt);
5680
5681#ifdef QL_DEBUG_LEVEL_3
5682    qla1280_print("qla1280_notify_entry: exiting normally\n\r");
5683#endif
5684}
5685
5686#endif  /* QLA1280_TARGET_MODE_SUPPORT */
5687/*
5688 *  qla1280_status_entry
5689 *      Processes received ISP status entry.
5690 *
5691 * Input:
5692 *      ha           = adapter block pointer.
5693 *      pkt          = entry pointer.
5694 *      done_q_first = done queue first pointer.
5695 *      done_q_last  = done queue last pointer.
5696 */
5697STATIC void
5698qla1280_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt, srb_t **done_q_first,
5699                     srb_t **done_q_last)
5700{
5701    uint32_t        b, t, l;
5702    uint8_t         sense_sz = 0;
5703    srb_t           *sp;
5704    scsi_lu_t       *q;
5705    Scsi_Cmnd       *cp;
5706
5707#ifdef QL_DEBUG_LEVEL_3
5708    ENTER("qla1280_status_entry");
5709#endif
5710
5711    /* Validate handle. */
5712    if (pkt->handle < MAX_OUTSTANDING_COMMANDS)
5713        sp = ha->outstanding_cmds[pkt->handle];
5714    else
5715        sp = 0;
5716
5717    if (sp)
5718    {
5719        /* Free outstanding command slot. */
5720        ha->outstanding_cmds[pkt->handle] = 0;
5721
5722        cp = sp->cmd;
5723        /* Generate LU queue on cntrl, target, LUN */
5724        b = SCSI_BUS_32(cp);
5725        t = SCSI_TCN_32(cp);
5726        l = SCSI_LUN_32(cp);
5727        q = LU_Q(ha, b, t, l);
5728        if( pkt->comp_status || pkt->scsi_status )
5729        {
5730            DEBUG(qla1280_print( "scsi: comp_status = ");)
5731            DEBUG(qla1280_output_number((uint32_t)pkt->comp_status,16);)
5732            DEBUG(qla1280_print( ", ");)
5733            DEBUG(qla1280_print( " scsi_status = ");)
5734            DEBUG(qla1280_output_number((uint32_t)pkt->scsi_status,16);)
5735            DEBUG(qla1280_print( "\n\r");)
5736            DEBUG(qla1280_print(", handle = ");)
5737            DEBUG(qla1280_output_number((uint32_t)pkt->handle, 16);)
5738            DEBUG(qla1280_print("\n\r");)
5739        }
5740
5741        /* Target busy */
5742        if ( pkt->scsi_status & SS_BUSY_CONDITION &&
5743            pkt->scsi_status != SS_RESERVE_CONFLICT   )
5744        {
5745            CMD_RESULT(cp) = (int) (DID_BUS_BUSY << 16) |
5746                    (pkt->scsi_status & 0xff);
5747        }
5748        else
5749        {
5750
5751            /* Save ISP completion status */
5752            CMD_RESULT(cp) = qla1280_return_status( pkt, cp );
5753
5754            if (pkt->scsi_status & SS_CHECK_CONDITION)
5755            {
5756                BZERO(cp->sense_buffer, CMD_SNSLEN(cp));
5757                if (pkt->comp_status != CS_ARS_FAILED)
5758                {
5759                    if ( pkt->req_sense_length < CMD_SNSLEN(cp)  )
5760                        sense_sz = pkt->req_sense_length;
5761                    else
5762                        sense_sz = CMD_SNSLEN(cp) - 1;
5763
5764                    BCOPY((caddr_t)&pkt->req_sense_data, cp->sense_buffer, sense_sz);
5765
5766                }
5767#ifdef QL_DEBUG_LEVEL_2
5768                DEBUG(qla1280_print(
5769                        "qla1280_status_entry: Check condition Sense data, b");)
5770                DEBUG(qla1280_output_number((uint32_t)b, 10);)
5771                DEBUG(qla1280_print("t");)
5772                DEBUG(qla1280_output_number((uint32_t)t, 10);)
5773                DEBUG(qla1280_print("d");)
5774                DEBUG(qla1280_output_number((uint32_t)l, 10);)
5775                DEBUG(qla1280_print("\n\r");)
5776                DEBUG(if (sense_sz))
5777                    DEBUG(qla1280_dump_buffer(cp->sense_buffer, sense_sz);)
5778#endif
5779            }
5780        }
5781        /* Place command on done queue. */
5782        qla1280_done_q_put(sp, done_q_first, done_q_last);
5783    }
5784    else
5785    {
5786#ifdef QL_DEBUG_LEVEL_2
5787        qla1280_print("qla1280_status_entry: ISP Invalid handle\n\r");
5788#endif
5789        printk(KERN_WARNING "qla1280: Status Entry invalid handle\n");
5790        ha->flags.isp_abort_needed = TRUE;
5791    }
5792#ifdef QL_DEBUG_LEVEL_3
5793    LEAVE("qla1280_status_entry");
5794#endif
5795}
5796
5797/*
5798 *  qla1280_error_entry
5799 *      Processes error entry.
5800 *
5801 * Input:
5802 *      ha           = adapter block pointer.
5803 *      pkt          = entry pointer.
5804 *      done_q_first = done queue first pointer.
5805 *      done_q_last  = done queue last pointer.
5806 */
5807STATIC void
5808qla1280_error_entry(scsi_qla_host_t *ha, response_t *pkt, srb_t **done_q_first,
5809                    srb_t **done_q_last)
5810{
5811    srb_t   *sp;
5812
5813#ifdef QL_DEBUG_LEVEL_3
5814    ENTER("qla1280_error_entry");
5815#endif
5816
5817#ifdef QL_DEBUG_LEVEL_2
5818    if (pkt->entry_status & BIT_3)
5819        qla1280_print("qla1280_error_entry: BAD PAYLOAD flag error\n\r");
5820    else if (pkt->entry_status & BIT_2)
5821        qla1280_print("qla1280_error_entry: BAD HEADER flag error\n\r");
5822    else if (pkt->entry_status & BIT_1)
5823        qla1280_print("qla1280_error_entry: FULL flag error\n\r");
5824    else
5825        qla1280_print("qla1280_error_entry: UNKNOWN flag error\n\r");
5826#endif
5827
5828    /* Validate handle. */
5829    if (pkt->handle < MAX_OUTSTANDING_COMMANDS)
5830        sp = ha->outstanding_cmds[pkt->handle];
5831    else
5832        sp = 0;
5833
5834    if (sp)
5835    {
5836        /* Free outstanding command slot. */
5837        ha->outstanding_cmds[pkt->handle] = 0;
5838
5839        /* Bad payload or header */
5840        if (pkt->entry_status & (BIT_3 + BIT_2))
5841        {
5842            /* Bad payload or header, set error status. */
5843            /* CMD_RESULT(sp->cmd) = CS_BAD_PAYLOAD; */
5844            CMD_RESULT(sp->cmd) = (int) DID_ERROR << 16;
5845        }
5846        else if (pkt->entry_status & BIT_1 ) /* FULL flag */
5847        {
5848            CMD_RESULT(sp->cmd) = (int) DID_BUS_BUSY << 16;
5849        }
5850        else
5851        {
5852            /* Set error status. */
5853            CMD_RESULT(sp->cmd) =(int)  DID_ERROR << 16;
5854        }
5855        /* Place command on done queue. */
5856        qla1280_done_q_put(sp, done_q_first, done_q_last);
5857    }
5858#if  QLA1280_64BIT_SUPPORT
5859    else if (pkt->entry_type == COMMAND_A64_TYPE)
5860    {
5861#ifdef QL_DEBUG_LEVEL_2
5862        qla1280_print("qla1280_error_entry: ISP Invalid handle\n\r");
5863#endif
5864        printk(KERN_WARNING "!qla1280: Error Entry invalid handle");
5865        ha->flags.isp_abort_needed = TRUE;
5866    }
5867#endif
5868
5869#ifdef QL_DEBUG_LEVEL_3
5870    LEAVE("qla1280_error_entry");
5871#endif
5872}
5873
5874/*
5875 *  qla1280_abort_isp
5876 *      Resets ISP and aborts all outstanding commands.
5877 *
5878 * Input:
5879 *      ha           = adapter block pointer.
5880 *
5881 * Returns:
5882 *      0 = success
5883 */
5884STATIC uint8_t
5885qla1280_abort_isp(scsi_qla_host_t *ha)
5886{
5887    device_reg_t    *reg = ha->iobase;
5888    uint8_t         status = 0;
5889    uint16_t        cnt;
5890    srb_t           *sp;
5891    scsi_lu_t       *q;
5892    uint32_t        b, t, l;
5893#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
5894    unsigned long cpu_flags = 0;
5895#endif
5896
5897#ifdef QL_DEBUG_LEVEL_3
5898    ENTER("qla1280_abort_isp");
5899#endif
5900
5901    DRIVER_LOCK
5902            ha->flags.isp_abort_needed = FALSE;
5903    if (!ha->flags.abort_isp_active && ha->flags.online)
5904    {
5905        ha->flags.abort_isp_active = TRUE;
5906
5907        /* Disable ISP interrupts. */
5908        WRT_REG_WORD(&reg->ictrl, 0);
5909
5910        /* Dequeue all commands in outstanding command list. */
5911        for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
5912        {
5913            sp = ha->outstanding_cmds[cnt];
5914            if (sp)
5915            {
5916                ha->outstanding_cmds[cnt] = 0;
5917
5918                /* Generate LU queue on controller, target, LUN */
5919                b = SCSI_BUS_32(sp->cmd);
5920                t = SCSI_TCN_32(sp->cmd);
5921                l = SCSI_LUN_32(sp->cmd);
5922
5923                q = (scsi_lu_t       *)LU_Q(ha, b, t, l);
5924
5925                /* Reset outstanding command count. */
5926                q->q_outcnt = 0;
5927                q->q_flag &= ~QLA1280_QBUSY;
5928                q->q_flag =  0;
5929
5930                /* Adjust watchdog timer for command. */
5931                /* if (sp->flags & SRB_WATCHDOG)
5932                sp->timeout += 2; */
5933
5934                /* Place request back on top of device queue. */
5935                /* sp->flags &= ~(SRB_SENT | SRB_TIMEOUT); */
5936                sp->flags = 0;
5937                qla1280_putq_t(q, sp);
5938            }
5939        }
5940
5941        /* If firmware needs to be loaded */
5942        if (qla1280_isp_firmware(ha))
5943        {
5944            if (!(status = qla1280_chip_diag(ha)))
5945                status = qla1280_setup_chip(ha);
5946        }
5947
5948        if (!status)
5949        {
5950            /* Setup adapter based on NVRAM parameters. */
5951            qla1280_nvram_config(ha);
5952
5953            if (!(status = qla1280_init_rings(ha)))
5954            {
5955                /* Issue SCSI reset. */
5956                for (b = 0; b < ha->ports; b++)
5957                {
5958                    qla1280_bus_reset(ha, b);
5959                }
5960                do
5961                {
5962                    /* Issue marker command. */
5963                    ha->flags.reset_marker = FALSE;
5964                    for (b = 0; b < ha->ports; b++)
5965                    {
5966                        ha->bus_settings[b].reset_marker = FALSE;
5967                        qla1280_marker(ha, b, 0, 0, MK_SYNC_ALL);
5968                    }
5969                }while (ha->flags.reset_marker);
5970
5971                /* Enable host adapter target mode. */
5972                for (b = 0; b < ha->ports; b++)
5973                {
5974                    if (!(status = qla1280_enable_tgt(ha, b)))
5975                    {
5976                        for (cnt = 0; cnt < MAX_LUNS; cnt++)
5977                        {
5978                            /* qla1280_enable_lun(ha, b, cnt); */
5979                            qla1280_poll(ha);
5980                        }
5981                    }
5982                    else
5983                        break;
5984                }
5985
5986                if (!status)
5987                {
5988                    /* Enable ISP interrupts. */
5989                    WRT_REG_WORD(&reg->ictrl, ISP_EN_INT + ISP_EN_RISC);
5990                    ha->flags.abort_isp_active = FALSE;
5991                    /* Restart queues that may have been stopped. */
5992                    qla1280_restart_queues(ha);
5993                }
5994            }
5995        }
5996    }
5997
5998    if (status)
5999    {
6000        printk(KERN_WARNING
6001                "qla1280: ISP error recovery failed, board disabled");
6002        qla1280_reset_adapter(ha);
6003        qla1280_abort_queues(ha);
6004
6005#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3)
6006        qla1280_print("qla1280_abort_isp: **** FAILED ****\n\r");
6007#endif
6008    }
6009#ifdef QL_DEBUG_LEVEL_3
6010    else
6011        LEAVE("qla1280_abort_isp");
6012#endif
6013    DRIVER_UNLOCK
6014
6015            return(status);
6016}
6017
6018/*
6019 *  qla1280_restart_queues
6020 *      Restart all device queues.
6021 *
6022 * Input:
6023 *      ha = adapter block pointer.
6024 */
6025STATIC void
6026qla1280_restart_queues(scsi_qla_host_t *ha)
6027{
6028    scsi_lu_t *q;
6029    uint32_t  b, t, l;
6030
6031#ifdef QL_DEBUG_LEVEL_3
6032    ENTER("qla1280_restart_queues");
6033#endif
6034
6035    for (b = 0; b < ha->ports; b++)
6036        for (t = 0; t < MAX_TARGETS; t++)
6037            for (l = 0; l < MAX_LUNS; l++)
6038            {
6039                q = (scsi_lu_t *) LU_Q(ha, b, t, l);
6040                if (q != NULL)
6041                {
6042                    /* Acquire LU queue specific lock */
6043                    QLA1280_SCSILU_LOCK(q);
6044
6045                    if (q->q_first)
6046                        qla1280_next(ha, q, b);
6047                    else
6048                        /* Release LU queue specific lock */
6049                        QLA1280_SCSILU_UNLOCK(q);
6050                }
6051            }
6052#ifdef QL_DEBUG_LEVEL_3
6053            qla1280_print("qla1280_restart_queues: exiting normally\n");
6054#endif
6055}
6056
6057/*
6058 *  qla1280_abort_queue_single
6059 *      Abort all commands on a device queues.
6060 *
6061 * Input:
6062 *      ha = adapter block pointer.
6063 */
6064STATIC void qla1280_abort_queue_single(scsi_qla_host_t *ha,uint32_t b,uint32_t t,uint32_t l,uint32_t stat)
6065{
6066    scsi_lu_t *q;
6067    srb_t     *sp, *sp_next;
6068
6069    ENTER("qla1280_abort_queue_single");
6070    q = (scsi_lu_t * )LU_Q(ha, b, t, l);
6071    if (q != NULL)
6072    {
6073        /* Acquire LU queue specific lock */
6074        QLA1280_SCSILU_LOCK(q);
6075
6076        sp = q->q_first;
6077        q->q_first = q->q_last = NULL;
6078
6079        QLA1280_SCSILU_UNLOCK(q);
6080
6081        while (sp)
6082        {
6083            sp_next = sp->s_next;
6084            CMD_RESULT(sp->cmd) = stat;
6085            qla1280_done_q_put(sp, (srb_t **)&ha->done_q_first, (srb_t **)&ha->done_q_last);
6086            sp = sp_next;
6087        }
6088    }
6089    LEAVE("qla1280_abort_queue_single");
6090}
6091
6092/*
6093 *  qla1280_abort_queues
6094 *      Abort all commands on device queues.
6095 *
6096 * Input:
6097 *      ha = adapter block pointer.
6098 */
6099STATIC void
6100qla1280_abort_queues(scsi_qla_host_t *ha)
6101{
6102    uint32_t  b, t, l;
6103
6104    ENTER("qla1280_abort_queues");
6105
6106    for (b = 0; b < ha->ports; b++)
6107        for (t = 0; t < MAX_TARGETS; t++)
6108            for (l = 0; l < MAX_LUNS; l++)
6109                qla1280_abort_queue_single(ha,b,t,l,DID_RESET);
6110
6111            LEAVE("qla1280_abort_queues");
6112}
6113
6114/*
6115 * qla1280_debounce_register
6116 *      Debounce register.
6117 *
6118 * Input:
6119 *      port = register address.
6120 *
6121 * Returns:
6122 *      register value.
6123 */
6124STATIC uint16_t
6125qla1280_debounce_register(volatile uint16_t *addr)
6126{
6127    volatile uint16_t ret;
6128    volatile uint16_t ret2;
6129
6130    do
6131    {
6132        ret = RD_REG_WORD(addr);
6133        ret2 = RD_REG_WORD(addr);
6134    }while (ret != ret2);
6135
6136    return(ret);
6137}
6138
6139
6140/*
6141 * Declarations for load module
6142 */
6143static Scsi_Host_Template driver_template = QLA1280_LINUX_TEMPLATE;
6144
6145#include "scsi_module.c"
6146
6147/************************************************************************
6148 * qla1280_check_for_dead_scsi_bus                                      *
6149 *                                                                      *
6150 *    This routine checks for a dead SCSI bus                           *
6151 ************************************************************************/
6152#define SET_SXP_BANK            0x0100
6153#define SCSI_PHASE_INVALID      0x87FF
6154int  qla1280_check_for_dead_scsi_bus(scsi_qla_host_t *ha, srb_t *sp)
6155{
6156    uint16_t  config_reg, scsi_control;
6157    device_reg_t    *reg = ha->iobase;
6158    uint32_t  b;
6159    Scsi_Cmnd       *cp;
6160
6161    /*
6162     * If SCSI Bus is Dead because of bad termination,
6163     * we will return a status of Selection timeout.
6164     */
6165
6166     cp = sp->cmd;
6167     b = SCSI_BUS_32(cp);
6168    if (ha->bus_settings[b].scsi_bus_dead)
6169    {
6170        WRT_REG_WORD(&reg->host_cmd, HC_PAUSE_RISC);
6171        config_reg = RD_REG_WORD(&reg->cfg_1);
6172        WRT_REG_WORD(&reg->cfg_1,SET_SXP_BANK);
6173        scsi_control = RD_REG_WORD(&reg->scsiControlPins);
6174        WRT_REG_WORD(&reg->cfg_1,config_reg);
6175        WRT_REG_WORD(&reg->host_cmd, HC_RELEASE_RISC);
6176
6177        if (scsi_control == SCSI_PHASE_INVALID)
6178        {
6179            CMD_RESULT(cp) = DID_NO_CONNECT << 16;
6180            CMD_HANDLE(cp) = (unsigned char *) 0;
6181            /* ha->actthreads--; */
6182#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
6183            sti();
6184            (*(cp)->scsi_done)(cp);
6185            cli();
6186#else
6187            (*(cp)->scsi_done)(cp);
6188#endif
6189            return(TRUE);   /* bus is dead */
6190        }
6191        else
6192        {
6193            ha->bus_settings[b].scsi_bus_dead = FALSE;
6194            ha->bus_settings[b].failed_reset_count= 0;
6195        }
6196    }
6197    return(FALSE);   /* bus is not dead */
6198}
6199
6200STATIC uint8_t
6201qla12160_set_target_parameters(scsi_qla_host_t *ha, uint32_t b, uint32_t t, uint32_t l, nvram160_t *nv)
6202{
6203    uint16_t        mb[MAILBOX_REGISTER_COUNT];
6204
6205    /* Set Target Parameters. */
6206    mb[0] = MBC_SET_TARGET_PARAMETERS;
6207    mb[1] = (uint16_t)(b ? t | BIT_7 :t);
6208    mb[1] <<= 8;
6209    mb[2] = nv->bus[b].target[t].parameter.c << 8;
6210    mb[2] |= TP_AUTO_REQUEST_SENSE;
6211    mb[2] &= ~TP_STOP_QUEUE;
6212    mb[2] |=  (nv->bus[b].target[t].flags.enable_ppr << 5);
6213    mb[3] = nv->bus[b].target[t].flags.sync_offset << 8;
6214    mb[3] |= nv->bus[b].target[t].sync_period;
6215
6216    mb[6] = nv->bus[b].target[t].flags.ppr_options << 8;
6217    mb[6] |= nv->bus[b].target[t].flags.ppr_bus_width;
6218    return( qla1280_mailbox_command(ha, BIT_6|BIT_3|BIT_2|BIT_1|BIT_0, &mb[0]) ) ;
6219}
6220
6221STATIC void
6222qla12160_get_target_parameters(scsi_qla_host_t *ha, uint32_t b, uint32_t t, uint32_t l)
6223{
6224    uint16_t        mb[MAILBOX_REGISTER_COUNT];
6225
6226    mb[0] = MBC_GET_TARGET_PARAMETERS;
6227    mb[1] = (uint16_t)(b ? t | BIT_7 :t);
6228    mb[1] <<= 8;
6229    qla1280_mailbox_command(ha, BIT_6|BIT_3|BIT_2|BIT_1|BIT_0, &mb[0]);
6230    if( mb[3] != 0 )
6231    printk(KERN_INFO "scsi(%d:%d:%d:%d): Synchronous tranfer at period %d, offset %d. \n",
6232        (int)ha->host_no, b, t, l, (mb[3] &0xff), (mb[3] >> 8));
6233
6234    if ( (mb[2] & BIT_5) &&  ((mb[6] >> 8) & 0xff) >= 2 )
6235            printk(KERN_INFO "scsi(%d:%d:%d:%d): Dual Transition enabled.\n",
6236                          (int)ha->host_no, b, t, l);
6237}
6238
6239
6240#ifdef QL_DEBUG_ROUTINES
6241/****************************************************************************/
6242/*                         Driver Debug Functions.                          */
6243/****************************************************************************/
6244
6245/*
6246 *  Get byte from I/O port
6247 */
6248STATIC uint8_t
6249qla1280_getbyte(uint8_t *port)
6250{
6251    uint8_t ret;
6252
6253#if MEMORY_MAPPED_IO
6254    ret = *port;
6255#else
6256    ret = inb((int)port);
6257#endif
6258
6259    if (ql_debug_print)
6260    {
6261        qla1280_print("qla1280_getbyte: address = ");
6262        qla1280_output_number((uint32_t)port, 16);
6263        qla1280_print(" data = 0x");
6264        qla1280_output_number((uint32_t)ret, 16);
6265        qla1280_print("\n\r");
6266    }
6267
6268    return(ret);
6269}
6270
6271/*
6272 *  Get word from I/O port
6273 */
6274STATIC uint16_t
6275qla1280_getword(uint16_t *port)
6276{
6277    uint16_t ret;
6278
6279#if MEMORY_MAPPED_IO
6280    ret = *port;
6281#else
6282    ret = inw((int)port);
6283#endif
6284
6285    if (ql_debug_print)
6286    {
6287        qla1280_print("qla1280_getword: address = ");
6288        qla1280_output_number((uint32_t)port, 16);
6289        qla1280_print(" data = 0x");
6290        qla1280_output_number((uint32_t)ret, 16);
6291        qla1280_print("\n\r");
6292    }
6293
6294    return(ret);
6295}
6296
6297/*
6298 *  Get double word from I/O port
6299 */
6300STATIC uint32_t
6301qla1280_getdword(uint32_t *port)
6302{
6303    uint32_t ret;
6304
6305#if MEMORY_MAPPED_IO
6306    ret = *port;
6307#else
6308    ret = inl((int)port);
6309#endif
6310
6311    if (ql_debug_print)
6312    {
6313        qla1280_print("qla1280_getdword: address = ");
6314        qla1280_output_number((uint32_t)port, 16);
6315        qla1280_print(" data = 0x");
6316        qla1280_output_number((uint32_t)ret, 16);
6317        qla1280_print("\n\r");
6318    }
6319
6320    return(ret);
6321}
6322
6323/*
6324 *  Send byte to I/O port
6325 */
6326STATIC void
6327qla1280_putbyte(uint8_t *port, uint8_t data)
6328{
6329#if MEMORY_MAPPED_IO
6330    *port = data;
6331#else
6332    outb(data, (int)port);
6333#endif
6334
6335    if (ql_debug_print)
6336    {
6337        qla1280_print("qla1280_putbyte: address = ");
6338        qla1280_output_number((uint32_t)port, 16);
6339        qla1280_print(" data = 0x");
6340        qla1280_output_number((uint32_t)data, 16);
6341        qla1280_print("\n\r");
6342    }
6343}
6344
6345/*
6346 *  Send word to I/O port
6347 */
6348STATIC void
6349qla1280_putword(uint16_t *port, uint16_t data)
6350{
6351#if MEMORY_MAPPED_IO
6352    *port = data;
6353#else
6354#ifdef _LINUX_IOPORTS
6355    outw(data, (int)port);
6356#else
6357    outw((int)port, data);
6358#endif
6359#endif
6360
6361    if (ql_debug_print)
6362    {
6363        qla1280_print("qla1280_putword: address = ");
6364        qla1280_output_number((uint32_t)port, 16);
6365        qla1280_print(" data = 0x");
6366        qla1280_output_number((uint32_t)data, 16);
6367        qla1280_print("\n\r");
6368    }
6369}
6370
6371/*
6372 *  Send double word to I/O port
6373 */
6374STATIC void
6375qla1280_putdword(uint32_t *port, uint32_t data)
6376{
6377#if MEMORY_MAPPED_IO
6378    *port = data;
6379#else
6380#ifdef _LINUX_IOPORTS
6381    outl(data,(int)port);
6382#else
6383    outl((int)port, data);
6384#endif
6385#endif
6386
6387    if (ql_debug_print)
6388    {
6389        qla1280_print("qla1280_putdword: address = ");
6390        qla1280_output_number((uint32_t)port, 16);
6391        qla1280_print(" data = 0x");
6392        qla1280_output_number((uint32_t)data, 16);
6393        qla1280_print("\n\r");
6394    }
6395}
6396
6397/*
6398 * Dummy function to prevent warnings for
6399 * declared and unused debug functions
6400 */
6401void
6402qla1280_debug(void)
6403{
6404    qla1280_getbyte(0);
6405    qla1280_getword(0);
6406    qla1280_getdword(0);
6407    qla1280_putbyte(0, 0);
6408    qla1280_putword(0, 0);
6409    qla1280_putdword(0, 0);
6410}
6411
6412/*
6413 *  Out character to COM2 port.
6414 *      PORT must be at standard address for COM2 = 0x2F8,
6415 *      or COM1 = 0x3F8
6416 */
6417#define OUTB(addr,data)   outb((data),(addr))
6418
6419STATIC void
6420qla1280_putc(uint8_t c)
6421{
6422#ifdef QL_DEBUG_CONSOLE
6423    printk("%c", c);
6424#else
6425    int     com_addr              = 0x2f8;
6426    int     hardware_flow_control = 1;
6427    int     software_flow_control = 0;
6428    uint8_t data;
6429
6430    /* Wait for transmitter holding and shift registers for empty. */
6431    do
6432    {
6433        data = inb(com_addr+5);
6434    }while (!(data & BIT_6));
6435
6436    /*
6437    * Set BAUD rate for COM2 to 19200 (0x6)
6438    */
6439
6440    /* Select rate divisor. */
6441    OUTB(com_addr+3, 0x83);
6442
6443    /* BAUD rate divisor LSB. */
6444    OUTB(com_addr, 0xc);                    /* 0xC = 9600 baud */
6445
6446    /* BAUD rate divisor MSB. */
6447    OUTB(com_addr+1, 0);
6448
6449    /* Set No parity, 8 bits, 1 stop bit and
6450    select interrupt enable register. */
6451    OUTB(com_addr+3, 3);
6452
6453    /* Disable interrupts. */
6454    OUTB(com_addr+1, 0);
6455
6456    /* Set data terminal ready and request to send */
6457    OUTB(com_addr+4,3);
6458
6459    if (hardware_flow_control)
6460    {
6461        /* Wait for clear-to-send and data-set-ready */
6462        do
6463        {
6464            data = inb(com_addr+6) & (BIT_5 + BIT_4);
6465        }while (data != (BIT_5 + BIT_4));
6466    }
6467    else if (software_flow_control)
6468    {
6469        /* Test for data ready. */
6470        data = inb(com_addr+5);
6471        if (data & BIT_0)
6472        {
6473            /* If XOFF */
6474            data = inb(com_addr);
6475            if (data == '\023')
6476            {
6477                /* Wait for XON */
6478                do
6479                {
6480                    /* Wait for char */
6481                    do
6482                    {
6483                        data = inb(com_addr+5);
6484                    }while (!(data & BIT_0));
6485                    data = inb(com_addr);
6486                }while (data != '\021');
6487            }
6488        }
6489    }
6490
6491    /* Output character. */
6492    OUTB(com_addr, c);
6493#endif
6494}
6495
6496/*
6497 *  Out NULL terminated string to COM port.
6498 */
6499STATIC void
6500qla1280_print(caddr_t s)
6501{
6502    if (ql_debug_print)
6503    {
6504#ifdef QL_DEBUG_CONSOLE
6505        printk("%s",s);
6506#else
6507        /* Output string. */
6508        while (*s)
6509            qla1280_putc(*s++);
6510#endif
6511    }
6512}
6513
6514/*
6515 *  Output long number to COM port.
6516 */
6517STATIC void
6518qla1280_output_number(uint32_t n, uint8_t base)
6519{
6520    int8_t str[12];
6521    int8_t *s     = &str[11];
6522    int8_t output = 0;
6523    int8_t hex    = FALSE;
6524
6525    if (ql_debug_print)
6526    {
6527        if (base == 10 || base == 16)
6528        {
6529            if (base == 16 && n > 9)
6530                hex = TRUE;
6531
6532            *s = 0;
6533            do
6534            {
6535                s--;
6536                *s = n % base;
6537                if (*s > 9)
6538                    *s += 55;
6539                else
6540                    *s += '0';
6541                n /= base;
6542            }while (n);
6543
6544            for (; *s; s++)
6545            {
6546                if (*s != '0')
6547                    output = 1;
6548                if (output)
6549                    qla1280_putc(*s);
6550            }
6551            if (!output)
6552                qla1280_putc(*--s);
6553
6554            if (hex)
6555                qla1280_putc('h');
6556        }
6557    }
6558}
6559
6560STATIC void
6561qla1280_dump_buffer(caddr_t b, uint32_t size)
6562{
6563    uint32_t cnt;
6564    uint8_t c;
6565
6566    if (ql_debug_print)
6567    {
6568        qla1280_print(
6569                " 0   1   2   3   4   5   6   7   8   9   Ah  Bh  Ch  Dh  Eh  Fh\n\r");
6570        qla1280_print(
6571                "---------------------------------------------------------------\n\r");
6572
6573        for (cnt = 0; cnt < size; )
6574        {
6575            c = *b++;
6576            if (c < 16)
6577                qla1280_putc(' ');
6578            qla1280_output_number((uint32_t)c, 16);
6579            cnt++;
6580            if (!(cnt % 16))
6581                qla1280_print("\n\r");
6582            else if (c < 10)
6583                qla1280_print("  ");
6584            else
6585                qla1280_putc(' ');
6586        }
6587        if (cnt % 16)
6588            qla1280_print("\n\r");
6589    }
6590}
6591/**************************************************************************
6592 *   ql1280_print_scsi_cmd
6593 *
6594 **************************************************************************/
6595void qla1280_print_scsi_cmd(Scsi_Cmnd *cmd)
6596{
6597    scsi_qla_host_t *ha;
6598    struct Scsi_Host  *host = cmd->host;
6599    srb_t           *sp;
6600   /* struct scatterlist *sg; */
6601
6602    int i;
6603    ha = (scsi_qla_host_t *) host->hostdata;
6604
6605    ql_debug_print = 1;
6606    sp = (srb_t *) CMD_SP(cmd);
6607    sprintf(debug_buff,"SCSI Command @= 0x%p, Handle=0x%p\n\r", cmd, CMD_HANDLE(cmd));
6608    qla1280_print(debug_buff);
6609    sprintf(debug_buff,"  chan=%d, target = 0x%02x, lun = 0x%02x, cmd_len = 0x%02x\n\r",
6610            cmd->channel, cmd->target, cmd->lun, cmd->cmd_len);
6611    qla1280_print(debug_buff);
6612    qla1280_print(" CDB = ");
6613    for (i = 0; i < cmd->cmd_len; i++)
6614    {
6615        sprintf(debug_buff,"0x%02x ", cmd->cmnd[i]);
6616        qla1280_print(debug_buff);
6617    }
6618    sprintf(debug_buff,"  seg_cnt =%d\n\r",cmd->use_sg);
6619    qla1280_print(debug_buff);
6620    sprintf(debug_buff,"  request buffer=0x%p, request buffer len=0x%x\n\r",cmd->request_buffer,cmd->request_bufflen);
6621    qla1280_print(debug_buff);
6622    /* if( cmd->use_sg )
6623    {
6624       sg = (struct scatterlist *) cmd->request_buffer;
6625       qla1280_print("  SG buffer: \n\r");
6626       qla1280_dump_buffer((caddr_t)sg, (cmd->use_sg*sizeof(struct scatterlist)) );
6627    } */
6628    sprintf(debug_buff,"  tag=%d, flags=0x%x, transfersize=0x%x \n\r",
6629            cmd->tag, cmd->flags,cmd->transfersize );
6630    qla1280_print(debug_buff);
6631    sprintf(debug_buff,"  Pid=%d, SP=0x%p\n\r", (int)cmd->pid, CMD_SP(cmd));
6632    qla1280_print(debug_buff);
6633    sprintf(debug_buff,"  r_start=0x%lx, u_start=0x%lx\n\r",sp->r_start,sp->u_start);
6634    qla1280_print(debug_buff);
6635    sprintf(debug_buff," underflow size = 0x%x, direction=0x%x, req.cmd=0x%x \n\r", cmd->underflow, sp->dir,cmd->request.cmd);
6636    qla1280_print(debug_buff);
6637}
6638/**************************************************************************
6639 *   ql1280_dump_device
6640 *
6641 **************************************************************************/
6642void
6643ql1280_dump_device(scsi_qla_host_t *ha)
6644{
6645
6646    Scsi_Cmnd       *cp;
6647    srb_t           *sp;
6648    int i;
6649    qla1280_print("Outstanding Commands on controller:\n\r");
6650    for ( i=0; i < MAX_OUTSTANDING_COMMANDS; i++ )
6651    {
6652        if( (sp = ha->outstanding_cmds[i]) == NULL )
6653            continue;
6654        if( (cp = sp->cmd) == NULL )
6655            continue;
6656        qla1280_print_scsi_cmd(cp);
6657    }
6658
6659}
6660#endif
6661
6662#ifdef  QLA1280_UNUSED
6663/**************************************************************************
6664 *   ql1280_dump_regs
6665 *
6666 **************************************************************************/
6667static void qla1280_dump_regs(struct Scsi_Host *host)
6668{
6669    printk("Mailbox registers:\n");
6670    printk("qla1280 : mbox 0 0x%04x \n", inw(host->io_port + 0x70));
6671    printk("qla1280 : mbox 1 0x%04x \n", inw(host->io_port + 0x72));
6672    printk("qla1280 : mbox 2 0x%04x \n", inw(host->io_port + 0x74));
6673    printk("qla1280 : mbox 3 0x%04x \n", inw(host->io_port + 0x76));
6674    printk("qla1280 : mbox 4 0x%04x \n", inw(host->io_port + 0x78));
6675    printk("qla1280 : mbox 5 0x%04x \n", inw(host->io_port + 0x7a));
6676}
6677#endif
6678
6679
6680
6681#if  STOP_ON_ERROR
6682/**************************************************************************
6683 *   ql1280_panic
6684 *
6685 **************************************************************************/
6686static void qla1280_panic(char *cp, struct Scsi_Host *host)
6687{
6688    scsi_qla_host_t *ha;
6689    long  *fp;
6690
6691    ha = (scsi_qla_host_t *) host->hostdata;
6692    printk("qla1280 - PANIC:  %s\n",cp);
6693    printk("Current time=0x%lx\n", jiffies);
6694    printk("Number of pending commands =0x%lx\n", ha->actthreads);
6695    printk("Number of SCSI queued commands =0x%lx\n", ha->qthreads);
6696    printk("Number of free entries = (%d)\n",ha->req_q_cnt);
6697    printk("Request Queue @ 0x%lx, Response Queue @ 0x%lx\n",
6698                        ha->request_dma,
6699                        ha->response_dma);
6700    printk("Request In Ptr %d\n", ha->req_ring_index );
6701    fp = (long *) &ha->flags;
6702    printk("HA flags =0x%lx\n", *fp);
6703    DEBUG2(ql_debug_print = 1;)
6704    /* DEBUG2(ql1280_dump_device((scsi_qla_host_t *) host->hostdata)); */
6705#ifdef  QLA1280_UNUSED
6706    qla1280_dump_regs(host);
6707#endif
6708    sti();
6709    panic("Ooops");
6710    /* cli();
6711    for(;;)
6712    {
6713        barrier();
6714    sti();
6715    }
6716    */
6717}
6718#endif
6719
6720#ifdef  QLA1280_UNUSED
6721static void qla1280_set_flags(char * s)
6722{
6723}
6724#endif
6725
6726/**************************************************************************
6727 *   qla1280_setup
6728 *
6729 *   Handle Linux boot parameters. This routine allows for assigning a value
6730 *   to a parameter with a ':' between the parameter and the value.
6731 *   ie. qla1280=max_reqs:0x0A,verbose
6732 **************************************************************************/
6733void
6734qla1280_setup(char *s, int *dummy)
6735{
6736    char *end, *str, *cp;
6737
6738#ifdef  QLA1280_UNUSED
6739    static struct
6740    {
6741        const char *name;
6742        int      siz;
6743        void  (*func)();
6744        int   arg;
6745    } options[] =
6746    {
6747        { "dump_regs", 9,  &qla1280_dump_regs, 0
6748        },
6749        { "verbose", 7, &qla1280_set_flags, 0x1
6750        },
6751        { "",    0, NULL, 0
6752        }
6753    };
6754#endif
6755
6756    printk("scsi: Processing Option str = %s\n", s);
6757    end = strchr(s, '\0');
6758    /* locate command */
6759    str = s;
6760    for( cp = s; *cp && cp != end; cp++ )
6761    {
6762       cp = qla1280_get_token(cp, str);
6763       printk("scsi: token str = %s\n", str);
6764       /* if found execute routine */
6765
6766    }
6767
6768}
6769
6770static char	*qla1280_get_token(char *cmdline, char *str )
6771{
6772    register	char 	*cp = cmdline;
6773
6774        /* skip preceeding spaces */
6775        while(strchr(cp,' '))
6776            ++cp;
6777        /* symbol starts here */
6778        str = cp;
6779        /* skip char if not a space or : */
6780        while (*cp && !( strchr(cp,' ') || strchr(cp,':'))  )
6781            cp++;
6782        *cp = '\0';
6783        return( cp );
6784}
6785MODULE_LICENSE("GPL");
6786
6787/*
6788 * Overrides for Emacs so that we almost follow Linus's tabbing style.
6789 * Emacs will notice this stuff at the end of the file and automatically
6790 * adjust the settings for this buffer only.  This must remain at the end
6791 * of the file.
6792 * ---------------------------------------------------------------------------
6793 * Local variables:
6794 * c-indent-level: 2
6795 * c-brace-imaginary-offset: 0
6796 * c-brace-offset: -2
6797 * c-argdecl-indent: 2
6798 * c-label-offset: -2
6799 * c-continued-statement-offset: 2
6800 * c-continued-brace-offset: 0
6801 * indent-tabs-mode: nil
6802 * tab-width: 8
6803 * End:
6804 */
6805
6806