blob: 47db029b2fce151c794d47eb199f329b9a3a8daf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Andrew Vasquezfa90c542005-10-27 11:10:08 -07002 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2005 QLogic Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andrew Vasquezfa90c542005-10-27 11:10:08 -07005 * See LICENSE.qla2xxx for copyright and licensing details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7#include "qla_def.h"
8
9#include <linux/moduleparam.h>
10#include <linux/vmalloc.h>
11#include <linux/smp_lock.h>
12#include <linux/delay.h>
13
14#include <scsi/scsi_tcq.h>
15#include <scsi/scsicam.h>
16#include <scsi/scsi_transport.h>
17#include <scsi/scsi_transport_fc.h>
18
19/*
20 * Driver version
21 */
22char qla2x00_version_str[40];
23
24/*
25 * SRB allocation cache
26 */
Andrew Vasquez 354d6b22005-04-23 02:47:27 -040027static kmem_cache_t *srb_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
30 * Ioctl related information.
31 */
Andrew Vasquez 354d6b22005-04-23 02:47:27 -040032static int num_hosts;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34int ql2xlogintimeout = 20;
35module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
36MODULE_PARM_DESC(ql2xlogintimeout,
37 "Login timeout value in seconds.");
38
8482e1182005-04-17 15:04:54 -050039int qlport_down_retry = 30;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
41MODULE_PARM_DESC(qlport_down_retry,
42 "Maximum number of command retries to a port that returns"
43 "a PORT-DOWN status.");
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045int ql2xplogiabsentdevice;
46module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
47MODULE_PARM_DESC(ql2xplogiabsentdevice,
48 "Option to enable PLOGI to devices that are not present after "
49 "a Fabric scan. This is needed for several broken switches."
50 "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052int ql2xloginretrycount = 0;
53module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
54MODULE_PARM_DESC(ql2xloginretrycount,
55 "Specify an alternate value for the NVRAM login retry count.");
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static void qla2x00_free_device(scsi_qla_host_t *);
58
59static void qla2x00_config_dma_addressing(scsi_qla_host_t *ha);
60
Andrew Vasquezcca53352005-08-26 19:08:30 -070061int ql2xfdmienable;
62module_param(ql2xfdmienable, int, S_IRUGO|S_IRUSR);
63MODULE_PARM_DESC(ql2xfdmienable,
64 "Enables FDMI registratons "
65 "Default is 0 - no FDMI. 1 - perfom FDMI.");
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -070068 * SCSI host template entry points
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 */
70static int qla2xxx_slave_configure(struct scsi_device * device);
f4f051e2005-04-17 15:02:26 -050071static int qla2xxx_slave_alloc(struct scsi_device *);
72static void qla2xxx_slave_destroy(struct scsi_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static int qla2x00_queuecommand(struct scsi_cmnd *cmd,
74 void (*fn)(struct scsi_cmnd *));
Andrew Vasquezfca29702005-07-06 10:31:47 -070075static int qla24xx_queuecommand(struct scsi_cmnd *cmd,
76 void (*fn)(struct scsi_cmnd *));
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int qla2xxx_eh_abort(struct scsi_cmnd *);
78static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
79static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
80static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
81static int qla2x00_loop_reset(scsi_qla_host_t *ha);
82static int qla2x00_device_reset(scsi_qla_host_t *, fc_port_t *);
83
Andrew Vasquezce7e4af2005-08-26 19:09:30 -070084static int qla2x00_change_queue_depth(struct scsi_device *, int);
85static int qla2x00_change_queue_type(struct scsi_device *, int);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static struct scsi_host_template qla2x00_driver_template = {
88 .module = THIS_MODULE,
89 .name = "qla2xxx",
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 .queuecommand = qla2x00_queuecommand,
91
92 .eh_abort_handler = qla2xxx_eh_abort,
93 .eh_device_reset_handler = qla2xxx_eh_device_reset,
94 .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
95 .eh_host_reset_handler = qla2xxx_eh_host_reset,
96
97 .slave_configure = qla2xxx_slave_configure,
98
f4f051e2005-04-17 15:02:26 -050099 .slave_alloc = qla2xxx_slave_alloc,
100 .slave_destroy = qla2xxx_slave_destroy,
Andrew Vasquezce7e4af2005-08-26 19:09:30 -0700101 .change_queue_depth = qla2x00_change_queue_depth,
102 .change_queue_type = qla2x00_change_queue_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 .this_id = -1,
104 .cmd_per_lun = 3,
105 .use_clustering = ENABLE_CLUSTERING,
106 .sg_tablesize = SG_ALL,
107
108 /*
109 * The RISC allows for each command to transfer (2^32-1) bytes of data,
110 * which equates to 0x800000 sectors.
111 */
112 .max_sectors = 0xFFFF,
Andrew Vasquezafb046e2005-08-26 19:09:40 -0700113 .shost_attrs = qla2x00_host_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
Andrew Vasquezfca29702005-07-06 10:31:47 -0700116static struct scsi_host_template qla24xx_driver_template = {
117 .module = THIS_MODULE,
118 .name = "qla2xxx",
119 .queuecommand = qla24xx_queuecommand,
120
121 .eh_abort_handler = qla2xxx_eh_abort,
122 .eh_device_reset_handler = qla2xxx_eh_device_reset,
123 .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
124 .eh_host_reset_handler = qla2xxx_eh_host_reset,
125
126 .slave_configure = qla2xxx_slave_configure,
127
128 .slave_alloc = qla2xxx_slave_alloc,
129 .slave_destroy = qla2xxx_slave_destroy,
Andrew Vasquezce7e4af2005-08-26 19:09:30 -0700130 .change_queue_depth = qla2x00_change_queue_depth,
131 .change_queue_type = qla2x00_change_queue_type,
Andrew Vasquezfca29702005-07-06 10:31:47 -0700132 .this_id = -1,
133 .cmd_per_lun = 3,
134 .use_clustering = ENABLE_CLUSTERING,
135 .sg_tablesize = SG_ALL,
136
137 .max_sectors = 0xFFFF,
Andrew Vasquezafb046e2005-08-26 19:09:40 -0700138 .shost_attrs = qla2x00_host_attrs,
Andrew Vasquezfca29702005-07-06 10:31:47 -0700139};
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static struct scsi_transport_template *qla2xxx_transport_template = NULL;
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/* TODO Convert to inlines
144 *
145 * Timer routines
146 */
147#define WATCH_INTERVAL 1 /* number of seconds */
148
149static void qla2x00_timer(scsi_qla_host_t *);
150
151static __inline__ void qla2x00_start_timer(scsi_qla_host_t *,
152 void *, unsigned long);
153static __inline__ void qla2x00_restart_timer(scsi_qla_host_t *, unsigned long);
154static __inline__ void qla2x00_stop_timer(scsi_qla_host_t *);
155
156static inline void
157qla2x00_start_timer(scsi_qla_host_t *ha, void *func, unsigned long interval)
158{
159 init_timer(&ha->timer);
160 ha->timer.expires = jiffies + interval * HZ;
161 ha->timer.data = (unsigned long)ha;
162 ha->timer.function = (void (*)(unsigned long))func;
163 add_timer(&ha->timer);
164 ha->timer_active = 1;
165}
166
167static inline void
168qla2x00_restart_timer(scsi_qla_host_t *ha, unsigned long interval)
169{
170 mod_timer(&ha->timer, jiffies + interval * HZ);
171}
172
173static __inline__ void
174qla2x00_stop_timer(scsi_qla_host_t *ha)
175{
176 del_timer_sync(&ha->timer);
177 ha->timer_active = 0;
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static int qla2x00_do_dpc(void *data);
181
182static void qla2x00_rst_aen(scsi_qla_host_t *);
183
184static uint8_t qla2x00_mem_alloc(scsi_qla_host_t *);
185static void qla2x00_mem_free(scsi_qla_host_t *ha);
186static int qla2x00_allocate_sp_pool( scsi_qla_host_t *ha);
187static void qla2x00_free_sp_pool(scsi_qla_host_t *ha);
f4f051e2005-04-17 15:02:26 -0500188static void qla2x00_sp_free_dma(scsi_qla_host_t *, srb_t *);
189void qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/* -------------------------------------------------------------------------- */
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static char *
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700194qla2x00_pci_info_str(struct scsi_qla_host *ha, char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 static char *pci_bus_modes[] = {
197 "33", "66", "100", "133",
198 };
199 uint16_t pci_bus;
200
201 strcpy(str, "PCI");
202 pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
203 if (pci_bus) {
204 strcat(str, "-X (");
205 strcat(str, pci_bus_modes[pci_bus]);
206 } else {
207 pci_bus = (ha->pci_attr & BIT_8) >> 8;
208 strcat(str, " (");
209 strcat(str, pci_bus_modes[pci_bus]);
210 }
211 strcat(str, " MHz)");
212
213 return (str);
214}
215
Andrew Vasquezfca29702005-07-06 10:31:47 -0700216static char *
217qla24xx_pci_info_str(struct scsi_qla_host *ha, char *str)
218{
219 static char *pci_bus_modes[] = { "33", "66", "100", "133", };
220 uint32_t pci_bus;
221 int pcie_reg;
222
223 pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
224 if (pcie_reg) {
225 char lwstr[6];
226 uint16_t pcie_lstat, lspeed, lwidth;
227
228 pcie_reg += 0x12;
229 pci_read_config_word(ha->pdev, pcie_reg, &pcie_lstat);
230 lspeed = pcie_lstat & (BIT_0 | BIT_1 | BIT_2 | BIT_3);
231 lwidth = (pcie_lstat &
232 (BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9)) >> 4;
233
234 strcpy(str, "PCIe (");
235 if (lspeed == 1)
236 strcat(str, "2.5Gb/s ");
237 else
238 strcat(str, "<unknown> ");
239 snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
240 strcat(str, lwstr);
241
242 return str;
243 }
244
245 strcpy(str, "PCI");
246 pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
247 if (pci_bus == 0 || pci_bus == 8) {
248 strcat(str, " (");
249 strcat(str, pci_bus_modes[pci_bus >> 3]);
250 } else {
251 strcat(str, "-X ");
252 if (pci_bus & BIT_2)
253 strcat(str, "Mode 2");
254 else
255 strcat(str, "Mode 1");
256 strcat(str, " (");
257 strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
258 }
259 strcat(str, " MHz)");
260
261 return str;
262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264char *
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700265qla2x00_fw_version_str(struct scsi_qla_host *ha, char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 char un_str[10];
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
270 ha->fw_minor_version,
271 ha->fw_subminor_version);
272
273 if (ha->fw_attributes & BIT_9) {
274 strcat(str, "FLX");
275 return (str);
276 }
277
278 switch (ha->fw_attributes & 0xFF) {
279 case 0x7:
280 strcat(str, "EF");
281 break;
282 case 0x17:
283 strcat(str, "TP");
284 break;
285 case 0x37:
286 strcat(str, "IP");
287 break;
288 case 0x77:
289 strcat(str, "VI");
290 break;
291 default:
292 sprintf(un_str, "(%x)", ha->fw_attributes);
293 strcat(str, un_str);
294 break;
295 }
296 if (ha->fw_attributes & 0x100)
297 strcat(str, "X");
298
299 return (str);
300}
301
Andrew Vasquezfca29702005-07-06 10:31:47 -0700302char *
303qla24xx_fw_version_str(struct scsi_qla_host *ha, char *str)
304{
Andrew Vasquezf0883ac2005-07-08 17:58:43 -0700305 sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
306 ha->fw_minor_version,
307 ha->fw_subminor_version);
308
Andrew Vasquezfca29702005-07-06 10:31:47 -0700309 if (ha->fw_attributes & BIT_0)
310 strcat(str, "[Class 2] ");
311 if (ha->fw_attributes & BIT_1)
312 strcat(str, "[IP] ");
313 if (ha->fw_attributes & BIT_2)
314 strcat(str, "[Multi-ID] ");
315 if (ha->fw_attributes & BIT_13)
316 strcat(str, "[Experimental]");
317 return str;
Andrew Vasquezfca29702005-07-06 10:31:47 -0700318}
319
320static inline srb_t *
321qla2x00_get_new_sp(scsi_qla_host_t *ha, fc_port_t *fcport,
322 struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
323{
324 srb_t *sp;
325
326 sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
327 if (!sp)
328 return sp;
329
330 atomic_set(&sp->ref_count, 1);
331 sp->ha = ha;
332 sp->fcport = fcport;
333 sp->cmd = cmd;
334 sp->flags = 0;
335 CMD_SP(cmd) = (void *)sp;
336 cmd->scsi_done = done;
337
338 return sp;
339}
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341static int
f4f051e2005-04-17 15:02:26 -0500342qla2x00_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
f4f051e2005-04-17 15:02:26 -0500344 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
bdf79622005-04-17 15:06:53 -0500345 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400346 struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
f4f051e2005-04-17 15:02:26 -0500347 srb_t *sp;
348 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400350 rval = fc_remote_port_chkready(rport);
351 if (rval) {
352 cmd->result = rval;
f4f051e2005-04-17 15:02:26 -0500353 goto qc_fail_command;
354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
f4f051e2005-04-17 15:02:26 -0500356 if (atomic_read(&fcport->state) != FCS_ONLINE) {
357 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
358 atomic_read(&ha->loop_state) == LOOP_DEAD) {
359 cmd->result = DID_NO_CONNECT << 16;
360 goto qc_fail_command;
361 }
362 goto qc_host_busy;
363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 spin_unlock_irq(ha->host->host_lock);
366
Andrew Vasquezfca29702005-07-06 10:31:47 -0700367 sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
368 if (!sp)
f4f051e2005-04-17 15:02:26 -0500369 goto qc_host_busy_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
f4f051e2005-04-17 15:02:26 -0500371 rval = qla2x00_start_scsi(sp);
372 if (rval != QLA_SUCCESS)
373 goto qc_host_busy_free_sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 spin_lock_irq(ha->host->host_lock);
376
f4f051e2005-04-17 15:02:26 -0500377 return 0;
378
379qc_host_busy_free_sp:
380 qla2x00_sp_free_dma(ha, sp);
f4f051e2005-04-17 15:02:26 -0500381 mempool_free(sp, ha->srb_mempool);
382
383qc_host_busy_lock:
384 spin_lock_irq(ha->host->host_lock);
385
386qc_host_busy:
387 return SCSI_MLQUEUE_HOST_BUSY;
388
389qc_fail_command:
390 done(cmd);
391
392 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Andrew Vasquezfca29702005-07-06 10:31:47 -0700395
396static int
397qla24xx_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
398{
399 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
400 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400401 struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
Andrew Vasquezfca29702005-07-06 10:31:47 -0700402 srb_t *sp;
403 int rval;
404
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400405 rval = fc_remote_port_chkready(rport);
406 if (rval) {
407 cmd->result = rval;
Andrew Vasquezfca29702005-07-06 10:31:47 -0700408 goto qc24_fail_command;
409 }
410
411 if (atomic_read(&fcport->state) != FCS_ONLINE) {
412 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
413 atomic_read(&ha->loop_state) == LOOP_DEAD) {
414 cmd->result = DID_NO_CONNECT << 16;
415 goto qc24_fail_command;
416 }
417 goto qc24_host_busy;
418 }
419
420 spin_unlock_irq(ha->host->host_lock);
421
422 sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
423 if (!sp)
424 goto qc24_host_busy_lock;
425
426 rval = qla24xx_start_scsi(sp);
427 if (rval != QLA_SUCCESS)
428 goto qc24_host_busy_free_sp;
429
430 spin_lock_irq(ha->host->host_lock);
431
432 return 0;
433
434qc24_host_busy_free_sp:
435 qla2x00_sp_free_dma(ha, sp);
436 mempool_free(sp, ha->srb_mempool);
437
438qc24_host_busy_lock:
439 spin_lock_irq(ha->host->host_lock);
440
441qc24_host_busy:
442 return SCSI_MLQUEUE_HOST_BUSY;
443
444qc24_fail_command:
445 done(cmd);
446
447 return 0;
448}
449
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/*
452 * qla2x00_eh_wait_on_command
453 * Waits for the command to be returned by the Firmware for some
454 * max time.
455 *
456 * Input:
457 * ha = actual ha whose done queue will contain the command
458 * returned by firmware.
459 * cmd = Scsi Command to wait on.
460 * flag = Abort/Reset(Bus or Device Reset)
461 *
462 * Return:
463 * Not Found : 0
464 * Found : 1
465 */
466static int
467qla2x00_eh_wait_on_command(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
468{
Andrew Vasquezfe74c712005-08-26 19:10:10 -0700469#define ABORT_POLLING_PERIOD 1000
470#define ABORT_WAIT_ITER ((10 * 1000) / (ABORT_POLLING_PERIOD))
f4f051e2005-04-17 15:02:26 -0500471 unsigned long wait_iter = ABORT_WAIT_ITER;
472 int ret = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
f4f051e2005-04-17 15:02:26 -0500474 while (CMD_SP(cmd)) {
Andrew Vasquezfe74c712005-08-26 19:10:10 -0700475 msleep(ABORT_POLLING_PERIOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
f4f051e2005-04-17 15:02:26 -0500477 if (--wait_iter)
478 break;
479 }
480 if (CMD_SP(cmd))
481 ret = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
f4f051e2005-04-17 15:02:26 -0500483 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486/*
487 * qla2x00_wait_for_hba_online
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700488 * Wait till the HBA is online after going through
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 * <= MAX_RETRIES_OF_ISP_ABORT or
490 * finally HBA is disabled ie marked offline
491 *
492 * Input:
493 * ha - pointer to host adapter structure
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700494 *
495 * Note:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * Does context switching-Release SPIN_LOCK
497 * (if any) before calling this routine.
498 *
499 * Return:
500 * Success (Adapter is online) : 0
501 * Failed (Adapter is offline/disabled) : 1
502 */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700503static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504qla2x00_wait_for_hba_online(scsi_qla_host_t *ha)
505{
Andrew Vasquezfca29702005-07-06 10:31:47 -0700506 int return_status;
507 unsigned long wait_online;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700509 wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 while (((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) ||
511 test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
512 test_bit(ISP_ABORT_RETRY, &ha->dpc_flags) ||
513 ha->dpc_active) && time_before(jiffies, wait_online)) {
514
515 msleep(1000);
516 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700517 if (ha->flags.online)
518 return_status = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 else
520 return_status = QLA_FUNCTION_FAILED;
521
522 DEBUG2(printk("%s return_status=%d\n",__func__,return_status));
523
524 return (return_status);
525}
526
527/*
528 * qla2x00_wait_for_loop_ready
529 * Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700530 * to be in LOOP_READY state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * Input:
532 * ha - pointer to host adapter structure
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700533 *
534 * Note:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * Does context switching-Release SPIN_LOCK
536 * (if any) before calling this routine.
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700537 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 *
539 * Return:
540 * Success (LOOP_READY) : 0
541 * Failed (LOOP_NOT_READY) : 1
542 */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700543static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544qla2x00_wait_for_loop_ready(scsi_qla_host_t *ha)
545{
546 int return_status = QLA_SUCCESS;
547 unsigned long loop_timeout ;
548
549 /* wait for 5 min at the max for loop to be ready */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700550 loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 while ((!atomic_read(&ha->loop_down_timer) &&
553 atomic_read(&ha->loop_state) == LOOP_DOWN) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 atomic_read(&ha->loop_state) != LOOP_READY) {
555 msleep(1000);
556 if (time_after_eq(jiffies, loop_timeout)) {
557 return_status = QLA_FUNCTION_FAILED;
558 break;
559 }
560 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700561 return (return_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564/**************************************************************************
565* qla2xxx_eh_abort
566*
567* Description:
568* The abort function will abort the specified command.
569*
570* Input:
571* cmd = Linux SCSI command packet to be aborted.
572*
573* Returns:
574* Either SUCCESS or FAILED.
575*
576* Note:
577**************************************************************************/
578int
579qla2xxx_eh_abort(struct scsi_cmnd *cmd)
580{
f4f051e2005-04-17 15:02:26 -0500581 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
582 srb_t *sp;
583 int ret, i;
584 unsigned int id, lun;
585 unsigned long serial;
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700586 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
f4f051e2005-04-17 15:02:26 -0500588 if (!CMD_SP(cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
f4f051e2005-04-17 15:02:26 -0500591 ret = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
f4f051e2005-04-17 15:02:26 -0500593 id = cmd->device->id;
594 lun = cmd->device->lun;
595 serial = cmd->serial_number;
596
597 /* Check active list for command command. */
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700598 spin_lock_irqsave(&ha->hardware_lock, flags);
f4f051e2005-04-17 15:02:26 -0500599 for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
600 sp = ha->outstanding_cmds[i];
601
602 if (sp == NULL)
603 continue;
604
605 if (sp->cmd != cmd)
606 continue;
607
608 DEBUG2(printk("%s(%ld): aborting sp %p from RISC. pid=%ld "
609 "sp->state=%x\n", __func__, ha->host_no, sp, serial,
610 sp->state));
611 DEBUG3(qla2x00_print_scsi_cmd(cmd);)
612
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700613 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700614 if (ha->isp_ops.abort_command(ha, sp)) {
f4f051e2005-04-17 15:02:26 -0500615 DEBUG2(printk("%s(%ld): abort_command "
616 "mbx failed.\n", __func__, ha->host_no));
617 } else {
618 DEBUG3(printk("%s(%ld): abort_command "
619 "mbx success.\n", __func__, ha->host_no));
620 ret = SUCCESS;
621 }
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700622 spin_lock_irqsave(&ha->hardware_lock, flags);
f4f051e2005-04-17 15:02:26 -0500623
624 break;
625 }
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700626 spin_unlock_irqrestore(&ha->hardware_lock, flags);
f4f051e2005-04-17 15:02:26 -0500627
628 /* Wait for the command to be returned. */
629 if (ret == SUCCESS) {
f4f051e2005-04-17 15:02:26 -0500630 if (qla2x00_eh_wait_on_command(ha, cmd) != QLA_SUCCESS) {
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700631 qla_printk(KERN_ERR, ha,
f4f051e2005-04-17 15:02:26 -0500632 "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
633 "%x.\n", ha->host_no, id, lun, serial, ret);
634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700637 qla_printk(KERN_INFO, ha,
f4f051e2005-04-17 15:02:26 -0500638 "scsi(%ld:%d:%d): Abort command issued -- %lx %x.\n", ha->host_no,
639 id, lun, serial, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
f4f051e2005-04-17 15:02:26 -0500641 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644/**************************************************************************
645* qla2x00_eh_wait_for_pending_target_commands
646*
647* Description:
648* Waits for all the commands to come back from the specified target.
649*
650* Input:
651* ha - pointer to scsi_qla_host structure.
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700652* t - target
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653* Returns:
654* Either SUCCESS or FAILED.
655*
656* Note:
657**************************************************************************/
658static int
659qla2x00_eh_wait_for_pending_target_commands(scsi_qla_host_t *ha, unsigned int t)
660{
661 int cnt;
662 int status;
663 srb_t *sp;
664 struct scsi_cmnd *cmd;
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700665 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 status = 0;
668
669 /*
670 * Waiting for all commands for the designated target in the active
671 * array
672 */
673 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700674 spin_lock_irqsave(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 sp = ha->outstanding_cmds[cnt];
676 if (sp) {
677 cmd = sp->cmd;
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700678 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (cmd->device->id == t) {
680 if (!qla2x00_eh_wait_on_command(ha, cmd)) {
681 status = 1;
682 break;
683 }
684 }
f4f051e2005-04-17 15:02:26 -0500685 } else {
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700686 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688 }
689 return (status);
690}
691
692
693/**************************************************************************
694* qla2xxx_eh_device_reset
695*
696* Description:
697* The device reset function will reset the target and abort any
698* executing commands.
699*
700* NOTE: The use of SP is undefined within this context. Do *NOT*
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700701* attempt to use this value, even if you determine it is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702* non-null.
703*
704* Input:
705* cmd = Linux SCSI command packet of the command that cause the
706* bus device reset.
707*
708* Returns:
709* SUCCESS/FAILURE (defined as macro in scsi.h).
710*
711**************************************************************************/
712int
713qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
714{
f4f051e2005-04-17 15:02:26 -0500715 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
bdf79622005-04-17 15:06:53 -0500716 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
f4f051e2005-04-17 15:02:26 -0500717 srb_t *sp;
718 int ret;
719 unsigned int id, lun;
720 unsigned long serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
f4f051e2005-04-17 15:02:26 -0500722 ret = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
f4f051e2005-04-17 15:02:26 -0500724 id = cmd->device->id;
725 lun = cmd->device->lun;
726 serial = cmd->serial_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
f4f051e2005-04-17 15:02:26 -0500728 sp = (srb_t *) CMD_SP(cmd);
bdf79622005-04-17 15:06:53 -0500729 if (!sp || !fcport)
f4f051e2005-04-17 15:02:26 -0500730 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 qla_printk(KERN_INFO, ha,
f4f051e2005-04-17 15:02:26 -0500733 "scsi(%ld:%d:%d): DEVICE RESET ISSUED.\n", ha->host_no, id, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -0400735 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 goto eh_dev_reset_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
f4f051e2005-04-17 15:02:26 -0500739 if (qla2x00_device_reset(ha, fcport) == 0)
740 ret = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742#if defined(LOGOUT_AFTER_DEVICE_RESET)
f4f051e2005-04-17 15:02:26 -0500743 if (ret == SUCCESS) {
744 if (fcport->flags & FC_FABRIC_DEVICE) {
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700745 ha->isp_ops.fabric_logout(ha, fcport->loop_id);
f4f051e2005-04-17 15:02:26 -0500746 qla2x00_mark_device_lost(ha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748 }
749#endif
750 } else {
751 DEBUG2(printk(KERN_INFO
752 "%s failed: loop not ready\n",__func__);)
753 }
754
f4f051e2005-04-17 15:02:26 -0500755 if (ret == FAILED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 DEBUG3(printk("%s(%ld): device reset failed\n",
757 __func__, ha->host_no));
758 qla_printk(KERN_INFO, ha, "%s: device reset failed\n",
759 __func__);
760
761 goto eh_dev_reset_done;
762 }
763
Andrew Vasquez9a41a622005-09-20 13:25:53 -0700764 /* Flush outstanding commands. */
765 if (qla2x00_eh_wait_for_pending_target_commands(ha, id))
766 ret = FAILED;
767 if (ret == FAILED) {
768 DEBUG3(printk("%s(%ld): failed while waiting for commands\n",
769 __func__, ha->host_no));
770 qla_printk(KERN_INFO, ha,
771 "%s: failed while waiting for commands\n", __func__);
772 } else
773 qla_printk(KERN_INFO, ha,
774 "scsi(%ld:%d:%d): DEVICE RESET SUCCEEDED.\n", ha->host_no,
775 id, lun);
James Bottomley28f22b02005-10-28 17:22:18 -0500776 eh_dev_reset_done:
f4f051e2005-04-17 15:02:26 -0500777 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
780/**************************************************************************
781* qla2x00_eh_wait_for_pending_commands
782*
783* Description:
784* Waits for all the commands to come back from the specified host.
785*
786* Input:
787* ha - pointer to scsi_qla_host structure.
788*
789* Returns:
790* 1 : SUCCESS
791* 0 : FAILED
792*
793* Note:
794**************************************************************************/
795static int
796qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *ha)
797{
798 int cnt;
799 int status;
800 srb_t *sp;
801 struct scsi_cmnd *cmd;
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700802 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 status = 1;
805
806 /*
807 * Waiting for all commands for the designated target in the active
808 * array
809 */
810 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700811 spin_lock_irqsave(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 sp = ha->outstanding_cmds[cnt];
813 if (sp) {
814 cmd = sp->cmd;
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700815 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 status = qla2x00_eh_wait_on_command(ha, cmd);
817 if (status == 0)
818 break;
819 }
820 else {
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700821 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823 }
824 return (status);
825}
826
827
828/**************************************************************************
829* qla2xxx_eh_bus_reset
830*
831* Description:
832* The bus reset function will reset the bus and abort any executing
833* commands.
834*
835* Input:
836* cmd = Linux SCSI command packet of the command that cause the
837* bus reset.
838*
839* Returns:
840* SUCCESS/FAILURE (defined as macro in scsi.h).
841*
842**************************************************************************/
843int
844qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
845{
f4f051e2005-04-17 15:02:26 -0500846 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
bdf79622005-04-17 15:06:53 -0500847 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 srb_t *sp;
f4f051e2005-04-17 15:02:26 -0500849 int ret;
850 unsigned int id, lun;
851 unsigned long serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
f4f051e2005-04-17 15:02:26 -0500853 ret = FAILED;
854
855 id = cmd->device->id;
856 lun = cmd->device->lun;
857 serial = cmd->serial_number;
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 sp = (srb_t *) CMD_SP(cmd);
bdf79622005-04-17 15:06:53 -0500860 if (!sp || !fcport)
f4f051e2005-04-17 15:02:26 -0500861 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 qla_printk(KERN_INFO, ha,
f4f051e2005-04-17 15:02:26 -0500864 "scsi(%ld:%d:%d): LOOP RESET ISSUED.\n", ha->host_no, id, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
867 DEBUG2(printk("%s failed:board disabled\n",__func__));
f4f051e2005-04-17 15:02:26 -0500868 goto eh_bus_reset_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870
871 if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
f4f051e2005-04-17 15:02:26 -0500872 if (qla2x00_loop_reset(ha) == QLA_SUCCESS)
873 ret = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
f4f051e2005-04-17 15:02:26 -0500875 if (ret == FAILED)
876 goto eh_bus_reset_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Andrew Vasquez9a41a622005-09-20 13:25:53 -0700878 /* Flush outstanding commands. */
879 if (!qla2x00_eh_wait_for_pending_commands(ha))
880 ret = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
f4f051e2005-04-17 15:02:26 -0500882eh_bus_reset_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
f4f051e2005-04-17 15:02:26 -0500884 (ret == FAILED) ? "failed" : "succeded");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
f4f051e2005-04-17 15:02:26 -0500886 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889/**************************************************************************
890* qla2xxx_eh_host_reset
891*
892* Description:
893* The reset function will reset the Adapter.
894*
895* Input:
896* cmd = Linux SCSI command packet of the command that cause the
897* adapter reset.
898*
899* Returns:
900* Either SUCCESS or FAILED.
901*
902* Note:
903**************************************************************************/
904int
905qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
906{
f4f051e2005-04-17 15:02:26 -0500907 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
bdf79622005-04-17 15:06:53 -0500908 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
f4f051e2005-04-17 15:02:26 -0500909 srb_t *sp;
910 int ret;
911 unsigned int id, lun;
912 unsigned long serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
f4f051e2005-04-17 15:02:26 -0500914 ret = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
f4f051e2005-04-17 15:02:26 -0500916 id = cmd->device->id;
917 lun = cmd->device->lun;
918 serial = cmd->serial_number;
919
920 sp = (srb_t *) CMD_SP(cmd);
bdf79622005-04-17 15:06:53 -0500921 if (!sp || !fcport)
f4f051e2005-04-17 15:02:26 -0500922 return ret;
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 qla_printk(KERN_INFO, ha,
f4f051e2005-04-17 15:02:26 -0500925 "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no, id, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
f4f051e2005-04-17 15:02:26 -0500928 goto eh_host_reset_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 /*
931 * Fixme-may be dpc thread is active and processing
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700932 * loop_resync,so wait a while for it to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 * be completed and then issue big hammer.Otherwise
934 * it may cause I/O failure as big hammer marks the
935 * devices as lost kicking of the port_down_timer
936 * while dpc is stuck for the mailbox to complete.
937 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 qla2x00_wait_for_loop_ready(ha);
939 set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
940 if (qla2x00_abort_isp(ha)) {
941 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
942 /* failed. schedule dpc to try */
943 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
944
945 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
f4f051e2005-04-17 15:02:26 -0500946 goto eh_host_reset_lock;
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 /* Waiting for our command in done_queue to be returned to OS.*/
f4f051e2005-04-17 15:02:26 -0500951 if (qla2x00_eh_wait_for_pending_commands(ha))
952 ret = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
f4f051e2005-04-17 15:02:26 -0500954eh_host_reset_lock:
f4f051e2005-04-17 15:02:26 -0500955 qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
956 (ret == FAILED) ? "failed" : "succeded");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
f4f051e2005-04-17 15:02:26 -0500958 return ret;
959}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961/*
962* qla2x00_loop_reset
963* Issue loop reset.
964*
965* Input:
966* ha = adapter block pointer.
967*
968* Returns:
969* 0 = success
970*/
971static int
972qla2x00_loop_reset(scsi_qla_host_t *ha)
973{
974 int status = QLA_SUCCESS;
bdf79622005-04-17 15:06:53 -0500975 struct fc_port *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 if (ha->flags.enable_lip_reset) {
978 status = qla2x00_lip_reset(ha);
979 }
980
981 if (status == QLA_SUCCESS && ha->flags.enable_target_reset) {
bdf79622005-04-17 15:06:53 -0500982 list_for_each_entry(fcport, &ha->fcports, list) {
983 if (fcport->port_type != FCT_TARGET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 continue;
985
Andrew Vasquezc00c72a2005-08-26 19:08:50 -0700986 status = qla2x00_device_reset(ha, fcport);
bdf79622005-04-17 15:06:53 -0500987 if (status != QLA_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990 }
991
992 if (status == QLA_SUCCESS &&
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700993 ((!ha->flags.enable_target_reset &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 !ha->flags.enable_lip_reset) ||
995 ha->flags.enable_lip_full_login)) {
996
997 status = qla2x00_full_login_lip(ha);
998 }
999
1000 /* Issue marker command only when we are going to start the I/O */
1001 ha->marker_needed = 1;
1002
1003 if (status) {
1004 /* Empty */
1005 DEBUG2_3(printk("%s(%ld): **** FAILED ****\n",
1006 __func__,
1007 ha->host_no);)
1008 } else {
1009 /* Empty */
1010 DEBUG3(printk("%s(%ld): exiting normally.\n",
1011 __func__,
1012 ha->host_no);)
1013 }
1014
1015 return(status);
1016}
1017
1018/*
1019 * qla2x00_device_reset
1020 * Issue bus device reset message to the target.
1021 *
1022 * Input:
1023 * ha = adapter block pointer.
1024 * t = SCSI ID.
1025 * TARGET_QUEUE_LOCK must be released.
1026 * ADAPTER_STATE_LOCK must be released.
1027 *
1028 * Context:
1029 * Kernel context.
1030 */
1031static int
1032qla2x00_device_reset(scsi_qla_host_t *ha, fc_port_t *reset_fcport)
1033{
1034 /* Abort Target command will clear Reservation */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001035 return ha->isp_ops.abort_target(reset_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
f4f051e2005-04-17 15:02:26 -05001038static int
1039qla2xxx_slave_alloc(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
bdf79622005-04-17 15:06:53 -05001041 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001043 if (!rport || fc_remote_port_chkready(rport))
f4f051e2005-04-17 15:02:26 -05001044 return -ENXIO;
1045
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001046 sdev->hostdata = *(fc_port_t **)rport->dd_data;
f4f051e2005-04-17 15:02:26 -05001047
1048 return 0;
1049}
1050
1051static int
1052qla2xxx_slave_configure(struct scsi_device *sdev)
1053{
8482e1182005-04-17 15:04:54 -05001054 scsi_qla_host_t *ha = to_qla_host(sdev->host);
1055 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1056
f4f051e2005-04-17 15:02:26 -05001057 if (sdev->tagged_supported)
1058 scsi_activate_tcq(sdev, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 else
f4f051e2005-04-17 15:02:26 -05001060 scsi_deactivate_tcq(sdev, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
8482e1182005-04-17 15:04:54 -05001062 rport->dev_loss_tmo = ha->port_down_retry_count + 5;
1063
f4f051e2005-04-17 15:02:26 -05001064 return 0;
1065}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
f4f051e2005-04-17 15:02:26 -05001067static void
1068qla2xxx_slave_destroy(struct scsi_device *sdev)
1069{
1070 sdev->hostdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Andrew Vasquezce7e4af2005-08-26 19:09:30 -07001073static int
1074qla2x00_change_queue_depth(struct scsi_device *sdev, int qdepth)
1075{
1076 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1077 return sdev->queue_depth;
1078}
1079
1080static int
1081qla2x00_change_queue_type(struct scsi_device *sdev, int tag_type)
1082{
1083 if (sdev->tagged_supported) {
1084 scsi_set_tag_type(sdev, tag_type);
1085 if (tag_type)
1086 scsi_activate_tcq(sdev, sdev->queue_depth);
1087 else
1088 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1089 } else
1090 tag_type = 0;
1091
1092 return tag_type;
1093}
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095/**
1096 * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
1097 * @ha: HA context
1098 *
1099 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1100 * supported addressing method.
1101 */
1102static void
1103qla2x00_config_dma_addressing(scsi_qla_host_t *ha)
1104{
Andrew Vasquez7524f9b2005-08-26 19:08:00 -07001105 /* Assume a 32bit DMA mask. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 ha->flags.enable_64bit_addressing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Andrew Vasquez7524f9b2005-08-26 19:08:00 -07001108 if (!dma_set_mask(&ha->pdev->dev, DMA_64BIT_MASK)) {
1109 /* Any upper-dword bits set? */
1110 if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
1111 !pci_set_consistent_dma_mask(ha->pdev, DMA_64BIT_MASK)) {
1112 /* Ok, a 64bit DMA mask is applicable. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 ha->flags.enable_64bit_addressing = 1;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001114 ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_64;
1115 ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_64;
Andrew Vasquez7524f9b2005-08-26 19:08:00 -07001116 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
Andrew Vasquez7524f9b2005-08-26 19:08:00 -07001119
1120 dma_set_mask(&ha->pdev->dev, DMA_32BIT_MASK);
1121 pci_set_consistent_dma_mask(ha->pdev, DMA_32BIT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
1124static int
1125qla2x00_iospace_config(scsi_qla_host_t *ha)
1126{
1127 unsigned long pio, pio_len, pio_flags;
1128 unsigned long mmio, mmio_len, mmio_flags;
1129
1130 /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1131 pio = pci_resource_start(ha->pdev, 0);
1132 pio_len = pci_resource_len(ha->pdev, 0);
1133 pio_flags = pci_resource_flags(ha->pdev, 0);
1134 if (pio_flags & IORESOURCE_IO) {
1135 if (pio_len < MIN_IOBASE_LEN) {
1136 qla_printk(KERN_WARNING, ha,
1137 "Invalid PCI I/O region size (%s)...\n",
1138 pci_name(ha->pdev));
1139 pio = 0;
1140 }
1141 } else {
1142 qla_printk(KERN_WARNING, ha,
1143 "region #0 not a PIO resource (%s)...\n",
1144 pci_name(ha->pdev));
1145 pio = 0;
1146 }
1147
1148 /* Use MMIO operations for all accesses. */
1149 mmio = pci_resource_start(ha->pdev, 1);
1150 mmio_len = pci_resource_len(ha->pdev, 1);
1151 mmio_flags = pci_resource_flags(ha->pdev, 1);
1152
1153 if (!(mmio_flags & IORESOURCE_MEM)) {
1154 qla_printk(KERN_ERR, ha,
1155 "region #0 not an MMIO resource (%s), aborting\n",
1156 pci_name(ha->pdev));
1157 goto iospace_error_exit;
1158 }
1159 if (mmio_len < MIN_IOBASE_LEN) {
1160 qla_printk(KERN_ERR, ha,
1161 "Invalid PCI mem region size (%s), aborting\n",
1162 pci_name(ha->pdev));
1163 goto iospace_error_exit;
1164 }
1165
1166 if (pci_request_regions(ha->pdev, ha->brd_info->drv_name)) {
1167 qla_printk(KERN_WARNING, ha,
1168 "Failed to reserve PIO/MMIO regions (%s)\n",
1169 pci_name(ha->pdev));
1170
1171 goto iospace_error_exit;
1172 }
1173
1174 ha->pio_address = pio;
1175 ha->pio_length = pio_len;
1176 ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
1177 if (!ha->iobase) {
1178 qla_printk(KERN_ERR, ha,
1179 "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
1180
1181 goto iospace_error_exit;
1182 }
1183
1184 return (0);
1185
1186iospace_error_exit:
1187 return (-ENOMEM);
1188}
1189
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001190static void
1191qla2x00_enable_intrs(scsi_qla_host_t *ha)
1192{
1193 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -07001194 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001195
1196 spin_lock_irqsave(&ha->hardware_lock, flags);
1197 ha->interrupts_on = 1;
1198 /* enable risc and host interrupts */
1199 WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
1200 RD_REG_WORD(&reg->ictrl);
1201 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1202
1203}
1204
1205static void
1206qla2x00_disable_intrs(scsi_qla_host_t *ha)
1207{
1208 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -07001209 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001210
1211 spin_lock_irqsave(&ha->hardware_lock, flags);
1212 ha->interrupts_on = 0;
1213 /* disable risc and host interrupts */
1214 WRT_REG_WORD(&reg->ictrl, 0);
1215 RD_REG_WORD(&reg->ictrl);
1216 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1217}
1218
Andrew Vasquezfca29702005-07-06 10:31:47 -07001219static void
1220qla24xx_enable_intrs(scsi_qla_host_t *ha)
1221{
1222 unsigned long flags = 0;
1223 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1224
1225 spin_lock_irqsave(&ha->hardware_lock, flags);
1226 ha->interrupts_on = 1;
1227 WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
1228 RD_REG_DWORD(&reg->ictrl);
1229 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1230}
1231
1232static void
1233qla24xx_disable_intrs(scsi_qla_host_t *ha)
1234{
1235 unsigned long flags = 0;
1236 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1237
1238 spin_lock_irqsave(&ha->hardware_lock, flags);
1239 ha->interrupts_on = 0;
1240 WRT_REG_DWORD(&reg->ictrl, 0);
1241 RD_REG_DWORD(&reg->ictrl);
1242 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1243}
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245/*
1246 * PCI driver interface
1247 */
1248int qla2x00_probe_one(struct pci_dev *pdev, struct qla_board_info *brd_info)
1249{
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001250 int ret = -ENODEV;
Andrew Vasquezfca29702005-07-06 10:31:47 -07001251 device_reg_t __iomem *reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 struct Scsi_Host *host;
1253 scsi_qla_host_t *ha;
1254 unsigned long flags = 0;
1255 unsigned long wait_switch = 0;
1256 char pci_info[20];
1257 char fw_str[30];
8482e1182005-04-17 15:04:54 -05001258 fc_port_t *fcport;
Andrew Vasquez54333832005-11-09 15:49:04 -08001259 struct scsi_host_template *sht;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 if (pci_enable_device(pdev))
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001262 goto probe_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Andrew Vasquez54333832005-11-09 15:49:04 -08001264 sht = &qla2x00_driver_template;
1265 if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
1266 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432)
1267 sht = &qla24xx_driver_template;
1268 host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (host == NULL) {
1270 printk(KERN_WARNING
1271 "qla2xxx: Couldn't allocate host from scsi layer!\n");
1272 goto probe_disable_device;
1273 }
1274
1275 /* Clear our data area */
1276 ha = (scsi_qla_host_t *)host->hostdata;
1277 memset(ha, 0, sizeof(scsi_qla_host_t));
1278
1279 ha->pdev = pdev;
1280 ha->host = host;
1281 ha->host_no = host->host_no;
1282 ha->brd_info = brd_info;
1283 sprintf(ha->host_str, "%s_%ld", ha->brd_info->drv_name, ha->host_no);
1284
Andrew Morton444d1d92005-10-25 11:00:56 -07001285 ha->dpc_pid = -1;
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 /* Configure PCI I/O space */
1288 ret = qla2x00_iospace_config(ha);
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001289 if (ret)
1290 goto probe_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 qla_printk(KERN_INFO, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08001293 "Found an ISP%04X, irq %d, iobase 0x%p\n", pdev->device, pdev->irq,
1294 ha->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 spin_lock_init(&ha->hardware_lock);
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 ha->prev_topology = 0;
1299 ha->ports = MAX_BUSES;
Andrew Vasquezfca29702005-07-06 10:31:47 -07001300 ha->init_cb_size = sizeof(init_cb_t);
Andrew Vasquezcca53352005-08-26 19:08:30 -07001301 ha->mgmt_svr_loop_id = MANAGEMENT_SERVER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001303 /* Assign ISP specific operations. */
1304 ha->isp_ops.pci_config = qla2100_pci_config;
1305 ha->isp_ops.reset_chip = qla2x00_reset_chip;
1306 ha->isp_ops.chip_diag = qla2x00_chip_diag;
1307 ha->isp_ops.config_rings = qla2x00_config_rings;
1308 ha->isp_ops.reset_adapter = qla2x00_reset_adapter;
1309 ha->isp_ops.nvram_config = qla2x00_nvram_config;
1310 ha->isp_ops.update_fw_options = qla2x00_update_fw_options;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001311 ha->isp_ops.load_risc = qla2x00_load_risc;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001312 ha->isp_ops.pci_info_str = qla2x00_pci_info_str;
1313 ha->isp_ops.fw_version_str = qla2x00_fw_version_str;
1314 ha->isp_ops.intr_handler = qla2100_intr_handler;
1315 ha->isp_ops.enable_intrs = qla2x00_enable_intrs;
1316 ha->isp_ops.disable_intrs = qla2x00_disable_intrs;
1317 ha->isp_ops.abort_command = qla2x00_abort_command;
1318 ha->isp_ops.abort_target = qla2x00_abort_target;
1319 ha->isp_ops.fabric_login = qla2x00_login_fabric;
1320 ha->isp_ops.fabric_logout = qla2x00_fabric_logout;
1321 ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_32;
1322 ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_32;
1323 ha->isp_ops.prep_ms_iocb = qla2x00_prep_ms_iocb;
Andrew Vasquezcca53352005-08-26 19:08:30 -07001324 ha->isp_ops.prep_ms_fdmi_iocb = qla2x00_prep_ms_fdmi_iocb;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001325 ha->isp_ops.read_nvram = qla2x00_read_nvram_data;
1326 ha->isp_ops.write_nvram = qla2x00_write_nvram_data;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001327 ha->isp_ops.fw_dump = qla2100_fw_dump;
1328 ha->isp_ops.ascii_fw_dump = qla2100_ascii_fw_dump;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (IS_QLA2100(ha)) {
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001330 host->max_id = MAX_TARGETS_2100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
1332 ha->request_q_length = REQUEST_ENTRY_CNT_2100;
1333 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1334 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1335 host->sg_tablesize = 32;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001336 ha->gid_list_info_size = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 } else if (IS_QLA2200(ha)) {
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001338 host->max_id = MAX_TARGETS_2200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1340 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1341 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1342 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001343 ha->gid_list_info_size = 4;
Andrew Vasquezfca29702005-07-06 10:31:47 -07001344 } else if (IS_QLA23XX(ha)) {
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001345 host->max_id = MAX_TARGETS_2200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1347 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1348 ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
1349 ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001350 ha->isp_ops.pci_config = qla2300_pci_config;
1351 ha->isp_ops.intr_handler = qla2300_intr_handler;
1352 ha->isp_ops.fw_dump = qla2300_fw_dump;
1353 ha->isp_ops.ascii_fw_dump = qla2300_ascii_fw_dump;
1354 ha->gid_list_info_size = 6;
Andrew Vasquezfca29702005-07-06 10:31:47 -07001355 } else if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1356 host->max_id = MAX_TARGETS_2200;
1357 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1358 ha->request_q_length = REQUEST_ENTRY_CNT_24XX;
1359 ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
1360 ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
1361 ha->init_cb_size = sizeof(struct init_cb_24xx);
Andrew Vasquezcca53352005-08-26 19:08:30 -07001362 ha->mgmt_svr_loop_id = 10;
Andrew Vasquezfca29702005-07-06 10:31:47 -07001363 ha->isp_ops.pci_config = qla24xx_pci_config;
1364 ha->isp_ops.reset_chip = qla24xx_reset_chip;
1365 ha->isp_ops.chip_diag = qla24xx_chip_diag;
1366 ha->isp_ops.config_rings = qla24xx_config_rings;
1367 ha->isp_ops.reset_adapter = qla24xx_reset_adapter;
1368 ha->isp_ops.nvram_config = qla24xx_nvram_config;
1369 ha->isp_ops.update_fw_options = qla24xx_update_fw_options;
Andrew Vasquez54333832005-11-09 15:49:04 -08001370#if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
Andrew Vasquezfca29702005-07-06 10:31:47 -07001371 ha->isp_ops.load_risc = qla24xx_load_risc_flash;
Andrew Vasquez54333832005-11-09 15:49:04 -08001372#else
1373 ha->isp_ops.load_risc = qla24xx_load_risc;
1374#endif
Andrew Vasquezfca29702005-07-06 10:31:47 -07001375 ha->isp_ops.pci_info_str = qla24xx_pci_info_str;
1376 ha->isp_ops.fw_version_str = qla24xx_fw_version_str;
1377 ha->isp_ops.intr_handler = qla24xx_intr_handler;
1378 ha->isp_ops.enable_intrs = qla24xx_enable_intrs;
1379 ha->isp_ops.disable_intrs = qla24xx_disable_intrs;
1380 ha->isp_ops.abort_command = qla24xx_abort_command;
1381 ha->isp_ops.abort_target = qla24xx_abort_target;
1382 ha->isp_ops.fabric_login = qla24xx_login_fabric;
1383 ha->isp_ops.fabric_logout = qla24xx_fabric_logout;
1384 ha->isp_ops.prep_ms_iocb = qla24xx_prep_ms_iocb;
Andrew Vasquezcca53352005-08-26 19:08:30 -07001385 ha->isp_ops.prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb;
Andrew Vasquezfca29702005-07-06 10:31:47 -07001386 ha->isp_ops.read_nvram = qla24xx_read_nvram_data;
1387 ha->isp_ops.write_nvram = qla24xx_write_nvram_data;
1388 ha->isp_ops.fw_dump = qla24xx_fw_dump;
1389 ha->isp_ops.ascii_fw_dump = qla24xx_ascii_fw_dump;
1390 ha->gid_list_info_size = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392 host->can_queue = ha->request_q_length + 128;
1393
1394 /* load the F/W, read paramaters, and init the H/W */
1395 ha->instance = num_hosts;
1396
1397 init_MUTEX(&ha->mbx_cmd_sem);
1398 init_MUTEX_LOCKED(&ha->mbx_intr_sem);
1399
1400 INIT_LIST_HEAD(&ha->list);
1401 INIT_LIST_HEAD(&ha->fcports);
1402 INIT_LIST_HEAD(&ha->rscn_fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 /*
1405 * These locks are used to prevent more than one CPU
1406 * from modifying the queue at the same time. The
1407 * higher level "host_lock" will reduce most
1408 * contention for these locks.
1409 */
1410 spin_lock_init(&ha->mbx_reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 init_completion(&ha->dpc_inited);
1413 init_completion(&ha->dpc_exited);
1414
1415 qla2x00_config_dma_addressing(ha);
1416 if (qla2x00_mem_alloc(ha)) {
1417 qla_printk(KERN_WARNING, ha,
1418 "[ERROR] Failed to allocate memory for adapter\n");
1419
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001420 ret = -ENOMEM;
1421 goto probe_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423
1424 if (qla2x00_initialize_adapter(ha) &&
1425 !(ha->device_flags & DFLG_NO_CABLE)) {
1426
1427 qla_printk(KERN_WARNING, ha,
1428 "Failed to initialize adapter\n");
1429
1430 DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
1431 "Adapter flags %x.\n",
1432 ha->host_no, ha->device_flags));
1433
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001434 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 goto probe_failed;
1436 }
1437
1438 /*
1439 * Startup the kernel thread for this host adapter
1440 */
1441 ha->dpc_should_die = 0;
1442 ha->dpc_pid = kernel_thread(qla2x00_do_dpc, ha, 0);
1443 if (ha->dpc_pid < 0) {
1444 qla_printk(KERN_WARNING, ha,
1445 "Unable to start DPC thread!\n");
1446
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001447 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 goto probe_failed;
1449 }
1450 wait_for_completion(&ha->dpc_inited);
1451
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001452 host->this_id = 255;
1453 host->cmd_per_lun = 3;
1454 host->unique_id = ha->instance;
1455 host->max_cmd_len = MAX_CMDSZ;
1456 host->max_channel = ha->ports - 1;
1457 host->max_lun = MAX_LUNS;
1458 host->transportt = qla2xxx_transport_template;
1459
Andrew Vasquezfca29702005-07-06 10:31:47 -07001460 ret = request_irq(pdev->irq, ha->isp_ops.intr_handler,
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001461 SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001462 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 qla_printk(KERN_WARNING, ha,
1464 "Failed to reserve interrupt %d already in use.\n",
Andrew Vasquezfca29702005-07-06 10:31:47 -07001465 pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 goto probe_failed;
1467 }
Andrew Vasquezfca29702005-07-06 10:31:47 -07001468 host->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 /* Initialized the timer */
1471 qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
1472
1473 DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
1474 ha->host_no, ha));
1475
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001476 ha->isp_ops.disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquezfca29702005-07-06 10:31:47 -07001479 reg = ha->iobase;
1480 if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1481 WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_HOST_INT);
1482 WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_RISC_INT);
1483 } else {
1484 WRT_REG_WORD(&reg->isp.semaphore, 0);
1485 WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_RISC_INT);
1486 WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_HOST_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Andrew Vasquezfca29702005-07-06 10:31:47 -07001488 /* Enable proper parity */
1489 if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
1490 if (IS_QLA2300(ha))
1491 /* SRAM parity */
1492 WRT_REG_WORD(&reg->isp.hccr,
1493 (HCCR_ENABLE_PARITY + 0x1));
1494 else
1495 /* SRAM, Instruction RAM and GP RAM parity */
1496 WRT_REG_WORD(&reg->isp.hccr,
1497 (HCCR_ENABLE_PARITY + 0x7));
1498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
1500 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1501
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001502 ha->isp_ops.enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 /* v2.19.5b6 */
1505 /*
1506 * Wait around max loop_reset_delay secs for the devices to come
1507 * on-line. We don't want Linux scanning before we are ready.
1508 *
1509 */
1510 for (wait_switch = jiffies + (ha->loop_reset_delay * HZ);
1511 time_before(jiffies,wait_switch) &&
1512 !(ha->device_flags & (DFLG_NO_CABLE | DFLG_FABRIC_DEVICES))
1513 && (ha->device_flags & SWITCH_FOUND) ;) {
1514
1515 qla2x00_check_fabric_devices(ha);
1516
1517 msleep(10);
1518 }
1519
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001520 pci_set_drvdata(pdev, ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 ha->flags.init_done = 1;
1522 num_hosts++;
1523
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001524 ret = scsi_add_host(host, &pdev->dev);
1525 if (ret)
1526 goto probe_failed;
1527
1528 qla2x00_alloc_sysfs_attr(ha);
1529
1530 qla2x00_init_host_attr(ha);
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 qla_printk(KERN_INFO, ha, "\n"
1533 " QLogic Fibre Channel HBA Driver: %s\n"
1534 " QLogic %s - %s\n"
Andrew Vasquez54333832005-11-09 15:49:04 -08001535 " ISP%04X: %s @ %s hdma%c, host#=%ld, fw=%s\n",
1536 qla2x00_version_str, ha->model_number,
1537 ha->model_desc ? ha->model_desc: "", pdev->device,
1538 ha->isp_ops.pci_info_str(ha, pci_info), pci_name(pdev),
1539 ha->flags.enable_64bit_addressing ? '+': '-', ha->host_no,
1540 ha->isp_ops.fw_version_str(ha, fw_str));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
8482e1182005-04-17 15:04:54 -05001542 /* Go with fc_rport registration. */
1543 list_for_each_entry(fcport, &ha->fcports, list)
1544 qla2x00_reg_remote_port(ha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 return 0;
1547
1548probe_failed:
1549 qla2x00_free_device(ha);
1550
1551 scsi_host_put(host);
1552
1553probe_disable_device:
1554 pci_disable_device(pdev);
1555
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001556probe_out:
1557 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559EXPORT_SYMBOL_GPL(qla2x00_probe_one);
1560
1561void qla2x00_remove_one(struct pci_dev *pdev)
1562{
1563 scsi_qla_host_t *ha;
1564
1565 ha = pci_get_drvdata(pdev);
1566
8482e1182005-04-17 15:04:54 -05001567 qla2x00_free_sysfs_attr(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
bdf79622005-04-17 15:06:53 -05001569 fc_remove_host(ha->host);
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 scsi_remove_host(ha->host);
1572
1573 qla2x00_free_device(ha);
1574
1575 scsi_host_put(ha->host);
1576
1577 pci_set_drvdata(pdev, NULL);
1578}
1579EXPORT_SYMBOL_GPL(qla2x00_remove_one);
1580
1581static void
1582qla2x00_free_device(scsi_qla_host_t *ha)
1583{
1584 int ret;
1585
1586 /* Abort any outstanding IO descriptors. */
1587 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1588 qla2x00_cancel_io_descriptors(ha);
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 /* Disable timer */
1591 if (ha->timer_active)
1592 qla2x00_stop_timer(ha);
1593
1594 /* Kill the kernel thread for this host */
1595 if (ha->dpc_pid >= 0) {
1596 ha->dpc_should_die = 1;
1597 wmb();
1598 ret = kill_proc(ha->dpc_pid, SIGHUP, 1);
1599 if (ret) {
1600 qla_printk(KERN_ERR, ha,
1601 "Unable to signal DPC thread -- (%d)\n", ret);
1602
1603 /* TODO: SOMETHING MORE??? */
1604 } else {
1605 wait_for_completion(&ha->dpc_exited);
1606 }
1607 }
1608
Andrew Vasquezf6ef3b12005-08-26 19:10:20 -07001609 /* Stop currently executing firmware. */
1610 qla2x00_stop_firmware(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Andrew Vasquezf6ef3b12005-08-26 19:10:20 -07001612 /* turn-off interrupts on the card */
1613 if (ha->interrupts_on)
1614 ha->isp_ops.disable_intrs(ha);
1615
1616 qla2x00_mem_free(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 ha->flags.online = 0;
1619
1620 /* Detach interrupts */
1621 if (ha->pdev->irq)
1622 free_irq(ha->pdev->irq, ha);
1623
1624 /* release io space registers */
1625 if (ha->iobase)
1626 iounmap(ha->iobase);
1627 pci_release_regions(ha->pdev);
1628
1629 pci_disable_device(ha->pdev);
1630}
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 * qla2x00_mark_device_lost Updates fcport state when device goes offline.
1634 *
1635 * Input: ha = adapter block pointer. fcport = port structure pointer.
1636 *
1637 * Return: None.
1638 *
1639 * Context:
1640 */
1641void qla2x00_mark_device_lost(scsi_qla_host_t *ha, fc_port_t *fcport,
1642 int do_login)
1643{
8482e1182005-04-17 15:04:54 -05001644 if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001645 schedule_work(&fcport->rport_del_work);
1646
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001647 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 * We may need to retry the login, so don't change the state of the
1649 * port but do the retries.
1650 */
1651 if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
1652 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1653
1654 if (!do_login)
1655 return;
1656
1657 if (fcport->login_retry == 0) {
1658 fcport->login_retry = ha->login_retry_count;
1659 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
1660
1661 DEBUG(printk("scsi(%ld): Port login retry: "
1662 "%02x%02x%02x%02x%02x%02x%02x%02x, "
1663 "id = 0x%04x retry cnt=%d\n",
1664 ha->host_no,
1665 fcport->port_name[0],
1666 fcport->port_name[1],
1667 fcport->port_name[2],
1668 fcport->port_name[3],
1669 fcport->port_name[4],
1670 fcport->port_name[5],
1671 fcport->port_name[6],
1672 fcport->port_name[7],
1673 fcport->loop_id,
1674 fcport->login_retry));
1675 }
1676}
1677
1678/*
1679 * qla2x00_mark_all_devices_lost
1680 * Updates fcport state when device goes offline.
1681 *
1682 * Input:
1683 * ha = adapter block pointer.
1684 * fcport = port structure pointer.
1685 *
1686 * Return:
1687 * None.
1688 *
1689 * Context:
1690 */
1691void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001692qla2x00_mark_all_devices_lost(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
1694 fc_port_t *fcport;
1695
1696 list_for_each_entry(fcport, &ha->fcports, list) {
1697 if (fcport->port_type != FCT_TARGET)
1698 continue;
1699
1700 /*
1701 * No point in marking the device as lost, if the device is
1702 * already DEAD.
1703 */
1704 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
1705 continue;
8482e1182005-04-17 15:04:54 -05001706 if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001707 schedule_work(&fcport->rport_del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1709 }
1710}
1711
1712/*
1713* qla2x00_mem_alloc
1714* Allocates adapter memory.
1715*
1716* Returns:
1717* 0 = success.
1718* 1 = failure.
1719*/
1720static uint8_t
1721qla2x00_mem_alloc(scsi_qla_host_t *ha)
1722{
1723 char name[16];
1724 uint8_t status = 1;
1725 int retry= 10;
1726
1727 do {
1728 /*
1729 * This will loop only once if everything goes well, else some
1730 * number of retries will be performed to get around a kernel
1731 * bug where available mem is not allocated until after a
1732 * little delay and a retry.
1733 */
1734 ha->request_ring = dma_alloc_coherent(&ha->pdev->dev,
1735 (ha->request_q_length + 1) * sizeof(request_t),
1736 &ha->request_dma, GFP_KERNEL);
1737 if (ha->request_ring == NULL) {
1738 qla_printk(KERN_WARNING, ha,
1739 "Memory Allocation failed - request_ring\n");
1740
1741 qla2x00_mem_free(ha);
1742 msleep(100);
1743
1744 continue;
1745 }
1746
1747 ha->response_ring = dma_alloc_coherent(&ha->pdev->dev,
1748 (ha->response_q_length + 1) * sizeof(response_t),
1749 &ha->response_dma, GFP_KERNEL);
1750 if (ha->response_ring == NULL) {
1751 qla_printk(KERN_WARNING, ha,
1752 "Memory Allocation failed - response_ring\n");
1753
1754 qla2x00_mem_free(ha);
1755 msleep(100);
1756
1757 continue;
1758 }
1759
1760 ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
1761 &ha->gid_list_dma, GFP_KERNEL);
1762 if (ha->gid_list == NULL) {
1763 qla_printk(KERN_WARNING, ha,
1764 "Memory Allocation failed - gid_list\n");
1765
1766 qla2x00_mem_free(ha);
1767 msleep(100);
1768
1769 continue;
1770 }
1771
1772 ha->rlc_rsp = dma_alloc_coherent(&ha->pdev->dev,
1773 sizeof(rpt_lun_cmd_rsp_t), &ha->rlc_rsp_dma, GFP_KERNEL);
1774 if (ha->rlc_rsp == NULL) {
1775 qla_printk(KERN_WARNING, ha,
1776 "Memory Allocation failed - rlc");
1777
1778 qla2x00_mem_free(ha);
1779 msleep(100);
1780
1781 continue;
1782 }
1783
1784 snprintf(name, sizeof(name), "qla2xxx_%ld", ha->host_no);
1785 ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
1786 DMA_POOL_SIZE, 8, 0);
1787 if (ha->s_dma_pool == NULL) {
1788 qla_printk(KERN_WARNING, ha,
1789 "Memory Allocation failed - s_dma_pool\n");
1790
1791 qla2x00_mem_free(ha);
1792 msleep(100);
1793
1794 continue;
1795 }
1796
1797 /* get consistent memory allocated for init control block */
1798 ha->init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1799 &ha->init_cb_dma);
1800 if (ha->init_cb == NULL) {
1801 qla_printk(KERN_WARNING, ha,
1802 "Memory Allocation failed - init_cb\n");
1803
1804 qla2x00_mem_free(ha);
1805 msleep(100);
1806
1807 continue;
1808 }
Andrew Vasquezfca29702005-07-06 10:31:47 -07001809 memset(ha->init_cb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 /* Get consistent memory allocated for Get Port Database cmd */
1812 ha->iodesc_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1813 &ha->iodesc_pd_dma);
1814 if (ha->iodesc_pd == NULL) {
1815 /* error */
1816 qla_printk(KERN_WARNING, ha,
1817 "Memory Allocation failed - iodesc_pd\n");
1818
1819 qla2x00_mem_free(ha);
1820 msleep(100);
1821
1822 continue;
1823 }
1824 memset(ha->iodesc_pd, 0, PORT_DATABASE_SIZE);
1825
1826 /* Allocate ioctl related memory. */
1827 if (qla2x00_alloc_ioctl_mem(ha)) {
1828 qla_printk(KERN_WARNING, ha,
1829 "Memory Allocation failed - ioctl_mem\n");
1830
1831 qla2x00_mem_free(ha);
1832 msleep(100);
1833
1834 continue;
1835 }
1836
1837 if (qla2x00_allocate_sp_pool(ha)) {
1838 qla_printk(KERN_WARNING, ha,
1839 "Memory Allocation failed - "
1840 "qla2x00_allocate_sp_pool()\n");
1841
1842 qla2x00_mem_free(ha);
1843 msleep(100);
1844
1845 continue;
1846 }
1847
1848 /* Allocate memory for SNS commands */
1849 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1850 /* Get consistent memory allocated for SNS commands */
1851 ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
1852 sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma,
1853 GFP_KERNEL);
1854 if (ha->sns_cmd == NULL) {
1855 /* error */
1856 qla_printk(KERN_WARNING, ha,
1857 "Memory Allocation failed - sns_cmd\n");
1858
1859 qla2x00_mem_free(ha);
1860 msleep(100);
1861
1862 continue;
1863 }
1864 memset(ha->sns_cmd, 0, sizeof(struct sns_cmd_pkt));
1865 } else {
1866 /* Get consistent memory allocated for MS IOCB */
1867 ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1868 &ha->ms_iocb_dma);
1869 if (ha->ms_iocb == NULL) {
1870 /* error */
1871 qla_printk(KERN_WARNING, ha,
1872 "Memory Allocation failed - ms_iocb\n");
1873
1874 qla2x00_mem_free(ha);
1875 msleep(100);
1876
1877 continue;
1878 }
1879 memset(ha->ms_iocb, 0, sizeof(ms_iocb_entry_t));
1880
1881 /*
1882 * Get consistent memory allocated for CT SNS
1883 * commands
1884 */
1885 ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
1886 sizeof(struct ct_sns_pkt), &ha->ct_sns_dma,
1887 GFP_KERNEL);
1888 if (ha->ct_sns == NULL) {
1889 /* error */
1890 qla_printk(KERN_WARNING, ha,
1891 "Memory Allocation failed - ct_sns\n");
1892
1893 qla2x00_mem_free(ha);
1894 msleep(100);
1895
1896 continue;
1897 }
1898 memset(ha->ct_sns, 0, sizeof(struct ct_sns_pkt));
1899 }
1900
1901 /* Done all allocations without any error. */
1902 status = 0;
1903
1904 } while (retry-- && status != 0);
1905
1906 if (status) {
1907 printk(KERN_WARNING
1908 "%s(): **** FAILED ****\n", __func__);
1909 }
1910
1911 return(status);
1912}
1913
1914/*
1915* qla2x00_mem_free
1916* Frees all adapter allocated memory.
1917*
1918* Input:
1919* ha = adapter block pointer.
1920*/
1921static void
1922qla2x00_mem_free(scsi_qla_host_t *ha)
1923{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 struct list_head *fcpl, *fcptemp;
1925 fc_port_t *fcport;
Andrew Vasquezfe74c712005-08-26 19:10:10 -07001926 unsigned int wtime;/* max wait time if mbx cmd is busy. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 if (ha == NULL) {
1929 /* error */
1930 DEBUG2(printk("%s(): ERROR invalid ha pointer.\n", __func__));
1931 return;
1932 }
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 /* Make sure all other threads are stopped. */
Andrew Vasquezfe74c712005-08-26 19:10:10 -07001935 wtime = 60 * 1000;
1936 while (ha->dpc_wait && wtime)
1937 wtime = msleep_interruptible(wtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 /* free ioctl memory */
1940 qla2x00_free_ioctl_mem(ha);
1941
1942 /* free sp pool */
1943 qla2x00_free_sp_pool(ha);
1944
1945 if (ha->sns_cmd)
1946 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
1947 ha->sns_cmd, ha->sns_cmd_dma);
1948
1949 if (ha->ct_sns)
1950 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
1951 ha->ct_sns, ha->ct_sns_dma);
1952
1953 if (ha->ms_iocb)
1954 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
1955
1956 if (ha->iodesc_pd)
1957 dma_pool_free(ha->s_dma_pool, ha->iodesc_pd, ha->iodesc_pd_dma);
1958
1959 if (ha->init_cb)
1960 dma_pool_free(ha->s_dma_pool, ha->init_cb, ha->init_cb_dma);
1961
1962 if (ha->s_dma_pool)
1963 dma_pool_destroy(ha->s_dma_pool);
1964
1965 if (ha->rlc_rsp)
1966 dma_free_coherent(&ha->pdev->dev,
1967 sizeof(rpt_lun_cmd_rsp_t), ha->rlc_rsp,
1968 ha->rlc_rsp_dma);
1969
1970 if (ha->gid_list)
1971 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
1972 ha->gid_list_dma);
1973
1974 if (ha->response_ring)
1975 dma_free_coherent(&ha->pdev->dev,
1976 (ha->response_q_length + 1) * sizeof(response_t),
1977 ha->response_ring, ha->response_dma);
1978
1979 if (ha->request_ring)
1980 dma_free_coherent(&ha->pdev->dev,
1981 (ha->request_q_length + 1) * sizeof(request_t),
1982 ha->request_ring, ha->request_dma);
1983
1984 ha->sns_cmd = NULL;
1985 ha->sns_cmd_dma = 0;
1986 ha->ct_sns = NULL;
1987 ha->ct_sns_dma = 0;
1988 ha->ms_iocb = NULL;
1989 ha->ms_iocb_dma = 0;
1990 ha->iodesc_pd = NULL;
1991 ha->iodesc_pd_dma = 0;
1992 ha->init_cb = NULL;
1993 ha->init_cb_dma = 0;
1994
1995 ha->s_dma_pool = NULL;
1996
1997 ha->rlc_rsp = NULL;
1998 ha->rlc_rsp_dma = 0;
1999 ha->gid_list = NULL;
2000 ha->gid_list_dma = 0;
2001
2002 ha->response_ring = NULL;
2003 ha->response_dma = 0;
2004 ha->request_ring = NULL;
2005 ha->request_dma = 0;
2006
2007 list_for_each_safe(fcpl, fcptemp, &ha->fcports) {
2008 fcport = list_entry(fcpl, fc_port_t, list);
2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 /* fc ports */
2011 list_del_init(&fcport->list);
2012 kfree(fcport);
2013 }
2014 INIT_LIST_HEAD(&ha->fcports);
2015
2016 if (ha->fw_dump)
2017 free_pages((unsigned long)ha->fw_dump, ha->fw_dump_order);
2018
Andrew Vasquezfca29702005-07-06 10:31:47 -07002019 vfree(ha->fw_dump24);
2020
2021 vfree(ha->fw_dump_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 ha->fw_dump = NULL;
Andrew Vasquezfca29702005-07-06 10:31:47 -07002024 ha->fw_dump24 = NULL;
2025 ha->fw_dumped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 ha->fw_dump_reading = 0;
2027 ha->fw_dump_buffer = NULL;
2028}
2029
2030/*
2031 * qla2x00_allocate_sp_pool
2032 * This routine is called during initialization to allocate
2033 * memory for local srb_t.
2034 *
2035 * Input:
2036 * ha = adapter block pointer.
2037 *
2038 * Context:
2039 * Kernel context.
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002040 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 * Note: Sets the ref_count for non Null sp to one.
2042 */
2043static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002044qla2x00_allocate_sp_pool(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 int rval;
2047
2048 rval = QLA_SUCCESS;
2049 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
2050 mempool_free_slab, srb_cachep);
2051 if (ha->srb_mempool == NULL) {
2052 qla_printk(KERN_INFO, ha, "Unable to allocate SRB mempool.\n");
2053 rval = QLA_FUNCTION_FAILED;
2054 }
2055 return (rval);
2056}
2057
2058/*
2059 * This routine frees all adapter allocated memory.
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002060 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 */
2062static void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002063qla2x00_free_sp_pool( scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 if (ha->srb_mempool) {
2066 mempool_destroy(ha->srb_mempool);
2067 ha->srb_mempool = NULL;
2068 }
2069}
2070
2071/**************************************************************************
2072* qla2x00_do_dpc
2073* This kernel thread is a task that is schedule by the interrupt handler
2074* to perform the background processing for interrupts.
2075*
2076* Notes:
2077* This task always run in the context of a kernel thread. It
2078* is kick-off by the driver's detect code and starts up
2079* up one per adapter. It immediately goes to sleep and waits for
2080* some fibre event. When either the interrupt handler or
2081* the timer routine detects a event it will one of the task
2082* bits then wake us up.
2083**************************************************************************/
2084static int
2085qla2x00_do_dpc(void *data)
2086{
2087 DECLARE_MUTEX_LOCKED(sem);
2088 scsi_qla_host_t *ha;
2089 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 uint8_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 uint16_t next_loopid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 ha = (scsi_qla_host_t *)data;
2094
2095 lock_kernel();
2096
2097 daemonize("%s_dpc", ha->host_str);
2098 allow_signal(SIGHUP);
2099
2100 ha->dpc_wait = &sem;
2101
2102 set_user_nice(current, -20);
2103
2104 unlock_kernel();
2105
2106 complete(&ha->dpc_inited);
2107
2108 while (1) {
2109 DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
2110
2111 if (down_interruptible(&sem))
2112 break;
2113
2114 if (ha->dpc_should_die)
2115 break;
2116
2117 DEBUG3(printk("qla2x00: DPC handler waking up\n"));
2118
2119 /* Initialization not yet finished. Don't do anything yet. */
2120 if (!ha->flags.init_done || ha->dpc_active)
2121 continue;
2122
2123 DEBUG3(printk("scsi(%ld): DPC handler\n", ha->host_no));
2124
2125 ha->dpc_active = 1;
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (ha->flags.mbox_busy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 ha->dpc_active = 0;
2129 continue;
2130 }
2131
2132 if (test_and_clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
2133
2134 DEBUG(printk("scsi(%ld): dpc: sched "
2135 "qla2x00_abort_isp ha = %p\n",
2136 ha->host_no, ha));
2137 if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
2138 &ha->dpc_flags))) {
2139
2140 if (qla2x00_abort_isp(ha)) {
2141 /* failed. retry later */
2142 set_bit(ISP_ABORT_NEEDED,
2143 &ha->dpc_flags);
2144 }
2145 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
2146 }
2147 DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
2148 ha->host_no));
2149 }
2150
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07002151 if (test_and_clear_bit(LOOP_RESET_NEEDED, &ha->dpc_flags)) {
2152 DEBUG(printk("scsi(%ld): dpc: sched loop_reset()\n",
2153 ha->host_no));
2154 qla2x00_loop_reset(ha);
2155 }
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 if (test_and_clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) &&
2158 (!(test_and_set_bit(RESET_ACTIVE, &ha->dpc_flags)))) {
2159
2160 DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
2161 ha->host_no));
2162
2163 qla2x00_rst_aen(ha);
2164 clear_bit(RESET_ACTIVE, &ha->dpc_flags);
2165 }
2166
2167 /* Retry each device up to login retry count */
2168 if ((test_and_clear_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
2169 !test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) &&
2170 atomic_read(&ha->loop_state) != LOOP_DOWN) {
2171
2172 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
2173 ha->host_no));
2174
2175 next_loopid = 0;
2176 list_for_each_entry(fcport, &ha->fcports, list) {
2177 if (fcport->port_type != FCT_TARGET)
2178 continue;
2179
2180 /*
2181 * If the port is not ONLINE then try to login
2182 * to it if we haven't run out of retries.
2183 */
2184 if (atomic_read(&fcport->state) != FCS_ONLINE &&
2185 fcport->login_retry) {
2186
2187 fcport->login_retry--;
2188 if (fcport->flags & FCF_FABRIC_DEVICE) {
2189 if (fcport->flags &
2190 FCF_TAPE_PRESENT)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002191 ha->isp_ops.fabric_logout(
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002192 ha, fcport->loop_id,
2193 fcport->d_id.b.domain,
2194 fcport->d_id.b.area,
2195 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 status = qla2x00_fabric_login(
2197 ha, fcport, &next_loopid);
2198 } else
2199 status =
2200 qla2x00_local_device_login(
2201 ha, fcport->loop_id);
2202
2203 if (status == QLA_SUCCESS) {
2204 fcport->old_loop_id = fcport->loop_id;
2205
2206 DEBUG(printk("scsi(%ld): port login OK: logged in ID 0x%x\n",
2207 ha->host_no, fcport->loop_id));
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 fcport->port_login_retry_count =
2210 ha->port_down_retry_count * PORT_RETRY_TIME;
2211 atomic_set(&fcport->state, FCS_ONLINE);
2212 atomic_set(&fcport->port_down_timer,
2213 ha->port_down_retry_count * PORT_RETRY_TIME);
2214
2215 fcport->login_retry = 0;
2216 } else if (status == 1) {
2217 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
2218 /* retry the login again */
2219 DEBUG(printk("scsi(%ld): Retrying %d login again loop_id 0x%x\n",
2220 ha->host_no,
2221 fcport->login_retry, fcport->loop_id));
2222 } else {
2223 fcport->login_retry = 0;
2224 }
2225 }
2226 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2227 break;
2228 }
2229 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
2230 ha->host_no));
2231 }
2232
2233 if ((test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags)) &&
2234 atomic_read(&ha->loop_state) != LOOP_DOWN) {
2235
2236 clear_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags);
2237 DEBUG(printk("scsi(%ld): qla2x00_login_retry()\n",
2238 ha->host_no));
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2241
2242 DEBUG(printk("scsi(%ld): qla2x00_login_retry - end\n",
2243 ha->host_no));
2244 }
2245
2246 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
2247
2248 DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
2249 ha->host_no));
2250
2251 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
2252 &ha->dpc_flags))) {
2253
2254 qla2x00_loop_resync(ha);
2255
2256 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
2257 }
2258
2259 DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
2260 ha->host_no));
2261 }
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if (test_and_clear_bit(FCPORT_RESCAN_NEEDED, &ha->dpc_flags)) {
2264
2265 DEBUG(printk("scsi(%ld): Rescan flagged fcports...\n",
2266 ha->host_no));
2267
2268 qla2x00_rescan_fcports(ha);
2269
2270 DEBUG(printk("scsi(%ld): Rescan flagged fcports..."
2271 "end.\n",
2272 ha->host_no));
2273 }
2274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (!ha->interrupts_on)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002276 ha->isp_ops.enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 ha->dpc_active = 0;
2279 } /* End of while(1) */
2280
2281 DEBUG(printk("scsi(%ld): DPC handler exiting\n", ha->host_no));
2282
2283 /*
2284 * Make sure that nobody tries to wake us up again.
2285 */
2286 ha->dpc_wait = NULL;
2287 ha->dpc_active = 0;
2288
2289 complete_and_exit(&ha->dpc_exited, 0);
2290}
2291
2292/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293* qla2x00_rst_aen
2294* Processes asynchronous reset.
2295*
2296* Input:
2297* ha = adapter block pointer.
2298*/
2299static void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002300qla2x00_rst_aen(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
2302 if (ha->flags.online && !ha->flags.reset_active &&
2303 !atomic_read(&ha->loop_down_timer) &&
2304 !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
2305 do {
2306 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
2307
2308 /*
2309 * Issue marker command only when we are going to start
2310 * the I/O.
2311 */
2312 ha->marker_needed = 1;
2313 } while (!atomic_read(&ha->loop_down_timer) &&
2314 (test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags)));
2315 }
2316}
2317
f4f051e2005-04-17 15:02:26 -05002318static void
2319qla2x00_sp_free_dma(scsi_qla_host_t *ha, srb_t *sp)
2320{
2321 struct scsi_cmnd *cmd = sp->cmd;
2322
2323 if (sp->flags & SRB_DMA_VALID) {
2324 if (cmd->use_sg) {
2325 dma_unmap_sg(&ha->pdev->dev, cmd->request_buffer,
2326 cmd->use_sg, cmd->sc_data_direction);
2327 } else if (cmd->request_bufflen) {
2328 dma_unmap_single(&ha->pdev->dev, sp->dma_handle,
2329 cmd->request_bufflen, cmd->sc_data_direction);
2330 }
2331 sp->flags &= ~SRB_DMA_VALID;
2332 }
Andrew Vasquezfca29702005-07-06 10:31:47 -07002333 CMD_SP(cmd) = NULL;
f4f051e2005-04-17 15:02:26 -05002334}
2335
2336void
2337qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *sp)
2338{
2339 struct scsi_cmnd *cmd = sp->cmd;
2340
2341 qla2x00_sp_free_dma(ha, sp);
2342
f4f051e2005-04-17 15:02:26 -05002343 mempool_free(sp, ha->srb_mempool);
2344
2345 cmd->scsi_done(cmd);
2346}
bdf79622005-04-17 15:06:53 -05002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348/**************************************************************************
2349* qla2x00_timer
2350*
2351* Description:
2352* One second timer
2353*
2354* Context: Interrupt
2355***************************************************************************/
2356static void
2357qla2x00_timer(scsi_qla_host_t *ha)
2358{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 unsigned long cpu_flags = 0;
2360 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 int start_dpc = 0;
2362 int index;
2363 srb_t *sp;
f4f051e2005-04-17 15:02:26 -05002364 int t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366 /*
2367 * Ports - Port down timer.
2368 *
2369 * Whenever, a port is in the LOST state we start decrementing its port
2370 * down timer every second until it reaches zero. Once it reaches zero
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002371 * the port it marked DEAD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 */
2373 t = 0;
2374 list_for_each_entry(fcport, &ha->fcports, list) {
2375 if (fcport->port_type != FCT_TARGET)
2376 continue;
2377
2378 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2379
2380 if (atomic_read(&fcport->port_down_timer) == 0)
2381 continue;
2382
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002383 if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
Andrew Vasquezfca29702005-07-06 10:31:47 -07002387 "%d remaining\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 ha->host_no,
2389 t, atomic_read(&fcport->port_down_timer)));
2390 }
2391 t++;
2392 } /* End of for fcport */
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395 /* Loop down handler. */
2396 if (atomic_read(&ha->loop_down_timer) > 0 &&
2397 !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) && ha->flags.online) {
2398
2399 if (atomic_read(&ha->loop_down_timer) ==
2400 ha->loop_down_abort_time) {
2401
2402 DEBUG(printk("scsi(%ld): Loop Down - aborting the "
2403 "queues before time expire\n",
2404 ha->host_no));
2405
2406 if (!IS_QLA2100(ha) && ha->link_down_timeout)
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002407 atomic_set(&ha->loop_state, LOOP_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
2409 /* Schedule an ISP abort to return any tape commands. */
2410 spin_lock_irqsave(&ha->hardware_lock, cpu_flags);
2411 for (index = 1; index < MAX_OUTSTANDING_COMMANDS;
2412 index++) {
bdf79622005-04-17 15:06:53 -05002413 fc_port_t *sfcp;
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 sp = ha->outstanding_cmds[index];
2416 if (!sp)
2417 continue;
bdf79622005-04-17 15:06:53 -05002418 sfcp = sp->fcport;
2419 if (!(sfcp->flags & FCF_TAPE_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 continue;
2421
2422 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2423 break;
2424 }
2425 spin_unlock_irqrestore(&ha->hardware_lock, cpu_flags);
2426
2427 set_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
2428 start_dpc++;
2429 }
2430
2431 /* if the loop has been down for 4 minutes, reinit adapter */
2432 if (atomic_dec_and_test(&ha->loop_down_timer) != 0) {
2433 DEBUG(printk("scsi(%ld): Loop down exceed 4 mins - "
2434 "restarting queues.\n",
2435 ha->host_no));
2436
2437 set_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
2438 start_dpc++;
2439
2440 if (!(ha->device_flags & DFLG_NO_CABLE)) {
2441 DEBUG(printk("scsi(%ld): Loop down - "
2442 "aborting ISP.\n",
2443 ha->host_no));
2444 qla_printk(KERN_WARNING, ha,
2445 "Loop down - aborting ISP.\n");
2446
2447 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2448 }
2449 }
Andrew Vasquezfca29702005-07-06 10:31:47 -07002450 DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 ha->host_no,
2452 atomic_read(&ha->loop_down_timer)));
2453 }
2454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 /* Schedule the DPC routine if needed */
2456 if ((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
2457 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) ||
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07002458 test_bit(LOOP_RESET_NEEDED, &ha->dpc_flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 start_dpc ||
2460 test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags) ||
f4f051e2005-04-17 15:02:26 -05002461 test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 test_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
2463 ha->dpc_wait && !ha->dpc_active) {
2464
2465 up(ha->dpc_wait);
2466 }
2467
2468 qla2x00_restart_timer(ha, WATCH_INTERVAL);
2469}
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471/* XXX(hch): crude hack to emulate a down_timeout() */
2472int
2473qla2x00_down_timeout(struct semaphore *sema, unsigned long timeout)
2474{
Andrew Vasquezfe74c712005-08-26 19:10:10 -07002475 const unsigned int step = 100; /* msecs */
2476 unsigned int iterations = jiffies_to_msecs(timeout)/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 do {
2479 if (!down_trylock(sema))
2480 return 0;
Andrew Vasquezfe74c712005-08-26 19:10:10 -07002481 if (msleep_interruptible(step))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 break;
Andrew Vasquezfe74c712005-08-26 19:10:10 -07002483 } while (--iterations >= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 return -ETIMEDOUT;
2486}
2487
Andrew Vasquez54333832005-11-09 15:49:04 -08002488#if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
2489
2490#define qla2x00_release_firmware() do { } while (0)
2491
Andrew Vasquezfca29702005-07-06 10:31:47 -07002492static struct qla_board_info qla_board_tbl[] = {
2493 {
2494 .drv_name = "qla2400",
2495 .isp_name = "ISP2422",
2496 .fw_fname = "ql2400_fw.bin",
2497 .sht = &qla24xx_driver_template,
2498 },
2499 {
2500 .drv_name = "qla2400",
2501 .isp_name = "ISP2432",
2502 .fw_fname = "ql2400_fw.bin",
2503 .sht = &qla24xx_driver_template,
2504 },
2505};
2506
2507static struct pci_device_id qla2xxx_pci_tbl[] = {
2508 {
2509 .vendor = PCI_VENDOR_ID_QLOGIC,
2510 .device = PCI_DEVICE_ID_QLOGIC_ISP2422,
2511 .subvendor = PCI_ANY_ID,
2512 .subdevice = PCI_ANY_ID,
2513 .driver_data = (unsigned long)&qla_board_tbl[0],
2514 },
2515 {
2516 .vendor = PCI_VENDOR_ID_QLOGIC,
2517 .device = PCI_DEVICE_ID_QLOGIC_ISP2432,
2518 .subvendor = PCI_ANY_ID,
2519 .subdevice = PCI_ANY_ID,
2520 .driver_data = (unsigned long)&qla_board_tbl[1],
2521 },
2522 {0, 0},
2523};
2524MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
2525
2526static int __devinit
2527qla2xxx_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2528{
2529 return qla2x00_probe_one(pdev,
2530 (struct qla_board_info *)id->driver_data);
2531}
2532
2533static void __devexit
2534qla2xxx_remove_one(struct pci_dev *pdev)
2535{
2536 qla2x00_remove_one(pdev);
2537}
2538
Andrew Vasquez54333832005-11-09 15:49:04 -08002539#else /* !defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE) */
2540
2541/* Firmware interface routines. */
2542
2543#define FW_BLOBS 6
2544#define FW_ISP21XX 0
2545#define FW_ISP22XX 1
2546#define FW_ISP2300 2
2547#define FW_ISP2322 3
2548#define FW_ISP63XX 4
2549#define FW_ISP24XX 5
2550
2551static DECLARE_MUTEX(qla_fw_lock);
2552
2553static struct fw_blob qla_fw_blobs[FW_BLOBS] = {
2554 { .name = "ql2100_fw.bin", .segs = { 0x1000, 0 }, },
2555 { .name = "ql2200_fw.bin", .segs = { 0x1000, 0 }, },
2556 { .name = "ql2300_fw.bin", .segs = { 0x800, 0 }, },
2557 { .name = "ql2322_fw.bin", .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
2558 { .name = "ql6312_fw.bin", .segs = { 0x800, 0 }, },
2559 { .name = "ql2400_fw.bin", },
2560};
2561
2562struct fw_blob *
2563qla2x00_request_firmware(scsi_qla_host_t *ha)
2564{
2565 struct fw_blob *blob;
2566
2567 blob = NULL;
2568 if (IS_QLA2100(ha)) {
2569 blob = &qla_fw_blobs[FW_ISP21XX];
2570 } else if (IS_QLA2200(ha)) {
2571 blob = &qla_fw_blobs[FW_ISP22XX];
2572 } else if (IS_QLA2300(ha) || IS_QLA2312(ha)) {
2573 blob = &qla_fw_blobs[FW_ISP2300];
2574 } else if (IS_QLA2322(ha)) {
2575 blob = &qla_fw_blobs[FW_ISP2322];
2576 } else if (IS_QLA6312(ha) || IS_QLA6322(ha)) {
2577 blob = &qla_fw_blobs[FW_ISP63XX];
2578 } else if (IS_QLA24XX(ha)) {
2579 blob = &qla_fw_blobs[FW_ISP24XX];
2580 }
2581
2582 down(&qla_fw_lock);
2583 if (blob->fw)
2584 goto out;
2585
2586 if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
2587 DEBUG2(printk("scsi(%ld): Failed to load firmware image "
2588 "(%s).\n", ha->host_no, blob->name));
2589 blob->fw = NULL;
2590 blob = NULL;
2591 goto out;
2592 }
2593
2594out:
2595 up(&qla_fw_lock);
2596 return blob;
2597}
2598
2599static void
2600qla2x00_release_firmware(void)
2601{
2602 int idx;
2603
2604 down(&qla_fw_lock);
2605 for (idx = 0; idx < FW_BLOBS; idx++)
2606 if (qla_fw_blobs[idx].fw)
2607 release_firmware(qla_fw_blobs[idx].fw);
2608 up(&qla_fw_lock);
2609}
2610
2611static struct qla_board_info qla_board_tbl = {
2612 .drv_name = "qla2xxx",
2613};
2614
2615static struct pci_device_id qla2xxx_pci_tbl[] = {
2616 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100,
2617 PCI_ANY_ID, PCI_ANY_ID, },
2618 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200,
2619 PCI_ANY_ID, PCI_ANY_ID, },
2620 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300,
2621 PCI_ANY_ID, PCI_ANY_ID, },
2622 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312,
2623 PCI_ANY_ID, PCI_ANY_ID, },
2624 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322,
2625 PCI_ANY_ID, PCI_ANY_ID, },
2626 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312,
2627 PCI_ANY_ID, PCI_ANY_ID, },
2628 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322,
2629 PCI_ANY_ID, PCI_ANY_ID, },
2630 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422,
2631 PCI_ANY_ID, PCI_ANY_ID, },
2632 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432,
2633 PCI_ANY_ID, PCI_ANY_ID, },
2634 { 0 },
2635};
2636MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
2637
2638static int __devinit
2639qla2xxx_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2640{
2641 return qla2x00_probe_one(pdev, &qla_board_tbl);
2642}
2643
2644static void __devexit
2645qla2xxx_remove_one(struct pci_dev *pdev)
2646{
2647 qla2x00_remove_one(pdev);
2648}
2649
2650#endif
2651
Andrew Vasquezfca29702005-07-06 10:31:47 -07002652static struct pci_driver qla2xxx_pci_driver = {
2653 .name = "qla2xxx",
Andrew Vasquez54333832005-11-09 15:49:04 -08002654 .owner = THIS_MODULE,
Andrew Vasquezfca29702005-07-06 10:31:47 -07002655 .id_table = qla2xxx_pci_tbl,
2656 .probe = qla2xxx_probe_one,
2657 .remove = __devexit_p(qla2xxx_remove_one),
2658};
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660/**
2661 * qla2x00_module_init - Module initialization.
2662 **/
2663static int __init
2664qla2x00_module_init(void)
2665{
Andrew Vasquezfca29702005-07-06 10:31:47 -07002666 int ret = 0;
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 /* Allocate cache for SRBs. */
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04002669 srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 SLAB_HWCACHE_ALIGN, NULL, NULL);
2671 if (srb_cachep == NULL) {
2672 printk(KERN_ERR
2673 "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
2674 return -ENOMEM;
2675 }
2676
2677 /* Derive version string. */
2678 strcpy(qla2x00_version_str, QLA2XXX_VERSION);
Andrew Vasquez54333832005-11-09 15:49:04 -08002679#if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
2680 strcat(qla2x00_version_str, "-fw");
2681#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682#if DEBUG_QLA2100
2683 strcat(qla2x00_version_str, "-debug");
2684#endif
Andrew Vasquez1c97a122005-04-21 16:13:36 -04002685 qla2xxx_transport_template =
2686 fc_attach_transport(&qla2xxx_transport_functions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 if (!qla2xxx_transport_template)
2688 return -ENODEV;
2689
2690 printk(KERN_INFO "QLogic Fibre Channel HBA Driver\n");
Andrew Vasquezfca29702005-07-06 10:31:47 -07002691 ret = pci_module_init(&qla2xxx_pci_driver);
2692 if (ret) {
2693 kmem_cache_destroy(srb_cachep);
2694 fc_release_transport(qla2xxx_transport_template);
2695 }
2696 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
2699/**
2700 * qla2x00_module_exit - Module cleanup.
2701 **/
2702static void __exit
2703qla2x00_module_exit(void)
2704{
Andrew Vasquezfca29702005-07-06 10:31:47 -07002705 pci_unregister_driver(&qla2xxx_pci_driver);
Andrew Vasquez54333832005-11-09 15:49:04 -08002706 qla2x00_release_firmware();
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04002707 kmem_cache_destroy(srb_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 fc_release_transport(qla2xxx_transport_template);
2709}
2710
2711module_init(qla2x00_module_init);
2712module_exit(qla2x00_module_exit);
2713
2714MODULE_AUTHOR("QLogic Corporation");
2715MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
2716MODULE_LICENSE("GPL");
2717MODULE_VERSION(QLA2XXX_VERSION);