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