]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/qla2xxx/qla_os.c
[PATCH] qla2xxx: remove lun discovery codes...
[linux-2.6-omap-h63xx.git] / drivers / scsi / qla2xxx / qla_os.c
1 /*
2  *                  QLOGIC LINUX SOFTWARE
3  *
4  * QLogic ISP2x00 device driver for Linux 2.6.x
5  * Copyright (C) 2003-2004 QLogic Corporation
6  * (www.qlogic.com)
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2, or (at your option) any
11  * later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  */
19 #include "qla_def.h"
20
21 #include <linux/moduleparam.h>
22 #include <linux/vmalloc.h>
23 #include <linux/smp_lock.h>
24 #include <linux/delay.h>
25
26 #include <scsi/scsi_tcq.h>
27 #include <scsi/scsicam.h>
28 #include <scsi/scsi_transport.h>
29 #include <scsi/scsi_transport_fc.h>
30
31 /*
32  * Driver version
33  */
34 char qla2x00_version_str[40];
35
36 /*
37  * SRB allocation cache
38  */
39 char srb_cachep_name[16];
40 kmem_cache_t *srb_cachep;
41
42 /*
43  * Stats for all adpaters.
44  */
45 struct _qla2x00stats qla2x00_stats;
46
47 /*
48  * Ioctl related information.
49  */
50 int num_hosts;
51 int apiHBAInstance;
52
53 /*
54  * Module parameter information and variables
55  */
56 int ql2xmaxqdepth;
57 module_param(ql2xmaxqdepth, int, S_IRUGO|S_IWUSR);
58 MODULE_PARM_DESC(ql2xmaxqdepth,
59                 "Maximum queue depth to report for target devices.");
60
61 int ql2xlogintimeout = 20;
62 module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
63 MODULE_PARM_DESC(ql2xlogintimeout,
64                 "Login timeout value in seconds.");
65
66 int qlport_down_retry = 30;
67 module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
68 MODULE_PARM_DESC(qlport_down_retry,
69                 "Maximum number of command retries to a port that returns"
70                 "a PORT-DOWN status.");
71
72 int ql2xretrycount = 20;
73 module_param(ql2xretrycount, int, S_IRUGO|S_IWUSR);
74 MODULE_PARM_DESC(ql2xretrycount,
75                 "Maximum number of mid-layer retries allowed for a command.  "
76                 "Default value is 20, ");
77
78 int ql2xplogiabsentdevice;
79 module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
80 MODULE_PARM_DESC(ql2xplogiabsentdevice,
81                 "Option to enable PLOGI to devices that are not present after "
82                 "a Fabric scan.  This is needed for several broken switches."
83                 "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
84
85 int ql2xenablezio = 0;
86 module_param(ql2xenablezio, int, S_IRUGO|S_IRUSR);
87 MODULE_PARM_DESC(ql2xenablezio,
88                 "Option to enable ZIO:If 1 then enable it otherwise" 
89                 " use the default set in the NVRAM."
90                 " Default is 0 : disabled");
91
92 int ql2xintrdelaytimer = 10;
93 module_param(ql2xintrdelaytimer, int, S_IRUGO|S_IRUSR);
94 MODULE_PARM_DESC(ql2xintrdelaytimer,
95                 "ZIO: Waiting time for Firmware before it generates an "
96                 "interrupt to the host to notify completion of request.");
97
98 int ConfigRequired;
99 module_param(ConfigRequired, int, S_IRUGO|S_IRUSR);
100 MODULE_PARM_DESC(ConfigRequired,
101                 "If 1, then only configured devices passed in through the"
102                 "ql2xopts parameter will be presented to the OS");
103
104 int Bind = BIND_BY_PORT_NAME;
105 module_param(Bind, int, S_IRUGO|S_IRUSR);
106 MODULE_PARM_DESC(Bind,
107                 "Target persistent binding method: "
108                 "0 by Portname (default); 1 by PortID; 2 by Nodename. ");
109
110 int ql2xsuspendcount = SUSPEND_COUNT;
111 module_param(ql2xsuspendcount, int, S_IRUGO|S_IWUSR);
112 MODULE_PARM_DESC(ql2xsuspendcount,
113                 "Number of 6-second suspend iterations to perform while a "
114                 "target returns a <NOT READY> status.  Default is 10 "
115                 "iterations.");
116
117 int ql2xloginretrycount = 0;
118 module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
119 MODULE_PARM_DESC(ql2xloginretrycount,
120                 "Specify an alternate value for the NVRAM login retry count.");
121
122 /*
123  * Proc structures and functions
124  */
125 struct info_str {
126         char    *buffer;
127         int     length;
128         off_t   offset;
129         int     pos;
130 };
131
132 static void copy_mem_info(struct info_str *, char *, int);
133 static int copy_info(struct info_str *, char *, ...);
134
135 static void qla2x00_free_device(scsi_qla_host_t *);
136
137 static void qla2x00_config_dma_addressing(scsi_qla_host_t *ha);
138
139 /*
140  * SCSI host template entry points 
141  */
142 static int qla2xxx_slave_configure(struct scsi_device * device);
143 static int qla2xxx_slave_alloc(struct scsi_device *);
144 static void qla2xxx_slave_destroy(struct scsi_device *);
145 static int qla2x00_queuecommand(struct scsi_cmnd *cmd,
146                 void (*fn)(struct scsi_cmnd *));
147 static int qla2xxx_eh_abort(struct scsi_cmnd *);
148 static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
149 static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
150 static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
151 static int qla2x00_loop_reset(scsi_qla_host_t *ha);
152 static int qla2x00_device_reset(scsi_qla_host_t *, fc_port_t *);
153
154 static int qla2x00_proc_info(struct Scsi_Host *, char *, char **,
155     off_t, int, int);
156
157 static struct scsi_host_template qla2x00_driver_template = {
158         .module                 = THIS_MODULE,
159         .name                   = "qla2xxx",
160         .proc_name              = "qla2xxx",
161         .proc_info              = qla2x00_proc_info,
162         .queuecommand           = qla2x00_queuecommand,
163
164         .eh_abort_handler       = qla2xxx_eh_abort,
165         .eh_device_reset_handler = qla2xxx_eh_device_reset,
166         .eh_bus_reset_handler   = qla2xxx_eh_bus_reset,
167         .eh_host_reset_handler  = qla2xxx_eh_host_reset,
168
169         .slave_configure        = qla2xxx_slave_configure,
170
171         .slave_alloc            = qla2xxx_slave_alloc,
172         .slave_destroy          = qla2xxx_slave_destroy,
173         .this_id                = -1,
174         .cmd_per_lun            = 3,
175         .use_clustering         = ENABLE_CLUSTERING,
176         .sg_tablesize           = SG_ALL,
177
178         /*
179          * The RISC allows for each command to transfer (2^32-1) bytes of data,
180          * which equates to 0x800000 sectors.
181          */
182         .max_sectors            = 0xFFFF,
183 };
184
185 static struct scsi_transport_template *qla2xxx_transport_template = NULL;
186
187 /* TODO Convert to inlines
188  *
189  * Timer routines
190  */
191 #define WATCH_INTERVAL          1       /* number of seconds */
192
193 static void qla2x00_timer(scsi_qla_host_t *);
194
195 static __inline__ void qla2x00_start_timer(scsi_qla_host_t *,
196     void *, unsigned long);
197 static __inline__ void qla2x00_restart_timer(scsi_qla_host_t *, unsigned long);
198 static __inline__ void qla2x00_stop_timer(scsi_qla_host_t *);
199
200 static inline void
201 qla2x00_start_timer(scsi_qla_host_t *ha, void *func, unsigned long interval)
202 {
203         init_timer(&ha->timer);
204         ha->timer.expires = jiffies + interval * HZ;
205         ha->timer.data = (unsigned long)ha;
206         ha->timer.function = (void (*)(unsigned long))func;
207         add_timer(&ha->timer);
208         ha->timer_active = 1;
209 }
210
211 static inline void
212 qla2x00_restart_timer(scsi_qla_host_t *ha, unsigned long interval)
213 {
214         mod_timer(&ha->timer, jiffies + interval * HZ);
215 }
216
217 static __inline__ void
218 qla2x00_stop_timer(scsi_qla_host_t *ha)
219 {
220         del_timer_sync(&ha->timer);
221         ha->timer_active = 0;
222 }
223
224 static int qla2x00_do_dpc(void *data);
225
226 static void qla2x00_rst_aen(scsi_qla_host_t *);
227
228 static uint8_t qla2x00_mem_alloc(scsi_qla_host_t *);
229 static void qla2x00_mem_free(scsi_qla_host_t *ha);
230 static int qla2x00_allocate_sp_pool( scsi_qla_host_t *ha);
231 static void qla2x00_free_sp_pool(scsi_qla_host_t *ha);
232 static srb_t *qla2x00_get_new_sp(scsi_qla_host_t *);
233 static void qla2x00_sp_free_dma(scsi_qla_host_t *, srb_t *);
234 void qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *);
235
236 /* -------------------------------------------------------------------------- */
237
238 static char *
239 qla2x00_get_pci_info_str(struct scsi_qla_host *ha, char *str)
240 {
241         static char *pci_bus_modes[] = {
242                 "33", "66", "100", "133",
243         };
244         uint16_t pci_bus;
245
246         strcpy(str, "PCI");
247         pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
248         if (pci_bus) {
249                 strcat(str, "-X (");
250                 strcat(str, pci_bus_modes[pci_bus]);
251         } else {
252                 pci_bus = (ha->pci_attr & BIT_8) >> 8;
253                 strcat(str, " (");
254                 strcat(str, pci_bus_modes[pci_bus]);
255         }
256         strcat(str, " MHz)");
257
258         return (str);
259 }
260
261 char *
262 qla2x00_get_fw_version_str(struct scsi_qla_host *ha, char *str)
263 {
264         char un_str[10];
265         
266         sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
267             ha->fw_minor_version,
268             ha->fw_subminor_version);
269
270         if (ha->fw_attributes & BIT_9) {
271                 strcat(str, "FLX");
272                 return (str);
273         }
274
275         switch (ha->fw_attributes & 0xFF) {
276         case 0x7:
277                 strcat(str, "EF");
278                 break;
279         case 0x17:
280                 strcat(str, "TP");
281                 break;
282         case 0x37:
283                 strcat(str, "IP");
284                 break;
285         case 0x77:
286                 strcat(str, "VI");
287                 break;
288         default:
289                 sprintf(un_str, "(%x)", ha->fw_attributes);
290                 strcat(str, un_str);
291                 break;
292         }
293         if (ha->fw_attributes & 0x100)
294                 strcat(str, "X");
295
296         return (str);
297 }
298
299 /**************************************************************************
300 * qla2x00_queuecommand
301 *
302 * Description:
303 *     Queue a command to the controller.
304 *
305 * Input:
306 *     cmd - pointer to Scsi cmd structure
307 *     fn - pointer to Scsi done function
308 *
309 * Returns:
310 *   0 - Always
311 *
312 * Note:
313 * The mid-level driver tries to ensures that queuecommand never gets invoked
314 * concurrently with itself or the interrupt handler (although the
315 * interrupt handler may call this routine as part of request-completion
316 * handling).
317 **************************************************************************/
318 static int
319 qla2x00_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
320 {
321         scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
322         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
323         srb_t *sp;
324         int rval;
325
326         if (!fcport) {
327                 cmd->result = DID_NO_CONNECT << 16;
328                 goto qc_fail_command;
329         }
330
331         if (atomic_read(&fcport->state) != FCS_ONLINE) {
332                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
333                     atomic_read(&ha->loop_state) == LOOP_DEAD) {
334                         cmd->result = DID_NO_CONNECT << 16;
335                         goto qc_fail_command;
336                 }
337                 goto qc_host_busy;
338         }
339
340         spin_unlock_irq(ha->host->host_lock);
341
342         /* Allocate a command packet from the "sp" pool. */
343         if ((sp = qla2x00_get_new_sp(ha)) == NULL) {
344                 goto qc_host_busy_lock;
345         }
346
347         sp->ha = ha;
348         sp->fcport = fcport;
349         sp->cmd = cmd;
350         sp->flags = 0;
351         sp->err_id = 0;
352
353         CMD_SP(cmd) = (void *)sp;
354         cmd->scsi_done = done;
355
356         rval = qla2x00_start_scsi(sp);
357         if (rval != QLA_SUCCESS)
358                 goto qc_host_busy_free_sp;
359
360         /* Manage unprocessed RIO/ZIO commands in response queue. */
361         if (ha->flags.online && ha->flags.process_response_queue &&
362             ha->response_ring_ptr->signature != RESPONSE_PROCESSED) {
363                 unsigned long flags;
364
365                 spin_lock_irqsave(&ha->hardware_lock, flags);
366                 qla2x00_process_response_queue(ha);
367                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
368         }
369
370         spin_lock_irq(ha->host->host_lock);
371
372         return 0;
373
374 qc_host_busy_free_sp:
375         qla2x00_sp_free_dma(ha, sp);
376         CMD_SP(cmd) = NULL;
377         mempool_free(sp, ha->srb_mempool);
378
379 qc_host_busy_lock:
380         spin_lock_irq(ha->host->host_lock);
381
382 qc_host_busy:
383         return SCSI_MLQUEUE_HOST_BUSY;
384
385 qc_fail_command:
386         done(cmd);
387
388         return 0;
389 }
390
391 /*
392  * qla2x00_eh_wait_on_command
393  *    Waits for the command to be returned by the Firmware for some
394  *    max time.
395  *
396  * Input:
397  *    ha = actual ha whose done queue will contain the command
398  *            returned by firmware.
399  *    cmd = Scsi Command to wait on.
400  *    flag = Abort/Reset(Bus or Device Reset)
401  *
402  * Return:
403  *    Not Found : 0
404  *    Found : 1
405  */
406 static int
407 qla2x00_eh_wait_on_command(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
408 {
409 #define ABORT_POLLING_PERIOD    HZ
410 #define ABORT_WAIT_ITER         ((10 * HZ) / (ABORT_POLLING_PERIOD))
411         unsigned long wait_iter = ABORT_WAIT_ITER;
412         int ret = QLA_SUCCESS;
413
414         while (CMD_SP(cmd)) {
415                 set_current_state(TASK_UNINTERRUPTIBLE);
416                 schedule_timeout(ABORT_POLLING_PERIOD);
417
418                 if (--wait_iter)
419                         break;
420         }
421         if (CMD_SP(cmd))
422                 ret = QLA_FUNCTION_FAILED;
423
424         return ret;
425 }
426
427 /*
428  * qla2x00_wait_for_hba_online
429  *    Wait till the HBA is online after going through 
430  *    <= MAX_RETRIES_OF_ISP_ABORT  or
431  *    finally HBA is disabled ie marked offline
432  *
433  * Input:
434  *     ha - pointer to host adapter structure
435  * 
436  * Note:    
437  *    Does context switching-Release SPIN_LOCK
438  *    (if any) before calling this routine.
439  *
440  * Return:
441  *    Success (Adapter is online) : 0
442  *    Failed  (Adapter is offline/disabled) : 1
443  */
444 static int 
445 qla2x00_wait_for_hba_online(scsi_qla_host_t *ha)
446 {
447         int      return_status;
448         unsigned long wait_online;
449
450         wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ); 
451         while (((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) ||
452             test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
453             test_bit(ISP_ABORT_RETRY, &ha->dpc_flags) ||
454             ha->dpc_active) && time_before(jiffies, wait_online)) {
455
456                 msleep(1000);
457         }
458         if (ha->flags.online) 
459                 return_status = QLA_SUCCESS; 
460         else
461                 return_status = QLA_FUNCTION_FAILED;
462
463         DEBUG2(printk("%s return_status=%d\n",__func__,return_status));
464
465         return (return_status);
466 }
467
468 /*
469  * qla2x00_wait_for_loop_ready
470  *    Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
471  *    to be in LOOP_READY state.         
472  * Input:
473  *     ha - pointer to host adapter structure
474  * 
475  * Note:    
476  *    Does context switching-Release SPIN_LOCK
477  *    (if any) before calling this routine.
478  *    
479  *
480  * Return:
481  *    Success (LOOP_READY) : 0
482  *    Failed  (LOOP_NOT_READY) : 1
483  */
484 static inline int 
485 qla2x00_wait_for_loop_ready(scsi_qla_host_t *ha)
486 {
487         int      return_status = QLA_SUCCESS;
488         unsigned long loop_timeout ;
489
490         /* wait for 5 min at the max for loop to be ready */
491         loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ); 
492
493         while ((!atomic_read(&ha->loop_down_timer) &&
494             atomic_read(&ha->loop_state) == LOOP_DOWN) ||
495             test_bit(CFG_ACTIVE, &ha->cfg_flags) ||
496             atomic_read(&ha->loop_state) != LOOP_READY) {
497                 msleep(1000);
498                 if (time_after_eq(jiffies, loop_timeout)) {
499                         return_status = QLA_FUNCTION_FAILED;
500                         break;
501                 }
502         }
503         return (return_status); 
504 }
505
506 /**************************************************************************
507 * qla2xxx_eh_abort
508 *
509 * Description:
510 *    The abort function will abort the specified command.
511 *
512 * Input:
513 *    cmd = Linux SCSI command packet to be aborted.
514 *
515 * Returns:
516 *    Either SUCCESS or FAILED.
517 *
518 * Note:
519 **************************************************************************/
520 int
521 qla2xxx_eh_abort(struct scsi_cmnd *cmd)
522 {
523         scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
524         srb_t *sp;
525         int ret, i;
526         unsigned int id, lun;
527         unsigned long serial;
528
529         if (!CMD_SP(cmd))
530                 return FAILED;
531
532         ret = FAILED;
533
534         id = cmd->device->id;
535         lun = cmd->device->lun;
536         serial = cmd->serial_number;
537
538         /* Check active list for command command. */
539         spin_unlock_irq(ha->host->host_lock);
540         spin_lock(&ha->hardware_lock);
541         for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
542                 sp = ha->outstanding_cmds[i];
543
544                 if (sp == NULL)
545                         continue;
546
547                 if (sp->cmd != cmd)
548                         continue;
549
550                 DEBUG2(printk("%s(%ld): aborting sp %p from RISC. pid=%ld "
551                     "sp->state=%x\n", __func__, ha->host_no, sp, serial,
552                     sp->state));
553                 DEBUG3(qla2x00_print_scsi_cmd(cmd);)
554
555                 spin_unlock(&ha->hardware_lock);
556                 if (qla2x00_abort_command(ha, sp)) {
557                         DEBUG2(printk("%s(%ld): abort_command "
558                             "mbx failed.\n", __func__, ha->host_no));
559                 } else {
560                         DEBUG3(printk("%s(%ld): abort_command "
561                             "mbx success.\n", __func__, ha->host_no));
562                         ret = SUCCESS;
563                 }
564                 spin_lock(&ha->hardware_lock);
565
566                 break;
567         }
568
569         /* Wait for the command to be returned. */
570         if (ret == SUCCESS) {
571                 spin_unlock(&ha->hardware_lock);
572                 if (qla2x00_eh_wait_on_command(ha, cmd) != QLA_SUCCESS) {
573                         qla_printk(KERN_ERR, ha, 
574                             "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
575                             "%x.\n", ha->host_no, id, lun, serial, ret);
576                 }
577                 spin_lock(&ha->hardware_lock);
578         }
579         spin_lock_irq(ha->host->host_lock);
580
581         qla_printk(KERN_INFO, ha, 
582             "scsi(%ld:%d:%d): Abort command issued -- %lx %x.\n", ha->host_no,
583             id, lun, serial, ret);
584
585         return ret;
586 }
587
588 /**************************************************************************
589 * qla2x00_eh_wait_for_pending_target_commands
590 *
591 * Description:
592 *    Waits for all the commands to come back from the specified target.
593 *
594 * Input:
595 *    ha - pointer to scsi_qla_host structure.
596 *    t  - target        
597 * Returns:
598 *    Either SUCCESS or FAILED.
599 *
600 * Note:
601 **************************************************************************/
602 static int
603 qla2x00_eh_wait_for_pending_target_commands(scsi_qla_host_t *ha, unsigned int t)
604 {
605         int     cnt;
606         int     status;
607         srb_t           *sp;
608         struct scsi_cmnd *cmd;
609
610         status = 0;
611
612         /*
613          * Waiting for all commands for the designated target in the active
614          * array
615          */
616         for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
617                 spin_lock(&ha->hardware_lock);
618                 sp = ha->outstanding_cmds[cnt];
619                 if (sp) {
620                         cmd = sp->cmd;
621                         spin_unlock(&ha->hardware_lock);
622                         if (cmd->device->id == t) {
623                                 if (!qla2x00_eh_wait_on_command(ha, cmd)) {
624                                         status = 1;
625                                         break;
626                                 }
627                         }
628                 } else {
629                         spin_unlock(&ha->hardware_lock);
630                 }
631         }
632         return (status);
633 }
634
635
636 /**************************************************************************
637 * qla2xxx_eh_device_reset
638 *
639 * Description:
640 *    The device reset function will reset the target and abort any
641 *    executing commands.
642 *
643 *    NOTE: The use of SP is undefined within this context.  Do *NOT*
644 *          attempt to use this value, even if you determine it is 
645 *          non-null.
646 *
647 * Input:
648 *    cmd = Linux SCSI command packet of the command that cause the
649 *          bus device reset.
650 *
651 * Returns:
652 *    SUCCESS/FAILURE (defined as macro in scsi.h).
653 *
654 **************************************************************************/
655 int
656 qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
657 {
658         scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
659         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
660         srb_t *sp;
661         int ret;
662         unsigned int id, lun;
663         unsigned long serial;
664
665         ret = FAILED;
666
667         id = cmd->device->id;
668         lun = cmd->device->lun;
669         serial = cmd->serial_number;
670
671         sp = (srb_t *) CMD_SP(cmd);
672         if (!sp || !fcport)
673                 return ret;
674
675         qla_printk(KERN_INFO, ha,
676             "scsi(%ld:%d:%d): DEVICE RESET ISSUED.\n", ha->host_no, id, lun);
677
678         spin_unlock_irq(ha->host->host_lock);
679
680         if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
681                 spin_lock_irq(ha->host->host_lock);
682                 goto eh_dev_reset_done;
683         }
684
685         if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
686                 if (qla2x00_device_reset(ha, fcport) == 0)
687                         ret = SUCCESS;
688
689 #if defined(LOGOUT_AFTER_DEVICE_RESET)
690                 if (ret == SUCCESS) {
691                         if (fcport->flags & FC_FABRIC_DEVICE) {
692                                 qla2x00_fabric_logout(ha, fcport->loop_id);
693                                 qla2x00_mark_device_lost(ha, fcport);
694                         }
695                 }
696 #endif
697         } else {
698                 DEBUG2(printk(KERN_INFO
699                     "%s failed: loop not ready\n",__func__);)
700         }
701
702         if (ret == FAILED) {
703                 DEBUG3(printk("%s(%ld): device reset failed\n",
704                     __func__, ha->host_no));
705                 qla_printk(KERN_INFO, ha, "%s: device reset failed\n",
706                     __func__);
707
708                 goto eh_dev_reset_done;
709         }
710
711         /*
712          * If we are coming down the EH path, wait for all commands to
713          * complete for the device.
714          */
715         if (cmd->device->host->eh_active) {
716                 if (qla2x00_eh_wait_for_pending_target_commands(ha, id))
717                         ret = FAILED;
718
719                 if (ret == FAILED) {
720                         DEBUG3(printk("%s(%ld): failed while waiting for "
721                             "commands\n", __func__, ha->host_no));
722                         qla_printk(KERN_INFO, ha,
723                             "%s: failed while waiting for commands\n",
724                             __func__); 
725
726                         goto eh_dev_reset_done;
727                 }
728         }
729
730         qla_printk(KERN_INFO, ha,
731             "scsi(%ld:%d:%d): DEVICE RESET SUCCEEDED.\n", ha->host_no, id, lun);
732
733 eh_dev_reset_done:
734         spin_lock_irq(ha->host->host_lock);
735
736         return ret;
737 }
738
739 /**************************************************************************
740 * qla2x00_eh_wait_for_pending_commands
741 *
742 * Description:
743 *    Waits for all the commands to come back from the specified host.
744 *
745 * Input:
746 *    ha - pointer to scsi_qla_host structure.
747 *
748 * Returns:
749 *    1 : SUCCESS
750 *    0 : FAILED
751 *
752 * Note:
753 **************************************************************************/
754 static int
755 qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *ha)
756 {
757         int     cnt;
758         int     status;
759         srb_t           *sp;
760         struct scsi_cmnd *cmd;
761
762         status = 1;
763
764         /*
765          * Waiting for all commands for the designated target in the active
766          * array
767          */
768         for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
769                 spin_lock(&ha->hardware_lock);
770                 sp = ha->outstanding_cmds[cnt];
771                 if (sp) {
772                         cmd = sp->cmd;
773                         spin_unlock(&ha->hardware_lock);
774                         status = qla2x00_eh_wait_on_command(ha, cmd);
775                         if (status == 0)
776                                 break;
777                 }
778                 else {
779                         spin_unlock(&ha->hardware_lock);
780                 }
781         }
782         return (status);
783 }
784
785
786 /**************************************************************************
787 * qla2xxx_eh_bus_reset
788 *
789 * Description:
790 *    The bus reset function will reset the bus and abort any executing
791 *    commands.
792 *
793 * Input:
794 *    cmd = Linux SCSI command packet of the command that cause the
795 *          bus reset.
796 *
797 * Returns:
798 *    SUCCESS/FAILURE (defined as macro in scsi.h).
799 *
800 **************************************************************************/
801 int
802 qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
803 {
804         scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
805         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
806         srb_t *sp;
807         int ret;
808         unsigned int id, lun;
809         unsigned long serial;
810
811         ret = FAILED;
812
813         id = cmd->device->id;
814         lun = cmd->device->lun;
815         serial = cmd->serial_number;
816
817         sp = (srb_t *) CMD_SP(cmd);
818         if (!sp || !fcport)
819                 return ret;
820
821         qla_printk(KERN_INFO, ha,
822             "scsi(%ld:%d:%d): LOOP RESET ISSUED.\n", ha->host_no, id, lun);
823
824         spin_unlock_irq(ha->host->host_lock);
825
826         if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
827                 DEBUG2(printk("%s failed:board disabled\n",__func__));
828                 goto eh_bus_reset_done;
829         }
830
831         if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
832                 if (qla2x00_loop_reset(ha) == QLA_SUCCESS)
833                         ret = SUCCESS;
834         }
835         if (ret == FAILED)
836                 goto eh_bus_reset_done;
837
838         /* Waiting for our command in done_queue to be returned to OS.*/
839         if (cmd->device->host->eh_active)
840                 if (!qla2x00_eh_wait_for_pending_commands(ha))
841                         ret = FAILED;
842
843 eh_bus_reset_done:
844         qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
845             (ret == FAILED) ? "failed" : "succeded");
846
847         spin_lock_irq(ha->host->host_lock);
848
849         return ret;
850 }
851
852 /**************************************************************************
853 * qla2xxx_eh_host_reset
854 *
855 * Description:
856 *    The reset function will reset the Adapter.
857 *
858 * Input:
859 *      cmd = Linux SCSI command packet of the command that cause the
860 *            adapter reset.
861 *
862 * Returns:
863 *      Either SUCCESS or FAILED.
864 *
865 * Note:
866 **************************************************************************/
867 int
868 qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
869 {
870         scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
871         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
872         srb_t *sp;
873         int ret;
874         unsigned int id, lun;
875         unsigned long serial;
876
877         ret = FAILED;
878
879         id = cmd->device->id;
880         lun = cmd->device->lun;
881         serial = cmd->serial_number;
882
883         sp = (srb_t *) CMD_SP(cmd);
884         if (!sp || !fcport)
885                 return ret;
886
887         qla_printk(KERN_INFO, ha,
888             "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no, id, lun);
889
890         spin_unlock_irq(ha->host->host_lock);
891
892         if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
893                 goto eh_host_reset_lock;
894
895         /*
896          * Fixme-may be dpc thread is active and processing
897          * loop_resync,so wait a while for it to 
898          * be completed and then issue big hammer.Otherwise
899          * it may cause I/O failure as big hammer marks the
900          * devices as lost kicking of the port_down_timer
901          * while dpc is stuck for the mailbox to complete.
902          */
903         qla2x00_wait_for_loop_ready(ha);
904         set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
905         if (qla2x00_abort_isp(ha)) {
906                 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
907                 /* failed. schedule dpc to try */
908                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
909
910                 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
911                         goto eh_host_reset_lock;
912         } 
913         clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
914
915         /* Waiting for our command in done_queue to be returned to OS.*/
916         if (qla2x00_eh_wait_for_pending_commands(ha))
917                 ret = SUCCESS;
918
919 eh_host_reset_lock:
920         spin_lock_irq(ha->host->host_lock);
921
922         qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
923             (ret == FAILED) ? "failed" : "succeded");
924
925         return ret;
926 }
927
928 /*
929 * qla2x00_loop_reset
930 *      Issue loop reset.
931 *
932 * Input:
933 *      ha = adapter block pointer.
934 *
935 * Returns:
936 *      0 = success
937 */
938 static int
939 qla2x00_loop_reset(scsi_qla_host_t *ha)
940 {
941         int status = QLA_SUCCESS;
942         struct fc_port *fcport;
943
944         if (ha->flags.enable_lip_reset) {
945                 status = qla2x00_lip_reset(ha);
946         }
947
948         if (status == QLA_SUCCESS && ha->flags.enable_target_reset) {
949                 list_for_each_entry(fcport, &ha->fcports, list) {
950                         if (fcport->port_type != FCT_TARGET)
951                                 continue;
952
953                         status = qla2x00_target_reset(ha, fcport);
954                         if (status != QLA_SUCCESS)
955                                 break;
956                 }
957         }
958
959         if (status == QLA_SUCCESS &&
960                 ((!ha->flags.enable_target_reset && 
961                   !ha->flags.enable_lip_reset) ||
962                 ha->flags.enable_lip_full_login)) {
963
964                 status = qla2x00_full_login_lip(ha);
965         }
966
967         /* Issue marker command only when we are going to start the I/O */
968         ha->marker_needed = 1;
969
970         if (status) {
971                 /* Empty */
972                 DEBUG2_3(printk("%s(%ld): **** FAILED ****\n",
973                                 __func__,
974                                 ha->host_no);)
975         } else {
976                 /* Empty */
977                 DEBUG3(printk("%s(%ld): exiting normally.\n",
978                                 __func__,
979                                 ha->host_no);)
980         }
981
982         return(status);
983 }
984
985 /*
986  * qla2x00_device_reset
987  *      Issue bus device reset message to the target.
988  *
989  * Input:
990  *      ha = adapter block pointer.
991  *      t = SCSI ID.
992  *      TARGET_QUEUE_LOCK must be released.
993  *      ADAPTER_STATE_LOCK must be released.
994  *
995  * Context:
996  *      Kernel context.
997  */
998 static int
999 qla2x00_device_reset(scsi_qla_host_t *ha, fc_port_t *reset_fcport)
1000 {
1001         /* Abort Target command will clear Reservation */
1002         return qla2x00_abort_target(reset_fcport);
1003 }
1004
1005 static int
1006 qla2xxx_slave_alloc(struct scsi_device *sdev)
1007 {
1008         scsi_qla_host_t *ha = to_qla_host(sdev->host);
1009         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1010         fc_port_t *fcport;
1011         int found;
1012
1013         if (!rport)
1014                 return -ENXIO;
1015
1016         found = 0;
1017         list_for_each_entry(fcport, &ha->fcports, list) {
1018                 if (rport->port_name ==
1019                     be64_to_cpu(*(uint64_t *)fcport->port_name)) {
1020                         found++;
1021                         break;
1022                 }
1023                 
1024         }
1025         if (!found)
1026                 return -ENXIO;
1027
1028         sdev->hostdata = fcport;
1029
1030         return 0;
1031 }
1032
1033 static int
1034 qla2xxx_slave_configure(struct scsi_device *sdev)
1035 {
1036         scsi_qla_host_t *ha = to_qla_host(sdev->host);
1037         struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1038
1039         if (sdev->tagged_supported)
1040                 scsi_activate_tcq(sdev, 32);
1041         else
1042                 scsi_deactivate_tcq(sdev, 32);
1043
1044         rport->dev_loss_tmo = ha->port_down_retry_count + 5;
1045
1046         return 0;
1047 }
1048
1049 static void
1050 qla2xxx_slave_destroy(struct scsi_device *sdev)
1051 {
1052         sdev->hostdata = NULL;
1053 }
1054
1055 /**
1056  * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
1057  * @ha: HA context
1058  *
1059  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1060  * supported addressing method.
1061  */
1062 static void
1063 qla2x00_config_dma_addressing(scsi_qla_host_t *ha)
1064 {
1065         /* Assume 32bit DMA address */
1066         ha->flags.enable_64bit_addressing = 0;
1067         ha->calc_request_entries = qla2x00_calc_iocbs_32;
1068         ha->build_scsi_iocbs = qla2x00_build_scsi_iocbs_32;
1069
1070         /*
1071          * Given the two variants pci_set_dma_mask(), allow the compiler to
1072          * assist in setting the proper dma mask.
1073          */
1074         if (sizeof(dma_addr_t) > 4) {
1075                 if (pci_set_dma_mask(ha->pdev, DMA_64BIT_MASK) == 0) {
1076                         ha->flags.enable_64bit_addressing = 1;
1077                         ha->calc_request_entries = qla2x00_calc_iocbs_64;
1078                         ha->build_scsi_iocbs = qla2x00_build_scsi_iocbs_64;
1079
1080                         if (pci_set_consistent_dma_mask(ha->pdev,
1081                             DMA_64BIT_MASK)) {
1082                                 qla_printk(KERN_DEBUG, ha, 
1083                                     "Failed to set 64 bit PCI consistent mask; "
1084                                     "using 32 bit.\n");
1085                                 pci_set_consistent_dma_mask(ha->pdev,
1086                                     DMA_32BIT_MASK);
1087                         }
1088                 } else {
1089                         qla_printk(KERN_DEBUG, ha,
1090                             "Failed to set 64 bit PCI DMA mask, falling back "
1091                             "to 32 bit MASK.\n");
1092                         pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1093                 }
1094         } else {
1095                 pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1096         }
1097 }
1098
1099 static int
1100 qla2x00_iospace_config(scsi_qla_host_t *ha)
1101 {
1102         unsigned long   pio, pio_len, pio_flags;
1103         unsigned long   mmio, mmio_len, mmio_flags;
1104
1105         /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1106         pio = pci_resource_start(ha->pdev, 0);
1107         pio_len = pci_resource_len(ha->pdev, 0);
1108         pio_flags = pci_resource_flags(ha->pdev, 0);
1109         if (pio_flags & IORESOURCE_IO) {
1110                 if (pio_len < MIN_IOBASE_LEN) {
1111                         qla_printk(KERN_WARNING, ha,
1112                             "Invalid PCI I/O region size (%s)...\n",
1113                                 pci_name(ha->pdev));
1114                         pio = 0;
1115                 }
1116         } else {
1117                 qla_printk(KERN_WARNING, ha,
1118                     "region #0 not a PIO resource (%s)...\n",
1119                     pci_name(ha->pdev));
1120                 pio = 0;
1121         }
1122
1123         /* Use MMIO operations for all accesses. */
1124         mmio = pci_resource_start(ha->pdev, 1);
1125         mmio_len = pci_resource_len(ha->pdev, 1);
1126         mmio_flags = pci_resource_flags(ha->pdev, 1);
1127
1128         if (!(mmio_flags & IORESOURCE_MEM)) {
1129                 qla_printk(KERN_ERR, ha,
1130                     "region #0 not an MMIO resource (%s), aborting\n",
1131                     pci_name(ha->pdev));
1132                 goto iospace_error_exit;
1133         }
1134         if (mmio_len < MIN_IOBASE_LEN) {
1135                 qla_printk(KERN_ERR, ha,
1136                     "Invalid PCI mem region size (%s), aborting\n",
1137                         pci_name(ha->pdev));
1138                 goto iospace_error_exit;
1139         }
1140
1141         if (pci_request_regions(ha->pdev, ha->brd_info->drv_name)) {
1142                 qla_printk(KERN_WARNING, ha,
1143                     "Failed to reserve PIO/MMIO regions (%s)\n",
1144                     pci_name(ha->pdev));
1145
1146                 goto iospace_error_exit;
1147         }
1148
1149         ha->pio_address = pio;
1150         ha->pio_length = pio_len;
1151         ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
1152         if (!ha->iobase) {
1153                 qla_printk(KERN_ERR, ha,
1154                     "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
1155
1156                 goto iospace_error_exit;
1157         }
1158
1159         return (0);
1160
1161 iospace_error_exit:
1162         return (-ENOMEM);
1163 }
1164
1165 /*
1166  * PCI driver interface
1167  */
1168 int qla2x00_probe_one(struct pci_dev *pdev, struct qla_board_info *brd_info)
1169 {
1170         int     ret;
1171         device_reg_t __iomem *reg;
1172         struct Scsi_Host *host;
1173         scsi_qla_host_t *ha;
1174         unsigned long   flags = 0;
1175         unsigned long   wait_switch = 0;
1176         char pci_info[20];
1177         char fw_str[30];
1178         fc_port_t *fcport;
1179
1180         if (pci_enable_device(pdev))
1181                 return -1;
1182
1183         host = scsi_host_alloc(&qla2x00_driver_template,
1184             sizeof(scsi_qla_host_t));
1185         if (host == NULL) {
1186                 printk(KERN_WARNING
1187                     "qla2xxx: Couldn't allocate host from scsi layer!\n");
1188                 goto probe_disable_device;
1189         }
1190
1191         /* Clear our data area */
1192         ha = (scsi_qla_host_t *)host->hostdata;
1193         memset(ha, 0, sizeof(scsi_qla_host_t));
1194
1195         ha->pdev = pdev;
1196         ha->host = host;
1197         ha->host_no = host->host_no;
1198         ha->brd_info = brd_info;
1199         sprintf(ha->host_str, "%s_%ld", ha->brd_info->drv_name, ha->host_no);
1200
1201         /* Configure PCI I/O space */
1202         ret = qla2x00_iospace_config(ha);
1203         if (ret != 0) {
1204                 goto probe_alloc_failed;
1205         }
1206
1207         /* Sanitize the information from PCI BIOS. */
1208         host->irq = pdev->irq;
1209
1210         qla_printk(KERN_INFO, ha,
1211             "Found an %s, irq %d, iobase 0x%p\n", ha->brd_info->isp_name,
1212             host->irq, ha->iobase);
1213
1214         spin_lock_init(&ha->hardware_lock);
1215
1216         /* 4.23 Initialize /proc/scsi/qla2x00 counters */
1217         ha->actthreads = 0;
1218         ha->qthreads   = 0;
1219         ha->total_isr_cnt = 0;
1220         ha->total_isp_aborts = 0;
1221         ha->total_lip_cnt = 0;
1222         ha->total_dev_errs = 0;
1223         ha->total_ios = 0;
1224         ha->total_bytes = 0;
1225
1226         ha->prev_topology = 0;
1227         ha->ports = MAX_BUSES;
1228
1229         if (IS_QLA2100(ha)) {
1230                 ha->max_targets = MAX_TARGETS_2100;
1231                 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
1232                 ha->request_q_length = REQUEST_ENTRY_CNT_2100;
1233                 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1234                 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1235                 host->sg_tablesize = 32;
1236         } else if (IS_QLA2200(ha)) {
1237                 ha->max_targets = MAX_TARGETS_2200;
1238                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1239                 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1240                 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1241                 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1242         } else /*if (IS_QLA2300(ha))*/ {
1243                 ha->max_targets = MAX_TARGETS_2200;
1244                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1245                 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1246                 ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
1247                 ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
1248         }
1249         host->can_queue = ha->request_q_length + 128;
1250
1251         /* load the F/W, read paramaters, and init the H/W */
1252         ha->instance = num_hosts;
1253
1254         init_MUTEX(&ha->mbx_cmd_sem);
1255         init_MUTEX_LOCKED(&ha->mbx_intr_sem);
1256
1257         INIT_LIST_HEAD(&ha->list);
1258         INIT_LIST_HEAD(&ha->fcports);
1259         INIT_LIST_HEAD(&ha->rscn_fcports);
1260
1261         /*
1262          * These locks are used to prevent more than one CPU
1263          * from modifying the queue at the same time. The
1264          * higher level "host_lock" will reduce most
1265          * contention for these locks.
1266          */
1267         spin_lock_init(&ha->mbx_reg_lock);
1268
1269         ha->dpc_pid = -1;
1270         init_completion(&ha->dpc_inited);
1271         init_completion(&ha->dpc_exited);
1272
1273         qla2x00_config_dma_addressing(ha);
1274         if (qla2x00_mem_alloc(ha)) {
1275                 qla_printk(KERN_WARNING, ha,
1276                     "[ERROR] Failed to allocate memory for adapter\n");
1277
1278                 goto probe_alloc_failed;
1279         }
1280
1281         pci_set_drvdata(pdev, ha);
1282         host->this_id = 255;
1283         host->cmd_per_lun = 3;
1284         host->unique_id = ha->instance;
1285         host->max_cmd_len = MAX_CMDSZ;
1286         host->max_channel = ha->ports - 1;
1287         host->max_id = ha->max_targets;
1288         host->max_lun = ha->max_luns;
1289         host->transportt = qla2xxx_transport_template;
1290         if (scsi_add_host(host, &pdev->dev))
1291                 goto probe_alloc_failed;
1292
1293         qla2x00_alloc_sysfs_attr(ha);
1294
1295         if (qla2x00_initialize_adapter(ha) &&
1296             !(ha->device_flags & DFLG_NO_CABLE)) {
1297
1298                 qla_printk(KERN_WARNING, ha,
1299                     "Failed to initialize adapter\n");
1300
1301                 DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
1302                     "Adapter flags %x.\n",
1303                     ha->host_no, ha->device_flags));
1304
1305                 goto probe_failed;
1306         }
1307
1308         qla2x00_init_host_attr(ha);
1309
1310         /*
1311          * Startup the kernel thread for this host adapter
1312          */
1313         ha->dpc_should_die = 0;
1314         ha->dpc_pid = kernel_thread(qla2x00_do_dpc, ha, 0);
1315         if (ha->dpc_pid < 0) {
1316                 qla_printk(KERN_WARNING, ha,
1317                     "Unable to start DPC thread!\n");
1318
1319                 goto probe_failed;
1320         }
1321         wait_for_completion(&ha->dpc_inited);
1322
1323         if (IS_QLA2100(ha) || IS_QLA2200(ha))
1324                 ret = request_irq(host->irq, qla2100_intr_handler,
1325                     SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
1326         else
1327                 ret = request_irq(host->irq, qla2300_intr_handler,
1328                     SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
1329         if (ret != 0) {
1330                 qla_printk(KERN_WARNING, ha,
1331                     "Failed to reserve interrupt %d already in use.\n",
1332                     host->irq);
1333                 goto probe_failed;
1334         }
1335
1336         /* Initialized the timer */
1337         qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
1338
1339         DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
1340             ha->host_no, ha));
1341
1342         reg = ha->iobase;
1343
1344         /* Disable ISP interrupts. */
1345         qla2x00_disable_intrs(ha);
1346
1347         /* Ensure mailbox registers are free. */
1348         spin_lock_irqsave(&ha->hardware_lock, flags);
1349         WRT_REG_WORD(&reg->semaphore, 0);
1350         WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
1351         WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
1352
1353         /* Enable proper parity */
1354         if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
1355                 if (IS_QLA2300(ha))
1356                         /* SRAM parity */
1357                         WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x1));
1358                 else
1359                         /* SRAM, Instruction RAM and GP RAM parity */
1360                         WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x7));
1361         }
1362         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1363
1364         /* Enable chip interrupts. */
1365         qla2x00_enable_intrs(ha);
1366
1367         /* v2.19.5b6 */
1368         /*
1369          * Wait around max loop_reset_delay secs for the devices to come
1370          * on-line. We don't want Linux scanning before we are ready.
1371          *
1372          */
1373         for (wait_switch = jiffies + (ha->loop_reset_delay * HZ);
1374             time_before(jiffies,wait_switch) &&
1375              !(ha->device_flags & (DFLG_NO_CABLE | DFLG_FABRIC_DEVICES))
1376              && (ha->device_flags & SWITCH_FOUND) ;) {
1377
1378                 qla2x00_check_fabric_devices(ha);
1379
1380                 msleep(10);
1381         }
1382
1383         ha->flags.init_done = 1;
1384         num_hosts++;
1385
1386         qla_printk(KERN_INFO, ha, "\n"
1387             " QLogic Fibre Channel HBA Driver: %s\n"
1388             "  QLogic %s - %s\n"
1389             "  %s: %s @ %s hdma%c, host#=%ld, fw=%s\n", qla2x00_version_str,
1390             ha->model_number, ha->model_desc ? ha->model_desc: "",
1391             ha->brd_info->isp_name, qla2x00_get_pci_info_str(ha, pci_info),
1392             pci_name(ha->pdev), ha->flags.enable_64bit_addressing ? '+': '-',
1393             ha->host_no, qla2x00_get_fw_version_str(ha, fw_str));
1394
1395         /* Go with fc_rport registration. */
1396         list_for_each_entry(fcport, &ha->fcports, list)
1397                 qla2x00_reg_remote_port(ha, fcport);
1398
1399         return 0;
1400
1401 probe_failed:
1402         fc_remove_host(ha->host);
1403
1404         scsi_remove_host(host);
1405
1406 probe_alloc_failed:
1407         qla2x00_free_device(ha);
1408
1409         scsi_host_put(host);
1410
1411 probe_disable_device:
1412         pci_disable_device(pdev);
1413
1414         return -1;
1415 }
1416 EXPORT_SYMBOL_GPL(qla2x00_probe_one);
1417
1418 void qla2x00_remove_one(struct pci_dev *pdev)
1419 {
1420         scsi_qla_host_t *ha;
1421
1422         ha = pci_get_drvdata(pdev);
1423
1424         qla2x00_free_sysfs_attr(ha);
1425
1426         fc_remove_host(ha->host);
1427
1428         scsi_remove_host(ha->host);
1429
1430         qla2x00_free_device(ha);
1431
1432         scsi_host_put(ha->host);
1433
1434         pci_set_drvdata(pdev, NULL);
1435 }
1436 EXPORT_SYMBOL_GPL(qla2x00_remove_one);
1437
1438 static void
1439 qla2x00_free_device(scsi_qla_host_t *ha)
1440 {
1441         int ret;
1442
1443         /* Abort any outstanding IO descriptors. */
1444         if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1445                 qla2x00_cancel_io_descriptors(ha);
1446
1447         /* turn-off interrupts on the card */
1448         if (ha->interrupts_on)
1449                 qla2x00_disable_intrs(ha);
1450
1451         /* Disable timer */
1452         if (ha->timer_active)
1453                 qla2x00_stop_timer(ha);
1454
1455         /* Kill the kernel thread for this host */
1456         if (ha->dpc_pid >= 0) {
1457                 ha->dpc_should_die = 1;
1458                 wmb();
1459                 ret = kill_proc(ha->dpc_pid, SIGHUP, 1);
1460                 if (ret) {
1461                         qla_printk(KERN_ERR, ha,
1462                             "Unable to signal DPC thread -- (%d)\n", ret);
1463
1464                         /* TODO: SOMETHING MORE??? */
1465                 } else {
1466                         wait_for_completion(&ha->dpc_exited);
1467                 }
1468         }
1469
1470         qla2x00_mem_free(ha);
1471
1472
1473         ha->flags.online = 0;
1474
1475         /* Detach interrupts */
1476         if (ha->pdev->irq)
1477                 free_irq(ha->pdev->irq, ha);
1478
1479         /* release io space registers  */
1480         if (ha->iobase)
1481                 iounmap(ha->iobase);
1482         pci_release_regions(ha->pdev);
1483
1484         pci_disable_device(ha->pdev);
1485 }
1486
1487
1488 /*
1489  * The following support functions are adopted to handle
1490  * the re-entrant qla2x00_proc_info correctly.
1491  */
1492 static void
1493 copy_mem_info(struct info_str *info, char *data, int len)
1494 {
1495         if (info->pos + len > info->offset + info->length)
1496                 len = info->offset + info->length - info->pos;
1497
1498         if (info->pos + len < info->offset) {
1499                 info->pos += len;
1500                 return;
1501         }
1502  
1503         if (info->pos < info->offset) {
1504                 off_t partial;
1505  
1506                 partial = info->offset - info->pos;
1507                 data += partial;
1508                 info->pos += partial;
1509                 len  -= partial;
1510         }
1511  
1512         if (len > 0) {
1513                 memcpy(info->buffer, data, len);
1514                 info->pos += len;
1515                 info->buffer += len;
1516         }
1517 }
1518
1519 static int
1520 copy_info(struct info_str *info, char *fmt, ...)
1521 {
1522         va_list args;
1523         char buf[256];
1524         int len;
1525  
1526         va_start(args, fmt);
1527         len = vsprintf(buf, fmt, args);
1528         va_end(args);
1529  
1530         copy_mem_info(info, buf, len);
1531
1532         return (len);
1533 }
1534
1535 /*************************************************************************
1536 * qla2x00_proc_info
1537 *
1538 * Description:
1539 *   Return information to handle /proc support for the driver.
1540 *
1541 * inout : decides the direction of the dataflow and the meaning of the
1542 *         variables
1543 * buffer: If inout==0 data is being written to it else read from it
1544 *         (ptr to a page buffer)
1545 * *start: If inout==0 start of the valid data in the buffer
1546 * offset: If inout==0 starting offset from the beginning of all
1547 *         possible data to return.
1548 * length: If inout==0 max number of bytes to be written into the buffer
1549 *         else number of bytes in "buffer"
1550 * Returns:
1551 *         < 0:  error. errno value.
1552 *         >= 0: sizeof data returned.
1553 *************************************************************************/
1554 int
1555 qla2x00_proc_info(struct Scsi_Host *shost, char *buffer,
1556     char **start, off_t offset, int length, int inout)
1557 {
1558         struct info_str info;
1559         int             retval = -EINVAL;
1560         uint32_t        tmp_sn;
1561         uint32_t        *flags;
1562         uint8_t         *loop_state;
1563         scsi_qla_host_t *ha;
1564         char fw_info[30];
1565  
1566         DEBUG3(printk(KERN_INFO
1567             "Entering proc_info buff_in=%p, offset=0x%lx, length=0x%x\n",
1568             buffer, offset, length);)
1569
1570         ha = (scsi_qla_host_t *) shost->hostdata;
1571
1572         if (inout) {
1573                 /* Has data been written to the file? */
1574                 DEBUG3(printk(
1575                     "%s: has data been written to the file. \n",
1576                     __func__);)
1577
1578                 return -ENOSYS;
1579         }
1580
1581         if (start) {
1582                 *start = buffer;
1583         }
1584
1585         info.buffer = buffer;
1586         info.length = length;
1587         info.offset = offset;
1588         info.pos    = 0;
1589
1590         /* start building the print buffer */
1591         copy_info(&info,
1592             "QLogic PCI to Fibre Channel Host Adapter for %s:\n"
1593             "        Firmware version %s, ",
1594             ha->model_number, qla2x00_get_fw_version_str(ha, fw_info));
1595
1596         copy_info(&info, "Driver version %s\n", qla2x00_version_str);
1597
1598         tmp_sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | 
1599             ha->serial1;
1600         copy_info(&info, "ISP: %s, Serial# %c%05d\n",
1601             ha->brd_info->isp_name, ('A' + tmp_sn/100000), (tmp_sn%100000));
1602
1603         copy_info(&info,
1604             "Request Queue = 0x%llx, Response Queue = 0x%llx\n",
1605                 (unsigned long long)ha->request_dma,
1606                 (unsigned long long)ha->response_dma);
1607
1608         copy_info(&info,
1609             "Request Queue count = %d, Response Queue count = %d\n",
1610             ha->request_q_length, ha->response_q_length);
1611
1612         copy_info(&info,
1613             "Total number of active commands = %ld\n",
1614             ha->actthreads);
1615
1616         copy_info(&info,
1617             "Total number of interrupts = %ld\n",
1618             (long)ha->total_isr_cnt);
1619
1620         copy_info(&info,
1621             "    Device queue depth = 0x%x\n",
1622             (ql2xmaxqdepth == 0) ? 16 : ql2xmaxqdepth);
1623
1624         copy_info(&info,
1625             "Number of free request entries = %d\n", ha->req_q_cnt);
1626
1627         copy_info(&info,
1628             "Number of mailbox timeouts = %ld\n", ha->total_mbx_timeout);
1629
1630         copy_info(&info,
1631             "Number of ISP aborts = %ld\n", ha->total_isp_aborts);
1632
1633         copy_info(&info,
1634             "Number of loop resyncs = %ld\n", ha->total_loop_resync);
1635
1636         copy_info(&info,
1637             "Number of retries for empty slots = %ld\n",
1638             qla2x00_stats.outarray_full);
1639
1640         flags = (uint32_t *) &ha->flags;
1641
1642         if (atomic_read(&ha->loop_state) == LOOP_DOWN) {
1643                 loop_state = "DOWN";
1644         } else if (atomic_read(&ha->loop_state) == LOOP_UP) {
1645                 loop_state = "UP";
1646         } else if (atomic_read(&ha->loop_state) == LOOP_READY) {
1647                 loop_state = "READY";
1648         } else if (atomic_read(&ha->loop_state) == LOOP_TIMEOUT) {
1649                 loop_state = "TIMEOUT";
1650         } else if (atomic_read(&ha->loop_state) == LOOP_UPDATE) {
1651                 loop_state = "UPDATE";
1652         } else {
1653                 loop_state = "UNKNOWN";
1654         }
1655
1656         copy_info(&info, 
1657             "Host adapter:loop state = <%s>, flags = 0x%lx\n",
1658             loop_state , *flags);
1659
1660         copy_info(&info, "Dpc flags = 0x%lx\n", ha->dpc_flags);
1661
1662         copy_info(&info, "MBX flags = 0x%x\n", ha->mbx_flags);
1663
1664         copy_info(&info, "Link down Timeout = %3.3d\n",
1665             ha->link_down_timeout);
1666
1667         copy_info(&info, "Port down retry = %3.3d\n",
1668             ha->port_down_retry_count);
1669
1670         copy_info(&info, "Login retry count = %3.3d\n",
1671             ha->login_retry_count);
1672
1673         copy_info(&info,
1674             "Commands retried with dropped frame(s) = %d\n",
1675             ha->dropped_frame_error_cnt);
1676
1677         copy_info(&info,
1678             "Product ID = %04x %04x %04x %04x\n", ha->product_id[0],
1679             ha->product_id[1], ha->product_id[2], ha->product_id[3]);
1680
1681         copy_info(&info, "\n");
1682
1683         /* 2.25 node/port display to proc */
1684         /* Display the node name for adapter */
1685         copy_info(&info, "\nSCSI Device Information:\n");
1686         copy_info(&info,
1687             "scsi-qla%d-adapter-node="
1688             "%02x%02x%02x%02x%02x%02x%02x%02x;\n",
1689             (int)ha->instance,
1690             ha->init_cb->node_name[0],
1691             ha->init_cb->node_name[1],
1692             ha->init_cb->node_name[2],
1693             ha->init_cb->node_name[3],
1694             ha->init_cb->node_name[4],
1695             ha->init_cb->node_name[5],
1696             ha->init_cb->node_name[6],
1697             ha->init_cb->node_name[7]);
1698
1699         /* display the port name for adapter */
1700         copy_info(&info,
1701             "scsi-qla%d-adapter-port="
1702             "%02x%02x%02x%02x%02x%02x%02x%02x;\n",
1703             (int)ha->instance,
1704             ha->init_cb->port_name[0],
1705             ha->init_cb->port_name[1],
1706             ha->init_cb->port_name[2],
1707             ha->init_cb->port_name[3],
1708             ha->init_cb->port_name[4],
1709             ha->init_cb->port_name[5],
1710             ha->init_cb->port_name[6],
1711             ha->init_cb->port_name[7]);
1712
1713         retval = info.pos > info.offset ? info.pos - info.offset : 0;
1714
1715         DEBUG3(printk(KERN_INFO 
1716             "Exiting proc_info: info.pos=%d, offset=0x%lx, "
1717             "length=0x%x\n", info.pos, offset, length);)
1718
1719         return (retval);
1720 }
1721
1722 /*
1723  * qla2x00_mark_device_lost Updates fcport state when device goes offline.
1724  *
1725  * Input: ha = adapter block pointer.  fcport = port structure pointer.
1726  *
1727  * Return: None.
1728  *
1729  * Context:
1730  */
1731 void qla2x00_mark_device_lost(scsi_qla_host_t *ha, fc_port_t *fcport,
1732     int do_login)
1733 {
1734         if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
1735                 fc_remote_port_block(fcport->rport);
1736         /* 
1737          * We may need to retry the login, so don't change the state of the
1738          * port but do the retries.
1739          */
1740         if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
1741                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1742
1743         if (!do_login)
1744                 return;
1745
1746         if (fcport->login_retry == 0) {
1747                 fcport->login_retry = ha->login_retry_count;
1748                 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
1749
1750                 DEBUG(printk("scsi(%ld): Port login retry: "
1751                     "%02x%02x%02x%02x%02x%02x%02x%02x, "
1752                     "id = 0x%04x retry cnt=%d\n",
1753                     ha->host_no,
1754                     fcport->port_name[0],
1755                     fcport->port_name[1],
1756                     fcport->port_name[2],
1757                     fcport->port_name[3],
1758                     fcport->port_name[4],
1759                     fcport->port_name[5],
1760                     fcport->port_name[6],
1761                     fcport->port_name[7],
1762                     fcport->loop_id,
1763                     fcport->login_retry));
1764         }
1765 }
1766
1767 /*
1768  * qla2x00_mark_all_devices_lost
1769  *      Updates fcport state when device goes offline.
1770  *
1771  * Input:
1772  *      ha = adapter block pointer.
1773  *      fcport = port structure pointer.
1774  *
1775  * Return:
1776  *      None.
1777  *
1778  * Context:
1779  */
1780 void
1781 qla2x00_mark_all_devices_lost(scsi_qla_host_t *ha) 
1782 {
1783         fc_port_t *fcport;
1784
1785         list_for_each_entry(fcport, &ha->fcports, list) {
1786                 if (fcport->port_type != FCT_TARGET)
1787                         continue;
1788
1789                 /*
1790                  * No point in marking the device as lost, if the device is
1791                  * already DEAD.
1792                  */
1793                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
1794                         continue;
1795                 if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
1796                         fc_remote_port_block(fcport->rport);
1797                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1798         }
1799 }
1800
1801 /*
1802 * qla2x00_mem_alloc
1803 *      Allocates adapter memory.
1804 *
1805 * Returns:
1806 *      0  = success.
1807 *      1  = failure.
1808 */
1809 static uint8_t
1810 qla2x00_mem_alloc(scsi_qla_host_t *ha)
1811 {
1812         char    name[16];
1813         uint8_t   status = 1;
1814         int     retry= 10;
1815
1816         do {
1817                 /*
1818                  * This will loop only once if everything goes well, else some
1819                  * number of retries will be performed to get around a kernel
1820                  * bug where available mem is not allocated until after a
1821                  * little delay and a retry.
1822                  */
1823                 ha->request_ring = dma_alloc_coherent(&ha->pdev->dev,
1824                     (ha->request_q_length + 1) * sizeof(request_t),
1825                     &ha->request_dma, GFP_KERNEL);
1826                 if (ha->request_ring == NULL) {
1827                         qla_printk(KERN_WARNING, ha,
1828                             "Memory Allocation failed - request_ring\n");
1829
1830                         qla2x00_mem_free(ha);
1831                         msleep(100);
1832
1833                         continue;
1834                 }
1835
1836                 ha->response_ring = dma_alloc_coherent(&ha->pdev->dev,
1837                     (ha->response_q_length + 1) * sizeof(response_t),
1838                     &ha->response_dma, GFP_KERNEL);
1839                 if (ha->response_ring == NULL) {
1840                         qla_printk(KERN_WARNING, ha,
1841                             "Memory Allocation failed - response_ring\n");
1842
1843                         qla2x00_mem_free(ha);
1844                         msleep(100);
1845
1846                         continue;
1847                 }
1848
1849                 ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
1850                     &ha->gid_list_dma, GFP_KERNEL);
1851                 if (ha->gid_list == NULL) {
1852                         qla_printk(KERN_WARNING, ha,
1853                             "Memory Allocation failed - gid_list\n");
1854
1855                         qla2x00_mem_free(ha);
1856                         msleep(100);
1857
1858                         continue;
1859                 }
1860
1861                 ha->rlc_rsp = dma_alloc_coherent(&ha->pdev->dev,
1862                     sizeof(rpt_lun_cmd_rsp_t), &ha->rlc_rsp_dma, GFP_KERNEL);
1863                 if (ha->rlc_rsp == NULL) {
1864                         qla_printk(KERN_WARNING, ha,
1865                                 "Memory Allocation failed - rlc");
1866
1867                         qla2x00_mem_free(ha);
1868                         msleep(100);
1869
1870                         continue;
1871                 }
1872
1873                 snprintf(name, sizeof(name), "qla2xxx_%ld", ha->host_no);
1874                 ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
1875                     DMA_POOL_SIZE, 8, 0);
1876                 if (ha->s_dma_pool == NULL) {
1877                         qla_printk(KERN_WARNING, ha,
1878                             "Memory Allocation failed - s_dma_pool\n");
1879
1880                         qla2x00_mem_free(ha);
1881                         msleep(100);
1882
1883                         continue;
1884                 }
1885
1886                 /* get consistent memory allocated for init control block */
1887                 ha->init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1888                     &ha->init_cb_dma);
1889                 if (ha->init_cb == NULL) {
1890                         qla_printk(KERN_WARNING, ha,
1891                             "Memory Allocation failed - init_cb\n");
1892
1893                         qla2x00_mem_free(ha);
1894                         msleep(100);
1895
1896                         continue;
1897                 }
1898                 memset(ha->init_cb, 0, sizeof(init_cb_t));
1899
1900                 /* Get consistent memory allocated for Get Port Database cmd */
1901                 ha->iodesc_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1902                     &ha->iodesc_pd_dma);
1903                 if (ha->iodesc_pd == NULL) {
1904                         /* error */
1905                         qla_printk(KERN_WARNING, ha,
1906                             "Memory Allocation failed - iodesc_pd\n");
1907
1908                         qla2x00_mem_free(ha);
1909                         msleep(100);
1910
1911                         continue;
1912                 }
1913                 memset(ha->iodesc_pd, 0, PORT_DATABASE_SIZE);
1914
1915                 /* Allocate ioctl related memory. */
1916                 if (qla2x00_alloc_ioctl_mem(ha)) {
1917                         qla_printk(KERN_WARNING, ha,
1918                             "Memory Allocation failed - ioctl_mem\n");
1919
1920                         qla2x00_mem_free(ha);
1921                         msleep(100);
1922
1923                         continue;
1924                 }
1925
1926                 if (qla2x00_allocate_sp_pool(ha)) {
1927                         qla_printk(KERN_WARNING, ha,
1928                             "Memory Allocation failed - "
1929                             "qla2x00_allocate_sp_pool()\n");
1930
1931                         qla2x00_mem_free(ha);
1932                         msleep(100);
1933
1934                         continue;
1935                 }
1936
1937                 /* Allocate memory for SNS commands */
1938                 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1939                         /* Get consistent memory allocated for SNS commands */
1940                         ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
1941                             sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma,
1942                             GFP_KERNEL);
1943                         if (ha->sns_cmd == NULL) {
1944                                 /* error */
1945                                 qla_printk(KERN_WARNING, ha,
1946                                     "Memory Allocation failed - sns_cmd\n");
1947
1948                                 qla2x00_mem_free(ha);
1949                                 msleep(100);
1950
1951                                 continue;
1952                         }
1953                         memset(ha->sns_cmd, 0, sizeof(struct sns_cmd_pkt));
1954                 } else {
1955                         /* Get consistent memory allocated for MS IOCB */
1956                         ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1957                             &ha->ms_iocb_dma);
1958                         if (ha->ms_iocb == NULL) {
1959                                 /* error */
1960                                 qla_printk(KERN_WARNING, ha,
1961                                     "Memory Allocation failed - ms_iocb\n");
1962
1963                                 qla2x00_mem_free(ha);
1964                                 msleep(100);
1965
1966                                 continue;
1967                         }
1968                         memset(ha->ms_iocb, 0, sizeof(ms_iocb_entry_t));
1969
1970                         /*
1971                          * Get consistent memory allocated for CT SNS
1972                          * commands
1973                          */
1974                         ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
1975                             sizeof(struct ct_sns_pkt), &ha->ct_sns_dma,
1976                             GFP_KERNEL);
1977                         if (ha->ct_sns == NULL) {
1978                                 /* error */
1979                                 qla_printk(KERN_WARNING, ha,
1980                                     "Memory Allocation failed - ct_sns\n");
1981
1982                                 qla2x00_mem_free(ha);
1983                                 msleep(100);
1984
1985                                 continue;
1986                         }
1987                         memset(ha->ct_sns, 0, sizeof(struct ct_sns_pkt));
1988                 }
1989
1990                 /* Done all allocations without any error. */
1991                 status = 0;
1992
1993         } while (retry-- && status != 0);
1994
1995         if (status) {
1996                 printk(KERN_WARNING
1997                         "%s(): **** FAILED ****\n", __func__);
1998         }
1999
2000         return(status);
2001 }
2002
2003 /*
2004 * qla2x00_mem_free
2005 *      Frees all adapter allocated memory.
2006 *
2007 * Input:
2008 *      ha = adapter block pointer.
2009 */
2010 static void
2011 qla2x00_mem_free(scsi_qla_host_t *ha)
2012 {
2013         struct list_head        *fcpl, *fcptemp;
2014         fc_port_t       *fcport;
2015         unsigned long   wtime;/* max wait time if mbx cmd is busy. */
2016
2017         if (ha == NULL) {
2018                 /* error */
2019                 DEBUG2(printk("%s(): ERROR invalid ha pointer.\n", __func__));
2020                 return;
2021         }
2022
2023         /* Make sure all other threads are stopped. */
2024         wtime = 60 * HZ;
2025         while (ha->dpc_wait && wtime) {
2026                 set_current_state(TASK_INTERRUPTIBLE);
2027                 wtime = schedule_timeout(wtime);
2028         }
2029
2030         /* free ioctl memory */
2031         qla2x00_free_ioctl_mem(ha);
2032
2033         /* free sp pool */
2034         qla2x00_free_sp_pool(ha);
2035
2036         if (ha->sns_cmd)
2037                 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
2038                     ha->sns_cmd, ha->sns_cmd_dma);
2039
2040         if (ha->ct_sns)
2041                 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
2042                     ha->ct_sns, ha->ct_sns_dma);
2043
2044         if (ha->ms_iocb)
2045                 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2046
2047         if (ha->iodesc_pd)
2048                 dma_pool_free(ha->s_dma_pool, ha->iodesc_pd, ha->iodesc_pd_dma);
2049
2050         if (ha->init_cb)
2051                 dma_pool_free(ha->s_dma_pool, ha->init_cb, ha->init_cb_dma);
2052
2053         if (ha->s_dma_pool)
2054                 dma_pool_destroy(ha->s_dma_pool);
2055
2056         if (ha->rlc_rsp)
2057                 dma_free_coherent(&ha->pdev->dev,
2058                     sizeof(rpt_lun_cmd_rsp_t), ha->rlc_rsp,
2059                     ha->rlc_rsp_dma);
2060
2061         if (ha->gid_list)
2062                 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
2063                     ha->gid_list_dma);
2064
2065         if (ha->response_ring)
2066                 dma_free_coherent(&ha->pdev->dev,
2067                     (ha->response_q_length + 1) * sizeof(response_t),
2068                     ha->response_ring, ha->response_dma);
2069
2070         if (ha->request_ring)
2071                 dma_free_coherent(&ha->pdev->dev,
2072                     (ha->request_q_length + 1) * sizeof(request_t),
2073                     ha->request_ring, ha->request_dma);
2074
2075         ha->sns_cmd = NULL;
2076         ha->sns_cmd_dma = 0;
2077         ha->ct_sns = NULL;
2078         ha->ct_sns_dma = 0;
2079         ha->ms_iocb = NULL;
2080         ha->ms_iocb_dma = 0;
2081         ha->iodesc_pd = NULL;
2082         ha->iodesc_pd_dma = 0;
2083         ha->init_cb = NULL;
2084         ha->init_cb_dma = 0;
2085
2086         ha->s_dma_pool = NULL;
2087
2088         ha->rlc_rsp = NULL;
2089         ha->rlc_rsp_dma = 0;
2090         ha->gid_list = NULL;
2091         ha->gid_list_dma = 0;
2092
2093         ha->response_ring = NULL;
2094         ha->response_dma = 0;
2095         ha->request_ring = NULL;
2096         ha->request_dma = 0;
2097
2098         list_for_each_safe(fcpl, fcptemp, &ha->fcports) {
2099                 fcport = list_entry(fcpl, fc_port_t, list);
2100
2101                 /* fc ports */
2102                 list_del_init(&fcport->list);
2103                 kfree(fcport);
2104         }
2105         INIT_LIST_HEAD(&ha->fcports);
2106
2107         if (ha->fw_dump)
2108                 free_pages((unsigned long)ha->fw_dump, ha->fw_dump_order);
2109
2110         if (ha->fw_dump_buffer)
2111                 vfree(ha->fw_dump_buffer);
2112
2113         ha->fw_dump = NULL;
2114         ha->fw_dump_reading = 0;
2115         ha->fw_dump_buffer = NULL;
2116 }
2117
2118 /*
2119  * qla2x00_allocate_sp_pool
2120  *       This routine is called during initialization to allocate
2121  *       memory for local srb_t.
2122  *
2123  * Input:
2124  *       ha   = adapter block pointer.
2125  *
2126  * Context:
2127  *      Kernel context.
2128  * 
2129  * Note: Sets the ref_count for non Null sp to one.
2130  */
2131 static int
2132 qla2x00_allocate_sp_pool(scsi_qla_host_t *ha) 
2133 {
2134         int      rval;
2135
2136         rval = QLA_SUCCESS;
2137         ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
2138             mempool_free_slab, srb_cachep);
2139         if (ha->srb_mempool == NULL) {
2140                 qla_printk(KERN_INFO, ha, "Unable to allocate SRB mempool.\n");
2141                 rval = QLA_FUNCTION_FAILED;
2142         }
2143         return (rval);
2144 }
2145
2146 /*
2147  *  This routine frees all adapter allocated memory.
2148  *  
2149  */
2150 static void
2151 qla2x00_free_sp_pool( scsi_qla_host_t *ha) 
2152 {
2153         if (ha->srb_mempool) {
2154                 mempool_destroy(ha->srb_mempool);
2155                 ha->srb_mempool = NULL;
2156         }
2157 }
2158
2159 /**************************************************************************
2160 * qla2x00_do_dpc
2161 *   This kernel thread is a task that is schedule by the interrupt handler
2162 *   to perform the background processing for interrupts.
2163 *
2164 * Notes:
2165 * This task always run in the context of a kernel thread.  It
2166 * is kick-off by the driver's detect code and starts up
2167 * up one per adapter. It immediately goes to sleep and waits for
2168 * some fibre event.  When either the interrupt handler or
2169 * the timer routine detects a event it will one of the task
2170 * bits then wake us up.
2171 **************************************************************************/
2172 static int
2173 qla2x00_do_dpc(void *data)
2174 {
2175         DECLARE_MUTEX_LOCKED(sem);
2176         scsi_qla_host_t *ha;
2177         fc_port_t       *fcport;
2178         uint8_t         status;
2179         uint16_t        next_loopid;
2180
2181         ha = (scsi_qla_host_t *)data;
2182
2183         lock_kernel();
2184
2185         daemonize("%s_dpc", ha->host_str);
2186         allow_signal(SIGHUP);
2187
2188         ha->dpc_wait = &sem;
2189
2190         set_user_nice(current, -20);
2191
2192         unlock_kernel();
2193
2194         complete(&ha->dpc_inited);
2195
2196         while (1) {
2197                 DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
2198
2199                 if (down_interruptible(&sem))
2200                         break;
2201
2202                 if (ha->dpc_should_die)
2203                         break;
2204
2205                 DEBUG3(printk("qla2x00: DPC handler waking up\n"));
2206
2207                 /* Initialization not yet finished. Don't do anything yet. */
2208                 if (!ha->flags.init_done || ha->dpc_active)
2209                         continue;
2210
2211                 DEBUG3(printk("scsi(%ld): DPC handler\n", ha->host_no));
2212
2213                 ha->dpc_active = 1;
2214
2215                 if (ha->flags.mbox_busy) {
2216                         ha->dpc_active = 0;
2217                         continue;
2218                 }
2219
2220                 if (test_and_clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
2221
2222                         DEBUG(printk("scsi(%ld): dpc: sched "
2223                             "qla2x00_abort_isp ha = %p\n",
2224                             ha->host_no, ha));
2225                         if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
2226                             &ha->dpc_flags))) {
2227
2228                                 if (qla2x00_abort_isp(ha)) {
2229                                         /* failed. retry later */
2230                                         set_bit(ISP_ABORT_NEEDED,
2231                                             &ha->dpc_flags);
2232                                 }
2233                                 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
2234                         }
2235                         DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
2236                             ha->host_no));
2237                 }
2238
2239                 if (test_and_clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) &&
2240                     (!(test_and_set_bit(RESET_ACTIVE, &ha->dpc_flags)))) {
2241
2242                         DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
2243                             ha->host_no));
2244
2245                         qla2x00_rst_aen(ha);
2246                         clear_bit(RESET_ACTIVE, &ha->dpc_flags);
2247                 }
2248
2249                 /* Retry each device up to login retry count */
2250                 if ((test_and_clear_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
2251                     !test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) &&
2252                     atomic_read(&ha->loop_state) != LOOP_DOWN) {
2253
2254                         DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
2255                             ha->host_no));
2256
2257                         next_loopid = 0;
2258                         list_for_each_entry(fcport, &ha->fcports, list) {
2259                                 if (fcport->port_type != FCT_TARGET)
2260                                         continue;
2261
2262                                 /*
2263                                  * If the port is not ONLINE then try to login
2264                                  * to it if we haven't run out of retries.
2265                                  */
2266                                 if (atomic_read(&fcport->state) != FCS_ONLINE &&
2267                                     fcport->login_retry) {
2268
2269                                         fcport->login_retry--;
2270                                         if (fcport->flags & FCF_FABRIC_DEVICE) {
2271                                                 if (fcport->flags &
2272                                                     FCF_TAPE_PRESENT)
2273                                                         qla2x00_fabric_logout(
2274                                                             ha,
2275                                                             fcport->loop_id);
2276                                                 status = qla2x00_fabric_login(
2277                                                     ha, fcport, &next_loopid);
2278                                         } else
2279                                                 status =
2280                                                     qla2x00_local_device_login(
2281                                                         ha, fcport->loop_id);
2282
2283                                         if (status == QLA_SUCCESS) {
2284                                                 fcport->old_loop_id = fcport->loop_id;
2285
2286                                                 DEBUG(printk("scsi(%ld): port login OK: logged in ID 0x%x\n",
2287                                                     ha->host_no, fcport->loop_id));
2288                                                 
2289                                                 fcport->port_login_retry_count =
2290                                                     ha->port_down_retry_count * PORT_RETRY_TIME;
2291                                                 atomic_set(&fcport->state, FCS_ONLINE);
2292                                                 atomic_set(&fcport->port_down_timer,
2293                                                     ha->port_down_retry_count * PORT_RETRY_TIME);
2294
2295                                                 fcport->login_retry = 0;
2296                                         } else if (status == 1) {
2297                                                 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
2298                                                 /* retry the login again */
2299                                                 DEBUG(printk("scsi(%ld): Retrying %d login again loop_id 0x%x\n",
2300                                                     ha->host_no,
2301                                                     fcport->login_retry, fcport->loop_id));
2302                                         } else {
2303                                                 fcport->login_retry = 0;
2304                                         }
2305                                 }
2306                                 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2307                                         break;
2308                         }
2309                         DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
2310                             ha->host_no));
2311                 }
2312
2313                 if ((test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags)) &&
2314                     atomic_read(&ha->loop_state) != LOOP_DOWN) {
2315
2316                         clear_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags);
2317                         DEBUG(printk("scsi(%ld): qla2x00_login_retry()\n",
2318                             ha->host_no));
2319                                 
2320                         set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2321
2322                         DEBUG(printk("scsi(%ld): qla2x00_login_retry - end\n",
2323                             ha->host_no));
2324                 }
2325
2326                 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
2327
2328                         DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
2329                             ha->host_no));
2330
2331                         if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
2332                             &ha->dpc_flags))) {
2333
2334                                 qla2x00_loop_resync(ha);
2335
2336                                 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
2337                         }
2338
2339                         DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
2340                             ha->host_no));
2341                 }
2342
2343                 if (test_and_clear_bit(FCPORT_RESCAN_NEEDED, &ha->dpc_flags)) {
2344
2345                         DEBUG(printk("scsi(%ld): Rescan flagged fcports...\n",
2346                             ha->host_no));
2347
2348                         qla2x00_rescan_fcports(ha);
2349
2350                         DEBUG(printk("scsi(%ld): Rescan flagged fcports..."
2351                             "end.\n",
2352                             ha->host_no));
2353                 }
2354
2355                 if (!ha->interrupts_on)
2356                         qla2x00_enable_intrs(ha);
2357
2358                 ha->dpc_active = 0;
2359         } /* End of while(1) */
2360
2361         DEBUG(printk("scsi(%ld): DPC handler exiting\n", ha->host_no));
2362
2363         /*
2364          * Make sure that nobody tries to wake us up again.
2365          */
2366         ha->dpc_wait = NULL;
2367         ha->dpc_active = 0;
2368
2369         complete_and_exit(&ha->dpc_exited, 0);
2370 }
2371
2372 /*
2373 *  qla2x00_rst_aen
2374 *      Processes asynchronous reset.
2375 *
2376 * Input:
2377 *      ha  = adapter block pointer.
2378 */
2379 static void
2380 qla2x00_rst_aen(scsi_qla_host_t *ha) 
2381 {
2382         if (ha->flags.online && !ha->flags.reset_active &&
2383             !atomic_read(&ha->loop_down_timer) &&
2384             !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
2385                 do {
2386                         clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
2387
2388                         /*
2389                          * Issue marker command only when we are going to start
2390                          * the I/O.
2391                          */
2392                         ha->marker_needed = 1;
2393                 } while (!atomic_read(&ha->loop_down_timer) &&
2394                     (test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags)));
2395         }
2396 }
2397
2398
2399 /*
2400  * This routine will allocate SP from the free queue
2401  * input:
2402  *        scsi_qla_host_t *
2403  * output:
2404  *        srb_t * or NULL
2405  */
2406 static srb_t *
2407 qla2x00_get_new_sp(scsi_qla_host_t *ha)
2408 {
2409         srb_t *sp;
2410
2411         sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
2412         if (sp)
2413                 atomic_set(&sp->ref_count, 1);
2414         return (sp);
2415 }
2416
2417 static void
2418 qla2x00_sp_free_dma(scsi_qla_host_t *ha, srb_t *sp)
2419 {
2420         struct scsi_cmnd *cmd = sp->cmd;
2421
2422         if (sp->flags & SRB_DMA_VALID) {
2423                 if (cmd->use_sg) {
2424                         dma_unmap_sg(&ha->pdev->dev, cmd->request_buffer,
2425                             cmd->use_sg, cmd->sc_data_direction);
2426                 } else if (cmd->request_bufflen) {
2427                         dma_unmap_single(&ha->pdev->dev, sp->dma_handle,
2428                             cmd->request_bufflen, cmd->sc_data_direction);
2429                 }
2430                 sp->flags &= ~SRB_DMA_VALID;
2431         }
2432 }
2433
2434 void
2435 qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *sp)
2436 {
2437         struct scsi_cmnd *cmd = sp->cmd;
2438
2439         qla2x00_sp_free_dma(ha, sp);
2440
2441         CMD_SP(cmd) = NULL;
2442         mempool_free(sp, ha->srb_mempool);
2443
2444         cmd->scsi_done(cmd);
2445 }
2446
2447 /**************************************************************************
2448 *   qla2x00_timer
2449 *
2450 * Description:
2451 *   One second timer
2452 *
2453 * Context: Interrupt
2454 ***************************************************************************/
2455 static void
2456 qla2x00_timer(scsi_qla_host_t *ha)
2457 {
2458         unsigned long   cpu_flags = 0;
2459         fc_port_t       *fcport;
2460         int             start_dpc = 0;
2461         int             index;
2462         srb_t           *sp;
2463         int             t;
2464
2465         /*
2466          * Ports - Port down timer.
2467          *
2468          * Whenever, a port is in the LOST state we start decrementing its port
2469          * down timer every second until it reaches zero. Once  it reaches zero
2470          * the port it marked DEAD. 
2471          */
2472         t = 0;
2473         list_for_each_entry(fcport, &ha->fcports, list) {
2474                 if (fcport->port_type != FCT_TARGET)
2475                         continue;
2476
2477                 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2478
2479                         if (atomic_read(&fcport->port_down_timer) == 0)
2480                                 continue;
2481
2482                         if (atomic_dec_and_test(&fcport->port_down_timer) != 0) 
2483                                 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
2484                         
2485                         DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
2486                             "%d remainning\n",
2487                             ha->host_no,
2488                             t, atomic_read(&fcport->port_down_timer)));
2489                 }
2490                 t++;
2491         } /* End of for fcport  */
2492
2493
2494         /* Loop down handler. */
2495         if (atomic_read(&ha->loop_down_timer) > 0 &&
2496             !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) && ha->flags.online) {
2497
2498                 if (atomic_read(&ha->loop_down_timer) ==
2499                     ha->loop_down_abort_time) {
2500
2501                         DEBUG(printk("scsi(%ld): Loop Down - aborting the "
2502                             "queues before time expire\n",
2503                             ha->host_no));
2504
2505                         if (!IS_QLA2100(ha) && ha->link_down_timeout)
2506                                 atomic_set(&ha->loop_state, LOOP_DEAD); 
2507
2508                         /* Schedule an ISP abort to return any tape commands. */
2509                         spin_lock_irqsave(&ha->hardware_lock, cpu_flags);
2510                         for (index = 1; index < MAX_OUTSTANDING_COMMANDS;
2511                             index++) {
2512                                 fc_port_t *sfcp;
2513
2514                                 sp = ha->outstanding_cmds[index];
2515                                 if (!sp)
2516                                         continue;
2517                                 sfcp = sp->fcport;
2518                                 if (!(sfcp->flags & FCF_TAPE_PRESENT))
2519                                         continue;
2520
2521                                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2522                                 break;
2523                         }
2524                         spin_unlock_irqrestore(&ha->hardware_lock, cpu_flags);
2525
2526                         set_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
2527                         start_dpc++;
2528                 }
2529
2530                 /* if the loop has been down for 4 minutes, reinit adapter */
2531                 if (atomic_dec_and_test(&ha->loop_down_timer) != 0) {
2532                         DEBUG(printk("scsi(%ld): Loop down exceed 4 mins - "
2533                             "restarting queues.\n",
2534                             ha->host_no));
2535
2536                         set_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
2537                         start_dpc++;
2538
2539                         if (!(ha->device_flags & DFLG_NO_CABLE)) {
2540                                 DEBUG(printk("scsi(%ld): Loop down - "
2541                                     "aborting ISP.\n",
2542                                     ha->host_no));
2543                                 qla_printk(KERN_WARNING, ha,
2544                                     "Loop down - aborting ISP.\n");
2545
2546                                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2547                         }
2548                 }
2549                 DEBUG3(printk("scsi(%ld): Loop Down - seconds remainning %d\n",
2550                     ha->host_no,
2551                     atomic_read(&ha->loop_down_timer)));
2552         }
2553
2554         /* Schedule the DPC routine if needed */
2555         if ((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
2556             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) ||
2557             start_dpc ||
2558             test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags) ||
2559             test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) ||
2560             test_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
2561             ha->dpc_wait && !ha->dpc_active) {
2562
2563                 up(ha->dpc_wait);
2564         }
2565
2566         qla2x00_restart_timer(ha, WATCH_INTERVAL);
2567 }
2568
2569 /* XXX(hch): crude hack to emulate a down_timeout() */
2570 int
2571 qla2x00_down_timeout(struct semaphore *sema, unsigned long timeout)
2572 {
2573         const unsigned int step = HZ/10;
2574
2575         do {
2576                 if (!down_trylock(sema))
2577                         return 0;
2578                 set_current_state(TASK_INTERRUPTIBLE);
2579                 if (schedule_timeout(step))
2580                         break;
2581         } while ((timeout -= step) > 0);
2582
2583         return -ETIMEDOUT;
2584 }
2585
2586 /**
2587  * qla2x00_module_init - Module initialization.
2588  **/
2589 static int __init
2590 qla2x00_module_init(void)
2591 {
2592         /* Allocate cache for SRBs. */
2593         sprintf(srb_cachep_name, "qla2xxx_srbs");
2594         srb_cachep = kmem_cache_create(srb_cachep_name, sizeof(srb_t), 0,
2595             SLAB_HWCACHE_ALIGN, NULL, NULL);
2596         if (srb_cachep == NULL) {
2597                 printk(KERN_ERR
2598                     "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
2599                 return -ENOMEM;
2600         }
2601
2602         /* Derive version string. */
2603         strcpy(qla2x00_version_str, QLA2XXX_VERSION);
2604 #if DEBUG_QLA2100
2605         strcat(qla2x00_version_str, "-debug");
2606 #endif
2607         qla2xxx_transport_template = qla2x00_alloc_transport_tmpl();
2608         if (!qla2xxx_transport_template)
2609                 return -ENODEV;
2610
2611         printk(KERN_INFO "QLogic Fibre Channel HBA Driver\n");
2612         return 0;
2613 }
2614
2615 /**
2616  * qla2x00_module_exit - Module cleanup.
2617  **/
2618 static void __exit
2619 qla2x00_module_exit(void)
2620 {
2621         /* Free SRBs cache. */
2622         if (srb_cachep != NULL) {
2623                 if (kmem_cache_destroy(srb_cachep) != 0) {
2624                         printk(KERN_ERR
2625                             "qla2xxx: Unable to free SRB cache...Memory pools "
2626                             "still active?\n");
2627                 }
2628                 srb_cachep = NULL;
2629         }
2630
2631         fc_release_transport(qla2xxx_transport_template);
2632 }
2633
2634 module_init(qla2x00_module_init);
2635 module_exit(qla2x00_module_exit);
2636
2637 MODULE_AUTHOR("QLogic Corporation");
2638 MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
2639 MODULE_LICENSE("GPL");
2640 MODULE_VERSION(QLA2XXX_VERSION);