]> Pileus Git - ~andy/linux/blob - drivers/scsi/ipr.c
aa6e14b5ebb2467585cfb2fd1f8528a657f76080
[~andy/linux] / drivers / scsi / ipr.c
1 /*
2  * ipr.c -- driver for IBM Power Linux RAID adapters
3  *
4  * Written By: Brian King <brking@us.ibm.com>, IBM Corporation
5  *
6  * Copyright (C) 2003, 2004 IBM Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 /*
25  * Notes:
26  *
27  * This driver is used to control the following SCSI adapters:
28  *
29  * IBM iSeries: 5702, 5703, 2780, 5709, 570A, 570B
30  *
31  * IBM pSeries: PCI-X Dual Channel Ultra 320 SCSI RAID Adapter
32  *              PCI-X Dual Channel Ultra 320 SCSI Adapter
33  *              PCI-X Dual Channel Ultra 320 SCSI RAID Enablement Card
34  *              Embedded SCSI adapter on p615 and p655 systems
35  *
36  * Supported Hardware Features:
37  *      - Ultra 320 SCSI controller
38  *      - PCI-X host interface
39  *      - Embedded PowerPC RISC Processor and Hardware XOR DMA Engine
40  *      - Non-Volatile Write Cache
41  *      - Supports attachment of non-RAID disks, tape, and optical devices
42  *      - RAID Levels 0, 5, 10
43  *      - Hot spare
44  *      - Background Parity Checking
45  *      - Background Data Scrubbing
46  *      - Ability to increase the capacity of an existing RAID 5 disk array
47  *              by adding disks
48  *
49  * Driver Features:
50  *      - Tagged command queuing
51  *      - Adapter microcode download
52  *      - PCI hot plug
53  *      - SCSI device hot plug
54  *
55  */
56
57 #include <linux/config.h>
58 #include <linux/fs.h>
59 #include <linux/init.h>
60 #include <linux/types.h>
61 #include <linux/errno.h>
62 #include <linux/kernel.h>
63 #include <linux/ioport.h>
64 #include <linux/delay.h>
65 #include <linux/pci.h>
66 #include <linux/wait.h>
67 #include <linux/spinlock.h>
68 #include <linux/sched.h>
69 #include <linux/interrupt.h>
70 #include <linux/blkdev.h>
71 #include <linux/firmware.h>
72 #include <linux/module.h>
73 #include <linux/moduleparam.h>
74 #include <asm/io.h>
75 #include <asm/irq.h>
76 #include <asm/processor.h>
77 #include <scsi/scsi.h>
78 #include <scsi/scsi_host.h>
79 #include <scsi/scsi_tcq.h>
80 #include <scsi/scsi_eh.h>
81 #include <scsi/scsi_cmnd.h>
82 #include <scsi/scsi_request.h>
83 #include "ipr.h"
84
85 /*
86  *   Global Data
87  */
88 static struct list_head ipr_ioa_head = LIST_HEAD_INIT(ipr_ioa_head);
89 static unsigned int ipr_log_level = IPR_DEFAULT_LOG_LEVEL;
90 static unsigned int ipr_max_speed = 1;
91 static int ipr_testmode = 0;
92 static unsigned int ipr_fastfail = 0;
93 static unsigned int ipr_transop_timeout = IPR_OPERATIONAL_TIMEOUT;
94 static DEFINE_SPINLOCK(ipr_driver_lock);
95
96 /* This table describes the differences between DMA controller chips */
97 static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
98         { /* Gemstone and Citrine */
99                 .mailbox = 0x0042C,
100                 .cache_line_size = 0x20,
101                 {
102                         .set_interrupt_mask_reg = 0x0022C,
103                         .clr_interrupt_mask_reg = 0x00230,
104                         .sense_interrupt_mask_reg = 0x0022C,
105                         .clr_interrupt_reg = 0x00228,
106                         .sense_interrupt_reg = 0x00224,
107                         .ioarrin_reg = 0x00404,
108                         .sense_uproc_interrupt_reg = 0x00214,
109                         .set_uproc_interrupt_reg = 0x00214,
110                         .clr_uproc_interrupt_reg = 0x00218
111                 }
112         },
113         { /* Snipe and Scamp */
114                 .mailbox = 0x0052C,
115                 .cache_line_size = 0x20,
116                 {
117                         .set_interrupt_mask_reg = 0x00288,
118                         .clr_interrupt_mask_reg = 0x0028C,
119                         .sense_interrupt_mask_reg = 0x00288,
120                         .clr_interrupt_reg = 0x00284,
121                         .sense_interrupt_reg = 0x00280,
122                         .ioarrin_reg = 0x00504,
123                         .sense_uproc_interrupt_reg = 0x00290,
124                         .set_uproc_interrupt_reg = 0x00290,
125                         .clr_uproc_interrupt_reg = 0x00294
126                 }
127         },
128 };
129
130 static const struct ipr_chip_t ipr_chip[] = {
131         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE, &ipr_chip_cfg[0] },
132         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE, &ipr_chip_cfg[0] },
133         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE, &ipr_chip_cfg[1] },
134         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP, &ipr_chip_cfg[1] }
135 };
136
137 static int ipr_max_bus_speeds [] = {
138         IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
139 };
140
141 MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
142 MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
143 module_param_named(max_speed, ipr_max_speed, uint, 0);
144 MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
145 module_param_named(log_level, ipr_log_level, uint, 0);
146 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
147 module_param_named(testmode, ipr_testmode, int, 0);
148 MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
149 module_param_named(fastfail, ipr_fastfail, int, 0);
150 MODULE_PARM_DESC(fastfail, "Reduce timeouts and retries");
151 module_param_named(transop_timeout, ipr_transop_timeout, int, 0);
152 MODULE_PARM_DESC(transop_timeout, "Time in seconds to wait for adapter to come operational (default: 300)");
153 MODULE_LICENSE("GPL");
154 MODULE_VERSION(IPR_DRIVER_VERSION);
155
156 static const char *ipr_gpdd_dev_end_states[] = {
157         "Command complete",
158         "Terminated by host",
159         "Terminated by device reset",
160         "Terminated by bus reset",
161         "Unknown",
162         "Command not started"
163 };
164
165 static const char *ipr_gpdd_dev_bus_phases[] = {
166         "Bus free",
167         "Arbitration",
168         "Selection",
169         "Message out",
170         "Command",
171         "Message in",
172         "Data out",
173         "Data in",
174         "Status",
175         "Reselection",
176         "Unknown"
177 };
178
179 /*  A constant array of IOASCs/URCs/Error Messages */
180 static const
181 struct ipr_error_table_t ipr_error_table[] = {
182         {0x00000000, 1, 1,
183         "8155: An unknown error was received"},
184         {0x00330000, 0, 0,
185         "Soft underlength error"},
186         {0x005A0000, 0, 0,
187         "Command to be cancelled not found"},
188         {0x00808000, 0, 0,
189         "Qualified success"},
190         {0x01080000, 1, 1,
191         "FFFE: Soft device bus error recovered by the IOA"},
192         {0x01170600, 0, 1,
193         "FFF9: Device sector reassign successful"},
194         {0x01170900, 0, 1,
195         "FFF7: Media error recovered by device rewrite procedures"},
196         {0x01180200, 0, 1,
197         "7001: IOA sector reassignment successful"},
198         {0x01180500, 0, 1,
199         "FFF9: Soft media error. Sector reassignment recommended"},
200         {0x01180600, 0, 1,
201         "FFF7: Media error recovered by IOA rewrite procedures"},
202         {0x01418000, 0, 1,
203         "FF3D: Soft PCI bus error recovered by the IOA"},
204         {0x01440000, 1, 1,
205         "FFF6: Device hardware error recovered by the IOA"},
206         {0x01448100, 0, 1,
207         "FFF6: Device hardware error recovered by the device"},
208         {0x01448200, 1, 1,
209         "FF3D: Soft IOA error recovered by the IOA"},
210         {0x01448300, 0, 1,
211         "FFFA: Undefined device response recovered by the IOA"},
212         {0x014A0000, 1, 1,
213         "FFF6: Device bus error, message or command phase"},
214         {0x015D0000, 0, 1,
215         "FFF6: Failure prediction threshold exceeded"},
216         {0x015D9200, 0, 1,
217         "8009: Impending cache battery pack failure"},
218         {0x02040400, 0, 0,
219         "34FF: Disk device format in progress"},
220         {0x023F0000, 0, 0,
221         "Synchronization required"},
222         {0x024E0000, 0, 0,
223         "No ready, IOA shutdown"},
224         {0x025A0000, 0, 0,
225         "Not ready, IOA has been shutdown"},
226         {0x02670100, 0, 1,
227         "3020: Storage subsystem configuration error"},
228         {0x03110B00, 0, 0,
229         "FFF5: Medium error, data unreadable, recommend reassign"},
230         {0x03110C00, 0, 0,
231         "7000: Medium error, data unreadable, do not reassign"},
232         {0x03310000, 0, 1,
233         "FFF3: Disk media format bad"},
234         {0x04050000, 0, 1,
235         "3002: Addressed device failed to respond to selection"},
236         {0x04080000, 1, 1,
237         "3100: Device bus error"},
238         {0x04080100, 0, 1,
239         "3109: IOA timed out a device command"},
240         {0x04088000, 0, 0,
241         "3120: SCSI bus is not operational"},
242         {0x04118000, 0, 1,
243         "9000: IOA reserved area data check"},
244         {0x04118100, 0, 1,
245         "9001: IOA reserved area invalid data pattern"},
246         {0x04118200, 0, 1,
247         "9002: IOA reserved area LRC error"},
248         {0x04320000, 0, 1,
249         "102E: Out of alternate sectors for disk storage"},
250         {0x04330000, 1, 1,
251         "FFF4: Data transfer underlength error"},
252         {0x04338000, 1, 1,
253         "FFF4: Data transfer overlength error"},
254         {0x043E0100, 0, 1,
255         "3400: Logical unit failure"},
256         {0x04408500, 0, 1,
257         "FFF4: Device microcode is corrupt"},
258         {0x04418000, 1, 1,
259         "8150: PCI bus error"},
260         {0x04430000, 1, 0,
261         "Unsupported device bus message received"},
262         {0x04440000, 1, 1,
263         "FFF4: Disk device problem"},
264         {0x04448200, 1, 1,
265         "8150: Permanent IOA failure"},
266         {0x04448300, 0, 1,
267         "3010: Disk device returned wrong response to IOA"},
268         {0x04448400, 0, 1,
269         "8151: IOA microcode error"},
270         {0x04448500, 0, 0,
271         "Device bus status error"},
272         {0x04448600, 0, 1,
273         "8157: IOA error requiring IOA reset to recover"},
274         {0x04490000, 0, 0,
275         "Message reject received from the device"},
276         {0x04449200, 0, 1,
277         "8008: A permanent cache battery pack failure occurred"},
278         {0x0444A000, 0, 1,
279         "9090: Disk unit has been modified after the last known status"},
280         {0x0444A200, 0, 1,
281         "9081: IOA detected device error"},
282         {0x0444A300, 0, 1,
283         "9082: IOA detected device error"},
284         {0x044A0000, 1, 1,
285         "3110: Device bus error, message or command phase"},
286         {0x04670400, 0, 1,
287         "9091: Incorrect hardware configuration change has been detected"},
288         {0x046E0000, 0, 1,
289         "FFF4: Command to logical unit failed"},
290         {0x05240000, 1, 0,
291         "Illegal request, invalid request type or request packet"},
292         {0x05250000, 0, 0,
293         "Illegal request, invalid resource handle"},
294         {0x05260000, 0, 0,
295         "Illegal request, invalid field in parameter list"},
296         {0x05260100, 0, 0,
297         "Illegal request, parameter not supported"},
298         {0x05260200, 0, 0,
299         "Illegal request, parameter value invalid"},
300         {0x052C0000, 0, 0,
301         "Illegal request, command sequence error"},
302         {0x06040500, 0, 1,
303         "9031: Array protection temporarily suspended, protection resuming"},
304         {0x06040600, 0, 1,
305         "9040: Array protection temporarily suspended, protection resuming"},
306         {0x06290000, 0, 1,
307         "FFFB: SCSI bus was reset"},
308         {0x06290500, 0, 0,
309         "FFFE: SCSI bus transition to single ended"},
310         {0x06290600, 0, 0,
311         "FFFE: SCSI bus transition to LVD"},
312         {0x06298000, 0, 1,
313         "FFFB: SCSI bus was reset by another initiator"},
314         {0x063F0300, 0, 1,
315         "3029: A device replacement has occurred"},
316         {0x064C8000, 0, 1,
317         "9051: IOA cache data exists for a missing or failed device"},
318         {0x06670100, 0, 1,
319         "9025: Disk unit is not supported at its physical location"},
320         {0x06670600, 0, 1,
321         "3020: IOA detected a SCSI bus configuration error"},
322         {0x06678000, 0, 1,
323         "3150: SCSI bus configuration error"},
324         {0x06690200, 0, 1,
325         "9041: Array protection temporarily suspended"},
326         {0x06698200, 0, 1,
327         "9042: Corrupt array parity detected on specified device"},
328         {0x066B0200, 0, 1,
329         "9030: Array no longer protected due to missing or failed disk unit"},
330         {0x066B8200, 0, 1,
331         "9032: Array exposed but still protected"},
332         {0x07270000, 0, 0,
333         "Failure due to other device"},
334         {0x07278000, 0, 1,
335         "9008: IOA does not support functions expected by devices"},
336         {0x07278100, 0, 1,
337         "9010: Cache data associated with attached devices cannot be found"},
338         {0x07278200, 0, 1,
339         "9011: Cache data belongs to devices other than those attached"},
340         {0x07278400, 0, 1,
341         "9020: Array missing 2 or more devices with only 1 device present"},
342         {0x07278500, 0, 1,
343         "9021: Array missing 2 or more devices with 2 or more devices present"},
344         {0x07278600, 0, 1,
345         "9022: Exposed array is missing a required device"},
346         {0x07278700, 0, 1,
347         "9023: Array member(s) not at required physical locations"},
348         {0x07278800, 0, 1,
349         "9024: Array not functional due to present hardware configuration"},
350         {0x07278900, 0, 1,
351         "9026: Array not functional due to present hardware configuration"},
352         {0x07278A00, 0, 1,
353         "9027: Array is missing a device and parity is out of sync"},
354         {0x07278B00, 0, 1,
355         "9028: Maximum number of arrays already exist"},
356         {0x07278C00, 0, 1,
357         "9050: Required cache data cannot be located for a disk unit"},
358         {0x07278D00, 0, 1,
359         "9052: Cache data exists for a device that has been modified"},
360         {0x07278F00, 0, 1,
361         "9054: IOA resources not available due to previous problems"},
362         {0x07279100, 0, 1,
363         "9092: Disk unit requires initialization before use"},
364         {0x07279200, 0, 1,
365         "9029: Incorrect hardware configuration change has been detected"},
366         {0x07279600, 0, 1,
367         "9060: One or more disk pairs are missing from an array"},
368         {0x07279700, 0, 1,
369         "9061: One or more disks are missing from an array"},
370         {0x07279800, 0, 1,
371         "9062: One or more disks are missing from an array"},
372         {0x07279900, 0, 1,
373         "9063: Maximum number of functional arrays has been exceeded"},
374         {0x0B260000, 0, 0,
375         "Aborted command, invalid descriptor"},
376         {0x0B5A0000, 0, 0,
377         "Command terminated by host"}
378 };
379
380 static const struct ipr_ses_table_entry ipr_ses_table[] = {
381         { "2104-DL1        ", "XXXXXXXXXXXXXXXX", 80 },
382         { "2104-TL1        ", "XXXXXXXXXXXXXXXX", 80 },
383         { "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
384         { "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
385         { "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
386         { "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
387         { "2104-DU3        ", "XXXXXXXXXXXXXXXX", 160 },
388         { "2104-TU3        ", "XXXXXXXXXXXXXXXX", 160 },
389         { "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
390         { "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
391         { "St  V1S2        ", "XXXXXXXXXXXXXXXX", 160 },
392         { "HSBPD4M  PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
393         { "VSBPD1H   U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
394 };
395
396 /*
397  *  Function Prototypes
398  */
399 static int ipr_reset_alert(struct ipr_cmnd *);
400 static void ipr_process_ccn(struct ipr_cmnd *);
401 static void ipr_process_error(struct ipr_cmnd *);
402 static void ipr_reset_ioa_job(struct ipr_cmnd *);
403 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
404                                    enum ipr_shutdown_type);
405
406 #ifdef CONFIG_SCSI_IPR_TRACE
407 /**
408  * ipr_trc_hook - Add a trace entry to the driver trace
409  * @ipr_cmd:    ipr command struct
410  * @type:               trace type
411  * @add_data:   additional data
412  *
413  * Return value:
414  *      none
415  **/
416 static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
417                          u8 type, u32 add_data)
418 {
419         struct ipr_trace_entry *trace_entry;
420         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
421
422         trace_entry = &ioa_cfg->trace[ioa_cfg->trace_index++];
423         trace_entry->time = jiffies;
424         trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
425         trace_entry->type = type;
426         trace_entry->cmd_index = ipr_cmd->cmd_index;
427         trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
428         trace_entry->u.add_data = add_data;
429 }
430 #else
431 #define ipr_trc_hook(ipr_cmd, type, add_data) do { } while(0)
432 #endif
433
434 /**
435  * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
436  * @ipr_cmd:    ipr command struct
437  *
438  * Return value:
439  *      none
440  **/
441 static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
442 {
443         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
444         struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
445
446         memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
447         ioarcb->write_data_transfer_length = 0;
448         ioarcb->read_data_transfer_length = 0;
449         ioarcb->write_ioadl_len = 0;
450         ioarcb->read_ioadl_len = 0;
451         ioasa->ioasc = 0;
452         ioasa->residual_data_len = 0;
453
454         ipr_cmd->scsi_cmd = NULL;
455         ipr_cmd->sense_buffer[0] = 0;
456         ipr_cmd->dma_use_sg = 0;
457 }
458
459 /**
460  * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
461  * @ipr_cmd:    ipr command struct
462  *
463  * Return value:
464  *      none
465  **/
466 static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
467 {
468         ipr_reinit_ipr_cmnd(ipr_cmd);
469         ipr_cmd->u.scratch = 0;
470         ipr_cmd->sibling = NULL;
471         init_timer(&ipr_cmd->timer);
472 }
473
474 /**
475  * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
476  * @ioa_cfg:    ioa config struct
477  *
478  * Return value:
479  *      pointer to ipr command struct
480  **/
481 static
482 struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
483 {
484         struct ipr_cmnd *ipr_cmd;
485
486         ipr_cmd = list_entry(ioa_cfg->free_q.next, struct ipr_cmnd, queue);
487         list_del(&ipr_cmd->queue);
488         ipr_init_ipr_cmnd(ipr_cmd);
489
490         return ipr_cmd;
491 }
492
493 /**
494  * ipr_unmap_sglist - Unmap scatterlist if mapped
495  * @ioa_cfg:    ioa config struct
496  * @ipr_cmd:    ipr command struct
497  *
498  * Return value:
499  *      nothing
500  **/
501 static void ipr_unmap_sglist(struct ipr_ioa_cfg *ioa_cfg,
502                              struct ipr_cmnd *ipr_cmd)
503 {
504         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
505
506         if (ipr_cmd->dma_use_sg) {
507                 if (scsi_cmd->use_sg > 0) {
508                         pci_unmap_sg(ioa_cfg->pdev, scsi_cmd->request_buffer,
509                                      scsi_cmd->use_sg,
510                                      scsi_cmd->sc_data_direction);
511                 } else {
512                         pci_unmap_single(ioa_cfg->pdev, ipr_cmd->dma_handle,
513                                          scsi_cmd->request_bufflen,
514                                          scsi_cmd->sc_data_direction);
515                 }
516         }
517 }
518
519 /**
520  * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
521  * @ioa_cfg:    ioa config struct
522  * @clr_ints:     interrupts to clear
523  *
524  * This function masks all interrupts on the adapter, then clears the
525  * interrupts specified in the mask
526  *
527  * Return value:
528  *      none
529  **/
530 static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
531                                           u32 clr_ints)
532 {
533         volatile u32 int_reg;
534
535         /* Stop new interrupts */
536         ioa_cfg->allow_interrupts = 0;
537
538         /* Set interrupt mask to stop all new interrupts */
539         writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
540
541         /* Clear any pending interrupts */
542         writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg);
543         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
544 }
545
546 /**
547  * ipr_save_pcix_cmd_reg - Save PCI-X command register
548  * @ioa_cfg:    ioa config struct
549  *
550  * Return value:
551  *      0 on success / -EIO on failure
552  **/
553 static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
554 {
555         int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
556
557         if (pcix_cmd_reg == 0) {
558                 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
559                 return -EIO;
560         }
561
562         if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
563                                  &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
564                 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
565                 return -EIO;
566         }
567
568         ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
569         return 0;
570 }
571
572 /**
573  * ipr_set_pcix_cmd_reg - Setup PCI-X command register
574  * @ioa_cfg:    ioa config struct
575  *
576  * Return value:
577  *      0 on success / -EIO on failure
578  **/
579 static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
580 {
581         int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
582
583         if (pcix_cmd_reg) {
584                 if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
585                                           ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
586                         dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
587                         return -EIO;
588                 }
589         } else {
590                 dev_err(&ioa_cfg->pdev->dev,
591                         "Failed to setup PCI-X command register\n");
592                 return -EIO;
593         }
594
595         return 0;
596 }
597
598 /**
599  * ipr_scsi_eh_done - mid-layer done function for aborted ops
600  * @ipr_cmd:    ipr command struct
601  *
602  * This function is invoked by the interrupt handler for
603  * ops generated by the SCSI mid-layer which are being aborted.
604  *
605  * Return value:
606  *      none
607  **/
608 static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
609 {
610         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
611         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
612
613         scsi_cmd->result |= (DID_ERROR << 16);
614
615         ipr_unmap_sglist(ioa_cfg, ipr_cmd);
616         scsi_cmd->scsi_done(scsi_cmd);
617         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
618 }
619
620 /**
621  * ipr_fail_all_ops - Fails all outstanding ops.
622  * @ioa_cfg:    ioa config struct
623  *
624  * This function fails all outstanding ops.
625  *
626  * Return value:
627  *      none
628  **/
629 static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
630 {
631         struct ipr_cmnd *ipr_cmd, *temp;
632
633         ENTER;
634         list_for_each_entry_safe(ipr_cmd, temp, &ioa_cfg->pending_q, queue) {
635                 list_del(&ipr_cmd->queue);
636
637                 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
638                 ipr_cmd->ioasa.ilid = cpu_to_be32(IPR_DRIVER_ILID);
639
640                 if (ipr_cmd->scsi_cmd)
641                         ipr_cmd->done = ipr_scsi_eh_done;
642
643                 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, IPR_IOASC_IOA_WAS_RESET);
644                 del_timer(&ipr_cmd->timer);
645                 ipr_cmd->done(ipr_cmd);
646         }
647
648         LEAVE;
649 }
650
651 /**
652  * ipr_do_req -  Send driver initiated requests.
653  * @ipr_cmd:            ipr command struct
654  * @done:                       done function
655  * @timeout_func:       timeout function
656  * @timeout:            timeout value
657  *
658  * This function sends the specified command to the adapter with the
659  * timeout given. The done function is invoked on command completion.
660  *
661  * Return value:
662  *      none
663  **/
664 static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
665                        void (*done) (struct ipr_cmnd *),
666                        void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
667 {
668         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
669
670         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
671
672         ipr_cmd->done = done;
673
674         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
675         ipr_cmd->timer.expires = jiffies + timeout;
676         ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;
677
678         add_timer(&ipr_cmd->timer);
679
680         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
681
682         mb();
683         writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
684                ioa_cfg->regs.ioarrin_reg);
685 }
686
687 /**
688  * ipr_internal_cmd_done - Op done function for an internally generated op.
689  * @ipr_cmd:    ipr command struct
690  *
691  * This function is the op done function for an internally generated,
692  * blocking op. It simply wakes the sleeping thread.
693  *
694  * Return value:
695  *      none
696  **/
697 static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
698 {
699         if (ipr_cmd->sibling)
700                 ipr_cmd->sibling = NULL;
701         else
702                 complete(&ipr_cmd->completion);
703 }
704
705 /**
706  * ipr_send_blocking_cmd - Send command and sleep on its completion.
707  * @ipr_cmd:    ipr command struct
708  * @timeout_func:       function to invoke if command times out
709  * @timeout:    timeout
710  *
711  * Return value:
712  *      none
713  **/
714 static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
715                                   void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
716                                   u32 timeout)
717 {
718         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
719
720         init_completion(&ipr_cmd->completion);
721         ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
722
723         spin_unlock_irq(ioa_cfg->host->host_lock);
724         wait_for_completion(&ipr_cmd->completion);
725         spin_lock_irq(ioa_cfg->host->host_lock);
726 }
727
728 /**
729  * ipr_send_hcam - Send an HCAM to the adapter.
730  * @ioa_cfg:    ioa config struct
731  * @type:               HCAM type
732  * @hostrcb:    hostrcb struct
733  *
734  * This function will send a Host Controlled Async command to the adapter.
735  * If HCAMs are currently not allowed to be issued to the adapter, it will
736  * place the hostrcb on the free queue.
737  *
738  * Return value:
739  *      none
740  **/
741 static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
742                           struct ipr_hostrcb *hostrcb)
743 {
744         struct ipr_cmnd *ipr_cmd;
745         struct ipr_ioarcb *ioarcb;
746
747         if (ioa_cfg->allow_cmds) {
748                 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
749                 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
750                 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);
751
752                 ipr_cmd->u.hostrcb = hostrcb;
753                 ioarcb = &ipr_cmd->ioarcb;
754
755                 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
756                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
757                 ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
758                 ioarcb->cmd_pkt.cdb[1] = type;
759                 ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
760                 ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;
761
762                 ioarcb->read_data_transfer_length = cpu_to_be32(sizeof(hostrcb->hcam));
763                 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
764                 ipr_cmd->ioadl[0].flags_and_data_len =
765                         cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(hostrcb->hcam));
766                 ipr_cmd->ioadl[0].address = cpu_to_be32(hostrcb->hostrcb_dma);
767
768                 if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
769                         ipr_cmd->done = ipr_process_ccn;
770                 else
771                         ipr_cmd->done = ipr_process_error;
772
773                 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
774
775                 mb();
776                 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
777                        ioa_cfg->regs.ioarrin_reg);
778         } else {
779                 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
780         }
781 }
782
783 /**
784  * ipr_init_res_entry - Initialize a resource entry struct.
785  * @res:        resource entry struct
786  *
787  * Return value:
788  *      none
789  **/
790 static void ipr_init_res_entry(struct ipr_resource_entry *res)
791 {
792         res->needs_sync_complete = 1;
793         res->in_erp = 0;
794         res->add_to_ml = 0;
795         res->del_from_ml = 0;
796         res->resetting_device = 0;
797         res->sdev = NULL;
798 }
799
800 /**
801  * ipr_handle_config_change - Handle a config change from the adapter
802  * @ioa_cfg:    ioa config struct
803  * @hostrcb:    hostrcb
804  *
805  * Return value:
806  *      none
807  **/
808 static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
809                               struct ipr_hostrcb *hostrcb)
810 {
811         struct ipr_resource_entry *res = NULL;
812         struct ipr_config_table_entry *cfgte;
813         u32 is_ndn = 1;
814
815         cfgte = &hostrcb->hcam.u.ccn.cfgte;
816
817         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
818                 if (!memcmp(&res->cfgte.res_addr, &cfgte->res_addr,
819                             sizeof(cfgte->res_addr))) {
820                         is_ndn = 0;
821                         break;
822                 }
823         }
824
825         if (is_ndn) {
826                 if (list_empty(&ioa_cfg->free_res_q)) {
827                         ipr_send_hcam(ioa_cfg,
828                                       IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
829                                       hostrcb);
830                         return;
831                 }
832
833                 res = list_entry(ioa_cfg->free_res_q.next,
834                                  struct ipr_resource_entry, queue);
835
836                 list_del(&res->queue);
837                 ipr_init_res_entry(res);
838                 list_add_tail(&res->queue, &ioa_cfg->used_res_q);
839         }
840
841         memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
842
843         if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
844                 if (res->sdev) {
845                         res->sdev->hostdata = NULL;
846                         res->del_from_ml = 1;
847                         if (ioa_cfg->allow_ml_add_del)
848                                 schedule_work(&ioa_cfg->work_q);
849                 } else
850                         list_move_tail(&res->queue, &ioa_cfg->free_res_q);
851         } else if (!res->sdev) {
852                 res->add_to_ml = 1;
853                 if (ioa_cfg->allow_ml_add_del)
854                         schedule_work(&ioa_cfg->work_q);
855         }
856
857         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
858 }
859
860 /**
861  * ipr_process_ccn - Op done function for a CCN.
862  * @ipr_cmd:    ipr command struct
863  *
864  * This function is the op done function for a configuration
865  * change notification host controlled async from the adapter.
866  *
867  * Return value:
868  *      none
869  **/
870 static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
871 {
872         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
873         struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
874         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
875
876         list_del(&hostrcb->queue);
877         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
878
879         if (ioasc) {
880                 if (ioasc != IPR_IOASC_IOA_WAS_RESET)
881                         dev_err(&ioa_cfg->pdev->dev,
882                                 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
883
884                 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
885         } else {
886                 ipr_handle_config_change(ioa_cfg, hostrcb);
887         }
888 }
889
890 /**
891  * ipr_log_vpd - Log the passed VPD to the error log.
892  * @vpd:                vendor/product id/sn struct
893  *
894  * Return value:
895  *      none
896  **/
897 static void ipr_log_vpd(struct ipr_vpd *vpd)
898 {
899         char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
900                     + IPR_SERIAL_NUM_LEN];
901
902         memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
903         memcpy(buffer + IPR_VENDOR_ID_LEN, vpd->vpids.product_id,
904                IPR_PROD_ID_LEN);
905         buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
906         ipr_err("Vendor/Product ID: %s\n", buffer);
907
908         memcpy(buffer, vpd->sn, IPR_SERIAL_NUM_LEN);
909         buffer[IPR_SERIAL_NUM_LEN] = '\0';
910         ipr_err("    Serial Number: %s\n", buffer);
911 }
912
913 /**
914  * ipr_log_cache_error - Log a cache error.
915  * @ioa_cfg:    ioa config struct
916  * @hostrcb:    hostrcb struct
917  *
918  * Return value:
919  *      none
920  **/
921 static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
922                                 struct ipr_hostrcb *hostrcb)
923 {
924         struct ipr_hostrcb_type_02_error *error =
925                 &hostrcb->hcam.u.error.u.type_02_error;
926
927         ipr_err("-----Current Configuration-----\n");
928         ipr_err("Cache Directory Card Information:\n");
929         ipr_log_vpd(&error->ioa_vpd);
930         ipr_err("Adapter Card Information:\n");
931         ipr_log_vpd(&error->cfc_vpd);
932
933         ipr_err("-----Expected Configuration-----\n");
934         ipr_err("Cache Directory Card Information:\n");
935         ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpd);
936         ipr_err("Adapter Card Information:\n");
937         ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpd);
938
939         ipr_err("Additional IOA Data: %08X %08X %08X\n",
940                      be32_to_cpu(error->ioa_data[0]),
941                      be32_to_cpu(error->ioa_data[1]),
942                      be32_to_cpu(error->ioa_data[2]));
943 }
944
945 /**
946  * ipr_log_config_error - Log a configuration error.
947  * @ioa_cfg:    ioa config struct
948  * @hostrcb:    hostrcb struct
949  *
950  * Return value:
951  *      none
952  **/
953 static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
954                                  struct ipr_hostrcb *hostrcb)
955 {
956         int errors_logged, i;
957         struct ipr_hostrcb_device_data_entry *dev_entry;
958         struct ipr_hostrcb_type_03_error *error;
959
960         error = &hostrcb->hcam.u.error.u.type_03_error;
961         errors_logged = be32_to_cpu(error->errors_logged);
962
963         ipr_err("Device Errors Detected/Logged: %d/%d\n",
964                 be32_to_cpu(error->errors_detected), errors_logged);
965
966         dev_entry = error->dev;
967
968         for (i = 0; i < errors_logged; i++, dev_entry++) {
969                 ipr_err_separator;
970
971                 ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
972                 ipr_log_vpd(&dev_entry->vpd);
973
974                 ipr_err("-----New Device Information-----\n");
975                 ipr_log_vpd(&dev_entry->new_vpd);
976
977                 ipr_err("Cache Directory Card Information:\n");
978                 ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpd);
979
980                 ipr_err("Adapter Card Information:\n");
981                 ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpd);
982
983                 ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
984                         be32_to_cpu(dev_entry->ioa_data[0]),
985                         be32_to_cpu(dev_entry->ioa_data[1]),
986                         be32_to_cpu(dev_entry->ioa_data[2]),
987                         be32_to_cpu(dev_entry->ioa_data[3]),
988                         be32_to_cpu(dev_entry->ioa_data[4]));
989         }
990 }
991
992 /**
993  * ipr_log_array_error - Log an array configuration error.
994  * @ioa_cfg:    ioa config struct
995  * @hostrcb:    hostrcb struct
996  *
997  * Return value:
998  *      none
999  **/
1000 static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
1001                                 struct ipr_hostrcb *hostrcb)
1002 {
1003         int i;
1004         struct ipr_hostrcb_type_04_error *error;
1005         struct ipr_hostrcb_array_data_entry *array_entry;
1006         const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
1007
1008         error = &hostrcb->hcam.u.error.u.type_04_error;
1009
1010         ipr_err_separator;
1011
1012         ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1013                 error->protection_level,
1014                 ioa_cfg->host->host_no,
1015                 error->last_func_vset_res_addr.bus,
1016                 error->last_func_vset_res_addr.target,
1017                 error->last_func_vset_res_addr.lun);
1018
1019         ipr_err_separator;
1020
1021         array_entry = error->array_member;
1022
1023         for (i = 0; i < 18; i++) {
1024                 if (!memcmp(array_entry->vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
1025                         continue;
1026
1027                 if (be32_to_cpu(error->exposed_mode_adn) == i)
1028                         ipr_err("Exposed Array Member %d:\n", i);
1029                 else
1030                         ipr_err("Array Member %d:\n", i);
1031
1032                 ipr_log_vpd(&array_entry->vpd);
1033
1034                 ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
1035                 ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
1036                                  "Expected Location");
1037
1038                 ipr_err_separator;
1039
1040                 if (i == 9)
1041                         array_entry = error->array_member2;
1042                 else
1043                         array_entry++;
1044         }
1045 }
1046
1047 /**
1048  * ipr_log_generic_error - Log an adapter error.
1049  * @ioa_cfg:    ioa config struct
1050  * @hostrcb:    hostrcb struct
1051  *
1052  * Return value:
1053  *      none
1054  **/
1055 static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
1056                                   struct ipr_hostrcb *hostrcb)
1057 {
1058         int i;
1059         int ioa_data_len = be32_to_cpu(hostrcb->hcam.length);
1060
1061         if (ioa_data_len == 0)
1062                 return;
1063
1064         for (i = 0; i < ioa_data_len / 4; i += 4) {
1065                 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
1066                         be32_to_cpu(hostrcb->hcam.u.raw.data[i]),
1067                         be32_to_cpu(hostrcb->hcam.u.raw.data[i+1]),
1068                         be32_to_cpu(hostrcb->hcam.u.raw.data[i+2]),
1069                         be32_to_cpu(hostrcb->hcam.u.raw.data[i+3]));
1070         }
1071 }
1072
1073 /**
1074  * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
1075  * @ioasc:      IOASC
1076  *
1077  * This function will return the index of into the ipr_error_table
1078  * for the specified IOASC. If the IOASC is not in the table,
1079  * 0 will be returned, which points to the entry used for unknown errors.
1080  *
1081  * Return value:
1082  *      index into the ipr_error_table
1083  **/
1084 static u32 ipr_get_error(u32 ioasc)
1085 {
1086         int i;
1087
1088         for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
1089                 if (ipr_error_table[i].ioasc == ioasc)
1090                         return i;
1091
1092         return 0;
1093 }
1094
1095 /**
1096  * ipr_handle_log_data - Log an adapter error.
1097  * @ioa_cfg:    ioa config struct
1098  * @hostrcb:    hostrcb struct
1099  *
1100  * This function logs an adapter error to the system.
1101  *
1102  * Return value:
1103  *      none
1104  **/
1105 static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
1106                                 struct ipr_hostrcb *hostrcb)
1107 {
1108         u32 ioasc;
1109         int error_index;
1110
1111         if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
1112                 return;
1113
1114         if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
1115                 dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
1116
1117         ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
1118
1119         if (ioasc == IPR_IOASC_BUS_WAS_RESET ||
1120             ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER) {
1121                 /* Tell the midlayer we had a bus reset so it will handle the UA properly */
1122                 scsi_report_bus_reset(ioa_cfg->host,
1123                                       hostrcb->hcam.u.error.failing_dev_res_addr.bus);
1124         }
1125
1126         error_index = ipr_get_error(ioasc);
1127
1128         if (!ipr_error_table[error_index].log_hcam)
1129                 return;
1130
1131         if (ipr_is_device(&hostrcb->hcam.u.error.failing_dev_res_addr)) {
1132                 ipr_res_err(ioa_cfg, hostrcb->hcam.u.error.failing_dev_res_addr,
1133                             "%s\n", ipr_error_table[error_index].error);
1134         } else {
1135                 dev_err(&ioa_cfg->pdev->dev, "%s\n",
1136                         ipr_error_table[error_index].error);
1137         }
1138
1139         /* Set indication we have logged an error */
1140         ioa_cfg->errors_logged++;
1141
1142         if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
1143                 return;
1144
1145         switch (hostrcb->hcam.overlay_id) {
1146         case IPR_HOST_RCB_OVERLAY_ID_1:
1147                 ipr_log_generic_error(ioa_cfg, hostrcb);
1148                 break;
1149         case IPR_HOST_RCB_OVERLAY_ID_2:
1150                 ipr_log_cache_error(ioa_cfg, hostrcb);
1151                 break;
1152         case IPR_HOST_RCB_OVERLAY_ID_3:
1153                 ipr_log_config_error(ioa_cfg, hostrcb);
1154                 break;
1155         case IPR_HOST_RCB_OVERLAY_ID_4:
1156         case IPR_HOST_RCB_OVERLAY_ID_6:
1157                 ipr_log_array_error(ioa_cfg, hostrcb);
1158                 break;
1159         case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
1160                 ipr_log_generic_error(ioa_cfg, hostrcb);
1161                 break;
1162         default:
1163                 dev_err(&ioa_cfg->pdev->dev,
1164                         "Unknown error received. Overlay ID: %d\n",
1165                         hostrcb->hcam.overlay_id);
1166                 break;
1167         }
1168 }
1169
1170 /**
1171  * ipr_process_error - Op done function for an adapter error log.
1172  * @ipr_cmd:    ipr command struct
1173  *
1174  * This function is the op done function for an error log host
1175  * controlled async from the adapter. It will log the error and
1176  * send the HCAM back to the adapter.
1177  *
1178  * Return value:
1179  *      none
1180  **/
1181 static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
1182 {
1183         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1184         struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
1185         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
1186
1187         list_del(&hostrcb->queue);
1188         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
1189
1190         if (!ioasc) {
1191                 ipr_handle_log_data(ioa_cfg, hostrcb);
1192         } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
1193                 dev_err(&ioa_cfg->pdev->dev,
1194                         "Host RCB failed with IOASC: 0x%08X\n", ioasc);
1195         }
1196
1197         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
1198 }
1199
1200 /**
1201  * ipr_timeout -  An internally generated op has timed out.
1202  * @ipr_cmd:    ipr command struct
1203  *
1204  * This function blocks host requests and initiates an
1205  * adapter reset.
1206  *
1207  * Return value:
1208  *      none
1209  **/
1210 static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
1211 {
1212         unsigned long lock_flags = 0;
1213         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1214
1215         ENTER;
1216         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1217
1218         ioa_cfg->errors_logged++;
1219         dev_err(&ioa_cfg->pdev->dev,
1220                 "Adapter being reset due to command timeout.\n");
1221
1222         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1223                 ioa_cfg->sdt_state = GET_DUMP;
1224
1225         if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
1226                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1227
1228         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1229         LEAVE;
1230 }
1231
1232 /**
1233  * ipr_oper_timeout -  Adapter timed out transitioning to operational
1234  * @ipr_cmd:    ipr command struct
1235  *
1236  * This function blocks host requests and initiates an
1237  * adapter reset.
1238  *
1239  * Return value:
1240  *      none
1241  **/
1242 static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
1243 {
1244         unsigned long lock_flags = 0;
1245         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1246
1247         ENTER;
1248         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1249
1250         ioa_cfg->errors_logged++;
1251         dev_err(&ioa_cfg->pdev->dev,
1252                 "Adapter timed out transitioning to operational.\n");
1253
1254         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1255                 ioa_cfg->sdt_state = GET_DUMP;
1256
1257         if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd) {
1258                 if (ipr_fastfail)
1259                         ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
1260                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1261         }
1262
1263         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1264         LEAVE;
1265 }
1266
1267 /**
1268  * ipr_reset_reload - Reset/Reload the IOA
1269  * @ioa_cfg:            ioa config struct
1270  * @shutdown_type:      shutdown type
1271  *
1272  * This function resets the adapter and re-initializes it.
1273  * This function assumes that all new host commands have been stopped.
1274  * Return value:
1275  *      SUCCESS / FAILED
1276  **/
1277 static int ipr_reset_reload(struct ipr_ioa_cfg *ioa_cfg,
1278                             enum ipr_shutdown_type shutdown_type)
1279 {
1280         if (!ioa_cfg->in_reset_reload)
1281                 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
1282
1283         spin_unlock_irq(ioa_cfg->host->host_lock);
1284         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
1285         spin_lock_irq(ioa_cfg->host->host_lock);
1286
1287         /* If we got hit with a host reset while we were already resetting
1288          the adapter for some reason, and the reset failed. */
1289         if (ioa_cfg->ioa_is_dead) {
1290                 ipr_trace;
1291                 return FAILED;
1292         }
1293
1294         return SUCCESS;
1295 }
1296
1297 /**
1298  * ipr_find_ses_entry - Find matching SES in SES table
1299  * @res:        resource entry struct of SES
1300  *
1301  * Return value:
1302  *      pointer to SES table entry / NULL on failure
1303  **/
1304 static const struct ipr_ses_table_entry *
1305 ipr_find_ses_entry(struct ipr_resource_entry *res)
1306 {
1307         int i, j, matches;
1308         const struct ipr_ses_table_entry *ste = ipr_ses_table;
1309
1310         for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
1311                 for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
1312                         if (ste->compare_product_id_byte[j] == 'X') {
1313                                 if (res->cfgte.std_inq_data.vpids.product_id[j] == ste->product_id[j])
1314                                         matches++;
1315                                 else
1316                                         break;
1317                         } else
1318                                 matches++;
1319                 }
1320
1321                 if (matches == IPR_PROD_ID_LEN)
1322                         return ste;
1323         }
1324
1325         return NULL;
1326 }
1327
1328 /**
1329  * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
1330  * @ioa_cfg:    ioa config struct
1331  * @bus:                SCSI bus
1332  * @bus_width:  bus width
1333  *
1334  * Return value:
1335  *      SCSI bus speed in units of 100KHz, 1600 is 160 MHz
1336  *      For a 2-byte wide SCSI bus, the maximum transfer speed is
1337  *      twice the maximum transfer rate (e.g. for a wide enabled bus,
1338  *      max 160MHz = max 320MB/sec).
1339  **/
1340 static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
1341 {
1342         struct ipr_resource_entry *res;
1343         const struct ipr_ses_table_entry *ste;
1344         u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
1345
1346         /* Loop through each config table entry in the config table buffer */
1347         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1348                 if (!(IPR_IS_SES_DEVICE(res->cfgte.std_inq_data)))
1349                         continue;
1350
1351                 if (bus != res->cfgte.res_addr.bus)
1352                         continue;
1353
1354                 if (!(ste = ipr_find_ses_entry(res)))
1355                         continue;
1356
1357                 max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
1358         }
1359
1360         return max_xfer_rate;
1361 }
1362
1363 /**
1364  * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
1365  * @ioa_cfg:            ioa config struct
1366  * @max_delay:          max delay in micro-seconds to wait
1367  *
1368  * Waits for an IODEBUG ACK from the IOA, doing busy looping.
1369  *
1370  * Return value:
1371  *      0 on success / other on failure
1372  **/
1373 static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
1374 {
1375         volatile u32 pcii_reg;
1376         int delay = 1;
1377
1378         /* Read interrupt reg until IOA signals IO Debug Acknowledge */
1379         while (delay < max_delay) {
1380                 pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
1381
1382                 if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
1383                         return 0;
1384
1385                 /* udelay cannot be used if delay is more than a few milliseconds */
1386                 if ((delay / 1000) > MAX_UDELAY_MS)
1387                         mdelay(delay / 1000);
1388                 else
1389                         udelay(delay);
1390
1391                 delay += delay;
1392         }
1393         return -EIO;
1394 }
1395
1396 /**
1397  * ipr_get_ldump_data_section - Dump IOA memory
1398  * @ioa_cfg:                    ioa config struct
1399  * @start_addr:                 adapter address to dump
1400  * @dest:                               destination kernel buffer
1401  * @length_in_words:    length to dump in 4 byte words
1402  *
1403  * Return value:
1404  *      0 on success / -EIO on failure
1405  **/
1406 static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
1407                                       u32 start_addr,
1408                                       __be32 *dest, u32 length_in_words)
1409 {
1410         volatile u32 temp_pcii_reg;
1411         int i, delay = 0;
1412
1413         /* Write IOA interrupt reg starting LDUMP state  */
1414         writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
1415                ioa_cfg->regs.set_uproc_interrupt_reg);
1416
1417         /* Wait for IO debug acknowledge */
1418         if (ipr_wait_iodbg_ack(ioa_cfg,
1419                                IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
1420                 dev_err(&ioa_cfg->pdev->dev,
1421                         "IOA dump long data transfer timeout\n");
1422                 return -EIO;
1423         }
1424
1425         /* Signal LDUMP interlocked - clear IO debug ack */
1426         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1427                ioa_cfg->regs.clr_interrupt_reg);
1428
1429         /* Write Mailbox with starting address */
1430         writel(start_addr, ioa_cfg->ioa_mailbox);
1431
1432         /* Signal address valid - clear IOA Reset alert */
1433         writel(IPR_UPROCI_RESET_ALERT,
1434                ioa_cfg->regs.clr_uproc_interrupt_reg);
1435
1436         for (i = 0; i < length_in_words; i++) {
1437                 /* Wait for IO debug acknowledge */
1438                 if (ipr_wait_iodbg_ack(ioa_cfg,
1439                                        IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
1440                         dev_err(&ioa_cfg->pdev->dev,
1441                                 "IOA dump short data transfer timeout\n");
1442                         return -EIO;
1443                 }
1444
1445                 /* Read data from mailbox and increment destination pointer */
1446                 *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
1447                 dest++;
1448
1449                 /* For all but the last word of data, signal data received */
1450                 if (i < (length_in_words - 1)) {
1451                         /* Signal dump data received - Clear IO debug Ack */
1452                         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1453                                ioa_cfg->regs.clr_interrupt_reg);
1454                 }
1455         }
1456
1457         /* Signal end of block transfer. Set reset alert then clear IO debug ack */
1458         writel(IPR_UPROCI_RESET_ALERT,
1459                ioa_cfg->regs.set_uproc_interrupt_reg);
1460
1461         writel(IPR_UPROCI_IO_DEBUG_ALERT,
1462                ioa_cfg->regs.clr_uproc_interrupt_reg);
1463
1464         /* Signal dump data received - Clear IO debug Ack */
1465         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1466                ioa_cfg->regs.clr_interrupt_reg);
1467
1468         /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
1469         while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
1470                 temp_pcii_reg =
1471                     readl(ioa_cfg->regs.sense_uproc_interrupt_reg);
1472
1473                 if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
1474                         return 0;
1475
1476                 udelay(10);
1477                 delay += 10;
1478         }
1479
1480         return 0;
1481 }
1482
1483 #ifdef CONFIG_SCSI_IPR_DUMP
1484 /**
1485  * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
1486  * @ioa_cfg:            ioa config struct
1487  * @pci_address:        adapter address
1488  * @length:                     length of data to copy
1489  *
1490  * Copy data from PCI adapter to kernel buffer.
1491  * Note: length MUST be a 4 byte multiple
1492  * Return value:
1493  *      0 on success / other on failure
1494  **/
1495 static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
1496                         unsigned long pci_address, u32 length)
1497 {
1498         int bytes_copied = 0;
1499         int cur_len, rc, rem_len, rem_page_len;
1500         __be32 *page;
1501         unsigned long lock_flags = 0;
1502         struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
1503
1504         while (bytes_copied < length &&
1505                (ioa_dump->hdr.len + bytes_copied) < IPR_MAX_IOA_DUMP_SIZE) {
1506                 if (ioa_dump->page_offset >= PAGE_SIZE ||
1507                     ioa_dump->page_offset == 0) {
1508                         page = (__be32 *)__get_free_page(GFP_ATOMIC);
1509
1510                         if (!page) {
1511                                 ipr_trace;
1512                                 return bytes_copied;
1513                         }
1514
1515                         ioa_dump->page_offset = 0;
1516                         ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
1517                         ioa_dump->next_page_index++;
1518                 } else
1519                         page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
1520
1521                 rem_len = length - bytes_copied;
1522                 rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
1523                 cur_len = min(rem_len, rem_page_len);
1524
1525                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1526                 if (ioa_cfg->sdt_state == ABORT_DUMP) {
1527                         rc = -EIO;
1528                 } else {
1529                         rc = ipr_get_ldump_data_section(ioa_cfg,
1530                                                         pci_address + bytes_copied,
1531                                                         &page[ioa_dump->page_offset / 4],
1532                                                         (cur_len / sizeof(u32)));
1533                 }
1534                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1535
1536                 if (!rc) {
1537                         ioa_dump->page_offset += cur_len;
1538                         bytes_copied += cur_len;
1539                 } else {
1540                         ipr_trace;
1541                         break;
1542                 }
1543                 schedule();
1544         }
1545
1546         return bytes_copied;
1547 }
1548
1549 /**
1550  * ipr_init_dump_entry_hdr - Initialize a dump entry header.
1551  * @hdr:        dump entry header struct
1552  *
1553  * Return value:
1554  *      nothing
1555  **/
1556 static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
1557 {
1558         hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
1559         hdr->num_elems = 1;
1560         hdr->offset = sizeof(*hdr);
1561         hdr->status = IPR_DUMP_STATUS_SUCCESS;
1562 }
1563
1564 /**
1565  * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
1566  * @ioa_cfg:    ioa config struct
1567  * @driver_dump:        driver dump struct
1568  *
1569  * Return value:
1570  *      nothing
1571  **/
1572 static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
1573                                    struct ipr_driver_dump *driver_dump)
1574 {
1575         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
1576
1577         ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
1578         driver_dump->ioa_type_entry.hdr.len =
1579                 sizeof(struct ipr_dump_ioa_type_entry) -
1580                 sizeof(struct ipr_dump_entry_header);
1581         driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
1582         driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
1583         driver_dump->ioa_type_entry.type = ioa_cfg->type;
1584         driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
1585                 (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
1586                 ucode_vpd->minor_release[1];
1587         driver_dump->hdr.num_entries++;
1588 }
1589
1590 /**
1591  * ipr_dump_version_data - Fill in the driver version in the dump.
1592  * @ioa_cfg:    ioa config struct
1593  * @driver_dump:        driver dump struct
1594  *
1595  * Return value:
1596  *      nothing
1597  **/
1598 static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
1599                                   struct ipr_driver_dump *driver_dump)
1600 {
1601         ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
1602         driver_dump->version_entry.hdr.len =
1603                 sizeof(struct ipr_dump_version_entry) -
1604                 sizeof(struct ipr_dump_entry_header);
1605         driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
1606         driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
1607         strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
1608         driver_dump->hdr.num_entries++;
1609 }
1610
1611 /**
1612  * ipr_dump_trace_data - Fill in the IOA trace in the dump.
1613  * @ioa_cfg:    ioa config struct
1614  * @driver_dump:        driver dump struct
1615  *
1616  * Return value:
1617  *      nothing
1618  **/
1619 static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
1620                                    struct ipr_driver_dump *driver_dump)
1621 {
1622         ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
1623         driver_dump->trace_entry.hdr.len =
1624                 sizeof(struct ipr_dump_trace_entry) -
1625                 sizeof(struct ipr_dump_entry_header);
1626         driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
1627         driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
1628         memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
1629         driver_dump->hdr.num_entries++;
1630 }
1631
1632 /**
1633  * ipr_dump_location_data - Fill in the IOA location in the dump.
1634  * @ioa_cfg:    ioa config struct
1635  * @driver_dump:        driver dump struct
1636  *
1637  * Return value:
1638  *      nothing
1639  **/
1640 static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
1641                                    struct ipr_driver_dump *driver_dump)
1642 {
1643         ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
1644         driver_dump->location_entry.hdr.len =
1645                 sizeof(struct ipr_dump_location_entry) -
1646                 sizeof(struct ipr_dump_entry_header);
1647         driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
1648         driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
1649         strcpy(driver_dump->location_entry.location, ioa_cfg->pdev->dev.bus_id);
1650         driver_dump->hdr.num_entries++;
1651 }
1652
1653 /**
1654  * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
1655  * @ioa_cfg:    ioa config struct
1656  * @dump:               dump struct
1657  *
1658  * Return value:
1659  *      nothing
1660  **/
1661 static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
1662 {
1663         unsigned long start_addr, sdt_word;
1664         unsigned long lock_flags = 0;
1665         struct ipr_driver_dump *driver_dump = &dump->driver_dump;
1666         struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
1667         u32 num_entries, start_off, end_off;
1668         u32 bytes_to_copy, bytes_copied, rc;
1669         struct ipr_sdt *sdt;
1670         int i;
1671
1672         ENTER;
1673
1674         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1675
1676         if (ioa_cfg->sdt_state != GET_DUMP) {
1677                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1678                 return;
1679         }
1680
1681         start_addr = readl(ioa_cfg->ioa_mailbox);
1682
1683         if (!ipr_sdt_is_fmt2(start_addr)) {
1684                 dev_err(&ioa_cfg->pdev->dev,
1685                         "Invalid dump table format: %lx\n", start_addr);
1686                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1687                 return;
1688         }
1689
1690         dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
1691
1692         driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
1693
1694         /* Initialize the overall dump header */
1695         driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
1696         driver_dump->hdr.num_entries = 1;
1697         driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
1698         driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
1699         driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
1700         driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
1701
1702         ipr_dump_version_data(ioa_cfg, driver_dump);
1703         ipr_dump_location_data(ioa_cfg, driver_dump);
1704         ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
1705         ipr_dump_trace_data(ioa_cfg, driver_dump);
1706
1707         /* Update dump_header */
1708         driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
1709
1710         /* IOA Dump entry */
1711         ipr_init_dump_entry_hdr(&ioa_dump->hdr);
1712         ioa_dump->format = IPR_SDT_FMT2;
1713         ioa_dump->hdr.len = 0;
1714         ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
1715         ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
1716
1717         /* First entries in sdt are actually a list of dump addresses and
1718          lengths to gather the real dump data.  sdt represents the pointer
1719          to the ioa generated dump table.  Dump data will be extracted based
1720          on entries in this table */
1721         sdt = &ioa_dump->sdt;
1722
1723         rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
1724                                         sizeof(struct ipr_sdt) / sizeof(__be32));
1725
1726         /* Smart Dump table is ready to use and the first entry is valid */
1727         if (rc || (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE)) {
1728                 dev_err(&ioa_cfg->pdev->dev,
1729                         "Dump of IOA failed. Dump table not valid: %d, %X.\n",
1730                         rc, be32_to_cpu(sdt->hdr.state));
1731                 driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
1732                 ioa_cfg->sdt_state = DUMP_OBTAINED;
1733                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1734                 return;
1735         }
1736
1737         num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
1738
1739         if (num_entries > IPR_NUM_SDT_ENTRIES)
1740                 num_entries = IPR_NUM_SDT_ENTRIES;
1741
1742         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1743
1744         for (i = 0; i < num_entries; i++) {
1745                 if (ioa_dump->hdr.len > IPR_MAX_IOA_DUMP_SIZE) {
1746                         driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
1747                         break;
1748                 }
1749
1750                 if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
1751                         sdt_word = be32_to_cpu(sdt->entry[i].bar_str_offset);
1752                         start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
1753                         end_off = be32_to_cpu(sdt->entry[i].end_offset);
1754
1755                         if (ipr_sdt_is_fmt2(sdt_word) && sdt_word) {
1756                                 bytes_to_copy = end_off - start_off;
1757                                 if (bytes_to_copy > IPR_MAX_IOA_DUMP_SIZE) {
1758                                         sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
1759                                         continue;
1760                                 }
1761
1762                                 /* Copy data from adapter to driver buffers */
1763                                 bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
1764                                                             bytes_to_copy);
1765
1766                                 ioa_dump->hdr.len += bytes_copied;
1767
1768                                 if (bytes_copied != bytes_to_copy) {
1769                                         driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
1770                                         break;
1771                                 }
1772                         }
1773                 }
1774         }
1775
1776         dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
1777
1778         /* Update dump_header */
1779         driver_dump->hdr.len += ioa_dump->hdr.len;
1780         wmb();
1781         ioa_cfg->sdt_state = DUMP_OBTAINED;
1782         LEAVE;
1783 }
1784
1785 #else
1786 #define ipr_get_ioa_dump(ioa_cfg, dump) do { } while(0)
1787 #endif
1788
1789 /**
1790  * ipr_release_dump - Free adapter dump memory
1791  * @kref:       kref struct
1792  *
1793  * Return value:
1794  *      nothing
1795  **/
1796 static void ipr_release_dump(struct kref *kref)
1797 {
1798         struct ipr_dump *dump = container_of(kref,struct ipr_dump,kref);
1799         struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
1800         unsigned long lock_flags = 0;
1801         int i;
1802
1803         ENTER;
1804         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1805         ioa_cfg->dump = NULL;
1806         ioa_cfg->sdt_state = INACTIVE;
1807         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1808
1809         for (i = 0; i < dump->ioa_dump.next_page_index; i++)
1810                 free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
1811
1812         kfree(dump);
1813         LEAVE;
1814 }
1815
1816 /**
1817  * ipr_worker_thread - Worker thread
1818  * @data:               ioa config struct
1819  *
1820  * Called at task level from a work thread. This function takes care
1821  * of adding and removing device from the mid-layer as configuration
1822  * changes are detected by the adapter.
1823  *
1824  * Return value:
1825  *      nothing
1826  **/
1827 static void ipr_worker_thread(void *data)
1828 {
1829         unsigned long lock_flags;
1830         struct ipr_resource_entry *res;
1831         struct scsi_device *sdev;
1832         struct ipr_dump *dump;
1833         struct ipr_ioa_cfg *ioa_cfg = data;
1834         u8 bus, target, lun;
1835         int did_work;
1836
1837         ENTER;
1838         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1839
1840         if (ioa_cfg->sdt_state == GET_DUMP) {
1841                 dump = ioa_cfg->dump;
1842                 if (!dump) {
1843                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1844                         return;
1845                 }
1846                 kref_get(&dump->kref);
1847                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1848                 ipr_get_ioa_dump(ioa_cfg, dump);
1849                 kref_put(&dump->kref, ipr_release_dump);
1850
1851                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1852                 if (ioa_cfg->sdt_state == DUMP_OBTAINED)
1853                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1854                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1855                 return;
1856         }
1857
1858 restart:
1859         do {
1860                 did_work = 0;
1861                 if (!ioa_cfg->allow_cmds || !ioa_cfg->allow_ml_add_del) {
1862                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1863                         return;
1864                 }
1865
1866                 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1867                         if (res->del_from_ml && res->sdev) {
1868                                 did_work = 1;
1869                                 sdev = res->sdev;
1870                                 if (!scsi_device_get(sdev)) {
1871                                         res->sdev = NULL;
1872                                         list_move_tail(&res->queue, &ioa_cfg->free_res_q);
1873                                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1874                                         scsi_remove_device(sdev);
1875                                         scsi_device_put(sdev);
1876                                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1877                                 }
1878                                 break;
1879                         }
1880                 }
1881         } while(did_work);
1882
1883         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1884                 if (res->add_to_ml) {
1885                         bus = res->cfgte.res_addr.bus;
1886                         target = res->cfgte.res_addr.target;
1887                         lun = res->cfgte.res_addr.lun;
1888                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1889                         scsi_add_device(ioa_cfg->host, bus, target, lun);
1890                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1891                         goto restart;
1892                 }
1893         }
1894
1895         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1896         kobject_uevent(&ioa_cfg->host->shost_classdev.kobj, KOBJ_CHANGE, NULL);
1897         LEAVE;
1898 }
1899
1900 #ifdef CONFIG_SCSI_IPR_TRACE
1901 /**
1902  * ipr_read_trace - Dump the adapter trace
1903  * @kobj:               kobject struct
1904  * @buf:                buffer
1905  * @off:                offset
1906  * @count:              buffer size
1907  *
1908  * Return value:
1909  *      number of bytes printed to buffer
1910  **/
1911 static ssize_t ipr_read_trace(struct kobject *kobj, char *buf,
1912                               loff_t off, size_t count)
1913 {
1914         struct class_device *cdev = container_of(kobj,struct class_device,kobj);
1915         struct Scsi_Host *shost = class_to_shost(cdev);
1916         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
1917         unsigned long lock_flags = 0;
1918         int size = IPR_TRACE_SIZE;
1919         char *src = (char *)ioa_cfg->trace;
1920
1921         if (off > size)
1922                 return 0;
1923         if (off + count > size) {
1924                 size -= off;
1925                 count = size;
1926         }
1927
1928         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1929         memcpy(buf, &src[off], count);
1930         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1931         return count;
1932 }
1933
1934 static struct bin_attribute ipr_trace_attr = {
1935         .attr = {
1936                 .name = "trace",
1937                 .mode = S_IRUGO,
1938         },
1939         .size = 0,
1940         .read = ipr_read_trace,
1941 };
1942 #endif
1943
1944 /**
1945  * ipr_show_fw_version - Show the firmware version
1946  * @class_dev:  class device struct
1947  * @buf:                buffer
1948  *
1949  * Return value:
1950  *      number of bytes printed to buffer
1951  **/
1952 static ssize_t ipr_show_fw_version(struct class_device *class_dev, char *buf)
1953 {
1954         struct Scsi_Host *shost = class_to_shost(class_dev);
1955         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
1956         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
1957         unsigned long lock_flags = 0;
1958         int len;
1959
1960         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1961         len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
1962                        ucode_vpd->major_release, ucode_vpd->card_type,
1963                        ucode_vpd->minor_release[0],
1964                        ucode_vpd->minor_release[1]);
1965         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1966         return len;
1967 }
1968
1969 static struct class_device_attribute ipr_fw_version_attr = {
1970         .attr = {
1971                 .name =         "fw_version",
1972                 .mode =         S_IRUGO,
1973         },
1974         .show = ipr_show_fw_version,
1975 };
1976
1977 /**
1978  * ipr_show_log_level - Show the adapter's error logging level
1979  * @class_dev:  class device struct
1980  * @buf:                buffer
1981  *
1982  * Return value:
1983  *      number of bytes printed to buffer
1984  **/
1985 static ssize_t ipr_show_log_level(struct class_device *class_dev, char *buf)
1986 {
1987         struct Scsi_Host *shost = class_to_shost(class_dev);
1988         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
1989         unsigned long lock_flags = 0;
1990         int len;
1991
1992         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1993         len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
1994         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1995         return len;
1996 }
1997
1998 /**
1999  * ipr_store_log_level - Change the adapter's error logging level
2000  * @class_dev:  class device struct
2001  * @buf:                buffer
2002  *
2003  * Return value:
2004  *      number of bytes printed to buffer
2005  **/
2006 static ssize_t ipr_store_log_level(struct class_device *class_dev,
2007                                    const char *buf, size_t count)
2008 {
2009         struct Scsi_Host *shost = class_to_shost(class_dev);
2010         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2011         unsigned long lock_flags = 0;
2012
2013         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2014         ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
2015         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2016         return strlen(buf);
2017 }
2018
2019 static struct class_device_attribute ipr_log_level_attr = {
2020         .attr = {
2021                 .name =         "log_level",
2022                 .mode =         S_IRUGO | S_IWUSR,
2023         },
2024         .show = ipr_show_log_level,
2025         .store = ipr_store_log_level
2026 };
2027
2028 /**
2029  * ipr_store_diagnostics - IOA Diagnostics interface
2030  * @class_dev:  class_device struct
2031  * @buf:                buffer
2032  * @count:              buffer size
2033  *
2034  * This function will reset the adapter and wait a reasonable
2035  * amount of time for any errors that the adapter might log.
2036  *
2037  * Return value:
2038  *      count on success / other on failure
2039  **/
2040 static ssize_t ipr_store_diagnostics(struct class_device *class_dev,
2041                                      const char *buf, size_t count)
2042 {
2043         struct Scsi_Host *shost = class_to_shost(class_dev);
2044         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2045         unsigned long lock_flags = 0;
2046         int rc = count;
2047
2048         if (!capable(CAP_SYS_ADMIN))
2049                 return -EACCES;
2050
2051         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2052         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2053         ioa_cfg->errors_logged = 0;
2054         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2055
2056         if (ioa_cfg->in_reset_reload) {
2057                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2058                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2059
2060                 /* Wait for a second for any errors to be logged */
2061                 msleep(1000);
2062         } else {
2063                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2064                 return -EIO;
2065         }
2066
2067         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2068         if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
2069                 rc = -EIO;
2070         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2071
2072         return rc;
2073 }
2074
2075 static struct class_device_attribute ipr_diagnostics_attr = {
2076         .attr = {
2077                 .name =         "run_diagnostics",
2078                 .mode =         S_IWUSR,
2079         },
2080         .store = ipr_store_diagnostics
2081 };
2082
2083 /**
2084  * ipr_store_reset_adapter - Reset the adapter
2085  * @class_dev:  class_device struct
2086  * @buf:                buffer
2087  * @count:              buffer size
2088  *
2089  * This function will reset the adapter.
2090  *
2091  * Return value:
2092  *      count on success / other on failure
2093  **/
2094 static ssize_t ipr_store_reset_adapter(struct class_device *class_dev,
2095                                        const char *buf, size_t count)
2096 {
2097         struct Scsi_Host *shost = class_to_shost(class_dev);
2098         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2099         unsigned long lock_flags;
2100         int result = count;
2101
2102         if (!capable(CAP_SYS_ADMIN))
2103                 return -EACCES;
2104
2105         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2106         if (!ioa_cfg->in_reset_reload)
2107                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2108         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2109         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2110
2111         return result;
2112 }
2113
2114 static struct class_device_attribute ipr_ioa_reset_attr = {
2115         .attr = {
2116                 .name =         "reset_host",
2117                 .mode =         S_IWUSR,
2118         },
2119         .store = ipr_store_reset_adapter
2120 };
2121
2122 /**
2123  * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
2124  * @buf_len:            buffer length
2125  *
2126  * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
2127  * list to use for microcode download
2128  *
2129  * Return value:
2130  *      pointer to sglist / NULL on failure
2131  **/
2132 static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
2133 {
2134         int sg_size, order, bsize_elem, num_elem, i, j;
2135         struct ipr_sglist *sglist;
2136         struct scatterlist *scatterlist;
2137         struct page *page;
2138
2139         /* Get the minimum size per scatter/gather element */
2140         sg_size = buf_len / (IPR_MAX_SGLIST - 1);
2141
2142         /* Get the actual size per element */
2143         order = get_order(sg_size);
2144
2145         /* Determine the actual number of bytes per element */
2146         bsize_elem = PAGE_SIZE * (1 << order);
2147
2148         /* Determine the actual number of sg entries needed */
2149         if (buf_len % bsize_elem)
2150                 num_elem = (buf_len / bsize_elem) + 1;
2151         else
2152                 num_elem = buf_len / bsize_elem;
2153
2154         /* Allocate a scatter/gather list for the DMA */
2155         sglist = kmalloc(sizeof(struct ipr_sglist) +
2156                          (sizeof(struct scatterlist) * (num_elem - 1)),
2157                          GFP_KERNEL);
2158
2159         if (sglist == NULL) {
2160                 ipr_trace;
2161                 return NULL;
2162         }
2163
2164         memset(sglist, 0, sizeof(struct ipr_sglist) +
2165                (sizeof(struct scatterlist) * (num_elem - 1)));
2166
2167         scatterlist = sglist->scatterlist;
2168
2169         sglist->order = order;
2170         sglist->num_sg = num_elem;
2171
2172         /* Allocate a bunch of sg elements */
2173         for (i = 0; i < num_elem; i++) {
2174                 page = alloc_pages(GFP_KERNEL, order);
2175                 if (!page) {
2176                         ipr_trace;
2177
2178                         /* Free up what we already allocated */
2179                         for (j = i - 1; j >= 0; j--)
2180                                 __free_pages(scatterlist[j].page, order);
2181                         kfree(sglist);
2182                         return NULL;
2183                 }
2184
2185                 scatterlist[i].page = page;
2186         }
2187
2188         return sglist;
2189 }
2190
2191 /**
2192  * ipr_free_ucode_buffer - Frees a microcode download buffer
2193  * @p_dnld:             scatter/gather list pointer
2194  *
2195  * Free a DMA'able ucode download buffer previously allocated with
2196  * ipr_alloc_ucode_buffer
2197  *
2198  * Return value:
2199  *      nothing
2200  **/
2201 static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
2202 {
2203         int i;
2204
2205         for (i = 0; i < sglist->num_sg; i++)
2206                 __free_pages(sglist->scatterlist[i].page, sglist->order);
2207
2208         kfree(sglist);
2209 }
2210
2211 /**
2212  * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
2213  * @sglist:             scatter/gather list pointer
2214  * @buffer:             buffer pointer
2215  * @len:                buffer length
2216  *
2217  * Copy a microcode image from a user buffer into a buffer allocated by
2218  * ipr_alloc_ucode_buffer
2219  *
2220  * Return value:
2221  *      0 on success / other on failure
2222  **/
2223 static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
2224                                  u8 *buffer, u32 len)
2225 {
2226         int bsize_elem, i, result = 0;
2227         struct scatterlist *scatterlist;
2228         void *kaddr;
2229
2230         /* Determine the actual number of bytes per element */
2231         bsize_elem = PAGE_SIZE * (1 << sglist->order);
2232
2233         scatterlist = sglist->scatterlist;
2234
2235         for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
2236                 kaddr = kmap(scatterlist[i].page);
2237                 memcpy(kaddr, buffer, bsize_elem);
2238                 kunmap(scatterlist[i].page);
2239
2240                 scatterlist[i].length = bsize_elem;
2241
2242                 if (result != 0) {
2243                         ipr_trace;
2244                         return result;
2245                 }
2246         }
2247
2248         if (len % bsize_elem) {
2249                 kaddr = kmap(scatterlist[i].page);
2250                 memcpy(kaddr, buffer, len % bsize_elem);
2251                 kunmap(scatterlist[i].page);
2252
2253                 scatterlist[i].length = len % bsize_elem;
2254         }
2255
2256         sglist->buffer_len = len;
2257         return result;
2258 }
2259
2260 /**
2261  * ipr_map_ucode_buffer - Map a microcode download buffer
2262  * @ipr_cmd:    ipr command struct
2263  * @sglist:             scatter/gather list
2264  * @len:                total length of download buffer
2265  *
2266  * Maps a microcode download scatter/gather list for DMA and
2267  * builds the IOADL.
2268  *
2269  * Return value:
2270  *      0 on success / -EIO on failure
2271  **/
2272 static int ipr_map_ucode_buffer(struct ipr_cmnd *ipr_cmd,
2273                                 struct ipr_sglist *sglist, int len)
2274 {
2275         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2276         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
2277         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
2278         struct scatterlist *scatterlist = sglist->scatterlist;
2279         int i;
2280
2281         ipr_cmd->dma_use_sg = pci_map_sg(ioa_cfg->pdev, scatterlist,
2282                                          sglist->num_sg, DMA_TO_DEVICE);
2283
2284         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
2285         ioarcb->write_data_transfer_length = cpu_to_be32(len);
2286         ioarcb->write_ioadl_len =
2287                 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
2288
2289         for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
2290                 ioadl[i].flags_and_data_len =
2291                         cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
2292                 ioadl[i].address =
2293                         cpu_to_be32(sg_dma_address(&scatterlist[i]));
2294         }
2295
2296         if (likely(ipr_cmd->dma_use_sg)) {
2297                 ioadl[i-1].flags_and_data_len |=
2298                         cpu_to_be32(IPR_IOADL_FLAGS_LAST);
2299         }
2300         else {
2301                 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
2302                 return -EIO;
2303         }
2304
2305         return 0;
2306 }
2307
2308 /**
2309  * ipr_store_update_fw - Update the firmware on the adapter
2310  * @class_dev:  class_device struct
2311  * @buf:                buffer
2312  * @count:              buffer size
2313  *
2314  * This function will update the firmware on the adapter.
2315  *
2316  * Return value:
2317  *      count on success / other on failure
2318  **/
2319 static ssize_t ipr_store_update_fw(struct class_device *class_dev,
2320                                        const char *buf, size_t count)
2321 {
2322         struct Scsi_Host *shost = class_to_shost(class_dev);
2323         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2324         struct ipr_ucode_image_header *image_hdr;
2325         const struct firmware *fw_entry;
2326         struct ipr_sglist *sglist;
2327         unsigned long lock_flags;
2328         char fname[100];
2329         char *src;
2330         int len, result, dnld_size;
2331
2332         if (!capable(CAP_SYS_ADMIN))
2333                 return -EACCES;
2334
2335         len = snprintf(fname, 99, "%s", buf);
2336         fname[len-1] = '\0';
2337
2338         if(request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
2339                 dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
2340                 return -EIO;
2341         }
2342
2343         image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
2344
2345         if (be32_to_cpu(image_hdr->header_length) > fw_entry->size ||
2346             (ioa_cfg->vpd_cbs->page3_data.card_type &&
2347              ioa_cfg->vpd_cbs->page3_data.card_type != image_hdr->card_type)) {
2348                 dev_err(&ioa_cfg->pdev->dev, "Invalid microcode buffer\n");
2349                 release_firmware(fw_entry);
2350                 return -EINVAL;
2351         }
2352
2353         src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
2354         dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
2355         sglist = ipr_alloc_ucode_buffer(dnld_size);
2356
2357         if (!sglist) {
2358                 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
2359                 release_firmware(fw_entry);
2360                 return -ENOMEM;
2361         }
2362
2363         result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
2364
2365         if (result) {
2366                 dev_err(&ioa_cfg->pdev->dev,
2367                         "Microcode buffer copy to DMA buffer failed\n");
2368                 ipr_free_ucode_buffer(sglist);
2369                 release_firmware(fw_entry);
2370                 return result;
2371         }
2372
2373         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2374
2375         if (ioa_cfg->ucode_sglist) {
2376                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2377                 dev_err(&ioa_cfg->pdev->dev,
2378                         "Microcode download already in progress\n");
2379                 ipr_free_ucode_buffer(sglist);
2380                 release_firmware(fw_entry);
2381                 return -EIO;
2382         }
2383
2384         ioa_cfg->ucode_sglist = sglist;
2385         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2386         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2387         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2388
2389         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2390         ioa_cfg->ucode_sglist = NULL;
2391         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2392
2393         ipr_free_ucode_buffer(sglist);
2394         release_firmware(fw_entry);
2395
2396         return count;
2397 }
2398
2399 static struct class_device_attribute ipr_update_fw_attr = {
2400         .attr = {
2401                 .name =         "update_fw",
2402                 .mode =         S_IWUSR,
2403         },
2404         .store = ipr_store_update_fw
2405 };
2406
2407 static struct class_device_attribute *ipr_ioa_attrs[] = {
2408         &ipr_fw_version_attr,
2409         &ipr_log_level_attr,
2410         &ipr_diagnostics_attr,
2411         &ipr_ioa_reset_attr,
2412         &ipr_update_fw_attr,
2413         NULL,
2414 };
2415
2416 #ifdef CONFIG_SCSI_IPR_DUMP
2417 /**
2418  * ipr_read_dump - Dump the adapter
2419  * @kobj:               kobject struct
2420  * @buf:                buffer
2421  * @off:                offset
2422  * @count:              buffer size
2423  *
2424  * Return value:
2425  *      number of bytes printed to buffer
2426  **/
2427 static ssize_t ipr_read_dump(struct kobject *kobj, char *buf,
2428                               loff_t off, size_t count)
2429 {
2430         struct class_device *cdev = container_of(kobj,struct class_device,kobj);
2431         struct Scsi_Host *shost = class_to_shost(cdev);
2432         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2433         struct ipr_dump *dump;
2434         unsigned long lock_flags = 0;
2435         char *src;
2436         int len;
2437         size_t rc = count;
2438
2439         if (!capable(CAP_SYS_ADMIN))
2440                 return -EACCES;
2441
2442         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2443         dump = ioa_cfg->dump;
2444
2445         if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
2446                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2447                 return 0;
2448         }
2449         kref_get(&dump->kref);
2450         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2451
2452         if (off > dump->driver_dump.hdr.len) {
2453                 kref_put(&dump->kref, ipr_release_dump);
2454                 return 0;
2455         }
2456
2457         if (off + count > dump->driver_dump.hdr.len) {
2458                 count = dump->driver_dump.hdr.len - off;
2459                 rc = count;
2460         }
2461
2462         if (count && off < sizeof(dump->driver_dump)) {
2463                 if (off + count > sizeof(dump->driver_dump))
2464                         len = sizeof(dump->driver_dump) - off;
2465                 else
2466                         len = count;
2467                 src = (u8 *)&dump->driver_dump + off;
2468                 memcpy(buf, src, len);
2469                 buf += len;
2470                 off += len;
2471                 count -= len;
2472         }
2473
2474         off -= sizeof(dump->driver_dump);
2475
2476         if (count && off < offsetof(struct ipr_ioa_dump, ioa_data)) {
2477                 if (off + count > offsetof(struct ipr_ioa_dump, ioa_data))
2478                         len = offsetof(struct ipr_ioa_dump, ioa_data) - off;
2479                 else
2480                         len = count;
2481                 src = (u8 *)&dump->ioa_dump + off;
2482                 memcpy(buf, src, len);
2483                 buf += len;
2484                 off += len;
2485                 count -= len;
2486         }
2487
2488         off -= offsetof(struct ipr_ioa_dump, ioa_data);
2489
2490         while (count) {
2491                 if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
2492                         len = PAGE_ALIGN(off) - off;
2493                 else
2494                         len = count;
2495                 src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
2496                 src += off & ~PAGE_MASK;
2497                 memcpy(buf, src, len);
2498                 buf += len;
2499                 off += len;
2500                 count -= len;
2501         }
2502
2503         kref_put(&dump->kref, ipr_release_dump);
2504         return rc;
2505 }
2506
2507 /**
2508  * ipr_alloc_dump - Prepare for adapter dump
2509  * @ioa_cfg:    ioa config struct
2510  *
2511  * Return value:
2512  *      0 on success / other on failure
2513  **/
2514 static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
2515 {
2516         struct ipr_dump *dump;
2517         unsigned long lock_flags = 0;
2518
2519         ENTER;
2520         dump = kmalloc(sizeof(struct ipr_dump), GFP_KERNEL);
2521
2522         if (!dump) {
2523                 ipr_err("Dump memory allocation failed\n");
2524                 return -ENOMEM;
2525         }
2526
2527         memset(dump, 0, sizeof(struct ipr_dump));
2528         kref_init(&dump->kref);
2529         dump->ioa_cfg = ioa_cfg;
2530
2531         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2532
2533         if (INACTIVE != ioa_cfg->sdt_state) {
2534                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2535                 kfree(dump);
2536                 return 0;
2537         }
2538
2539         ioa_cfg->dump = dump;
2540         ioa_cfg->sdt_state = WAIT_FOR_DUMP;
2541         if (ioa_cfg->ioa_is_dead && !ioa_cfg->dump_taken) {
2542                 ioa_cfg->dump_taken = 1;
2543                 schedule_work(&ioa_cfg->work_q);
2544         }
2545         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2546
2547         LEAVE;
2548         return 0;
2549 }
2550
2551 /**
2552  * ipr_free_dump - Free adapter dump memory
2553  * @ioa_cfg:    ioa config struct
2554  *
2555  * Return value:
2556  *      0 on success / other on failure
2557  **/
2558 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
2559 {
2560         struct ipr_dump *dump;
2561         unsigned long lock_flags = 0;
2562
2563         ENTER;
2564
2565         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2566         dump = ioa_cfg->dump;
2567         if (!dump) {
2568                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2569                 return 0;
2570         }
2571
2572         ioa_cfg->dump = NULL;
2573         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2574
2575         kref_put(&dump->kref, ipr_release_dump);
2576
2577         LEAVE;
2578         return 0;
2579 }
2580
2581 /**
2582  * ipr_write_dump - Setup dump state of adapter
2583  * @kobj:               kobject struct
2584  * @buf:                buffer
2585  * @off:                offset
2586  * @count:              buffer size
2587  *
2588  * Return value:
2589  *      number of bytes printed to buffer
2590  **/
2591 static ssize_t ipr_write_dump(struct kobject *kobj, char *buf,
2592                               loff_t off, size_t count)
2593 {
2594         struct class_device *cdev = container_of(kobj,struct class_device,kobj);
2595         struct Scsi_Host *shost = class_to_shost(cdev);
2596         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2597         int rc;
2598
2599         if (!capable(CAP_SYS_ADMIN))
2600                 return -EACCES;
2601
2602         if (buf[0] == '1')
2603                 rc = ipr_alloc_dump(ioa_cfg);
2604         else if (buf[0] == '0')
2605                 rc = ipr_free_dump(ioa_cfg);
2606         else
2607                 return -EINVAL;
2608
2609         if (rc)
2610                 return rc;
2611         else
2612                 return count;
2613 }
2614
2615 static struct bin_attribute ipr_dump_attr = {
2616         .attr = {
2617                 .name = "dump",
2618                 .mode = S_IRUSR | S_IWUSR,
2619         },
2620         .size = 0,
2621         .read = ipr_read_dump,
2622         .write = ipr_write_dump
2623 };
2624 #else
2625 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
2626 #endif
2627
2628 /**
2629  * ipr_change_queue_depth - Change the device's queue depth
2630  * @sdev:       scsi device struct
2631  * @qdepth:     depth to set
2632  *
2633  * Return value:
2634  *      actual depth set
2635  **/
2636 static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth)
2637 {
2638         scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
2639         return sdev->queue_depth;
2640 }
2641
2642 /**
2643  * ipr_change_queue_type - Change the device's queue type
2644  * @dsev:               scsi device struct
2645  * @tag_type:   type of tags to use
2646  *
2647  * Return value:
2648  *      actual queue type set
2649  **/
2650 static int ipr_change_queue_type(struct scsi_device *sdev, int tag_type)
2651 {
2652         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
2653         struct ipr_resource_entry *res;
2654         unsigned long lock_flags = 0;
2655
2656         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2657         res = (struct ipr_resource_entry *)sdev->hostdata;
2658
2659         if (res) {
2660                 if (ipr_is_gscsi(res) && sdev->tagged_supported) {
2661                         /*
2662                          * We don't bother quiescing the device here since the
2663                          * adapter firmware does it for us.
2664                          */
2665                         scsi_set_tag_type(sdev, tag_type);
2666
2667                         if (tag_type)
2668                                 scsi_activate_tcq(sdev, sdev->queue_depth);
2669                         else
2670                                 scsi_deactivate_tcq(sdev, sdev->queue_depth);
2671                 } else
2672                         tag_type = 0;
2673         } else
2674                 tag_type = 0;
2675
2676         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2677         return tag_type;
2678 }
2679
2680 /**
2681  * ipr_show_adapter_handle - Show the adapter's resource handle for this device
2682  * @dev:        device struct
2683  * @buf:        buffer
2684  *
2685  * Return value:
2686  *      number of bytes printed to buffer
2687  **/
2688 static ssize_t ipr_show_adapter_handle(struct device *dev, struct device_attribute *attr, char *buf)
2689 {
2690         struct scsi_device *sdev = to_scsi_device(dev);
2691         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
2692         struct ipr_resource_entry *res;
2693         unsigned long lock_flags = 0;
2694         ssize_t len = -ENXIO;
2695
2696         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2697         res = (struct ipr_resource_entry *)sdev->hostdata;
2698         if (res)
2699                 len = snprintf(buf, PAGE_SIZE, "%08X\n", res->cfgte.res_handle);
2700         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2701         return len;
2702 }
2703
2704 static struct device_attribute ipr_adapter_handle_attr = {
2705         .attr = {
2706                 .name =         "adapter_handle",
2707                 .mode =         S_IRUSR,
2708         },
2709         .show = ipr_show_adapter_handle
2710 };
2711
2712 static struct device_attribute *ipr_dev_attrs[] = {
2713         &ipr_adapter_handle_attr,
2714         NULL,
2715 };
2716
2717 /**
2718  * ipr_biosparam - Return the HSC mapping
2719  * @sdev:                       scsi device struct
2720  * @block_device:       block device pointer
2721  * @capacity:           capacity of the device
2722  * @parm:                       Array containing returned HSC values.
2723  *
2724  * This function generates the HSC parms that fdisk uses.
2725  * We want to make sure we return something that places partitions
2726  * on 4k boundaries for best performance with the IOA.
2727  *
2728  * Return value:
2729  *      0 on success
2730  **/
2731 static int ipr_biosparam(struct scsi_device *sdev,
2732                          struct block_device *block_device,
2733                          sector_t capacity, int *parm)
2734 {
2735         int heads, sectors;
2736         sector_t cylinders;
2737
2738         heads = 128;
2739         sectors = 32;
2740
2741         cylinders = capacity;
2742         sector_div(cylinders, (128 * 32));
2743
2744         /* return result */
2745         parm[0] = heads;
2746         parm[1] = sectors;
2747         parm[2] = cylinders;
2748
2749         return 0;
2750 }
2751
2752 /**
2753  * ipr_slave_destroy - Unconfigure a SCSI device
2754  * @sdev:       scsi device struct
2755  *
2756  * Return value:
2757  *      nothing
2758  **/
2759 static void ipr_slave_destroy(struct scsi_device *sdev)
2760 {
2761         struct ipr_resource_entry *res;
2762         struct ipr_ioa_cfg *ioa_cfg;
2763         unsigned long lock_flags = 0;
2764
2765         ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
2766
2767         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2768         res = (struct ipr_resource_entry *) sdev->hostdata;
2769         if (res) {
2770                 sdev->hostdata = NULL;
2771                 res->sdev = NULL;
2772         }
2773         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2774 }
2775
2776 /**
2777  * ipr_slave_configure - Configure a SCSI device
2778  * @sdev:       scsi device struct
2779  *
2780  * This function configures the specified scsi device.
2781  *
2782  * Return value:
2783  *      0 on success
2784  **/
2785 static int ipr_slave_configure(struct scsi_device *sdev)
2786 {
2787         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
2788         struct ipr_resource_entry *res;
2789         unsigned long lock_flags = 0;
2790
2791         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2792         res = sdev->hostdata;
2793         if (res) {
2794                 if (ipr_is_af_dasd_device(res))
2795                         sdev->type = TYPE_RAID;
2796                 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res))
2797                         sdev->scsi_level = 4;
2798                 if (ipr_is_vset_device(res)) {
2799                         sdev->timeout = IPR_VSET_RW_TIMEOUT;
2800                         blk_queue_max_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
2801                 }
2802                 if (IPR_IS_DASD_DEVICE(res->cfgte.std_inq_data))
2803                         sdev->allow_restart = 1;
2804                 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
2805         }
2806         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2807         return 0;
2808 }
2809
2810 /**
2811  * ipr_slave_alloc - Prepare for commands to a device.
2812  * @sdev:       scsi device struct
2813  *
2814  * This function saves a pointer to the resource entry
2815  * in the scsi device struct if the device exists. We
2816  * can then use this pointer in ipr_queuecommand when
2817  * handling new commands.
2818  *
2819  * Return value:
2820  *      0 on success
2821  **/
2822 static int ipr_slave_alloc(struct scsi_device *sdev)
2823 {
2824         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
2825         struct ipr_resource_entry *res;
2826         unsigned long lock_flags;
2827
2828         sdev->hostdata = NULL;
2829
2830         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2831
2832         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2833                 if ((res->cfgte.res_addr.bus == sdev->channel) &&
2834                     (res->cfgte.res_addr.target == sdev->id) &&
2835                     (res->cfgte.res_addr.lun == sdev->lun)) {
2836                         res->sdev = sdev;
2837                         res->add_to_ml = 0;
2838                         res->in_erp = 0;
2839                         sdev->hostdata = res;
2840                         res->needs_sync_complete = 1;
2841                         break;
2842                 }
2843         }
2844
2845         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2846
2847         return 0;
2848 }
2849
2850 /**
2851  * ipr_eh_host_reset - Reset the host adapter
2852  * @scsi_cmd:   scsi command struct
2853  *
2854  * Return value:
2855  *      SUCCESS / FAILED
2856  **/
2857 static int __ipr_eh_host_reset(struct scsi_cmnd * scsi_cmd)
2858 {
2859         struct ipr_ioa_cfg *ioa_cfg;
2860         int rc;
2861
2862         ENTER;
2863         ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
2864
2865         dev_err(&ioa_cfg->pdev->dev,
2866                 "Adapter being reset as a result of error recovery.\n");
2867
2868         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
2869                 ioa_cfg->sdt_state = GET_DUMP;
2870
2871         rc = ipr_reset_reload(ioa_cfg, IPR_SHUTDOWN_ABBREV);
2872
2873         LEAVE;
2874         return rc;
2875 }
2876
2877 static int ipr_eh_host_reset(struct scsi_cmnd * cmd)
2878 {
2879         int rc;
2880
2881         spin_lock_irq(cmd->device->host->host_lock);
2882         rc = __ipr_eh_host_reset(cmd);
2883         spin_unlock_irq(cmd->device->host->host_lock);
2884
2885         return rc;
2886 }
2887
2888 /**
2889  * ipr_eh_dev_reset - Reset the device
2890  * @scsi_cmd:   scsi command struct
2891  *
2892  * This function issues a device reset to the affected device.
2893  * A LUN reset will be sent to the device first. If that does
2894  * not work, a target reset will be sent.
2895  *
2896  * Return value:
2897  *      SUCCESS / FAILED
2898  **/
2899 static int __ipr_eh_dev_reset(struct scsi_cmnd * scsi_cmd)
2900 {
2901         struct ipr_cmnd *ipr_cmd;
2902         struct ipr_ioa_cfg *ioa_cfg;
2903         struct ipr_resource_entry *res;
2904         struct ipr_cmd_pkt *cmd_pkt;
2905         u32 ioasc;
2906
2907         ENTER;
2908         ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
2909         res = scsi_cmd->device->hostdata;
2910
2911         if (!res || (!ipr_is_gscsi(res) && !ipr_is_vset_device(res)))
2912                 return FAILED;
2913
2914         /*
2915          * If we are currently going through reset/reload, return failed. This will force the
2916          * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
2917          * reset to complete
2918          */
2919         if (ioa_cfg->in_reset_reload)
2920                 return FAILED;
2921         if (ioa_cfg->ioa_is_dead)
2922                 return FAILED;
2923
2924         list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
2925                 if (ipr_cmd->ioarcb.res_handle == res->cfgte.res_handle) {
2926                         if (ipr_cmd->scsi_cmd)
2927                                 ipr_cmd->done = ipr_scsi_eh_done;
2928                 }
2929         }
2930
2931         res->resetting_device = 1;
2932
2933         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
2934
2935         ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
2936         cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
2937         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
2938         cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
2939
2940         ipr_sdev_err(scsi_cmd->device, "Resetting device\n");
2941         ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
2942
2943         ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
2944
2945         res->resetting_device = 0;
2946
2947         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
2948
2949         LEAVE;
2950         return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
2951 }
2952
2953 static int ipr_eh_dev_reset(struct scsi_cmnd * cmd)
2954 {
2955         int rc;
2956
2957         spin_lock_irq(cmd->device->host->host_lock);
2958         rc = __ipr_eh_dev_reset(cmd);
2959         spin_unlock_irq(cmd->device->host->host_lock);
2960
2961         return rc;
2962 }
2963
2964 /**
2965  * ipr_bus_reset_done - Op done function for bus reset.
2966  * @ipr_cmd:    ipr command struct
2967  *
2968  * This function is the op done function for a bus reset
2969  *
2970  * Return value:
2971  *      none
2972  **/
2973 static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
2974 {
2975         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2976         struct ipr_resource_entry *res;
2977
2978         ENTER;
2979         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2980                 if (!memcmp(&res->cfgte.res_handle, &ipr_cmd->ioarcb.res_handle,
2981                             sizeof(res->cfgte.res_handle))) {
2982                         scsi_report_bus_reset(ioa_cfg->host, res->cfgte.res_addr.bus);
2983                         break;
2984                 }
2985         }
2986
2987         /*
2988          * If abort has not completed, indicate the reset has, else call the
2989          * abort's done function to wake the sleeping eh thread
2990          */
2991         if (ipr_cmd->sibling->sibling)
2992                 ipr_cmd->sibling->sibling = NULL;
2993         else
2994                 ipr_cmd->sibling->done(ipr_cmd->sibling);
2995
2996         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
2997         LEAVE;
2998 }
2999
3000 /**
3001  * ipr_abort_timeout - An abort task has timed out
3002  * @ipr_cmd:    ipr command struct
3003  *
3004  * This function handles when an abort task times out. If this
3005  * happens we issue a bus reset since we have resources tied
3006  * up that must be freed before returning to the midlayer.
3007  *
3008  * Return value:
3009  *      none
3010  **/
3011 static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
3012 {
3013         struct ipr_cmnd *reset_cmd;
3014         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3015         struct ipr_cmd_pkt *cmd_pkt;
3016         unsigned long lock_flags = 0;
3017
3018         ENTER;
3019         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3020         if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
3021                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3022                 return;
3023         }
3024
3025         ipr_sdev_err(ipr_cmd->u.sdev, "Abort timed out. Resetting bus\n");
3026         reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3027         ipr_cmd->sibling = reset_cmd;
3028         reset_cmd->sibling = ipr_cmd;
3029         reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
3030         cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
3031         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3032         cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
3033         cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
3034
3035         ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
3036         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3037         LEAVE;
3038 }
3039
3040 /**
3041  * ipr_cancel_op - Cancel specified op
3042  * @scsi_cmd:   scsi command struct
3043  *
3044  * This function cancels specified op.
3045  *
3046  * Return value:
3047  *      SUCCESS / FAILED
3048  **/
3049 static int ipr_cancel_op(struct scsi_cmnd * scsi_cmd)
3050 {
3051         struct ipr_cmnd *ipr_cmd;
3052         struct ipr_ioa_cfg *ioa_cfg;
3053         struct ipr_resource_entry *res;
3054         struct ipr_cmd_pkt *cmd_pkt;
3055         u32 ioasc;
3056         int op_found = 0;
3057
3058         ENTER;
3059         ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
3060         res = scsi_cmd->device->hostdata;
3061
3062         /* If we are currently going through reset/reload, return failed.
3063          * This will force the mid-layer to call ipr_eh_host_reset,
3064          * which will then go to sleep and wait for the reset to complete
3065          */
3066         if (ioa_cfg->in_reset_reload || ioa_cfg->ioa_is_dead)
3067                 return FAILED;
3068         if (!res || (!ipr_is_gscsi(res) && !ipr_is_vset_device(res)))
3069                 return FAILED;
3070
3071         list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
3072                 if (ipr_cmd->scsi_cmd == scsi_cmd) {
3073                         ipr_cmd->done = ipr_scsi_eh_done;
3074                         op_found = 1;
3075                         break;
3076                 }
3077         }
3078
3079         if (!op_found)
3080                 return SUCCESS;
3081
3082         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3083         ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
3084         cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
3085         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3086         cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
3087         ipr_cmd->u.sdev = scsi_cmd->device;
3088
3089         ipr_sdev_err(scsi_cmd->device, "Aborting command: %02X\n", scsi_cmd->cmnd[0]);
3090         ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
3091         ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3092
3093         /*
3094          * If the abort task timed out and we sent a bus reset, we will get
3095          * one the following responses to the abort
3096          */
3097         if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
3098                 ioasc = 0;
3099                 ipr_trace;
3100         }
3101
3102         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3103         res->needs_sync_complete = 1;
3104
3105         LEAVE;
3106         return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
3107 }
3108
3109 /**
3110  * ipr_eh_abort - Abort a single op
3111  * @scsi_cmd:   scsi command struct
3112  *
3113  * Return value:
3114  *      SUCCESS / FAILED
3115  **/
3116 static int ipr_eh_abort(struct scsi_cmnd * scsi_cmd)
3117 {
3118         unsigned long flags;
3119         int rc;
3120
3121         ENTER;
3122
3123         spin_lock_irqsave(scsi_cmd->device->host->host_lock, flags);
3124         rc = ipr_cancel_op(scsi_cmd);
3125         spin_unlock_irqrestore(scsi_cmd->device->host->host_lock, flags);
3126
3127         LEAVE;
3128         return rc;
3129 }
3130
3131 /**
3132  * ipr_handle_other_interrupt - Handle "other" interrupts
3133  * @ioa_cfg:    ioa config struct
3134  * @int_reg:    interrupt register
3135  *
3136  * Return value:
3137  *      IRQ_NONE / IRQ_HANDLED
3138  **/
3139 static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
3140                                               volatile u32 int_reg)
3141 {
3142         irqreturn_t rc = IRQ_HANDLED;
3143
3144         if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
3145                 /* Mask the interrupt */
3146                 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
3147
3148                 /* Clear the interrupt */
3149                 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.clr_interrupt_reg);
3150                 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
3151
3152                 list_del(&ioa_cfg->reset_cmd->queue);
3153                 del_timer(&ioa_cfg->reset_cmd->timer);
3154                 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
3155         } else {
3156                 if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
3157                         ioa_cfg->ioa_unit_checked = 1;
3158                 else
3159                         dev_err(&ioa_cfg->pdev->dev,
3160                                 "Permanent IOA failure. 0x%08X\n", int_reg);
3161
3162                 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3163                         ioa_cfg->sdt_state = GET_DUMP;
3164
3165                 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
3166                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3167         }
3168
3169         return rc;
3170 }
3171
3172 /**
3173  * ipr_isr - Interrupt service routine
3174  * @irq:        irq number
3175  * @devp:       pointer to ioa config struct
3176  * @regs:       pt_regs struct
3177  *
3178  * Return value:
3179  *      IRQ_NONE / IRQ_HANDLED
3180  **/
3181 static irqreturn_t ipr_isr(int irq, void *devp, struct pt_regs *regs)
3182 {
3183         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
3184         unsigned long lock_flags = 0;
3185         volatile u32 int_reg, int_mask_reg;
3186         u32 ioasc;
3187         u16 cmd_index;
3188         struct ipr_cmnd *ipr_cmd;
3189         irqreturn_t rc = IRQ_NONE;
3190
3191         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3192
3193         /* If interrupts are disabled, ignore the interrupt */
3194         if (!ioa_cfg->allow_interrupts) {
3195                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3196                 return IRQ_NONE;
3197         }
3198
3199         int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
3200         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
3201
3202         /* If an interrupt on the adapter did not occur, ignore it */
3203         if (unlikely((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0)) {
3204                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3205                 return IRQ_NONE;
3206         }
3207
3208         while (1) {
3209                 ipr_cmd = NULL;
3210
3211                 while ((be32_to_cpu(*ioa_cfg->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
3212                        ioa_cfg->toggle_bit) {
3213
3214                         cmd_index = (be32_to_cpu(*ioa_cfg->hrrq_curr) &
3215                                      IPR_HRRQ_REQ_RESP_HANDLE_MASK) >> IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
3216
3217                         if (unlikely(cmd_index >= IPR_NUM_CMD_BLKS)) {
3218                                 ioa_cfg->errors_logged++;
3219                                 dev_err(&ioa_cfg->pdev->dev, "Invalid response handle from IOA\n");
3220
3221                                 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3222                                         ioa_cfg->sdt_state = GET_DUMP;
3223
3224                                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3225                                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3226                                 return IRQ_HANDLED;
3227                         }
3228
3229                         ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
3230
3231                         ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3232
3233                         ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
3234
3235                         list_del(&ipr_cmd->queue);
3236                         del_timer(&ipr_cmd->timer);
3237                         ipr_cmd->done(ipr_cmd);
3238
3239                         rc = IRQ_HANDLED;
3240
3241                         if (ioa_cfg->hrrq_curr < ioa_cfg->hrrq_end) {
3242                                 ioa_cfg->hrrq_curr++;
3243                         } else {
3244                                 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
3245                                 ioa_cfg->toggle_bit ^= 1u;
3246                         }
3247                 }
3248
3249                 if (ipr_cmd != NULL) {
3250                         /* Clear the PCI interrupt */
3251                         writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg);
3252                         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
3253                 } else
3254                         break;
3255         }
3256
3257         if (unlikely(rc == IRQ_NONE))
3258                 rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
3259
3260         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3261         return rc;
3262 }
3263
3264 /**
3265  * ipr_build_ioadl - Build a scatter/gather list and map the buffer
3266  * @ioa_cfg:    ioa config struct
3267  * @ipr_cmd:    ipr command struct
3268  *
3269  * Return value:
3270  *      0 on success / -1 on failure
3271  **/
3272 static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
3273                            struct ipr_cmnd *ipr_cmd)
3274 {
3275         int i;
3276         struct scatterlist *sglist;
3277         u32 length;
3278         u32 ioadl_flags = 0;
3279         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3280         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
3281         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
3282
3283         length = scsi_cmd->request_bufflen;
3284
3285         if (length == 0)
3286                 return 0;
3287
3288         if (scsi_cmd->use_sg) {
3289                 ipr_cmd->dma_use_sg = pci_map_sg(ioa_cfg->pdev,
3290                                                  scsi_cmd->request_buffer,
3291                                                  scsi_cmd->use_sg,
3292                                                  scsi_cmd->sc_data_direction);
3293
3294                 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
3295                         ioadl_flags = IPR_IOADL_FLAGS_WRITE;
3296                         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3297                         ioarcb->write_data_transfer_length = cpu_to_be32(length);
3298                         ioarcb->write_ioadl_len =
3299                                 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3300                 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
3301                         ioadl_flags = IPR_IOADL_FLAGS_READ;
3302                         ioarcb->read_data_transfer_length = cpu_to_be32(length);
3303                         ioarcb->read_ioadl_len =
3304                                 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3305                 }
3306
3307                 sglist = scsi_cmd->request_buffer;
3308
3309                 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3310                         ioadl[i].flags_and_data_len =
3311                                 cpu_to_be32(ioadl_flags | sg_dma_len(&sglist[i]));
3312                         ioadl[i].address =
3313                                 cpu_to_be32(sg_dma_address(&sglist[i]));
3314                 }
3315
3316                 if (likely(ipr_cmd->dma_use_sg)) {
3317                         ioadl[i-1].flags_and_data_len |=
3318                                 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3319                         return 0;
3320                 } else
3321                         dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
3322         } else {
3323                 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
3324                         ioadl_flags = IPR_IOADL_FLAGS_WRITE;
3325                         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3326                         ioarcb->write_data_transfer_length = cpu_to_be32(length);
3327                         ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
3328                 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
3329                         ioadl_flags = IPR_IOADL_FLAGS_READ;
3330                         ioarcb->read_data_transfer_length = cpu_to_be32(length);
3331                         ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
3332                 }
3333
3334                 ipr_cmd->dma_handle = pci_map_single(ioa_cfg->pdev,
3335                                                      scsi_cmd->request_buffer, length,
3336                                                      scsi_cmd->sc_data_direction);
3337
3338                 if (likely(!pci_dma_mapping_error(ipr_cmd->dma_handle))) {
3339                         ipr_cmd->dma_use_sg = 1;
3340                         ioadl[0].flags_and_data_len =
3341                                 cpu_to_be32(ioadl_flags | length | IPR_IOADL_FLAGS_LAST);
3342                         ioadl[0].address = cpu_to_be32(ipr_cmd->dma_handle);
3343                         return 0;
3344                 } else
3345                         dev_err(&ioa_cfg->pdev->dev, "pci_map_single failed!\n");
3346         }
3347
3348         return -1;
3349 }
3350
3351 /**
3352  * ipr_get_task_attributes - Translate SPI Q-Tag to task attributes
3353  * @scsi_cmd:   scsi command struct
3354  *
3355  * Return value:
3356  *      task attributes
3357  **/
3358 static u8 ipr_get_task_attributes(struct scsi_cmnd *scsi_cmd)
3359 {
3360         u8 tag[2];
3361         u8 rc = IPR_FLAGS_LO_UNTAGGED_TASK;
3362
3363         if (scsi_populate_tag_msg(scsi_cmd, tag)) {
3364                 switch (tag[0]) {
3365                 case MSG_SIMPLE_TAG:
3366                         rc = IPR_FLAGS_LO_SIMPLE_TASK;
3367                         break;
3368                 case MSG_HEAD_TAG:
3369                         rc = IPR_FLAGS_LO_HEAD_OF_Q_TASK;
3370                         break;
3371                 case MSG_ORDERED_TAG:
3372                         rc = IPR_FLAGS_LO_ORDERED_TASK;
3373                         break;
3374                 };
3375         }
3376
3377         return rc;
3378 }
3379
3380 /**
3381  * ipr_erp_done - Process completion of ERP for a device
3382  * @ipr_cmd:            ipr command struct
3383  *
3384  * This function copies the sense buffer into the scsi_cmd
3385  * struct and pushes the scsi_done function.
3386  *
3387  * Return value:
3388  *      nothing
3389  **/
3390 static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
3391 {
3392         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3393         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
3394         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3395         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3396
3397         if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
3398                 scsi_cmd->result |= (DID_ERROR << 16);
3399                 ipr_sdev_err(scsi_cmd->device,
3400                              "Request Sense failed with IOASC: 0x%08X\n", ioasc);
3401         } else {
3402                 memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
3403                        SCSI_SENSE_BUFFERSIZE);
3404         }
3405
3406         if (res) {
3407                 res->needs_sync_complete = 1;
3408                 res->in_erp = 0;
3409         }
3410         ipr_unmap_sglist(ioa_cfg, ipr_cmd);
3411         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3412         scsi_cmd->scsi_done(scsi_cmd);
3413 }
3414
3415 /**
3416  * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
3417  * @ipr_cmd:    ipr command struct
3418  *
3419  * Return value:
3420  *      none
3421  **/
3422 static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
3423 {
3424         struct ipr_ioarcb *ioarcb;
3425         struct ipr_ioasa *ioasa;
3426
3427         ioarcb = &ipr_cmd->ioarcb;
3428         ioasa = &ipr_cmd->ioasa;
3429
3430         memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
3431         ioarcb->write_data_transfer_length = 0;
3432         ioarcb->read_data_transfer_length = 0;
3433         ioarcb->write_ioadl_len = 0;
3434         ioarcb->read_ioadl_len = 0;
3435         ioasa->ioasc = 0;
3436         ioasa->residual_data_len = 0;
3437 }
3438
3439 /**
3440  * ipr_erp_request_sense - Send request sense to a device
3441  * @ipr_cmd:    ipr command struct
3442  *
3443  * This function sends a request sense to a device as a result
3444  * of a check condition.
3445  *
3446  * Return value:
3447  *      nothing
3448  **/
3449 static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
3450 {
3451         struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
3452         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3453
3454         if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
3455                 ipr_erp_done(ipr_cmd);
3456                 return;
3457         }
3458
3459         ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
3460
3461         cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
3462         cmd_pkt->cdb[0] = REQUEST_SENSE;
3463         cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
3464         cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
3465         cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
3466         cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);
3467
3468         ipr_cmd->ioadl[0].flags_and_data_len =
3469                 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | SCSI_SENSE_BUFFERSIZE);
3470         ipr_cmd->ioadl[0].address =
3471                 cpu_to_be32(ipr_cmd->sense_buffer_dma);
3472
3473         ipr_cmd->ioarcb.read_ioadl_len =
3474                 cpu_to_be32(sizeof(struct ipr_ioadl_desc));
3475         ipr_cmd->ioarcb.read_data_transfer_length =
3476                 cpu_to_be32(SCSI_SENSE_BUFFERSIZE);
3477
3478         ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
3479                    IPR_REQUEST_SENSE_TIMEOUT * 2);
3480 }
3481
3482 /**
3483  * ipr_erp_cancel_all - Send cancel all to a device
3484  * @ipr_cmd:    ipr command struct
3485  *
3486  * This function sends a cancel all to a device to clear the
3487  * queue. If we are running TCQ on the device, QERR is set to 1,
3488  * which means all outstanding ops have been dropped on the floor.
3489  * Cancel all will return them to us.
3490  *
3491  * Return value:
3492  *      nothing
3493  **/
3494 static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
3495 {
3496         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3497         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
3498         struct ipr_cmd_pkt *cmd_pkt;
3499
3500         res->in_erp = 1;
3501
3502         ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
3503
3504         if (!scsi_get_tag_type(scsi_cmd->device)) {
3505                 ipr_erp_request_sense(ipr_cmd);
3506                 return;
3507         }
3508
3509         cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
3510         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3511         cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
3512
3513         ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
3514                    IPR_CANCEL_ALL_TIMEOUT);
3515 }
3516
3517 /**
3518  * ipr_dump_ioasa - Dump contents of IOASA
3519  * @ioa_cfg:    ioa config struct
3520  * @ipr_cmd:    ipr command struct
3521  *
3522  * This function is invoked by the interrupt handler when ops
3523  * fail. It will log the IOASA if appropriate. Only called
3524  * for GPDD ops.
3525  *
3526  * Return value:
3527  *      none
3528  **/
3529 static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
3530                            struct ipr_cmnd *ipr_cmd)
3531 {
3532         int i;
3533         u16 data_len;
3534         u32 ioasc;
3535         struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
3536         __be32 *ioasa_data = (__be32 *)ioasa;
3537         int error_index;
3538
3539         ioasc = be32_to_cpu(ioasa->ioasc) & IPR_IOASC_IOASC_MASK;
3540
3541         if (0 == ioasc)
3542                 return;
3543
3544         if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
3545                 return;
3546
3547         error_index = ipr_get_error(ioasc);
3548
3549         if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
3550                 /* Don't log an error if the IOA already logged one */
3551                 if (ioasa->ilid != 0)
3552                         return;
3553
3554                 if (ipr_error_table[error_index].log_ioasa == 0)
3555                         return;
3556         }
3557
3558         ipr_sdev_err(ipr_cmd->scsi_cmd->device, "%s\n",
3559                      ipr_error_table[error_index].error);
3560
3561         if ((ioasa->u.gpdd.end_state <= ARRAY_SIZE(ipr_gpdd_dev_end_states)) &&
3562             (ioasa->u.gpdd.bus_phase <=  ARRAY_SIZE(ipr_gpdd_dev_bus_phases))) {
3563                 ipr_sdev_err(ipr_cmd->scsi_cmd->device,
3564                              "Device End state: %s Phase: %s\n",
3565                              ipr_gpdd_dev_end_states[ioasa->u.gpdd.end_state],
3566                              ipr_gpdd_dev_bus_phases[ioasa->u.gpdd.bus_phase]);
3567         }
3568
3569         if (sizeof(struct ipr_ioasa) < be16_to_cpu(ioasa->ret_stat_len))
3570                 data_len = sizeof(struct ipr_ioasa);
3571         else
3572                 data_len = be16_to_cpu(ioasa->ret_stat_len);
3573
3574         ipr_err("IOASA Dump:\n");
3575
3576         for (i = 0; i < data_len / 4; i += 4) {
3577                 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
3578                         be32_to_cpu(ioasa_data[i]),
3579                         be32_to_cpu(ioasa_data[i+1]),
3580                         be32_to_cpu(ioasa_data[i+2]),
3581                         be32_to_cpu(ioasa_data[i+3]));
3582         }
3583 }
3584
3585 /**
3586  * ipr_gen_sense - Generate SCSI sense data from an IOASA
3587  * @ioasa:              IOASA
3588  * @sense_buf:  sense data buffer
3589  *
3590  * Return value:
3591  *      none
3592  **/
3593 static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
3594 {
3595         u32 failing_lba;
3596         u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
3597         struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
3598         struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
3599         u32 ioasc = be32_to_cpu(ioasa->ioasc);
3600
3601         memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
3602
3603         if (ioasc >= IPR_FIRST_DRIVER_IOASC)
3604                 return;
3605
3606         ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
3607
3608         if (ipr_is_vset_device(res) &&
3609             ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
3610             ioasa->u.vset.failing_lba_hi != 0) {
3611                 sense_buf[0] = 0x72;
3612                 sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
3613                 sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
3614                 sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);
3615
3616                 sense_buf[7] = 12;
3617                 sense_buf[8] = 0;
3618                 sense_buf[9] = 0x0A;
3619                 sense_buf[10] = 0x80;
3620
3621                 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
3622
3623                 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
3624                 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
3625                 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
3626                 sense_buf[15] = failing_lba & 0x000000ff;
3627
3628                 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
3629
3630                 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
3631                 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
3632                 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
3633                 sense_buf[19] = failing_lba & 0x000000ff;
3634         } else {
3635                 sense_buf[0] = 0x70;
3636                 sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
3637                 sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
3638                 sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);
3639
3640                 /* Illegal request */
3641                 if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
3642                     (be32_to_cpu(ioasa->ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
3643                         sense_buf[7] = 10;      /* additional length */
3644
3645                         /* IOARCB was in error */
3646                         if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
3647                                 sense_buf[15] = 0xC0;
3648                         else    /* Parameter data was invalid */
3649                                 sense_buf[15] = 0x80;
3650
3651                         sense_buf[16] =
3652                             ((IPR_FIELD_POINTER_MASK &
3653                               be32_to_cpu(ioasa->ioasc_specific)) >> 8) & 0xff;
3654                         sense_buf[17] =
3655                             (IPR_FIELD_POINTER_MASK &
3656                              be32_to_cpu(ioasa->ioasc_specific)) & 0xff;
3657                 } else {
3658                         if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
3659                                 if (ipr_is_vset_device(res))
3660                                         failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
3661                                 else
3662                                         failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);
3663
3664                                 sense_buf[0] |= 0x80;   /* Or in the Valid bit */
3665                                 sense_buf[3] = (failing_lba & 0xff000000) >> 24;
3666                                 sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
3667                                 sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
3668                                 sense_buf[6] = failing_lba & 0x000000ff;
3669                         }
3670
3671                         sense_buf[7] = 6;       /* additional length */
3672                 }
3673         }
3674 }
3675
3676 /**
3677  * ipr_erp_start - Process an error response for a SCSI op
3678  * @ioa_cfg:    ioa config struct
3679  * @ipr_cmd:    ipr command struct
3680  *
3681  * This function determines whether or not to initiate ERP
3682  * on the affected device.
3683  *
3684  * Return value:
3685  *      nothing
3686  **/
3687 static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
3688                               struct ipr_cmnd *ipr_cmd)
3689 {
3690         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3691         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
3692         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3693
3694         if (!res) {
3695                 ipr_scsi_eh_done(ipr_cmd);
3696                 return;
3697         }
3698
3699         if (ipr_is_gscsi(res))
3700                 ipr_dump_ioasa(ioa_cfg, ipr_cmd);
3701         else
3702                 ipr_gen_sense(ipr_cmd);
3703
3704         switch (ioasc & IPR_IOASC_IOASC_MASK) {
3705         case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
3706                 scsi_cmd->result |= (DID_IMM_RETRY << 16);
3707                 break;
3708         case IPR_IOASC_IR_RESOURCE_HANDLE:
3709                 scsi_cmd->result |= (DID_NO_CONNECT << 16);
3710                 break;
3711         case IPR_IOASC_HW_SEL_TIMEOUT:
3712                 scsi_cmd->result |= (DID_NO_CONNECT << 16);
3713                 res->needs_sync_complete = 1;
3714                 break;
3715         case IPR_IOASC_SYNC_REQUIRED:
3716                 if (!res->in_erp)
3717                         res->needs_sync_complete = 1;
3718                 scsi_cmd->result |= (DID_IMM_RETRY << 16);
3719                 break;
3720         case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
3721                 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
3722                 break;
3723         case IPR_IOASC_BUS_WAS_RESET:
3724         case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
3725                 /*
3726                  * Report the bus reset and ask for a retry. The device
3727                  * will give CC/UA the next command.
3728                  */
3729                 if (!res->resetting_device)
3730                         scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
3731                 scsi_cmd->result |= (DID_ERROR << 16);
3732                 res->needs_sync_complete = 1;
3733                 break;
3734         case IPR_IOASC_HW_DEV_BUS_STATUS:
3735                 scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
3736                 if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
3737                         ipr_erp_cancel_all(ipr_cmd);
3738                         return;
3739                 }
3740                 res->needs_sync_complete = 1;
3741                 break;
3742         case IPR_IOASC_NR_INIT_CMD_REQUIRED:
3743                 break;
3744         default:
3745                 scsi_cmd->result |= (DID_ERROR << 16);
3746                 if (!ipr_is_vset_device(res))
3747                         res->needs_sync_complete = 1;
3748                 break;
3749         }
3750
3751         ipr_unmap_sglist(ioa_cfg, ipr_cmd);
3752         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3753         scsi_cmd->scsi_done(scsi_cmd);
3754 }
3755
3756 /**
3757  * ipr_scsi_done - mid-layer done function
3758  * @ipr_cmd:    ipr command struct
3759  *
3760  * This function is invoked by the interrupt handler for
3761  * ops generated by the SCSI mid-layer
3762  *
3763  * Return value:
3764  *      none
3765  **/
3766 static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
3767 {
3768         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3769         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3770         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3771
3772         scsi_cmd->resid = be32_to_cpu(ipr_cmd->ioasa.residual_data_len);
3773
3774         if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
3775                 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
3776                 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3777                 scsi_cmd->scsi_done(scsi_cmd);
3778         } else
3779                 ipr_erp_start(ioa_cfg, ipr_cmd);
3780 }
3781
3782 /**
3783  * ipr_save_ioafp_mode_select - Save adapters mode select data
3784  * @ioa_cfg:    ioa config struct
3785  * @scsi_cmd:   scsi command struct
3786  *
3787  * This function saves mode select data for the adapter to
3788  * use following an adapter reset.
3789  *
3790  * Return value:
3791  *      0 on success / SCSI_MLQUEUE_HOST_BUSY on failure
3792  **/
3793 static int ipr_save_ioafp_mode_select(struct ipr_ioa_cfg *ioa_cfg,
3794                                        struct scsi_cmnd *scsi_cmd)
3795 {
3796         if (!ioa_cfg->saved_mode_pages) {
3797                 ioa_cfg->saved_mode_pages  = kmalloc(sizeof(struct ipr_mode_pages),
3798                                                      GFP_ATOMIC);
3799                 if (!ioa_cfg->saved_mode_pages) {
3800                         dev_err(&ioa_cfg->pdev->dev,
3801                                 "IOA mode select buffer allocation failed\n");
3802                         return SCSI_MLQUEUE_HOST_BUSY;
3803                 }
3804         }
3805
3806         memcpy(ioa_cfg->saved_mode_pages, scsi_cmd->buffer, scsi_cmd->cmnd[4]);
3807         ioa_cfg->saved_mode_page_len = scsi_cmd->cmnd[4];
3808         return 0;
3809 }
3810
3811 /**
3812  * ipr_queuecommand - Queue a mid-layer request
3813  * @scsi_cmd:   scsi command struct
3814  * @done:               done function
3815  *
3816  * This function queues a request generated by the mid-layer.
3817  *
3818  * Return value:
3819  *      0 on success
3820  *      SCSI_MLQUEUE_DEVICE_BUSY if device is busy
3821  *      SCSI_MLQUEUE_HOST_BUSY if host is busy
3822  **/
3823 static int ipr_queuecommand(struct scsi_cmnd *scsi_cmd,
3824                             void (*done) (struct scsi_cmnd *))
3825 {
3826         struct ipr_ioa_cfg *ioa_cfg;
3827         struct ipr_resource_entry *res;
3828         struct ipr_ioarcb *ioarcb;
3829         struct ipr_cmnd *ipr_cmd;
3830         int rc = 0;
3831
3832         scsi_cmd->scsi_done = done;
3833         ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
3834         res = scsi_cmd->device->hostdata;
3835         scsi_cmd->result = (DID_OK << 16);
3836
3837         /*
3838          * We are currently blocking all devices due to a host reset
3839          * We have told the host to stop giving us new requests, but
3840          * ERP ops don't count. FIXME
3841          */
3842         if (unlikely(!ioa_cfg->allow_cmds && !ioa_cfg->ioa_is_dead))
3843                 return SCSI_MLQUEUE_HOST_BUSY;
3844
3845         /*
3846          * FIXME - Create scsi_set_host_offline interface
3847          *  and the ioa_is_dead check can be removed
3848          */
3849         if (unlikely(ioa_cfg->ioa_is_dead || !res)) {
3850                 memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
3851                 scsi_cmd->result = (DID_NO_CONNECT << 16);
3852                 scsi_cmd->scsi_done(scsi_cmd);
3853                 return 0;
3854         }
3855
3856         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3857         ioarcb = &ipr_cmd->ioarcb;
3858         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
3859
3860         memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
3861         ipr_cmd->scsi_cmd = scsi_cmd;
3862         ioarcb->res_handle = res->cfgte.res_handle;
3863         ipr_cmd->done = ipr_scsi_done;
3864         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_PHYS_LOC(res->cfgte.res_addr));
3865
3866         if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
3867                 if (scsi_cmd->underflow == 0)
3868                         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
3869
3870                 if (res->needs_sync_complete) {
3871                         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
3872                         res->needs_sync_complete = 0;
3873                 }
3874
3875                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
3876                 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
3877                 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
3878                 ioarcb->cmd_pkt.flags_lo |= ipr_get_task_attributes(scsi_cmd);
3879         }
3880
3881         if (scsi_cmd->cmnd[0] >= 0xC0 &&
3882             (!ipr_is_gscsi(res) || scsi_cmd->cmnd[0] == IPR_QUERY_RSRC_STATE))
3883                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
3884
3885         if (ipr_is_ioa_resource(res) && scsi_cmd->cmnd[0] == MODE_SELECT)
3886                 rc = ipr_save_ioafp_mode_select(ioa_cfg, scsi_cmd);
3887
3888         if (likely(rc == 0))
3889                 rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
3890
3891         if (likely(rc == 0)) {
3892                 mb();
3893                 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
3894                        ioa_cfg->regs.ioarrin_reg);
3895         } else {
3896                  list_move_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3897                  return SCSI_MLQUEUE_HOST_BUSY;
3898         }
3899
3900         return 0;
3901 }
3902
3903 /**
3904  * ipr_info - Get information about the card/driver
3905  * @scsi_host:  scsi host struct
3906  *
3907  * Return value:
3908  *      pointer to buffer with description string
3909  **/
3910 static const char * ipr_ioa_info(struct Scsi_Host *host)
3911 {
3912         static char buffer[512];
3913         struct ipr_ioa_cfg *ioa_cfg;
3914         unsigned long lock_flags = 0;
3915
3916         ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
3917
3918         spin_lock_irqsave(host->host_lock, lock_flags);
3919         sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
3920         spin_unlock_irqrestore(host->host_lock, lock_flags);
3921
3922         return buffer;
3923 }
3924
3925 static struct scsi_host_template driver_template = {
3926         .module = THIS_MODULE,
3927         .name = "IPR",
3928         .info = ipr_ioa_info,
3929         .queuecommand = ipr_queuecommand,
3930         .eh_abort_handler = ipr_eh_abort,
3931         .eh_device_reset_handler = ipr_eh_dev_reset,
3932         .eh_host_reset_handler = ipr_eh_host_reset,
3933         .slave_alloc = ipr_slave_alloc,
3934         .slave_configure = ipr_slave_configure,
3935         .slave_destroy = ipr_slave_destroy,
3936         .change_queue_depth = ipr_change_queue_depth,
3937         .change_queue_type = ipr_change_queue_type,
3938         .bios_param = ipr_biosparam,
3939         .can_queue = IPR_MAX_COMMANDS,
3940         .this_id = -1,
3941         .sg_tablesize = IPR_MAX_SGLIST,
3942         .max_sectors = IPR_IOA_MAX_SECTORS,
3943         .cmd_per_lun = IPR_MAX_CMD_PER_LUN,
3944         .use_clustering = ENABLE_CLUSTERING,
3945         .shost_attrs = ipr_ioa_attrs,
3946         .sdev_attrs = ipr_dev_attrs,
3947         .proc_name = IPR_NAME
3948 };
3949
3950 #ifdef CONFIG_PPC_PSERIES
3951 static const u16 ipr_blocked_processors[] = {
3952         PV_NORTHSTAR,
3953         PV_PULSAR,
3954         PV_POWER4,
3955         PV_ICESTAR,
3956         PV_SSTAR,
3957         PV_POWER4p,
3958         PV_630,
3959         PV_630p
3960 };
3961
3962 /**
3963  * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
3964  * @ioa_cfg:    ioa cfg struct
3965  *
3966  * Adapters that use Gemstone revision < 3.1 do not work reliably on
3967  * certain pSeries hardware. This function determines if the given
3968  * adapter is in one of these confgurations or not.
3969  *
3970  * Return value:
3971  *      1 if adapter is not supported / 0 if adapter is supported
3972  **/
3973 static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
3974 {
3975         u8 rev_id;
3976         int i;
3977
3978         if (ioa_cfg->type == 0x5702) {
3979                 if (pci_read_config_byte(ioa_cfg->pdev, PCI_REVISION_ID,
3980                                          &rev_id) == PCIBIOS_SUCCESSFUL) {
3981                         if (rev_id < 4) {
3982                                 for (i = 0; i < ARRAY_SIZE(ipr_blocked_processors); i++){
3983                                         if (__is_processor(ipr_blocked_processors[i]))
3984                                                 return 1;
3985                                 }
3986                         }
3987                 }
3988         }
3989         return 0;
3990 }
3991 #else
3992 #define ipr_invalid_adapter(ioa_cfg) 0
3993 #endif
3994
3995 /**
3996  * ipr_ioa_bringdown_done - IOA bring down completion.
3997  * @ipr_cmd:    ipr command struct
3998  *
3999  * This function processes the completion of an adapter bring down.
4000  * It wakes any reset sleepers.
4001  *
4002  * Return value:
4003  *      IPR_RC_JOB_RETURN
4004  **/
4005 static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
4006 {
4007         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4008
4009         ENTER;
4010         ioa_cfg->in_reset_reload = 0;
4011         ioa_cfg->reset_retries = 0;
4012         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4013         wake_up_all(&ioa_cfg->reset_wait_q);
4014
4015         spin_unlock_irq(ioa_cfg->host->host_lock);
4016         scsi_unblock_requests(ioa_cfg->host);
4017         spin_lock_irq(ioa_cfg->host->host_lock);
4018         LEAVE;
4019
4020         return IPR_RC_JOB_RETURN;
4021 }
4022
4023 /**
4024  * ipr_ioa_reset_done - IOA reset completion.
4025  * @ipr_cmd:    ipr command struct
4026  *
4027  * This function processes the completion of an adapter reset.
4028  * It schedules any necessary mid-layer add/removes and
4029  * wakes any reset sleepers.
4030  *
4031  * Return value:
4032  *      IPR_RC_JOB_RETURN
4033  **/
4034 static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
4035 {
4036         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4037         struct ipr_resource_entry *res;
4038         struct ipr_hostrcb *hostrcb, *temp;
4039         int i = 0;
4040
4041         ENTER;
4042         ioa_cfg->in_reset_reload = 0;
4043         ioa_cfg->allow_cmds = 1;
4044         ioa_cfg->reset_cmd = NULL;
4045
4046         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4047                 if (ioa_cfg->allow_ml_add_del && (res->add_to_ml || res->del_from_ml)) {
4048                         ipr_trace;
4049                         break;
4050                 }
4051         }
4052         schedule_work(&ioa_cfg->work_q);
4053
4054         list_for_each_entry_safe(hostrcb, temp, &ioa_cfg->hostrcb_free_q, queue) {
4055                 list_del(&hostrcb->queue);
4056                 if (i++ < IPR_NUM_LOG_HCAMS)
4057                         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
4058                 else
4059                         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
4060         }
4061
4062         dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");
4063
4064         ioa_cfg->reset_retries = 0;
4065         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4066         wake_up_all(&ioa_cfg->reset_wait_q);
4067
4068         spin_unlock_irq(ioa_cfg->host->host_lock);
4069         scsi_unblock_requests(ioa_cfg->host);
4070         spin_lock_irq(ioa_cfg->host->host_lock);
4071
4072         if (!ioa_cfg->allow_cmds)
4073                 scsi_block_requests(ioa_cfg->host);
4074
4075         LEAVE;
4076         return IPR_RC_JOB_RETURN;
4077 }
4078
4079 /**
4080  * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
4081  * @supported_dev:      supported device struct
4082  * @vpids:                      vendor product id struct
4083  *
4084  * Return value:
4085  *      none
4086  **/
4087 static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
4088                                  struct ipr_std_inq_vpids *vpids)
4089 {
4090         memset(supported_dev, 0, sizeof(struct ipr_supported_device));
4091         memcpy(&supported_dev->vpids, vpids, sizeof(struct ipr_std_inq_vpids));
4092         supported_dev->num_records = 1;
4093         supported_dev->data_length =
4094                 cpu_to_be16(sizeof(struct ipr_supported_device));
4095         supported_dev->reserved = 0;
4096 }
4097
4098 /**
4099  * ipr_set_supported_devs - Send Set Supported Devices for a device
4100  * @ipr_cmd:    ipr command struct
4101  *
4102  * This function send a Set Supported Devices to the adapter
4103  *
4104  * Return value:
4105  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4106  **/
4107 static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
4108 {
4109         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4110         struct ipr_supported_device *supp_dev = &ioa_cfg->vpd_cbs->supp_dev;
4111         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4112         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4113         struct ipr_resource_entry *res = ipr_cmd->u.res;
4114
4115         ipr_cmd->job_step = ipr_ioa_reset_done;
4116
4117         list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
4118                 if (!ipr_is_af_dasd_device(res))
4119                         continue;
4120
4121                 ipr_cmd->u.res = res;
4122                 ipr_set_sup_dev_dflt(supp_dev, &res->cfgte.std_inq_data.vpids);
4123
4124                 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4125                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
4126                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4127
4128                 ioarcb->cmd_pkt.cdb[0] = IPR_SET_SUPPORTED_DEVICES;
4129                 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_supported_device) >> 8) & 0xff;
4130                 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_supported_device) & 0xff;
4131
4132                 ioadl->flags_and_data_len = cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST |
4133                                                         sizeof(struct ipr_supported_device));
4134                 ioadl->address = cpu_to_be32(ioa_cfg->vpd_cbs_dma +
4135                                              offsetof(struct ipr_misc_cbs, supp_dev));
4136                 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4137                 ioarcb->write_data_transfer_length =
4138                         cpu_to_be32(sizeof(struct ipr_supported_device));
4139
4140                 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
4141                            IPR_SET_SUP_DEVICE_TIMEOUT);
4142
4143                 ipr_cmd->job_step = ipr_set_supported_devs;
4144                 return IPR_RC_JOB_RETURN;
4145         }
4146
4147         return IPR_RC_JOB_CONTINUE;
4148 }
4149
4150 /**
4151  * ipr_get_mode_page - Locate specified mode page
4152  * @mode_pages: mode page buffer
4153  * @page_code:  page code to find
4154  * @len:                minimum required length for mode page
4155  *
4156  * Return value:
4157  *      pointer to mode page / NULL on failure
4158  **/
4159 static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
4160                                u32 page_code, u32 len)
4161 {
4162         struct ipr_mode_page_hdr *mode_hdr;
4163         u32 page_length;
4164         u32 length;
4165
4166         if (!mode_pages || (mode_pages->hdr.length == 0))
4167                 return NULL;
4168
4169         length = (mode_pages->hdr.length + 1) - 4 - mode_pages->hdr.block_desc_len;
4170         mode_hdr = (struct ipr_mode_page_hdr *)
4171                 (mode_pages->data + mode_pages->hdr.block_desc_len);
4172
4173         while (length) {
4174                 if (IPR_GET_MODE_PAGE_CODE(mode_hdr) == page_code) {
4175                         if (mode_hdr->page_length >= (len - sizeof(struct ipr_mode_page_hdr)))
4176                                 return mode_hdr;
4177                         break;
4178                 } else {
4179                         page_length = (sizeof(struct ipr_mode_page_hdr) +
4180                                        mode_hdr->page_length);
4181                         length -= page_length;
4182                         mode_hdr = (struct ipr_mode_page_hdr *)
4183                                 ((unsigned long)mode_hdr + page_length);
4184                 }
4185         }
4186         return NULL;
4187 }
4188
4189 /**
4190  * ipr_check_term_power - Check for term power errors
4191  * @ioa_cfg:    ioa config struct
4192  * @mode_pages: IOAFP mode pages buffer
4193  *
4194  * Check the IOAFP's mode page 28 for term power errors
4195  *
4196  * Return value:
4197  *      nothing
4198  **/
4199 static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
4200                                  struct ipr_mode_pages *mode_pages)
4201 {
4202         int i;
4203         int entry_length;
4204         struct ipr_dev_bus_entry *bus;
4205         struct ipr_mode_page28 *mode_page;
4206
4207         mode_page = ipr_get_mode_page(mode_pages, 0x28,
4208                                       sizeof(struct ipr_mode_page28));
4209
4210         entry_length = mode_page->entry_length;
4211
4212         bus = mode_page->bus;
4213
4214         for (i = 0; i < mode_page->num_entries; i++) {
4215                 if (bus->flags & IPR_SCSI_ATTR_NO_TERM_PWR) {
4216                         dev_err(&ioa_cfg->pdev->dev,
4217                                 "Term power is absent on scsi bus %d\n",
4218                                 bus->res_addr.bus);
4219                 }
4220
4221                 bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
4222         }
4223 }
4224
4225 /**
4226  * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
4227  * @ioa_cfg:    ioa config struct
4228  *
4229  * Looks through the config table checking for SES devices. If
4230  * the SES device is in the SES table indicating a maximum SCSI
4231  * bus speed, the speed is limited for the bus.
4232  *
4233  * Return value:
4234  *      none
4235  **/
4236 static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
4237 {
4238         u32 max_xfer_rate;
4239         int i;
4240
4241         for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
4242                 max_xfer_rate = ipr_get_max_scsi_speed(ioa_cfg, i,
4243                                                        ioa_cfg->bus_attr[i].bus_width);
4244
4245                 if (max_xfer_rate < ioa_cfg->bus_attr[i].max_xfer_rate)
4246                         ioa_cfg->bus_attr[i].max_xfer_rate = max_xfer_rate;
4247         }
4248 }
4249
4250 /**
4251  * ipr_modify_ioafp_mode_page_28 - Modify IOAFP Mode Page 28
4252  * @ioa_cfg:    ioa config struct
4253  * @mode_pages: mode page 28 buffer
4254  *
4255  * Updates mode page 28 based on driver configuration
4256  *
4257  * Return value:
4258  *      none
4259  **/
4260 static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
4261                                                 struct ipr_mode_pages *mode_pages)
4262 {
4263         int i, entry_length;
4264         struct ipr_dev_bus_entry *bus;
4265         struct ipr_bus_attributes *bus_attr;
4266         struct ipr_mode_page28 *mode_page;
4267
4268         mode_page = ipr_get_mode_page(mode_pages, 0x28,
4269                                       sizeof(struct ipr_mode_page28));
4270
4271         entry_length = mode_page->entry_length;
4272
4273         /* Loop for each device bus entry */
4274         for (i = 0, bus = mode_page->bus;
4275              i < mode_page->num_entries;
4276              i++, bus = (struct ipr_dev_bus_entry *)((u8 *)bus + entry_length)) {
4277                 if (bus->res_addr.bus > IPR_MAX_NUM_BUSES) {
4278                         dev_err(&ioa_cfg->pdev->dev,
4279                                 "Invalid resource address reported: 0x%08X\n",
4280                                 IPR_GET_PHYS_LOC(bus->res_addr));
4281                         continue;
4282                 }
4283
4284                 bus_attr = &ioa_cfg->bus_attr[i];
4285                 bus->extended_reset_delay = IPR_EXTENDED_RESET_DELAY;
4286                 bus->bus_width = bus_attr->bus_width;
4287                 bus->max_xfer_rate = cpu_to_be32(bus_attr->max_xfer_rate);
4288                 bus->flags &= ~IPR_SCSI_ATTR_QAS_MASK;
4289                 if (bus_attr->qas_enabled)
4290                         bus->flags |= IPR_SCSI_ATTR_ENABLE_QAS;
4291                 else
4292                         bus->flags |= IPR_SCSI_ATTR_DISABLE_QAS;
4293         }
4294 }
4295
4296 /**
4297  * ipr_build_mode_select - Build a mode select command
4298  * @ipr_cmd:    ipr command struct
4299  * @res_handle: resource handle to send command to
4300  * @parm:               Byte 2 of Mode Sense command
4301  * @dma_addr:   DMA buffer address
4302  * @xfer_len:   data transfer length
4303  *
4304  * Return value:
4305  *      none
4306  **/
4307 static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
4308                                   __be32 res_handle, u8 parm, u32 dma_addr,
4309                                   u8 xfer_len)
4310 {
4311         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4312         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4313
4314         ioarcb->res_handle = res_handle;
4315         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
4316         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
4317         ioarcb->cmd_pkt.cdb[0] = MODE_SELECT;
4318         ioarcb->cmd_pkt.cdb[1] = parm;
4319         ioarcb->cmd_pkt.cdb[4] = xfer_len;
4320
4321         ioadl->flags_and_data_len =
4322                 cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST | xfer_len);
4323         ioadl->address = cpu_to_be32(dma_addr);
4324         ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4325         ioarcb->write_data_transfer_length = cpu_to_be32(xfer_len);
4326 }
4327
4328 /**
4329  * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
4330  * @ipr_cmd:    ipr command struct
4331  *
4332  * This function sets up the SCSI bus attributes and sends
4333  * a Mode Select for Page 28 to activate them.
4334  *
4335  * Return value:
4336  *      IPR_RC_JOB_RETURN
4337  **/
4338 static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
4339 {
4340         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4341         struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
4342         int length;
4343
4344         ENTER;
4345         if (ioa_cfg->saved_mode_pages) {
4346                 memcpy(mode_pages, ioa_cfg->saved_mode_pages,
4347                        ioa_cfg->saved_mode_page_len);
4348                 length = ioa_cfg->saved_mode_page_len;
4349         } else {
4350                 ipr_scsi_bus_speed_limit(ioa_cfg);
4351                 ipr_check_term_power(ioa_cfg, mode_pages);
4352                 ipr_modify_ioafp_mode_page_28(ioa_cfg, mode_pages);
4353                 length = mode_pages->hdr.length + 1;
4354                 mode_pages->hdr.length = 0;
4355         }
4356
4357         ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
4358                               ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
4359                               length);
4360
4361         ipr_cmd->job_step = ipr_set_supported_devs;
4362         ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
4363                                     struct ipr_resource_entry, queue);
4364
4365         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4366
4367         LEAVE;
4368         return IPR_RC_JOB_RETURN;
4369 }
4370
4371 /**
4372  * ipr_build_mode_sense - Builds a mode sense command
4373  * @ipr_cmd:    ipr command struct
4374  * @res:                resource entry struct
4375  * @parm:               Byte 2 of mode sense command
4376  * @dma_addr:   DMA address of mode sense buffer
4377  * @xfer_len:   Size of DMA buffer
4378  *
4379  * Return value:
4380  *      none
4381  **/
4382 static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
4383                                  __be32 res_handle,
4384                                  u8 parm, u32 dma_addr, u8 xfer_len)
4385 {
4386         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4387         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4388
4389         ioarcb->res_handle = res_handle;
4390         ioarcb->cmd_pkt.cdb[0] = MODE_SENSE;
4391         ioarcb->cmd_pkt.cdb[2] = parm;
4392         ioarcb->cmd_pkt.cdb[4] = xfer_len;
4393         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
4394
4395         ioadl->flags_and_data_len =
4396                 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
4397         ioadl->address = cpu_to_be32(dma_addr);
4398         ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4399         ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
4400 }
4401
4402 /**
4403  * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
4404  * @ipr_cmd:    ipr command struct
4405  *
4406  * This function send a Page 28 mode sense to the IOA to
4407  * retrieve SCSI bus attributes.
4408  *
4409  * Return value:
4410  *      IPR_RC_JOB_RETURN
4411  **/
4412 static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
4413 {
4414         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4415
4416         ENTER;
4417         ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
4418                              0x28, ioa_cfg->vpd_cbs_dma +
4419                              offsetof(struct ipr_misc_cbs, mode_pages),
4420                              sizeof(struct ipr_mode_pages));
4421
4422         ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
4423
4424         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4425
4426         LEAVE;
4427         return IPR_RC_JOB_RETURN;
4428 }
4429
4430 /**
4431  * ipr_init_res_table - Initialize the resource table
4432  * @ipr_cmd:    ipr command struct
4433  *
4434  * This function looks through the existing resource table, comparing
4435  * it with the config table. This function will take care of old/new
4436  * devices and schedule adding/removing them from the mid-layer
4437  * as appropriate.
4438  *
4439  * Return value:
4440  *      IPR_RC_JOB_CONTINUE
4441  **/
4442 static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
4443 {
4444         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4445         struct ipr_resource_entry *res, *temp;
4446         struct ipr_config_table_entry *cfgte;
4447         int found, i;
4448         LIST_HEAD(old_res);
4449
4450         ENTER;
4451         if (ioa_cfg->cfg_table->hdr.flags & IPR_UCODE_DOWNLOAD_REQ)
4452                 dev_err(&ioa_cfg->pdev->dev, "Microcode download required\n");
4453
4454         list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
4455                 list_move_tail(&res->queue, &old_res);
4456
4457         for (i = 0; i < ioa_cfg->cfg_table->hdr.num_entries; i++) {
4458                 cfgte = &ioa_cfg->cfg_table->dev[i];
4459                 found = 0;
4460
4461                 list_for_each_entry_safe(res, temp, &old_res, queue) {
4462                         if (!memcmp(&res->cfgte.res_addr,
4463                                     &cfgte->res_addr, sizeof(cfgte->res_addr))) {
4464                                 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
4465                                 found = 1;
4466                                 break;
4467                         }
4468                 }
4469
4470                 if (!found) {
4471                         if (list_empty(&ioa_cfg->free_res_q)) {
4472                                 dev_err(&ioa_cfg->pdev->dev, "Too many devices attached\n");
4473                                 break;
4474                         }
4475
4476                         found = 1;
4477                         res = list_entry(ioa_cfg->free_res_q.next,
4478                                          struct ipr_resource_entry, queue);
4479                         list_move_tail(&res->queue, &ioa_cfg->used_res_q);
4480                         ipr_init_res_entry(res);
4481                         res->add_to_ml = 1;
4482                 }
4483
4484                 if (found)
4485                         memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
4486         }
4487
4488         list_for_each_entry_safe(res, temp, &old_res, queue) {
4489                 if (res->sdev) {
4490                         res->del_from_ml = 1;
4491                         res->sdev->hostdata = NULL;
4492                         list_move_tail(&res->queue, &ioa_cfg->used_res_q);
4493                 } else {
4494                         list_move_tail(&res->queue, &ioa_cfg->free_res_q);
4495                 }
4496         }
4497
4498         ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
4499
4500         LEAVE;
4501         return IPR_RC_JOB_CONTINUE;
4502 }
4503
4504 /**
4505  * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
4506  * @ipr_cmd:    ipr command struct
4507  *
4508  * This function sends a Query IOA Configuration command
4509  * to the adapter to retrieve the IOA configuration table.
4510  *
4511  * Return value:
4512  *      IPR_RC_JOB_RETURN
4513  **/
4514 static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
4515 {
4516         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4517         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4518         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4519         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
4520
4521         ENTER;
4522         dev_info(&ioa_cfg->pdev->dev, "Adapter firmware version: %02X%02X%02X%02X\n",
4523                  ucode_vpd->major_release, ucode_vpd->card_type,
4524                  ucode_vpd->minor_release[0], ucode_vpd->minor_release[1]);
4525         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4526         ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4527
4528         ioarcb->cmd_pkt.cdb[0] = IPR_QUERY_IOA_CONFIG;
4529         ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_config_table) >> 8) & 0xff;
4530         ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_config_table) & 0xff;
4531
4532         ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4533         ioarcb->read_data_transfer_length =
4534                 cpu_to_be32(sizeof(struct ipr_config_table));
4535
4536         ioadl->address = cpu_to_be32(ioa_cfg->cfg_table_dma);
4537         ioadl->flags_and_data_len =
4538                 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(struct ipr_config_table));
4539
4540         ipr_cmd->job_step = ipr_init_res_table;
4541
4542         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4543
4544         LEAVE;
4545         return IPR_RC_JOB_RETURN;
4546 }
4547
4548 /**
4549  * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
4550  * @ipr_cmd:    ipr command struct
4551  *
4552  * This utility function sends an inquiry to the adapter.
4553  *
4554  * Return value:
4555  *      none
4556  **/
4557 static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
4558                               u32 dma_addr, u8 xfer_len)
4559 {
4560         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4561         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4562
4563         ENTER;
4564         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
4565         ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4566
4567         ioarcb->cmd_pkt.cdb[0] = INQUIRY;
4568         ioarcb->cmd_pkt.cdb[1] = flags;
4569         ioarcb->cmd_pkt.cdb[2] = page;
4570         ioarcb->cmd_pkt.cdb[4] = xfer_len;
4571
4572         ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4573         ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
4574
4575         ioadl->address = cpu_to_be32(dma_addr);
4576         ioadl->flags_and_data_len =
4577                 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
4578
4579         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4580         LEAVE;
4581 }
4582
4583 /**
4584  * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
4585  * @ipr_cmd:    ipr command struct
4586  *
4587  * This function sends a Page 3 inquiry to the adapter
4588  * to retrieve software VPD information.
4589  *
4590  * Return value:
4591  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4592  **/
4593 static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
4594 {
4595         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4596         char type[5];
4597
4598         ENTER;
4599
4600         /* Grab the type out of the VPD and store it away */
4601         memcpy(type, ioa_cfg->vpd_cbs->ioa_vpd.std_inq_data.vpids.product_id, 4);
4602         type[4] = '\0';
4603         ioa_cfg->type = simple_strtoul((char *)type, NULL, 16);
4604
4605         ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
4606
4607         ipr_ioafp_inquiry(ipr_cmd, 1, 3,
4608                           ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page3_data),
4609                           sizeof(struct ipr_inquiry_page3));
4610
4611         LEAVE;
4612         return IPR_RC_JOB_RETURN;
4613 }
4614
4615 /**
4616  * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
4617  * @ipr_cmd:    ipr command struct
4618  *
4619  * This function sends a standard inquiry to the adapter.
4620  *
4621  * Return value:
4622  *      IPR_RC_JOB_RETURN
4623  **/
4624 static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
4625 {
4626         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4627
4628         ENTER;
4629         ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
4630
4631         ipr_ioafp_inquiry(ipr_cmd, 0, 0,
4632                           ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, ioa_vpd),
4633                           sizeof(struct ipr_ioa_vpd));
4634
4635         LEAVE;
4636         return IPR_RC_JOB_RETURN;
4637 }
4638
4639 /**
4640  * ipr_ioafp_indentify_hrrq - Send Identify Host RRQ.
4641  * @ipr_cmd:    ipr command struct
4642  *
4643  * This function send an Identify Host Request Response Queue
4644  * command to establish the HRRQ with the adapter.
4645  *
4646  * Return value:
4647  *      IPR_RC_JOB_RETURN
4648  **/
4649 static int ipr_ioafp_indentify_hrrq(struct ipr_cmnd *ipr_cmd)
4650 {
4651         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4652         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4653
4654         ENTER;
4655         dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");
4656
4657         ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
4658         ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4659
4660         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4661         ioarcb->cmd_pkt.cdb[2] =
4662                 ((u32) ioa_cfg->host_rrq_dma >> 24) & 0xff;
4663         ioarcb->cmd_pkt.cdb[3] =
4664                 ((u32) ioa_cfg->host_rrq_dma >> 16) & 0xff;
4665         ioarcb->cmd_pkt.cdb[4] =
4666                 ((u32) ioa_cfg->host_rrq_dma >> 8) & 0xff;
4667         ioarcb->cmd_pkt.cdb[5] =
4668                 ((u32) ioa_cfg->host_rrq_dma) & 0xff;
4669         ioarcb->cmd_pkt.cdb[7] =
4670                 ((sizeof(u32) * IPR_NUM_CMD_BLKS) >> 8) & 0xff;
4671         ioarcb->cmd_pkt.cdb[8] =
4672                 (sizeof(u32) * IPR_NUM_CMD_BLKS) & 0xff;
4673
4674         ipr_cmd->job_step = ipr_ioafp_std_inquiry;
4675
4676         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4677
4678         LEAVE;
4679         return IPR_RC_JOB_RETURN;
4680 }
4681
4682 /**
4683  * ipr_reset_timer_done - Adapter reset timer function
4684  * @ipr_cmd:    ipr command struct
4685  *
4686  * Description: This function is used in adapter reset processing
4687  * for timing events. If the reset_cmd pointer in the IOA
4688  * config struct is not this adapter's we are doing nested
4689  * resets and fail_all_ops will take care of freeing the
4690  * command block.
4691  *
4692  * Return value:
4693  *      none
4694  **/
4695 static void ipr_reset_timer_done(struct ipr_cmnd *ipr_cmd)
4696 {
4697         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4698         unsigned long lock_flags = 0;
4699
4700         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4701
4702         if (ioa_cfg->reset_cmd == ipr_cmd) {
4703                 list_del(&ipr_cmd->queue);
4704                 ipr_cmd->done(ipr_cmd);
4705         }
4706
4707         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4708 }
4709
4710 /**
4711  * ipr_reset_start_timer - Start a timer for adapter reset job
4712  * @ipr_cmd:    ipr command struct
4713  * @timeout:    timeout value
4714  *
4715  * Description: This function is used in adapter reset processing
4716  * for timing events. If the reset_cmd pointer in the IOA
4717  * config struct is not this adapter's we are doing nested
4718  * resets and fail_all_ops will take care of freeing the
4719  * command block.
4720  *
4721  * Return value:
4722  *      none
4723  **/
4724 static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
4725                                   unsigned long timeout)
4726 {
4727         list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
4728         ipr_cmd->done = ipr_reset_ioa_job;
4729
4730         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
4731         ipr_cmd->timer.expires = jiffies + timeout;
4732         ipr_cmd->timer.function = (void (*)(unsigned long))ipr_reset_timer_done;
4733         add_timer(&ipr_cmd->timer);
4734 }
4735
4736 /**
4737  * ipr_init_ioa_mem - Initialize ioa_cfg control block
4738  * @ioa_cfg:    ioa cfg struct
4739  *
4740  * Return value:
4741  *      nothing
4742  **/
4743 static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
4744 {
4745         memset(ioa_cfg->host_rrq, 0, sizeof(u32) * IPR_NUM_CMD_BLKS);
4746
4747         /* Initialize Host RRQ pointers */
4748         ioa_cfg->hrrq_start = ioa_cfg->host_rrq;
4749         ioa_cfg->hrrq_end = &ioa_cfg->host_rrq[IPR_NUM_CMD_BLKS - 1];
4750         ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
4751         ioa_cfg->toggle_bit = 1;
4752
4753         /* Zero out config table */
4754         memset(ioa_cfg->cfg_table, 0, sizeof(struct ipr_config_table));
4755 }
4756
4757 /**
4758  * ipr_reset_enable_ioa - Enable the IOA following a reset.
4759  * @ipr_cmd:    ipr command struct
4760  *
4761  * This function reinitializes some control blocks and
4762  * enables destructive diagnostics on the adapter.
4763  *
4764  * Return value:
4765  *      IPR_RC_JOB_RETURN
4766  **/
4767 static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
4768 {
4769         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4770         volatile u32 int_reg;
4771
4772         ENTER;
4773         ipr_cmd->job_step = ipr_ioafp_indentify_hrrq;
4774         ipr_init_ioa_mem(ioa_cfg);
4775
4776         ioa_cfg->allow_interrupts = 1;
4777         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
4778
4779         if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
4780                 writel((IPR_PCII_ERROR_INTERRUPTS | IPR_PCII_HRRQ_UPDATED),
4781                        ioa_cfg->regs.clr_interrupt_mask_reg);
4782                 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
4783                 return IPR_RC_JOB_CONTINUE;
4784         }
4785
4786         /* Enable destructive diagnostics on IOA */
4787         writel(IPR_DOORBELL, ioa_cfg->regs.set_uproc_interrupt_reg);
4788
4789         writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg);
4790         int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
4791
4792         dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");
4793
4794         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
4795         ipr_cmd->timer.expires = jiffies + (ipr_transop_timeout * HZ);
4796         ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
4797         ipr_cmd->done = ipr_reset_ioa_job;
4798         add_timer(&ipr_cmd->timer);
4799         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
4800
4801         LEAVE;
4802         return IPR_RC_JOB_RETURN;
4803 }
4804
4805 /**
4806  * ipr_reset_wait_for_dump - Wait for a dump to timeout.
4807  * @ipr_cmd:    ipr command struct
4808  *
4809  * This function is invoked when an adapter dump has run out
4810  * of processing time.
4811  *
4812  * Return value:
4813  *      IPR_RC_JOB_CONTINUE
4814  **/
4815 static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
4816 {
4817         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4818
4819         if (ioa_cfg->sdt_state == GET_DUMP)
4820                 ioa_cfg->sdt_state = ABORT_DUMP;
4821
4822         ipr_cmd->job_step = ipr_reset_alert;
4823
4824         return IPR_RC_JOB_CONTINUE;
4825 }
4826
4827 /**
4828  * ipr_unit_check_no_data - Log a unit check/no data error log
4829  * @ioa_cfg:            ioa config struct
4830  *
4831  * Logs an error indicating the adapter unit checked, but for some
4832  * reason, we were unable to fetch the unit check buffer.
4833  *
4834  * Return value:
4835  *      nothing
4836  **/
4837 static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
4838 {
4839         ioa_cfg->errors_logged++;
4840         dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
4841 }
4842
4843 /**
4844  * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
4845  * @ioa_cfg:            ioa config struct
4846  *
4847  * Fetches the unit check buffer from the adapter by clocking the data
4848  * through the mailbox register.
4849  *
4850  * Return value:
4851  *      nothing
4852  **/
4853 static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
4854 {
4855         unsigned long mailbox;
4856         struct ipr_hostrcb *hostrcb;
4857         struct ipr_uc_sdt sdt;
4858         int rc, length;
4859
4860         mailbox = readl(ioa_cfg->ioa_mailbox);
4861
4862         if (!ipr_sdt_is_fmt2(mailbox)) {
4863                 ipr_unit_check_no_data(ioa_cfg);
4864                 return;
4865         }
4866
4867         memset(&sdt, 0, sizeof(struct ipr_uc_sdt));
4868         rc = ipr_get_ldump_data_section(ioa_cfg, mailbox, (__be32 *) &sdt,
4869                                         (sizeof(struct ipr_uc_sdt)) / sizeof(__be32));
4870
4871         if (rc || (be32_to_cpu(sdt.hdr.state) != IPR_FMT2_SDT_READY_TO_USE) ||
4872             !(sdt.entry[0].flags & IPR_SDT_VALID_ENTRY)) {
4873                 ipr_unit_check_no_data(ioa_cfg);
4874                 return;
4875         }
4876
4877         /* Find length of the first sdt entry (UC buffer) */
4878         length = (be32_to_cpu(sdt.entry[0].end_offset) -
4879                   be32_to_cpu(sdt.entry[0].bar_str_offset)) & IPR_FMT2_MBX_ADDR_MASK;
4880
4881         hostrcb = list_entry(ioa_cfg->hostrcb_free_q.next,
4882                              struct ipr_hostrcb, queue);
4883         list_del(&hostrcb->queue);
4884         memset(&hostrcb->hcam, 0, sizeof(hostrcb->hcam));
4885
4886         rc = ipr_get_ldump_data_section(ioa_cfg,
4887                                         be32_to_cpu(sdt.entry[0].bar_str_offset),
4888                                         (__be32 *)&hostrcb->hcam,
4889                                         min(length, (int)sizeof(hostrcb->hcam)) / sizeof(__be32));
4890
4891         if (!rc)
4892                 ipr_handle_log_data(ioa_cfg, hostrcb);
4893         else
4894                 ipr_unit_check_no_data(ioa_cfg);
4895
4896         list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
4897 }
4898
4899 /**
4900  * ipr_reset_restore_cfg_space - Restore PCI config space.
4901  * @ipr_cmd:    ipr command struct
4902  *
4903  * Description: This function restores the saved PCI config space of
4904  * the adapter, fails all outstanding ops back to the callers, and
4905  * fetches the dump/unit check if applicable to this reset.
4906  *
4907  * Return value:
4908  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4909  **/
4910 static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
4911 {
4912         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4913         int rc;
4914
4915         ENTER;
4916         pci_unblock_user_cfg_access(ioa_cfg->pdev);
4917         rc = pci_restore_state(ioa_cfg->pdev);
4918
4919         if (rc != PCIBIOS_SUCCESSFUL) {
4920                 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
4921                 return IPR_RC_JOB_CONTINUE;
4922         }
4923
4924         if (ipr_set_pcix_cmd_reg(ioa_cfg)) {
4925                 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
4926                 return IPR_RC_JOB_CONTINUE;
4927         }
4928
4929         ipr_fail_all_ops(ioa_cfg);
4930
4931         if (ioa_cfg->ioa_unit_checked) {
4932                 ioa_cfg->ioa_unit_checked = 0;
4933                 ipr_get_unit_check_buffer(ioa_cfg);
4934                 ipr_cmd->job_step = ipr_reset_alert;
4935                 ipr_reset_start_timer(ipr_cmd, 0);
4936                 return IPR_RC_JOB_RETURN;
4937         }
4938
4939         if (ioa_cfg->in_ioa_bringdown) {
4940                 ipr_cmd->job_step = ipr_ioa_bringdown_done;
4941         } else {
4942                 ipr_cmd->job_step = ipr_reset_enable_ioa;
4943
4944                 if (GET_DUMP == ioa_cfg->sdt_state) {
4945                         ipr_reset_start_timer(ipr_cmd, IPR_DUMP_TIMEOUT);
4946                         ipr_cmd->job_step = ipr_reset_wait_for_dump;
4947                         schedule_work(&ioa_cfg->work_q);
4948                         return IPR_RC_JOB_RETURN;
4949                 }
4950         }
4951
4952         ENTER;
4953         return IPR_RC_JOB_CONTINUE;
4954 }
4955
4956 /**
4957  * ipr_reset_start_bist - Run BIST on the adapter.
4958  * @ipr_cmd:    ipr command struct
4959  *
4960  * Description: This function runs BIST on the adapter, then delays 2 seconds.
4961  *
4962  * Return value:
4963  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4964  **/
4965 static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
4966 {
4967         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4968         int rc;
4969
4970         ENTER;
4971         pci_block_user_cfg_access(ioa_cfg->pdev);
4972         rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);
4973
4974         if (rc != PCIBIOS_SUCCESSFUL) {
4975                 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
4976                 rc = IPR_RC_JOB_CONTINUE;
4977         } else {
4978                 ipr_cmd->job_step = ipr_reset_restore_cfg_space;
4979                 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
4980                 rc = IPR_RC_JOB_RETURN;
4981         }
4982
4983         LEAVE;
4984         return rc;
4985 }
4986
4987 /**
4988  * ipr_reset_allowed - Query whether or not IOA can be reset
4989  * @ioa_cfg:    ioa config struct
4990  *
4991  * Return value:
4992  *      0 if reset not allowed / non-zero if reset is allowed
4993  **/
4994 static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
4995 {
4996         volatile u32 temp_reg;
4997
4998         temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
4999         return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
5000 }
5001
5002 /**
5003  * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
5004  * @ipr_cmd:    ipr command struct
5005  *
5006  * Description: This function waits for adapter permission to run BIST,
5007  * then runs BIST. If the adapter does not give permission after a
5008  * reasonable time, we will reset the adapter anyway. The impact of
5009  * resetting the adapter without warning the adapter is the risk of
5010  * losing the persistent error log on the adapter. If the adapter is
5011  * reset while it is writing to the flash on the adapter, the flash
5012  * segment will have bad ECC and be zeroed.
5013  *
5014  * Return value:
5015  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5016  **/
5017 static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
5018 {
5019         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5020         int rc = IPR_RC_JOB_RETURN;
5021
5022         if (!ipr_reset_allowed(ioa_cfg) && ipr_cmd->u.time_left) {
5023                 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
5024                 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
5025         } else {
5026                 ipr_cmd->job_step = ipr_reset_start_bist;
5027                 rc = IPR_RC_JOB_CONTINUE;
5028         }
5029
5030         return rc;
5031 }
5032
5033 /**
5034  * ipr_reset_alert_part2 - Alert the adapter of a pending reset
5035  * @ipr_cmd:    ipr command struct
5036  *
5037  * Description: This function alerts the adapter that it will be reset.
5038  * If memory space is not currently enabled, proceed directly
5039  * to running BIST on the adapter. The timer must always be started
5040  * so we guarantee we do not run BIST from ipr_isr.
5041  *
5042  * Return value:
5043  *      IPR_RC_JOB_RETURN
5044  **/
5045 static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
5046 {
5047         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5048         u16 cmd_reg;
5049         int rc;
5050
5051         ENTER;
5052         rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);
5053
5054         if ((rc == PCIBIOS_SUCCESSFUL) && (cmd_reg & PCI_COMMAND_MEMORY)) {
5055                 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
5056                 writel(IPR_UPROCI_RESET_ALERT, ioa_cfg->regs.set_uproc_interrupt_reg);
5057                 ipr_cmd->job_step = ipr_reset_wait_to_start_bist;
5058         } else {
5059                 ipr_cmd->job_step = ipr_reset_start_bist;
5060         }
5061
5062         ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
5063         ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
5064
5065         LEAVE;
5066         return IPR_RC_JOB_RETURN;
5067 }
5068
5069 /**
5070  * ipr_reset_ucode_download_done - Microcode download completion
5071  * @ipr_cmd:    ipr command struct
5072  *
5073  * Description: This function unmaps the microcode download buffer.
5074  *
5075  * Return value:
5076  *      IPR_RC_JOB_CONTINUE
5077  **/
5078 static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
5079 {
5080         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5081         struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
5082
5083         pci_unmap_sg(ioa_cfg->pdev, sglist->scatterlist,
5084                      sglist->num_sg, DMA_TO_DEVICE);
5085
5086         ipr_cmd->job_step = ipr_reset_alert;
5087         return IPR_RC_JOB_CONTINUE;
5088 }
5089
5090 /**
5091  * ipr_reset_ucode_download - Download microcode to the adapter
5092  * @ipr_cmd:    ipr command struct
5093  *
5094  * Description: This function checks to see if it there is microcode
5095  * to download to the adapter. If there is, a download is performed.
5096  *
5097  * Return value:
5098  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5099  **/
5100 static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
5101 {
5102         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5103         struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
5104
5105         ENTER;
5106         ipr_cmd->job_step = ipr_reset_alert;
5107
5108         if (!sglist)
5109                 return IPR_RC_JOB_CONTINUE;
5110
5111         ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5112         ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
5113         ipr_cmd->ioarcb.cmd_pkt.cdb[0] = WRITE_BUFFER;
5114         ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_WR_BUF_DOWNLOAD_AND_SAVE;
5115         ipr_cmd->ioarcb.cmd_pkt.cdb[6] = (sglist->buffer_len & 0xff0000) >> 16;
5116         ipr_cmd->ioarcb.cmd_pkt.cdb[7] = (sglist->buffer_len & 0x00ff00) >> 8;
5117         ipr_cmd->ioarcb.cmd_pkt.cdb[8] = sglist->buffer_len & 0x0000ff;
5118
5119         if (ipr_map_ucode_buffer(ipr_cmd, sglist, sglist->buffer_len)) {
5120                 dev_err(&ioa_cfg->pdev->dev,
5121                         "Failed to map microcode download buffer\n");
5122                 return IPR_RC_JOB_CONTINUE;
5123         }
5124
5125         ipr_cmd->job_step = ipr_reset_ucode_download_done;
5126
5127         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
5128                    IPR_WRITE_BUFFER_TIMEOUT);
5129
5130         LEAVE;
5131         return IPR_RC_JOB_RETURN;
5132 }
5133
5134 /**
5135  * ipr_reset_shutdown_ioa - Shutdown the adapter
5136  * @ipr_cmd:    ipr command struct
5137  *
5138  * Description: This function issues an adapter shutdown of the
5139  * specified type to the specified adapter as part of the
5140  * adapter reset job.
5141  *
5142  * Return value:
5143  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5144  **/
5145 static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
5146 {
5147         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5148         enum ipr_shutdown_type shutdown_type = ipr_cmd->u.shutdown_type;
5149         unsigned long timeout;
5150         int rc = IPR_RC_JOB_CONTINUE;
5151
5152         ENTER;
5153         if (shutdown_type != IPR_SHUTDOWN_NONE && !ioa_cfg->ioa_is_dead) {
5154                 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5155                 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5156                 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
5157                 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = shutdown_type;
5158
5159                 if (shutdown_type == IPR_SHUTDOWN_ABBREV)
5160                         timeout = IPR_ABBREV_SHUTDOWN_TIMEOUT;
5161                 else if (shutdown_type == IPR_SHUTDOWN_PREPARE_FOR_NORMAL)
5162                         timeout = IPR_INTERNAL_TIMEOUT;
5163                 else
5164                         timeout = IPR_SHUTDOWN_TIMEOUT;
5165
5166                 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);
5167
5168                 rc = IPR_RC_JOB_RETURN;
5169                 ipr_cmd->job_step = ipr_reset_ucode_download;
5170         } else
5171                 ipr_cmd->job_step = ipr_reset_alert;
5172
5173         LEAVE;
5174         return rc;
5175 }
5176
5177 /**
5178  * ipr_reset_ioa_job - Adapter reset job
5179  * @ipr_cmd:    ipr command struct
5180  *
5181  * Description: This function is the job router for the adapter reset job.
5182  *
5183  * Return value:
5184  *      none
5185  **/
5186 static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
5187 {
5188         u32 rc, ioasc;
5189         unsigned long scratch = ipr_cmd->u.scratch;
5190         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5191
5192         do {
5193                 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5194
5195                 if (ioa_cfg->reset_cmd != ipr_cmd) {
5196                         /*
5197                          * We are doing nested adapter resets and this is
5198                          * not the current reset job.
5199                          */
5200                         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5201                         return;
5202                 }
5203
5204                 if (IPR_IOASC_SENSE_KEY(ioasc)) {
5205                         dev_err(&ioa_cfg->pdev->dev,
5206                                 "0x%02X failed with IOASC: 0x%08X\n",
5207                                 ipr_cmd->ioarcb.cmd_pkt.cdb[0], ioasc);
5208
5209                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5210                         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5211                         return;
5212                 }
5213
5214                 ipr_reinit_ipr_cmnd(ipr_cmd);
5215                 ipr_cmd->u.scratch = scratch;
5216                 rc = ipr_cmd->job_step(ipr_cmd);
5217         } while(rc == IPR_RC_JOB_CONTINUE);
5218 }
5219
5220 /**
5221  * _ipr_initiate_ioa_reset - Initiate an adapter reset
5222  * @ioa_cfg:            ioa config struct
5223  * @job_step:           first job step of reset job
5224  * @shutdown_type:      shutdown type
5225  *
5226  * Description: This function will initiate the reset of the given adapter
5227  * starting at the selected job step.
5228  * If the caller needs to wait on the completion of the reset,
5229  * the caller must sleep on the reset_wait_q.
5230  *
5231  * Return value:
5232  *      none
5233  **/
5234 static void _ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
5235                                     int (*job_step) (struct ipr_cmnd *),
5236                                     enum ipr_shutdown_type shutdown_type)
5237 {
5238         struct ipr_cmnd *ipr_cmd;
5239
5240         ioa_cfg->in_reset_reload = 1;
5241         ioa_cfg->allow_cmds = 0;
5242         scsi_block_requests(ioa_cfg->host);
5243
5244         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5245         ioa_cfg->reset_cmd = ipr_cmd;
5246         ipr_cmd->job_step = job_step;
5247         ipr_cmd->u.shutdown_type = shutdown_type;
5248
5249         ipr_reset_ioa_job(ipr_cmd);
5250 }
5251
5252 /**
5253  * ipr_initiate_ioa_reset - Initiate an adapter reset
5254  * @ioa_cfg:            ioa config struct
5255  * @shutdown_type:      shutdown type
5256  *
5257  * Description: This function will initiate the reset of the given adapter.
5258  * If the caller needs to wait on the completion of the reset,
5259  * the caller must sleep on the reset_wait_q.
5260  *
5261  * Return value:
5262  *      none
5263  **/
5264 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
5265                                    enum ipr_shutdown_type shutdown_type)
5266 {
5267         if (ioa_cfg->ioa_is_dead)
5268                 return;
5269
5270         if (ioa_cfg->in_reset_reload && ioa_cfg->sdt_state == GET_DUMP)
5271                 ioa_cfg->sdt_state = ABORT_DUMP;
5272
5273         if (ioa_cfg->reset_retries++ >= IPR_NUM_RESET_RELOAD_RETRIES) {
5274                 dev_err(&ioa_cfg->pdev->dev,
5275                         "IOA taken offline - error recovery failed\n");
5276
5277                 ioa_cfg->reset_retries = 0;
5278                 ioa_cfg->ioa_is_dead = 1;
5279
5280                 if (ioa_cfg->in_ioa_bringdown) {
5281                         ioa_cfg->reset_cmd = NULL;
5282                         ioa_cfg->in_reset_reload = 0;
5283                         ipr_fail_all_ops(ioa_cfg);
5284                         wake_up_all(&ioa_cfg->reset_wait_q);
5285
5286                         spin_unlock_irq(ioa_cfg->host->host_lock);
5287                         scsi_unblock_requests(ioa_cfg->host);
5288                         spin_lock_irq(ioa_cfg->host->host_lock);
5289                         return;
5290                 } else {
5291                         ioa_cfg->in_ioa_bringdown = 1;
5292                         shutdown_type = IPR_SHUTDOWN_NONE;
5293                 }
5294         }
5295
5296         _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
5297                                 shutdown_type);
5298 }
5299
5300 /**
5301  * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
5302  * @ioa_cfg:    ioa cfg struct
5303  *
5304  * Description: This is the second phase of adapter intialization
5305  * This function takes care of initilizing the adapter to the point
5306  * where it can accept new commands.
5307
5308  * Return value:
5309  *      0 on sucess / -EIO on failure
5310  **/
5311 static int __devinit ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
5312 {
5313         int rc = 0;
5314         unsigned long host_lock_flags = 0;
5315
5316         ENTER;
5317         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5318         dev_dbg(&ioa_cfg->pdev->dev, "ioa_cfg adx: 0x%p\n", ioa_cfg);
5319         _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_enable_ioa, IPR_SHUTDOWN_NONE);
5320
5321         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5322         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5323         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5324
5325         if (ioa_cfg->ioa_is_dead) {
5326                 rc = -EIO;
5327         } else if (ipr_invalid_adapter(ioa_cfg)) {
5328                 if (!ipr_testmode)
5329                         rc = -EIO;
5330
5331                 dev_err(&ioa_cfg->pdev->dev,
5332                         "Adapter not supported in this hardware configuration.\n");
5333         }
5334
5335         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5336
5337         LEAVE;
5338         return rc;
5339 }
5340
5341 /**
5342  * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
5343  * @ioa_cfg:    ioa config struct
5344  *
5345  * Return value:
5346  *      none
5347  **/
5348 static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
5349 {
5350         int i;
5351
5352         for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
5353                 if (ioa_cfg->ipr_cmnd_list[i])
5354                         pci_pool_free(ioa_cfg->ipr_cmd_pool,
5355                                       ioa_cfg->ipr_cmnd_list[i],
5356                                       ioa_cfg->ipr_cmnd_list_dma[i]);
5357
5358                 ioa_cfg->ipr_cmnd_list[i] = NULL;
5359         }
5360
5361         if (ioa_cfg->ipr_cmd_pool)
5362                 pci_pool_destroy (ioa_cfg->ipr_cmd_pool);
5363
5364         ioa_cfg->ipr_cmd_pool = NULL;
5365 }
5366
5367 /**
5368  * ipr_free_mem - Frees memory allocated for an adapter
5369  * @ioa_cfg:    ioa cfg struct
5370  *
5371  * Return value:
5372  *      nothing
5373  **/
5374 static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
5375 {
5376         int i;
5377
5378         kfree(ioa_cfg->res_entries);
5379         pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_misc_cbs),
5380                             ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
5381         ipr_free_cmd_blks(ioa_cfg);
5382         pci_free_consistent(ioa_cfg->pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
5383                             ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
5384         pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_config_table),
5385                             ioa_cfg->cfg_table,
5386                             ioa_cfg->cfg_table_dma);
5387
5388         for (i = 0; i < IPR_NUM_HCAMS; i++) {
5389                 pci_free_consistent(ioa_cfg->pdev,
5390                                     sizeof(struct ipr_hostrcb),
5391                                     ioa_cfg->hostrcb[i],
5392                                     ioa_cfg->hostrcb_dma[i]);
5393         }
5394
5395         ipr_free_dump(ioa_cfg);
5396         kfree(ioa_cfg->saved_mode_pages);
5397         kfree(ioa_cfg->trace);
5398 }
5399
5400 /**
5401  * ipr_free_all_resources - Free all allocated resources for an adapter.
5402  * @ipr_cmd:    ipr command struct
5403  *
5404  * This function frees all allocated resources for the
5405  * specified adapter.
5406  *
5407  * Return value:
5408  *      none
5409  **/
5410 static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
5411 {
5412         struct pci_dev *pdev = ioa_cfg->pdev;
5413
5414         ENTER;
5415         free_irq(pdev->irq, ioa_cfg);
5416         iounmap(ioa_cfg->hdw_dma_regs);
5417         pci_release_regions(pdev);
5418         ipr_free_mem(ioa_cfg);
5419         scsi_host_put(ioa_cfg->host);
5420         pci_disable_device(pdev);
5421         LEAVE;
5422 }
5423
5424 /**
5425  * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
5426  * @ioa_cfg:    ioa config struct
5427  *
5428  * Return value:
5429  *      0 on success / -ENOMEM on allocation failure
5430  **/
5431 static int __devinit ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
5432 {
5433         struct ipr_cmnd *ipr_cmd;
5434         struct ipr_ioarcb *ioarcb;
5435         dma_addr_t dma_addr;
5436         int i;
5437
5438         ioa_cfg->ipr_cmd_pool = pci_pool_create (IPR_NAME, ioa_cfg->pdev,
5439                                                  sizeof(struct ipr_cmnd), 8, 0);
5440
5441         if (!ioa_cfg->ipr_cmd_pool)
5442                 return -ENOMEM;
5443
5444         for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
5445                 ipr_cmd = pci_pool_alloc (ioa_cfg->ipr_cmd_pool, SLAB_KERNEL, &dma_addr);
5446
5447                 if (!ipr_cmd) {
5448                         ipr_free_cmd_blks(ioa_cfg);
5449                         return -ENOMEM;
5450                 }
5451
5452                 memset(ipr_cmd, 0, sizeof(*ipr_cmd));
5453                 ioa_cfg->ipr_cmnd_list[i] = ipr_cmd;
5454                 ioa_cfg->ipr_cmnd_list_dma[i] = dma_addr;
5455
5456                 ioarcb = &ipr_cmd->ioarcb;
5457                 ioarcb->ioarcb_host_pci_addr = cpu_to_be32(dma_addr);
5458                 ioarcb->host_response_handle = cpu_to_be32(i << 2);
5459                 ioarcb->write_ioadl_addr =
5460                         cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
5461                 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
5462                 ioarcb->ioasa_host_pci_addr =
5463                         cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioasa));
5464                 ioarcb->ioasa_len = cpu_to_be16(sizeof(struct ipr_ioasa));
5465                 ipr_cmd->cmd_index = i;
5466                 ipr_cmd->ioa_cfg = ioa_cfg;
5467                 ipr_cmd->sense_buffer_dma = dma_addr +
5468                         offsetof(struct ipr_cmnd, sense_buffer);
5469
5470                 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5471         }
5472
5473         return 0;
5474 }
5475
5476 /**
5477  * ipr_alloc_mem - Allocate memory for an adapter
5478  * @ioa_cfg:    ioa config struct
5479  *
5480  * Return value:
5481  *      0 on success / non-zero for error
5482  **/
5483 static int __devinit ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
5484 {
5485         struct pci_dev *pdev = ioa_cfg->pdev;
5486         int i, rc = -ENOMEM;
5487
5488         ENTER;
5489         ioa_cfg->res_entries = kmalloc(sizeof(struct ipr_resource_entry) *
5490                                        IPR_MAX_PHYSICAL_DEVS, GFP_KERNEL);
5491
5492         if (!ioa_cfg->res_entries)
5493                 goto out;
5494
5495         memset(ioa_cfg->res_entries, 0,
5496                sizeof(struct ipr_resource_entry) * IPR_MAX_PHYSICAL_DEVS);
5497
5498         for (i = 0; i < IPR_MAX_PHYSICAL_DEVS; i++)
5499                 list_add_tail(&ioa_cfg->res_entries[i].queue, &ioa_cfg->free_res_q);
5500
5501         ioa_cfg->vpd_cbs = pci_alloc_consistent(ioa_cfg->pdev,
5502                                                 sizeof(struct ipr_misc_cbs),
5503                                                 &ioa_cfg->vpd_cbs_dma);
5504
5505         if (!ioa_cfg->vpd_cbs)
5506                 goto out_free_res_entries;
5507
5508         if (ipr_alloc_cmd_blks(ioa_cfg))
5509                 goto out_free_vpd_cbs;
5510
5511         ioa_cfg->host_rrq = pci_alloc_consistent(ioa_cfg->pdev,
5512                                                  sizeof(u32) * IPR_NUM_CMD_BLKS,
5513                                                  &ioa_cfg->host_rrq_dma);
5514
5515         if (!ioa_cfg->host_rrq)
5516                 goto out_ipr_free_cmd_blocks;
5517
5518         ioa_cfg->cfg_table = pci_alloc_consistent(ioa_cfg->pdev,
5519                                                   sizeof(struct ipr_config_table),
5520                                                   &ioa_cfg->cfg_table_dma);
5521
5522         if (!ioa_cfg->cfg_table)
5523                 goto out_free_host_rrq;
5524
5525         for (i = 0; i < IPR_NUM_HCAMS; i++) {
5526                 ioa_cfg->hostrcb[i] = pci_alloc_consistent(ioa_cfg->pdev,
5527                                                            sizeof(struct ipr_hostrcb),
5528                                                            &ioa_cfg->hostrcb_dma[i]);
5529
5530                 if (!ioa_cfg->hostrcb[i])
5531                         goto out_free_hostrcb_dma;
5532
5533                 ioa_cfg->hostrcb[i]->hostrcb_dma =
5534                         ioa_cfg->hostrcb_dma[i] + offsetof(struct ipr_hostrcb, hcam);
5535                 list_add_tail(&ioa_cfg->hostrcb[i]->queue, &ioa_cfg->hostrcb_free_q);
5536         }
5537
5538         ioa_cfg->trace = kmalloc(sizeof(struct ipr_trace_entry) *
5539                                  IPR_NUM_TRACE_ENTRIES, GFP_KERNEL);
5540
5541         if (!ioa_cfg->trace)
5542                 goto out_free_hostrcb_dma;
5543
5544         memset(ioa_cfg->trace, 0,
5545                sizeof(struct ipr_trace_entry) * IPR_NUM_TRACE_ENTRIES);
5546
5547         rc = 0;
5548 out:
5549         LEAVE;
5550         return rc;
5551
5552 out_free_hostrcb_dma:
5553         while (i-- > 0) {
5554                 pci_free_consistent(pdev, sizeof(struct ipr_hostrcb),
5555                                     ioa_cfg->hostrcb[i],
5556                                     ioa_cfg->hostrcb_dma[i]);
5557         }
5558         pci_free_consistent(pdev, sizeof(struct ipr_config_table),
5559                             ioa_cfg->cfg_table, ioa_cfg->cfg_table_dma);
5560 out_free_host_rrq:
5561         pci_free_consistent(pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
5562                             ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
5563 out_ipr_free_cmd_blocks:
5564         ipr_free_cmd_blks(ioa_cfg);
5565 out_free_vpd_cbs:
5566         pci_free_consistent(pdev, sizeof(struct ipr_misc_cbs),
5567                             ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
5568 out_free_res_entries:
5569         kfree(ioa_cfg->res_entries);
5570         goto out;
5571 }
5572
5573 /**
5574  * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
5575  * @ioa_cfg:    ioa config struct
5576  *
5577  * Return value:
5578  *      none
5579  **/
5580 static void __devinit ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
5581 {
5582         int i;
5583
5584         for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
5585                 ioa_cfg->bus_attr[i].bus = i;
5586                 ioa_cfg->bus_attr[i].qas_enabled = 0;
5587                 ioa_cfg->bus_attr[i].bus_width = IPR_DEFAULT_BUS_WIDTH;
5588                 if (ipr_max_speed < ARRAY_SIZE(ipr_max_bus_speeds))
5589                         ioa_cfg->bus_attr[i].max_xfer_rate = ipr_max_bus_speeds[ipr_max_speed];
5590                 else
5591                         ioa_cfg->bus_attr[i].max_xfer_rate = IPR_U160_SCSI_RATE;
5592         }
5593 }
5594
5595 /**
5596  * ipr_init_ioa_cfg - Initialize IOA config struct
5597  * @ioa_cfg:    ioa config struct
5598  * @host:               scsi host struct
5599  * @pdev:               PCI dev struct
5600  *
5601  * Return value:
5602  *      none
5603  **/
5604 static void __devinit ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
5605                                        struct Scsi_Host *host, struct pci_dev *pdev)
5606 {
5607         const struct ipr_interrupt_offsets *p;
5608         struct ipr_interrupts *t;
5609         void __iomem *base;
5610
5611         ioa_cfg->host = host;
5612         ioa_cfg->pdev = pdev;
5613         ioa_cfg->log_level = ipr_log_level;
5614         sprintf(ioa_cfg->eye_catcher, IPR_EYECATCHER);
5615         sprintf(ioa_cfg->trace_start, IPR_TRACE_START_LABEL);
5616         sprintf(ioa_cfg->ipr_free_label, IPR_FREEQ_LABEL);
5617         sprintf(ioa_cfg->ipr_pending_label, IPR_PENDQ_LABEL);
5618         sprintf(ioa_cfg->cfg_table_start, IPR_CFG_TBL_START);
5619         sprintf(ioa_cfg->resource_table_label, IPR_RES_TABLE_LABEL);
5620         sprintf(ioa_cfg->ipr_hcam_label, IPR_HCAM_LABEL);
5621         sprintf(ioa_cfg->ipr_cmd_label, IPR_CMD_LABEL);
5622
5623         INIT_LIST_HEAD(&ioa_cfg->free_q);
5624         INIT_LIST_HEAD(&ioa_cfg->pending_q);
5625         INIT_LIST_HEAD(&ioa_cfg->hostrcb_free_q);
5626         INIT_LIST_HEAD(&ioa_cfg->hostrcb_pending_q);
5627         INIT_LIST_HEAD(&ioa_cfg->free_res_q);
5628         INIT_LIST_HEAD(&ioa_cfg->used_res_q);
5629         INIT_WORK(&ioa_cfg->work_q, ipr_worker_thread, ioa_cfg);
5630         init_waitqueue_head(&ioa_cfg->reset_wait_q);
5631         ioa_cfg->sdt_state = INACTIVE;
5632
5633         ipr_initialize_bus_attr(ioa_cfg);
5634
5635         host->max_id = IPR_MAX_NUM_TARGETS_PER_BUS;
5636         host->max_lun = IPR_MAX_NUM_LUNS_PER_TARGET;
5637         host->max_channel = IPR_MAX_BUS_TO_SCAN;
5638         host->unique_id = host->host_no;
5639         host->max_cmd_len = IPR_MAX_CDB_LEN;
5640         pci_set_drvdata(pdev, ioa_cfg);
5641
5642         p = &ioa_cfg->chip_cfg->regs;
5643         t = &ioa_cfg->regs;
5644         base = ioa_cfg->hdw_dma_regs;
5645
5646         t->set_interrupt_mask_reg = base + p->set_interrupt_mask_reg;
5647         t->clr_interrupt_mask_reg = base + p->clr_interrupt_mask_reg;
5648         t->sense_interrupt_mask_reg = base + p->sense_interrupt_mask_reg;
5649         t->clr_interrupt_reg = base + p->clr_interrupt_reg;
5650         t->sense_interrupt_reg = base + p->sense_interrupt_reg;
5651         t->ioarrin_reg = base + p->ioarrin_reg;
5652         t->sense_uproc_interrupt_reg = base + p->sense_uproc_interrupt_reg;
5653         t->set_uproc_interrupt_reg = base + p->set_uproc_interrupt_reg;
5654         t->clr_uproc_interrupt_reg = base + p->clr_uproc_interrupt_reg;
5655 }
5656
5657 /**
5658  * ipr_get_chip_cfg - Find adapter chip configuration
5659  * @dev_id:             PCI device id struct
5660  *
5661  * Return value:
5662  *      ptr to chip config on success / NULL on failure
5663  **/
5664 static const struct ipr_chip_cfg_t * __devinit
5665 ipr_get_chip_cfg(const struct pci_device_id *dev_id)
5666 {
5667         int i;
5668
5669         if (dev_id->driver_data)
5670                 return (const struct ipr_chip_cfg_t *)dev_id->driver_data;
5671
5672         for (i = 0; i < ARRAY_SIZE(ipr_chip); i++)
5673                 if (ipr_chip[i].vendor == dev_id->vendor &&
5674                     ipr_chip[i].device == dev_id->device)
5675                         return ipr_chip[i].cfg;
5676         return NULL;
5677 }
5678
5679 /**
5680  * ipr_probe_ioa - Allocates memory and does first stage of initialization
5681  * @pdev:               PCI device struct
5682  * @dev_id:             PCI device id struct
5683  *
5684  * Return value:
5685  *      0 on success / non-zero on failure
5686  **/
5687 static int __devinit ipr_probe_ioa(struct pci_dev *pdev,
5688                                    const struct pci_device_id *dev_id)
5689 {
5690         struct ipr_ioa_cfg *ioa_cfg;
5691         struct Scsi_Host *host;
5692         unsigned long ipr_regs_pci;
5693         void __iomem *ipr_regs;
5694         u32 rc = PCIBIOS_SUCCESSFUL;
5695
5696         ENTER;
5697
5698         if ((rc = pci_enable_device(pdev))) {
5699                 dev_err(&pdev->dev, "Cannot enable adapter\n");
5700                 goto out;
5701         }
5702
5703         dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
5704
5705         host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));
5706
5707         if (!host) {
5708                 dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
5709                 rc = -ENOMEM;
5710                 goto out_disable;
5711         }
5712
5713         ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
5714         memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
5715
5716         ioa_cfg->chip_cfg = ipr_get_chip_cfg(dev_id);
5717
5718         if (!ioa_cfg->chip_cfg) {
5719                 dev_err(&pdev->dev, "Unknown adapter chipset 0x%04X 0x%04X\n",
5720                         dev_id->vendor, dev_id->device);
5721                 goto out_scsi_host_put;
5722         }
5723
5724         ipr_regs_pci = pci_resource_start(pdev, 0);
5725
5726         rc = pci_request_regions(pdev, IPR_NAME);
5727         if (rc < 0) {
5728                 dev_err(&pdev->dev,
5729                         "Couldn't register memory range of registers\n");
5730                 goto out_scsi_host_put;
5731         }
5732
5733         ipr_regs = ioremap(ipr_regs_pci, pci_resource_len(pdev, 0));
5734
5735         if (!ipr_regs) {
5736                 dev_err(&pdev->dev,
5737                         "Couldn't map memory range of registers\n");
5738                 rc = -ENOMEM;
5739                 goto out_release_regions;
5740         }
5741
5742         ioa_cfg->hdw_dma_regs = ipr_regs;
5743         ioa_cfg->hdw_dma_regs_pci = ipr_regs_pci;
5744         ioa_cfg->ioa_mailbox = ioa_cfg->chip_cfg->mailbox + ipr_regs;
5745
5746         ipr_init_ioa_cfg(ioa_cfg, host, pdev);
5747
5748         pci_set_master(pdev);
5749
5750         rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
5751         if (rc < 0) {
5752                 dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
5753                 goto cleanup_nomem;
5754         }
5755
5756         rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
5757                                    ioa_cfg->chip_cfg->cache_line_size);
5758
5759         if (rc != PCIBIOS_SUCCESSFUL) {
5760                 dev_err(&pdev->dev, "Write of cache line size failed\n");
5761                 rc = -EIO;
5762                 goto cleanup_nomem;
5763         }
5764
5765         /* Save away PCI config space for use following IOA reset */
5766         rc = pci_save_state(pdev);
5767
5768         if (rc != PCIBIOS_SUCCESSFUL) {
5769                 dev_err(&pdev->dev, "Failed to save PCI config space\n");
5770                 rc = -EIO;
5771                 goto cleanup_nomem;
5772         }
5773
5774         if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
5775                 goto cleanup_nomem;
5776
5777         if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
5778                 goto cleanup_nomem;
5779
5780         rc = ipr_alloc_mem(ioa_cfg);
5781         if (rc < 0) {
5782                 dev_err(&pdev->dev,
5783                         "Couldn't allocate enough memory for device driver!\n");
5784                 goto cleanup_nomem;
5785         }
5786
5787         ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
5788         rc = request_irq(pdev->irq, ipr_isr, SA_SHIRQ, IPR_NAME, ioa_cfg);
5789
5790         if (rc) {
5791                 dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
5792                         pdev->irq, rc);
5793                 goto cleanup_nolog;
5794         }
5795
5796         spin_lock(&ipr_driver_lock);
5797         list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
5798         spin_unlock(&ipr_driver_lock);
5799
5800         LEAVE;
5801 out:
5802         return rc;
5803
5804 cleanup_nolog:
5805         ipr_free_mem(ioa_cfg);
5806 cleanup_nomem:
5807         iounmap(ipr_regs);
5808 out_release_regions:
5809         pci_release_regions(pdev);
5810 out_scsi_host_put:
5811         scsi_host_put(host);
5812 out_disable:
5813         pci_disable_device(pdev);
5814         goto out;
5815 }
5816
5817 /**
5818  * ipr_scan_vsets - Scans for VSET devices
5819  * @ioa_cfg:    ioa config struct
5820  *
5821  * Description: Since the VSET resources do not follow SAM in that we can have
5822  * sparse LUNs with no LUN 0, we have to scan for these ourselves.
5823  *
5824  * Return value:
5825  *      none
5826  **/
5827 static void ipr_scan_vsets(struct ipr_ioa_cfg *ioa_cfg)
5828 {
5829         int target, lun;
5830
5831         for (target = 0; target < IPR_MAX_NUM_TARGETS_PER_BUS; target++)
5832                 for (lun = 0; lun < IPR_MAX_NUM_VSET_LUNS_PER_TARGET; lun++ )
5833                         scsi_add_device(ioa_cfg->host, IPR_VSET_BUS, target, lun);
5834 }
5835
5836 /**
5837  * ipr_initiate_ioa_bringdown - Bring down an adapter
5838  * @ioa_cfg:            ioa config struct
5839  * @shutdown_type:      shutdown type
5840  *
5841  * Description: This function will initiate bringing down the adapter.
5842  * This consists of issuing an IOA shutdown to the adapter
5843  * to flush the cache, and running BIST.
5844  * If the caller needs to wait on the completion of the reset,
5845  * the caller must sleep on the reset_wait_q.
5846  *
5847  * Return value:
5848  *      none
5849  **/
5850 static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
5851                                        enum ipr_shutdown_type shutdown_type)
5852 {
5853         ENTER;
5854         if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
5855                 ioa_cfg->sdt_state = ABORT_DUMP;
5856         ioa_cfg->reset_retries = 0;
5857         ioa_cfg->in_ioa_bringdown = 1;
5858         ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
5859         LEAVE;
5860 }
5861
5862 /**
5863  * __ipr_remove - Remove a single adapter
5864  * @pdev:       pci device struct
5865  *
5866  * Adapter hot plug remove entry point.
5867  *
5868  * Return value:
5869  *      none
5870  **/
5871 static void __ipr_remove(struct pci_dev *pdev)
5872 {
5873         unsigned long host_lock_flags = 0;
5874         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
5875         ENTER;
5876
5877         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5878         ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
5879
5880         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5881         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5882         flush_scheduled_work();
5883         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5884
5885         spin_lock(&ipr_driver_lock);
5886         list_del(&ioa_cfg->queue);
5887         spin_unlock(&ipr_driver_lock);
5888
5889         if (ioa_cfg->sdt_state == ABORT_DUMP)
5890                 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
5891         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5892
5893         ipr_free_all_resources(ioa_cfg);
5894
5895         LEAVE;
5896 }
5897
5898 /**
5899  * ipr_remove - IOA hot plug remove entry point
5900  * @pdev:       pci device struct
5901  *
5902  * Adapter hot plug remove entry point.
5903  *
5904  * Return value:
5905  *      none
5906  **/
5907 static void ipr_remove(struct pci_dev *pdev)
5908 {
5909         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
5910
5911         ENTER;
5912
5913         ipr_remove_trace_file(&ioa_cfg->host->shost_classdev.kobj,
5914                               &ipr_trace_attr);
5915         ipr_remove_dump_file(&ioa_cfg->host->shost_classdev.kobj,
5916                              &ipr_dump_attr);
5917         scsi_remove_host(ioa_cfg->host);
5918
5919         __ipr_remove(pdev);
5920
5921         LEAVE;
5922 }
5923
5924 /**
5925  * ipr_probe - Adapter hot plug add entry point
5926  *
5927  * Return value:
5928  *      0 on success / non-zero on failure
5929  **/
5930 static int __devinit ipr_probe(struct pci_dev *pdev,
5931                                const struct pci_device_id *dev_id)
5932 {
5933         struct ipr_ioa_cfg *ioa_cfg;
5934         int rc;
5935
5936         rc = ipr_probe_ioa(pdev, dev_id);
5937
5938         if (rc)
5939                 return rc;
5940
5941         ioa_cfg = pci_get_drvdata(pdev);
5942         rc = ipr_probe_ioa_part2(ioa_cfg);
5943
5944         if (rc) {
5945                 __ipr_remove(pdev);
5946                 return rc;
5947         }
5948
5949         rc = scsi_add_host(ioa_cfg->host, &pdev->dev);
5950
5951         if (rc) {
5952                 __ipr_remove(pdev);
5953                 return rc;
5954         }
5955
5956         rc = ipr_create_trace_file(&ioa_cfg->host->shost_classdev.kobj,
5957                                    &ipr_trace_attr);
5958
5959         if (rc) {
5960                 scsi_remove_host(ioa_cfg->host);
5961                 __ipr_remove(pdev);
5962                 return rc;
5963         }
5964
5965         rc = ipr_create_dump_file(&ioa_cfg->host->shost_classdev.kobj,
5966                                    &ipr_dump_attr);
5967
5968         if (rc) {
5969                 ipr_remove_trace_file(&ioa_cfg->host->shost_classdev.kobj,
5970                                       &ipr_trace_attr);
5971                 scsi_remove_host(ioa_cfg->host);
5972                 __ipr_remove(pdev);
5973                 return rc;
5974         }
5975
5976         scsi_scan_host(ioa_cfg->host);
5977         ipr_scan_vsets(ioa_cfg);
5978         scsi_add_device(ioa_cfg->host, IPR_IOA_BUS, IPR_IOA_TARGET, IPR_IOA_LUN);
5979         ioa_cfg->allow_ml_add_del = 1;
5980         ioa_cfg->host->max_channel = IPR_VSET_BUS;
5981         schedule_work(&ioa_cfg->work_q);
5982         return 0;
5983 }
5984
5985 /**
5986  * ipr_shutdown - Shutdown handler.
5987  * @pdev:       pci device struct
5988  *
5989  * This function is invoked upon system shutdown/reboot. It will issue
5990  * an adapter shutdown to the adapter to flush the write cache.
5991  *
5992  * Return value:
5993  *      none
5994  **/
5995 static void ipr_shutdown(struct pci_dev *pdev)
5996 {
5997         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
5998         unsigned long lock_flags = 0;
5999
6000         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
6001         ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
6002         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
6003         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
6004 }
6005
6006 static struct pci_device_id ipr_pci_table[] __devinitdata = {
6007         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6008                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5702,
6009                 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6010         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6011                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5703,
6012               0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6013         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6014                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573D,
6015               0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6016         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6017                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573E,
6018               0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6019         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
6020                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571B,
6021               0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6022         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
6023                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572E,
6024               0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6025         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
6026                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571A,
6027               0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6028         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
6029                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2780,
6030                 0, 0, (kernel_ulong_t)&ipr_chip_cfg[1] },
6031         { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
6032                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571E,
6033                 0, 0, (kernel_ulong_t)&ipr_chip_cfg[1] },
6034         { }
6035 };
6036 MODULE_DEVICE_TABLE(pci, ipr_pci_table);
6037
6038 static struct pci_driver ipr_driver = {
6039         .name = IPR_NAME,
6040         .id_table = ipr_pci_table,
6041         .probe = ipr_probe,
6042         .remove = ipr_remove,
6043         .shutdown = ipr_shutdown,
6044 };
6045
6046 /**
6047  * ipr_init - Module entry point
6048  *
6049  * Return value:
6050  *      0 on success / negative value on failure
6051  **/
6052 static int __init ipr_init(void)
6053 {
6054         ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
6055                  IPR_DRIVER_VERSION, IPR_DRIVER_DATE);
6056
6057         return pci_module_init(&ipr_driver);
6058 }
6059
6060 /**
6061  * ipr_exit - Module unload
6062  *
6063  * Module unload entry point.
6064  *
6065  * Return value:
6066  *      none
6067  **/
6068 static void __exit ipr_exit(void)
6069 {
6070         pci_unregister_driver(&ipr_driver);
6071 }
6072
6073 module_init(ipr_init);
6074 module_exit(ipr_exit);