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