Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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