Version:  2.0.40 2.2.26 2.4.37 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2

Linux/drivers/scsi/mpt2sas/mpt2sas_scsih.c

  1 /*
  2  * Scsi Host Layer for MPT (Message Passing Technology) based controllers
  3  *
  4  * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
  5  * Copyright (C) 2007-2014  LSI Corporation
  6  * Copyright (C) 20013-2014 Avago Technologies
  7  *  (mailto: MPT-FusionLinux.pdl@avagotech.com)
  8  *
  9  * This program is free software; you can redistribute it and/or
 10  * modify it under the terms of the GNU General Public License
 11  * as published by the Free Software Foundation; either version 2
 12  * of the License, or (at your option) any later version.
 13  *
 14  * This program is distributed in the hope that it will be useful,
 15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17  * GNU General Public License for more details.
 18  *
 19  * NO WARRANTY
 20  * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
 21  * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
 22  * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
 23  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
 24  * solely responsible for determining the appropriateness of using and
 25  * distributing the Program and assumes all risks associated with its
 26  * exercise of rights under this Agreement, including but not limited to
 27  * the risks and costs of program errors, damage to or loss of data,
 28  * programs or equipment, and unavailability or interruption of operations.
 29 
 30  * DISCLAIMER OF LIABILITY
 31  * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
 32  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 33  * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
 34  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 35  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 36  * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
 37  * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
 38 
 39  * You should have received a copy of the GNU General Public License
 40  * along with this program; if not, write to the Free Software
 41  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 42  * USA.
 43  */
 44 
 45 #include <linux/module.h>
 46 #include <linux/kernel.h>
 47 #include <linux/init.h>
 48 #include <linux/errno.h>
 49 #include <linux/blkdev.h>
 50 #include <linux/sched.h>
 51 #include <linux/workqueue.h>
 52 #include <linux/delay.h>
 53 #include <linux/pci.h>
 54 #include <linux/interrupt.h>
 55 #include <linux/aer.h>
 56 #include <linux/raid_class.h>
 57 #include <linux/slab.h>
 58 
 59 #include <asm/unaligned.h>
 60 
 61 #include "mpt2sas_base.h"
 62 
 63 MODULE_AUTHOR(MPT2SAS_AUTHOR);
 64 MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
 65 MODULE_LICENSE("GPL");
 66 MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
 67 
 68 #define RAID_CHANNEL 1
 69 
 70 /* forward proto's */
 71 static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
 72     struct _sas_node *sas_expander);
 73 static void _firmware_event_work(struct work_struct *work);
 74 
 75 static u8 _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid);
 76 
 77 static void _scsih_scan_start(struct Scsi_Host *shost);
 78 static int _scsih_scan_finished(struct Scsi_Host *shost, unsigned long time);
 79 
 80 /* global parameters */
 81 LIST_HEAD(mpt2sas_ioc_list);
 82 
 83 /* local parameters */
 84 static u8 scsi_io_cb_idx = -1;
 85 static u8 tm_cb_idx = -1;
 86 static u8 ctl_cb_idx = -1;
 87 static u8 base_cb_idx = -1;
 88 static u8 port_enable_cb_idx = -1;
 89 static u8 transport_cb_idx = -1;
 90 static u8 scsih_cb_idx = -1;
 91 static u8 config_cb_idx = -1;
 92 static int mpt_ids;
 93 
 94 static u8 tm_tr_cb_idx = -1 ;
 95 static u8 tm_tr_volume_cb_idx = -1 ;
 96 static u8 tm_sas_control_cb_idx = -1;
 97 
 98 /* command line options */
 99 static u32 logging_level;
100 MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
101     "(default=0)");
102 
103 static ushort max_sectors = 0xFFFF;
104 module_param(max_sectors, ushort, 0);
105 MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767  default=32767");
106 
107 static int missing_delay[2] = {-1, -1};
108 module_param_array(missing_delay, int, NULL, 0);
109 MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
110 
111 /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
112 #define MPT2SAS_MAX_LUN (16895)
113 static int max_lun = MPT2SAS_MAX_LUN;
114 module_param(max_lun, int, 0);
115 MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
116 
117 /* diag_buffer_enable is bitwise
118  * bit 0 set = TRACE
119  * bit 1 set = SNAPSHOT
120  * bit 2 set = EXTENDED
121  *
122  * Either bit can be set, or both
123  */
124 static int diag_buffer_enable = -1;
125 module_param(diag_buffer_enable, int, 0);
126 MODULE_PARM_DESC(diag_buffer_enable, " post diag buffers "
127         "(TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
128 
129 static int disable_discovery = -1;
130 module_param(disable_discovery, int, 0);
131 MODULE_PARM_DESC(disable_discovery, " disable discovery ");
132 
133 /* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
134 static int prot_mask = 0;
135 module_param(prot_mask, int, 0);
136 MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
137 
138 /**
139  * struct sense_info - common structure for obtaining sense keys
140  * @skey: sense key
141  * @asc: additional sense code
142  * @ascq: additional sense code qualifier
143  */
144 struct sense_info {
145         u8 skey;
146         u8 asc;
147         u8 ascq;
148 };
149 
150 
151 #define MPT2SAS_TURN_ON_PFA_LED (0xFFFC)
152 #define MPT2SAS_PORT_ENABLE_COMPLETE (0xFFFD)
153 #define MPT2SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
154 /**
155  * struct fw_event_work - firmware event struct
156  * @list: link list framework
157  * @work: work object (ioc->fault_reset_work_q)
158  * @cancel_pending_work: flag set during reset handling
159  * @ioc: per adapter object
160  * @device_handle: device handle
161  * @VF_ID: virtual function id
162  * @VP_ID: virtual port id
163  * @ignore: flag meaning this event has been marked to ignore
164  * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
165  * @event_data: reply event data payload follows
166  *
167  * This object stored on ioc->fw_event_list.
168  */
169 struct fw_event_work {
170         struct list_head        list;
171         u8                      cancel_pending_work;
172         struct delayed_work     delayed_work;
173         struct MPT2SAS_ADAPTER *ioc;
174         u16                     device_handle;
175         u8                      VF_ID;
176         u8                      VP_ID;
177         u8                      ignore;
178         u16                     event;
179         char                    event_data[0] __aligned(4);
180 };
181 
182 /* raid transport support */
183 static struct raid_template *mpt2sas_raid_template;
184 
185 /**
186  * struct _scsi_io_transfer - scsi io transfer
187  * @handle: sas device handle (assigned by firmware)
188  * @is_raid: flag set for hidden raid components
189  * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
190  * @data_length: data transfer length
191  * @data_dma: dma pointer to data
192  * @sense: sense data
193  * @lun: lun number
194  * @cdb_length: cdb length
195  * @cdb: cdb contents
196  * @timeout: timeout for this command
197  * @VF_ID: virtual function id
198  * @VP_ID: virtual port id
199  * @valid_reply: flag set for reply message
200  * @sense_length: sense length
201  * @ioc_status: ioc status
202  * @scsi_state: scsi state
203  * @scsi_status: scsi staus
204  * @log_info: log information
205  * @transfer_length: data length transfer when there is a reply message
206  *
207  * Used for sending internal scsi commands to devices within this module.
208  * Refer to _scsi_send_scsi_io().
209  */
210 struct _scsi_io_transfer {
211         u16     handle;
212         u8      is_raid;
213         enum dma_data_direction dir;
214         u32     data_length;
215         dma_addr_t data_dma;
216         u8      sense[SCSI_SENSE_BUFFERSIZE];
217         u32     lun;
218         u8      cdb_length;
219         u8      cdb[32];
220         u8      timeout;
221         u8      VF_ID;
222         u8      VP_ID;
223         u8      valid_reply;
224   /* the following bits are only valid when 'valid_reply = 1' */
225         u32     sense_length;
226         u16     ioc_status;
227         u8      scsi_state;
228         u8      scsi_status;
229         u32     log_info;
230         u32     transfer_length;
231 };
232 
233 /*
234  * The pci device ids are defined in mpi/mpi2_cnfg.h.
235  */
236 static struct pci_device_id scsih_pci_table[] = {
237         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
238                 PCI_ANY_ID, PCI_ANY_ID },
239         /* Falcon ~ 2008*/
240         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
241                 PCI_ANY_ID, PCI_ANY_ID },
242         /* Liberator ~ 2108 */
243         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
244                 PCI_ANY_ID, PCI_ANY_ID },
245         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
246                 PCI_ANY_ID, PCI_ANY_ID },
247         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
248                 PCI_ANY_ID, PCI_ANY_ID },
249         /* Meteor ~ 2116 */
250         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
251                 PCI_ANY_ID, PCI_ANY_ID },
252         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
253                 PCI_ANY_ID, PCI_ANY_ID },
254         /* Thunderbolt ~ 2208 */
255         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
256                 PCI_ANY_ID, PCI_ANY_ID },
257         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
258                 PCI_ANY_ID, PCI_ANY_ID },
259         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
260                 PCI_ANY_ID, PCI_ANY_ID },
261         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
262                 PCI_ANY_ID, PCI_ANY_ID },
263         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
264                 PCI_ANY_ID, PCI_ANY_ID },
265         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
266                 PCI_ANY_ID, PCI_ANY_ID },
267         /* Mustang ~ 2308 */
268         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
269                 PCI_ANY_ID, PCI_ANY_ID },
270         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
271                 PCI_ANY_ID, PCI_ANY_ID },
272         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
273                 PCI_ANY_ID, PCI_ANY_ID },
274         /* SSS6200 */
275         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
276                 PCI_ANY_ID, PCI_ANY_ID },
277         {0}     /* Terminating entry */
278 };
279 MODULE_DEVICE_TABLE(pci, scsih_pci_table);
280 
281 /**
282  * _scsih_set_debug_level - global setting of ioc->logging_level.
283  *
284  * Note: The logging levels are defined in mpt2sas_debug.h.
285  */
286 static int
287 _scsih_set_debug_level(const char *val, struct kernel_param *kp)
288 {
289         int ret = param_set_int(val, kp);
290         struct MPT2SAS_ADAPTER *ioc;
291 
292         if (ret)
293                 return ret;
294 
295         printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
296         list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
297                 ioc->logging_level = logging_level;
298         return 0;
299 }
300 module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
301     &logging_level, 0644);
302 
303 /**
304  * _scsih_srch_boot_sas_address - search based on sas_address
305  * @sas_address: sas address
306  * @boot_device: boot device object from bios page 2
307  *
308  * Returns 1 when there's a match, 0 means no match.
309  */
310 static inline int
311 _scsih_srch_boot_sas_address(u64 sas_address,
312     Mpi2BootDeviceSasWwid_t *boot_device)
313 {
314         return (sas_address == le64_to_cpu(boot_device->SASAddress)) ?  1 : 0;
315 }
316 
317 /**
318  * _scsih_srch_boot_device_name - search based on device name
319  * @device_name: device name specified in INDENTIFY fram
320  * @boot_device: boot device object from bios page 2
321  *
322  * Returns 1 when there's a match, 0 means no match.
323  */
324 static inline int
325 _scsih_srch_boot_device_name(u64 device_name,
326     Mpi2BootDeviceDeviceName_t *boot_device)
327 {
328         return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
329 }
330 
331 /**
332  * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
333  * @enclosure_logical_id: enclosure logical id
334  * @slot_number: slot number
335  * @boot_device: boot device object from bios page 2
336  *
337  * Returns 1 when there's a match, 0 means no match.
338  */
339 static inline int
340 _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
341     Mpi2BootDeviceEnclosureSlot_t *boot_device)
342 {
343         return (enclosure_logical_id == le64_to_cpu(boot_device->
344             EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
345             SlotNumber)) ? 1 : 0;
346 }
347 
348 /**
349  * _scsih_is_boot_device - search for matching boot device.
350  * @sas_address: sas address
351  * @device_name: device name specified in INDENTIFY fram
352  * @enclosure_logical_id: enclosure logical id
353  * @slot_number: slot number
354  * @form: specifies boot device form
355  * @boot_device: boot device object from bios page 2
356  *
357  * Returns 1 when there's a match, 0 means no match.
358  */
359 static int
360 _scsih_is_boot_device(u64 sas_address, u64 device_name,
361     u64 enclosure_logical_id, u16 slot, u8 form,
362     Mpi2BiosPage2BootDevice_t *boot_device)
363 {
364         int rc = 0;
365 
366         switch (form) {
367         case MPI2_BIOSPAGE2_FORM_SAS_WWID:
368                 if (!sas_address)
369                         break;
370                 rc = _scsih_srch_boot_sas_address(
371                     sas_address, &boot_device->SasWwid);
372                 break;
373         case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
374                 if (!enclosure_logical_id)
375                         break;
376                 rc = _scsih_srch_boot_encl_slot(
377                     enclosure_logical_id,
378                     slot, &boot_device->EnclosureSlot);
379                 break;
380         case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
381                 if (!device_name)
382                         break;
383                 rc = _scsih_srch_boot_device_name(
384                     device_name, &boot_device->DeviceName);
385                 break;
386         case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
387                 break;
388         }
389 
390         return rc;
391 }
392 
393 /**
394  * _scsih_get_sas_address - set the sas_address for given device handle
395  * @handle: device handle
396  * @sas_address: sas address
397  *
398  * Returns 0 success, non-zero when failure
399  */
400 static int
401 _scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
402     u64 *sas_address)
403 {
404         Mpi2SasDevicePage0_t sas_device_pg0;
405         Mpi2ConfigReply_t mpi_reply;
406         u32 ioc_status;
407         *sas_address = 0;
408 
409         if (handle <= ioc->sas_hba.num_phys) {
410                 *sas_address = ioc->sas_hba.sas_address;
411                 return 0;
412         }
413 
414         if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
415             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
416                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
417                 __FILE__, __LINE__, __func__);
418                 return -ENXIO;
419         }
420 
421         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
422         if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
423                 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
424                 return 0;
425         }
426 
427         /* we hit this becuase the given parent handle doesn't exist */
428         if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
429                 return -ENXIO;
430         /* else error case */
431         printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x), "
432             "failure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
433              __FILE__, __LINE__, __func__);
434         return -EIO;
435 }
436 
437 /**
438  * _scsih_determine_boot_device - determine boot device.
439  * @ioc: per adapter object
440  * @device: either sas_device or raid_device object
441  * @is_raid: [flag] 1 = raid object, 0 = sas object
442  *
443  * Determines whether this device should be first reported device to
444  * to scsi-ml or sas transport, this purpose is for persistent boot device.
445  * There are primary, alternate, and current entries in bios page 2. The order
446  * priority is primary, alternate, then current.  This routine saves
447  * the corresponding device object and is_raid flag in the ioc object.
448  * The saved data to be used later in _scsih_probe_boot_devices().
449  */
450 static void
451 _scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
452     void *device, u8 is_raid)
453 {
454         struct _sas_device *sas_device;
455         struct _raid_device *raid_device;
456         u64 sas_address;
457         u64 device_name;
458         u64 enclosure_logical_id;
459         u16 slot;
460 
461          /* only process this function when driver loads */
462         if (!ioc->is_driver_loading)
463                 return;
464 
465          /* no Bios, return immediately */
466         if (!ioc->bios_pg3.BiosVersion)
467                 return;
468 
469         if (!is_raid) {
470                 sas_device = device;
471                 sas_address = sas_device->sas_address;
472                 device_name = sas_device->device_name;
473                 enclosure_logical_id = sas_device->enclosure_logical_id;
474                 slot = sas_device->slot;
475         } else {
476                 raid_device = device;
477                 sas_address = raid_device->wwid;
478                 device_name = 0;
479                 enclosure_logical_id = 0;
480                 slot = 0;
481         }
482 
483         if (!ioc->req_boot_device.device) {
484                 if (_scsih_is_boot_device(sas_address, device_name,
485                     enclosure_logical_id, slot,
486                     (ioc->bios_pg2.ReqBootDeviceForm &
487                     MPI2_BIOSPAGE2_FORM_MASK),
488                     &ioc->bios_pg2.RequestedBootDevice)) {
489                         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
490                            "%s: req_boot_device(0x%016llx)\n",
491                             ioc->name, __func__,
492                             (unsigned long long)sas_address));
493                         ioc->req_boot_device.device = device;
494                         ioc->req_boot_device.is_raid = is_raid;
495                 }
496         }
497 
498         if (!ioc->req_alt_boot_device.device) {
499                 if (_scsih_is_boot_device(sas_address, device_name,
500                     enclosure_logical_id, slot,
501                     (ioc->bios_pg2.ReqAltBootDeviceForm &
502                     MPI2_BIOSPAGE2_FORM_MASK),
503                     &ioc->bios_pg2.RequestedAltBootDevice)) {
504                         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
505                            "%s: req_alt_boot_device(0x%016llx)\n",
506                             ioc->name, __func__,
507                             (unsigned long long)sas_address));
508                         ioc->req_alt_boot_device.device = device;
509                         ioc->req_alt_boot_device.is_raid = is_raid;
510                 }
511         }
512 
513         if (!ioc->current_boot_device.device) {
514                 if (_scsih_is_boot_device(sas_address, device_name,
515                     enclosure_logical_id, slot,
516                     (ioc->bios_pg2.CurrentBootDeviceForm &
517                     MPI2_BIOSPAGE2_FORM_MASK),
518                     &ioc->bios_pg2.CurrentBootDevice)) {
519                         dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
520                            "%s: current_boot_device(0x%016llx)\n",
521                             ioc->name, __func__,
522                             (unsigned long long)sas_address));
523                         ioc->current_boot_device.device = device;
524                         ioc->current_boot_device.is_raid = is_raid;
525                 }
526         }
527 }
528 
529 /**
530  * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
531  * @ioc: per adapter object
532  * @sas_address: sas address
533  * Context: Calling function should acquire ioc->sas_device_lock
534  *
535  * This searches for sas_device based on sas_address, then return sas_device
536  * object.
537  */
538 struct _sas_device *
539 mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
540     u64 sas_address)
541 {
542         struct _sas_device *sas_device;
543 
544         list_for_each_entry(sas_device, &ioc->sas_device_list, list)
545                 if (sas_device->sas_address == sas_address)
546                         return sas_device;
547 
548         list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
549                 if (sas_device->sas_address == sas_address)
550                         return sas_device;
551 
552         return NULL;
553 }
554 
555 /**
556  * _scsih_sas_device_find_by_handle - sas device search
557  * @ioc: per adapter object
558  * @handle: sas device handle (assigned by firmware)
559  * Context: Calling function should acquire ioc->sas_device_lock
560  *
561  * This searches for sas_device based on sas_address, then return sas_device
562  * object.
563  */
564 static struct _sas_device *
565 _scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
566 {
567         struct _sas_device *sas_device;
568 
569         list_for_each_entry(sas_device, &ioc->sas_device_list, list)
570                 if (sas_device->handle == handle)
571                         return sas_device;
572 
573         list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
574                 if (sas_device->handle == handle)
575                         return sas_device;
576 
577         return NULL;
578 }
579 
580 /**
581  * _scsih_sas_device_remove - remove sas_device from list.
582  * @ioc: per adapter object
583  * @sas_device: the sas_device object
584  * Context: This function will acquire ioc->sas_device_lock.
585  *
586  * Removing object and freeing associated memory from the ioc->sas_device_list.
587  */
588 static void
589 _scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
590     struct _sas_device *sas_device)
591 {
592         unsigned long flags;
593 
594         if (!sas_device)
595                 return;
596 
597         spin_lock_irqsave(&ioc->sas_device_lock, flags);
598         list_del(&sas_device->list);
599         kfree(sas_device);
600         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
601 }
602 
603 
604 /**
605  * _scsih_sas_device_add - insert sas_device to the list.
606  * @ioc: per adapter object
607  * @sas_device: the sas_device object
608  * Context: This function will acquire ioc->sas_device_lock.
609  *
610  * Adding new object to the ioc->sas_device_list.
611  */
612 static void
613 _scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
614     struct _sas_device *sas_device)
615 {
616         unsigned long flags;
617 
618         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
619             "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
620             sas_device->handle, (unsigned long long)sas_device->sas_address));
621 
622         spin_lock_irqsave(&ioc->sas_device_lock, flags);
623         list_add_tail(&sas_device->list, &ioc->sas_device_list);
624         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
625 
626         if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
627              sas_device->sas_address_parent)) {
628                 _scsih_sas_device_remove(ioc, sas_device);
629         } else if (!sas_device->starget) {
630                 /* When asyn scanning is enabled, its not possible to remove
631                  * devices while scanning is turned on due to an oops in
632                  * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
633                  */
634                 if (!ioc->is_driver_loading) {
635                         mpt2sas_transport_port_remove(ioc,
636                         sas_device->sas_address,
637                         sas_device->sas_address_parent);
638                         _scsih_sas_device_remove(ioc, sas_device);
639                 }
640         }
641 }
642 
643 /**
644  * _scsih_sas_device_init_add - insert sas_device to the list.
645  * @ioc: per adapter object
646  * @sas_device: the sas_device object
647  * Context: This function will acquire ioc->sas_device_lock.
648  *
649  * Adding new object at driver load time to the ioc->sas_device_init_list.
650  */
651 static void
652 _scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
653     struct _sas_device *sas_device)
654 {
655         unsigned long flags;
656 
657         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
658             "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
659             sas_device->handle, (unsigned long long)sas_device->sas_address));
660 
661         spin_lock_irqsave(&ioc->sas_device_lock, flags);
662         list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
663         _scsih_determine_boot_device(ioc, sas_device, 0);
664         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
665 }
666 
667 /**
668  * _scsih_raid_device_find_by_id - raid device search
669  * @ioc: per adapter object
670  * @id: sas device target id
671  * @channel: sas device channel
672  * Context: Calling function should acquire ioc->raid_device_lock
673  *
674  * This searches for raid_device based on target id, then return raid_device
675  * object.
676  */
677 static struct _raid_device *
678 _scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
679 {
680         struct _raid_device *raid_device, *r;
681 
682         r = NULL;
683         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
684                 if (raid_device->id == id && raid_device->channel == channel) {
685                         r = raid_device;
686                         goto out;
687                 }
688         }
689 
690  out:
691         return r;
692 }
693 
694 /**
695  * _scsih_raid_device_find_by_handle - raid device search
696  * @ioc: per adapter object
697  * @handle: sas device handle (assigned by firmware)
698  * Context: Calling function should acquire ioc->raid_device_lock
699  *
700  * This searches for raid_device based on handle, then return raid_device
701  * object.
702  */
703 static struct _raid_device *
704 _scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
705 {
706         struct _raid_device *raid_device, *r;
707 
708         r = NULL;
709         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
710                 if (raid_device->handle != handle)
711                         continue;
712                 r = raid_device;
713                 goto out;
714         }
715 
716  out:
717         return r;
718 }
719 
720 /**
721  * _scsih_raid_device_find_by_wwid - raid device search
722  * @ioc: per adapter object
723  * @handle: sas device handle (assigned by firmware)
724  * Context: Calling function should acquire ioc->raid_device_lock
725  *
726  * This searches for raid_device based on wwid, then return raid_device
727  * object.
728  */
729 static struct _raid_device *
730 _scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
731 {
732         struct _raid_device *raid_device, *r;
733 
734         r = NULL;
735         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
736                 if (raid_device->wwid != wwid)
737                         continue;
738                 r = raid_device;
739                 goto out;
740         }
741 
742  out:
743         return r;
744 }
745 
746 /**
747  * _scsih_raid_device_add - add raid_device object
748  * @ioc: per adapter object
749  * @raid_device: raid_device object
750  *
751  * This is added to the raid_device_list link list.
752  */
753 static void
754 _scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
755     struct _raid_device *raid_device)
756 {
757         unsigned long flags;
758 
759         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
760             "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
761             raid_device->handle, (unsigned long long)raid_device->wwid));
762 
763         spin_lock_irqsave(&ioc->raid_device_lock, flags);
764         list_add_tail(&raid_device->list, &ioc->raid_device_list);
765         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
766 }
767 
768 /**
769  * _scsih_raid_device_remove - delete raid_device object
770  * @ioc: per adapter object
771  * @raid_device: raid_device object
772  *
773  */
774 static void
775 _scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
776     struct _raid_device *raid_device)
777 {
778         unsigned long flags;
779 
780         spin_lock_irqsave(&ioc->raid_device_lock, flags);
781         list_del(&raid_device->list);
782         kfree(raid_device);
783         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
784 }
785 
786 /**
787  * mpt2sas_scsih_expander_find_by_handle - expander device search
788  * @ioc: per adapter object
789  * @handle: expander handle (assigned by firmware)
790  * Context: Calling function should acquire ioc->sas_device_lock
791  *
792  * This searches for expander device based on handle, then returns the
793  * sas_node object.
794  */
795 struct _sas_node *
796 mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
797 {
798         struct _sas_node *sas_expander, *r;
799 
800         r = NULL;
801         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
802                 if (sas_expander->handle != handle)
803                         continue;
804                 r = sas_expander;
805                 goto out;
806         }
807  out:
808         return r;
809 }
810 
811 /**
812  * mpt2sas_scsih_expander_find_by_sas_address - expander device search
813  * @ioc: per adapter object
814  * @sas_address: sas address
815  * Context: Calling function should acquire ioc->sas_node_lock.
816  *
817  * This searches for expander device based on sas_address, then returns the
818  * sas_node object.
819  */
820 struct _sas_node *
821 mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
822     u64 sas_address)
823 {
824         struct _sas_node *sas_expander, *r;
825 
826         r = NULL;
827         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
828                 if (sas_expander->sas_address != sas_address)
829                         continue;
830                 r = sas_expander;
831                 goto out;
832         }
833  out:
834         return r;
835 }
836 
837 /**
838  * _scsih_expander_node_add - insert expander device to the list.
839  * @ioc: per adapter object
840  * @sas_expander: the sas_device object
841  * Context: This function will acquire ioc->sas_node_lock.
842  *
843  * Adding new object to the ioc->sas_expander_list.
844  *
845  * Return nothing.
846  */
847 static void
848 _scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
849     struct _sas_node *sas_expander)
850 {
851         unsigned long flags;
852 
853         spin_lock_irqsave(&ioc->sas_node_lock, flags);
854         list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
855         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
856 }
857 
858 /**
859  * _scsih_is_end_device - determines if device is an end device
860  * @device_info: bitfield providing information about the device.
861  * Context: none
862  *
863  * Returns 1 if end device.
864  */
865 static int
866 _scsih_is_end_device(u32 device_info)
867 {
868         if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
869                 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
870                 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
871                 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
872                 return 1;
873         else
874                 return 0;
875 }
876 
877 /**
878  * _scsih_scsi_lookup_get - returns scmd entry
879  * @ioc: per adapter object
880  * @smid: system request message index
881  *
882  * Returns the smid stored scmd pointer.
883  */
884 static struct scsi_cmnd *
885 _scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
886 {
887         return ioc->scsi_lookup[smid - 1].scmd;
888 }
889 
890 /**
891  * _scsih_scsi_lookup_get_clear - returns scmd entry
892  * @ioc: per adapter object
893  * @smid: system request message index
894  *
895  * Returns the smid stored scmd pointer.
896  * Then will derefrence the stored scmd pointer.
897  */
898 static inline struct scsi_cmnd *
899 _scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
900 {
901         unsigned long flags;
902         struct scsi_cmnd *scmd;
903 
904         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
905         scmd = ioc->scsi_lookup[smid - 1].scmd;
906         ioc->scsi_lookup[smid - 1].scmd = NULL;
907         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
908 
909         return scmd;
910 }
911 
912 /**
913  * _scsih_scsi_lookup_find_by_scmd - scmd lookup
914  * @ioc: per adapter object
915  * @smid: system request message index
916  * @scmd: pointer to scsi command object
917  * Context: This function will acquire ioc->scsi_lookup_lock.
918  *
919  * This will search for a scmd pointer in the scsi_lookup array,
920  * returning the revelent smid.  A returned value of zero means invalid.
921  */
922 static u16
923 _scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
924     *scmd)
925 {
926         u16 smid;
927         unsigned long   flags;
928         int i;
929 
930         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
931         smid = 0;
932         for (i = 0; i < ioc->scsiio_depth; i++) {
933                 if (ioc->scsi_lookup[i].scmd == scmd) {
934                         smid = ioc->scsi_lookup[i].smid;
935                         goto out;
936                 }
937         }
938  out:
939         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
940         return smid;
941 }
942 
943 /**
944  * _scsih_scsi_lookup_find_by_target - search for matching channel:id
945  * @ioc: per adapter object
946  * @id: target id
947  * @channel: channel
948  * Context: This function will acquire ioc->scsi_lookup_lock.
949  *
950  * This will search for a matching channel:id in the scsi_lookup array,
951  * returning 1 if found.
952  */
953 static u8
954 _scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
955     int channel)
956 {
957         u8 found;
958         unsigned long   flags;
959         int i;
960 
961         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
962         found = 0;
963         for (i = 0 ; i < ioc->scsiio_depth; i++) {
964                 if (ioc->scsi_lookup[i].scmd &&
965                     (ioc->scsi_lookup[i].scmd->device->id == id &&
966                     ioc->scsi_lookup[i].scmd->device->channel == channel)) {
967                         found = 1;
968                         goto out;
969                 }
970         }
971  out:
972         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
973         return found;
974 }
975 
976 /**
977  * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
978  * @ioc: per adapter object
979  * @id: target id
980  * @lun: lun number
981  * @channel: channel
982  * Context: This function will acquire ioc->scsi_lookup_lock.
983  *
984  * This will search for a matching channel:id:lun in the scsi_lookup array,
985  * returning 1 if found.
986  */
987 static u8
988 _scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
989     unsigned int lun, int channel)
990 {
991         u8 found;
992         unsigned long   flags;
993         int i;
994 
995         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
996         found = 0;
997         for (i = 0 ; i < ioc->scsiio_depth; i++) {
998                 if (ioc->scsi_lookup[i].scmd &&
999                     (ioc->scsi_lookup[i].scmd->device->id == id &&
1000                     ioc->scsi_lookup[i].scmd->device->channel == channel &&
1001                     ioc->scsi_lookup[i].scmd->device->lun == lun)) {
1002                         found = 1;
1003                         goto out;
1004                 }
1005         }
1006  out:
1007         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1008         return found;
1009 }
1010 
1011 /**
1012  * _scsih_get_chain_buffer_tracker - obtain chain tracker
1013  * @ioc: per adapter object
1014  * @smid: smid associated to an IO request
1015  *
1016  * Returns chain tracker(from ioc->free_chain_list)
1017  */
1018 static struct chain_tracker *
1019 _scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1020 {
1021         struct chain_tracker *chain_req;
1022         unsigned long flags;
1023 
1024         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1025         if (list_empty(&ioc->free_chain_list)) {
1026                 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1027                 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT "chain buffers not "
1028                         "available\n", ioc->name));
1029                 return NULL;
1030         }
1031         chain_req = list_entry(ioc->free_chain_list.next,
1032             struct chain_tracker, tracker_list);
1033         list_del_init(&chain_req->tracker_list);
1034         list_add_tail(&chain_req->tracker_list,
1035             &ioc->scsi_lookup[smid - 1].chain_list);
1036         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1037         return chain_req;
1038 }
1039 
1040 /**
1041  * _scsih_build_scatter_gather - main sg creation routine
1042  * @ioc: per adapter object
1043  * @scmd: scsi command
1044  * @smid: system request message index
1045  * Context: none.
1046  *
1047  * The main routine that builds scatter gather table from a given
1048  * scsi request sent via the .queuecommand main handler.
1049  *
1050  * Returns 0 success, anything else error
1051  */
1052 static int
1053 _scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
1054     struct scsi_cmnd *scmd, u16 smid)
1055 {
1056         Mpi2SCSIIORequest_t *mpi_request;
1057         dma_addr_t chain_dma;
1058         struct scatterlist *sg_scmd;
1059         void *sg_local, *chain;
1060         u32 chain_offset;
1061         u32 chain_length;
1062         u32 chain_flags;
1063         int sges_left;
1064         u32 sges_in_segment;
1065         u32 sgl_flags;
1066         u32 sgl_flags_last_element;
1067         u32 sgl_flags_end_buffer;
1068         struct chain_tracker *chain_req;
1069 
1070         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1071 
1072         /* init scatter gather flags */
1073         sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1074         if (scmd->sc_data_direction == DMA_TO_DEVICE)
1075                 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1076         sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1077             << MPI2_SGE_FLAGS_SHIFT;
1078         sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1079             MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1080             << MPI2_SGE_FLAGS_SHIFT;
1081         sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1082 
1083         sg_scmd = scsi_sglist(scmd);
1084         sges_left = scsi_dma_map(scmd);
1085         if (sges_left < 0) {
1086                 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1087                 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1088                 return -ENOMEM;
1089         }
1090 
1091         sg_local = &mpi_request->SGL;
1092         sges_in_segment = ioc->max_sges_in_main_message;
1093         if (sges_left <= sges_in_segment)
1094                 goto fill_in_last_segment;
1095 
1096         mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1097             (sges_in_segment * ioc->sge_size))/4;
1098 
1099         /* fill in main message segment when there is a chain following */
1100         while (sges_in_segment) {
1101                 if (sges_in_segment == 1)
1102                         ioc->base_add_sg_single(sg_local,
1103                             sgl_flags_last_element | sg_dma_len(sg_scmd),
1104                             sg_dma_address(sg_scmd));
1105                 else
1106                         ioc->base_add_sg_single(sg_local, sgl_flags |
1107                             sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1108                 sg_scmd = sg_next(sg_scmd);
1109                 sg_local += ioc->sge_size;
1110                 sges_left--;
1111                 sges_in_segment--;
1112         }
1113 
1114         /* initializing the chain flags and pointers */
1115         chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1116         chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1117         if (!chain_req)
1118                 return -1;
1119         chain = chain_req->chain_buffer;
1120         chain_dma = chain_req->chain_buffer_dma;
1121         do {
1122                 sges_in_segment = (sges_left <=
1123                     ioc->max_sges_in_chain_message) ? sges_left :
1124                     ioc->max_sges_in_chain_message;
1125                 chain_offset = (sges_left == sges_in_segment) ?
1126                     0 : (sges_in_segment * ioc->sge_size)/4;
1127                 chain_length = sges_in_segment * ioc->sge_size;
1128                 if (chain_offset) {
1129                         chain_offset = chain_offset <<
1130                             MPI2_SGE_CHAIN_OFFSET_SHIFT;
1131                         chain_length += ioc->sge_size;
1132                 }
1133                 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1134                     chain_length, chain_dma);
1135                 sg_local = chain;
1136                 if (!chain_offset)
1137                         goto fill_in_last_segment;
1138 
1139                 /* fill in chain segments */
1140                 while (sges_in_segment) {
1141                         if (sges_in_segment == 1)
1142                                 ioc->base_add_sg_single(sg_local,
1143                                     sgl_flags_last_element |
1144                                     sg_dma_len(sg_scmd),
1145                                     sg_dma_address(sg_scmd));
1146                         else
1147                                 ioc->base_add_sg_single(sg_local, sgl_flags |
1148                                     sg_dma_len(sg_scmd),
1149                                     sg_dma_address(sg_scmd));
1150                         sg_scmd = sg_next(sg_scmd);
1151                         sg_local += ioc->sge_size;
1152                         sges_left--;
1153                         sges_in_segment--;
1154                 }
1155 
1156                 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1157                 if (!chain_req)
1158                         return -1;
1159                 chain = chain_req->chain_buffer;
1160                 chain_dma = chain_req->chain_buffer_dma;
1161         } while (1);
1162 
1163 
1164  fill_in_last_segment:
1165 
1166         /* fill the last segment */
1167         while (sges_left) {
1168                 if (sges_left == 1)
1169                         ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1170                             sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1171                 else
1172                         ioc->base_add_sg_single(sg_local, sgl_flags |
1173                             sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1174                 sg_scmd = sg_next(sg_scmd);
1175                 sg_local += ioc->sge_size;
1176                 sges_left--;
1177         }
1178 
1179         return 0;
1180 }
1181 
1182 /**
1183  * _scsih_change_queue_depth - setting device queue depth
1184  * @sdev: scsi device struct
1185  * @qdepth: requested queue depth
1186  *
1187  * Returns queue depth.
1188  */
1189 static int
1190 _scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1191 {
1192         struct Scsi_Host *shost = sdev->host;
1193         int max_depth;
1194         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1195         struct MPT2SAS_DEVICE *sas_device_priv_data;
1196         struct MPT2SAS_TARGET *sas_target_priv_data;
1197         struct _sas_device *sas_device;
1198         unsigned long flags;
1199 
1200         max_depth = shost->can_queue;
1201 
1202         /* limit max device queue for SATA to 32 */
1203         sas_device_priv_data = sdev->hostdata;
1204         if (!sas_device_priv_data)
1205                 goto not_sata;
1206         sas_target_priv_data = sas_device_priv_data->sas_target;
1207         if (!sas_target_priv_data)
1208                 goto not_sata;
1209         if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1210                 goto not_sata;
1211         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1212         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1213            sas_device_priv_data->sas_target->sas_address);
1214         if (sas_device && sas_device->device_info &
1215             MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1216                 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1217         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1218 
1219  not_sata:
1220         if (!sdev->tagged_supported)
1221                 max_depth = 1;
1222         if (qdepth > max_depth)
1223                 qdepth = max_depth;
1224         return scsi_change_queue_depth(sdev, qdepth);
1225 }
1226 
1227 /**
1228  * _scsih_target_alloc - target add routine
1229  * @starget: scsi target struct
1230  *
1231  * Returns 0 if ok. Any other return is assumed to be an error and
1232  * the device is ignored.
1233  */
1234 static int
1235 _scsih_target_alloc(struct scsi_target *starget)
1236 {
1237         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1238         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1239         struct MPT2SAS_TARGET *sas_target_priv_data;
1240         struct _sas_device *sas_device;
1241         struct _raid_device *raid_device;
1242         unsigned long flags;
1243         struct sas_rphy *rphy;
1244 
1245         sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1246                                        GFP_KERNEL);
1247         if (!sas_target_priv_data)
1248                 return -ENOMEM;
1249 
1250         starget->hostdata = sas_target_priv_data;
1251         sas_target_priv_data->starget = starget;
1252         sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1253 
1254         /* RAID volumes */
1255         if (starget->channel == RAID_CHANNEL) {
1256                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1257                 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1258                     starget->channel);
1259                 if (raid_device) {
1260                         sas_target_priv_data->handle = raid_device->handle;
1261                         sas_target_priv_data->sas_address = raid_device->wwid;
1262                         sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1263                         if (ioc->is_warpdrive)
1264                                 sas_target_priv_data->raid_device = raid_device;
1265                         raid_device->starget = starget;
1266                 }
1267                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1268                 return 0;
1269         }
1270 
1271         /* sas/sata devices */
1272         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1273         rphy = dev_to_rphy(starget->dev.parent);
1274         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1275            rphy->identify.sas_address);
1276 
1277         if (sas_device) {
1278                 sas_target_priv_data->handle = sas_device->handle;
1279                 sas_target_priv_data->sas_address = sas_device->sas_address;
1280                 sas_device->starget = starget;
1281                 sas_device->id = starget->id;
1282                 sas_device->channel = starget->channel;
1283                 if (test_bit(sas_device->handle, ioc->pd_handles))
1284                         sas_target_priv_data->flags |=
1285                             MPT_TARGET_FLAGS_RAID_COMPONENT;
1286         }
1287         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1288 
1289         return 0;
1290 }
1291 
1292 /**
1293  * _scsih_target_destroy - target destroy routine
1294  * @starget: scsi target struct
1295  *
1296  * Returns nothing.
1297  */
1298 static void
1299 _scsih_target_destroy(struct scsi_target *starget)
1300 {
1301         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1302         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1303         struct MPT2SAS_TARGET *sas_target_priv_data;
1304         struct _sas_device *sas_device;
1305         struct _raid_device *raid_device;
1306         unsigned long flags;
1307         struct sas_rphy *rphy;
1308 
1309         sas_target_priv_data = starget->hostdata;
1310         if (!sas_target_priv_data)
1311                 return;
1312 
1313         if (starget->channel == RAID_CHANNEL) {
1314                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1315                 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1316                     starget->channel);
1317                 if (raid_device) {
1318                         raid_device->starget = NULL;
1319                         raid_device->sdev = NULL;
1320                 }
1321                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1322                 goto out;
1323         }
1324 
1325         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1326         rphy = dev_to_rphy(starget->dev.parent);
1327         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1328            rphy->identify.sas_address);
1329         if (sas_device && (sas_device->starget == starget) &&
1330             (sas_device->id == starget->id) &&
1331             (sas_device->channel == starget->channel))
1332                 sas_device->starget = NULL;
1333 
1334         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1335 
1336  out:
1337         kfree(sas_target_priv_data);
1338         starget->hostdata = NULL;
1339 }
1340 
1341 /**
1342  * _scsih_slave_alloc - device add routine
1343  * @sdev: scsi device struct
1344  *
1345  * Returns 0 if ok. Any other return is assumed to be an error and
1346  * the device is ignored.
1347  */
1348 static int
1349 _scsih_slave_alloc(struct scsi_device *sdev)
1350 {
1351         struct Scsi_Host *shost;
1352         struct MPT2SAS_ADAPTER *ioc;
1353         struct MPT2SAS_TARGET *sas_target_priv_data;
1354         struct MPT2SAS_DEVICE *sas_device_priv_data;
1355         struct scsi_target *starget;
1356         struct _raid_device *raid_device;
1357         struct _sas_device *sas_device;
1358         unsigned long flags;
1359 
1360         sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
1361                                        GFP_KERNEL);
1362         if (!sas_device_priv_data)
1363                 return -ENOMEM;
1364 
1365         sas_device_priv_data->lun = sdev->lun;
1366         sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1367 
1368         starget = scsi_target(sdev);
1369         sas_target_priv_data = starget->hostdata;
1370         sas_target_priv_data->num_luns++;
1371         sas_device_priv_data->sas_target = sas_target_priv_data;
1372         sdev->hostdata = sas_device_priv_data;
1373         if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1374                 sdev->no_uld_attach = 1;
1375 
1376         shost = dev_to_shost(&starget->dev);
1377         ioc = shost_priv(shost);
1378         if (starget->channel == RAID_CHANNEL) {
1379                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1380                 raid_device = _scsih_raid_device_find_by_id(ioc,
1381                     starget->id, starget->channel);
1382                 if (raid_device)
1383                         raid_device->sdev = sdev; /* raid is single lun */
1384                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1385         }
1386 
1387         if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1388                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1389                 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1390                                 sas_target_priv_data->sas_address);
1391                 if (sas_device && (sas_device->starget == NULL)) {
1392                         sdev_printk(KERN_INFO, sdev,
1393                              "%s : sas_device->starget set to starget @ %d\n",
1394                              __func__, __LINE__);
1395                         sas_device->starget = starget;
1396                 }
1397                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1398         }
1399 
1400         return 0;
1401 }
1402 
1403 /**
1404  * _scsih_slave_destroy - device destroy routine
1405  * @sdev: scsi device struct
1406  *
1407  * Returns nothing.
1408  */
1409 static void
1410 _scsih_slave_destroy(struct scsi_device *sdev)
1411 {
1412         struct MPT2SAS_TARGET *sas_target_priv_data;
1413         struct scsi_target *starget;
1414         struct Scsi_Host *shost;
1415         struct MPT2SAS_ADAPTER *ioc;
1416         struct _sas_device *sas_device;
1417         unsigned long flags;
1418 
1419         if (!sdev->hostdata)
1420                 return;
1421 
1422         starget = scsi_target(sdev);
1423         sas_target_priv_data = starget->hostdata;
1424         sas_target_priv_data->num_luns--;
1425 
1426         shost = dev_to_shost(&starget->dev);
1427         ioc = shost_priv(shost);
1428 
1429         if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1430                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1431                 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1432                    sas_target_priv_data->sas_address);
1433                 if (sas_device && !sas_target_priv_data->num_luns)
1434                         sas_device->starget = NULL;
1435                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1436         }
1437 
1438         kfree(sdev->hostdata);
1439         sdev->hostdata = NULL;
1440 }
1441 
1442 /**
1443  * _scsih_display_sata_capabilities - sata capabilities
1444  * @ioc: per adapter object
1445  * @handle: device handle
1446  * @sdev: scsi device struct
1447  */
1448 static void
1449 _scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
1450         u16 handle, struct scsi_device *sdev)
1451 {
1452         Mpi2ConfigReply_t mpi_reply;
1453         Mpi2SasDevicePage0_t sas_device_pg0;
1454         u32 ioc_status;
1455         u16 flags;
1456         u32 device_info;
1457 
1458         if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1459             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
1460                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1461                     ioc->name, __FILE__, __LINE__, __func__);
1462                 return;
1463         }
1464 
1465         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1466             MPI2_IOCSTATUS_MASK;
1467         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1468                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1469                     ioc->name, __FILE__, __LINE__, __func__);
1470                 return;
1471         }
1472 
1473         flags = le16_to_cpu(sas_device_pg0.Flags);
1474         device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1475 
1476         sdev_printk(KERN_INFO, sdev,
1477             "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1478             "sw_preserve(%s)\n",
1479             (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1480             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1481             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1482             "n",
1483             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1484             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1485             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1486 }
1487 
1488 /**
1489  * _scsih_is_raid - return boolean indicating device is raid volume
1490  * @dev the device struct object
1491  */
1492 static int
1493 _scsih_is_raid(struct device *dev)
1494 {
1495         struct scsi_device *sdev = to_scsi_device(dev);
1496         struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1497 
1498         if (ioc->is_warpdrive)
1499                 return 0;
1500         return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1501 }
1502 
1503 /**
1504  * _scsih_get_resync - get raid volume resync percent complete
1505  * @dev the device struct object
1506  */
1507 static void
1508 _scsih_get_resync(struct device *dev)
1509 {
1510         struct scsi_device *sdev = to_scsi_device(dev);
1511         struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1512         static struct _raid_device *raid_device;
1513         unsigned long flags;
1514         Mpi2RaidVolPage0_t vol_pg0;
1515         Mpi2ConfigReply_t mpi_reply;
1516         u32 volume_status_flags;
1517         u8 percent_complete;
1518         u16 handle;
1519 
1520         percent_complete = 0;
1521         handle = 0;
1522         if (ioc->is_warpdrive)
1523                 goto out;
1524 
1525         spin_lock_irqsave(&ioc->raid_device_lock, flags);
1526         raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1527             sdev->channel);
1528         if (raid_device) {
1529                 handle = raid_device->handle;
1530                 percent_complete = raid_device->percent_complete;
1531         }
1532         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1533 
1534         if (!handle)
1535                 goto out;
1536 
1537         if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1538              MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1539              sizeof(Mpi2RaidVolPage0_t))) {
1540                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1541                     ioc->name, __FILE__, __LINE__, __func__);
1542                 percent_complete = 0;
1543                 goto out;
1544         }
1545 
1546         volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1547         if (!(volume_status_flags &
1548             MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1549                 percent_complete = 0;
1550 
1551  out:
1552         raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1553 }
1554 
1555 /**
1556  * _scsih_get_state - get raid volume level
1557  * @dev the device struct object
1558  */
1559 static void
1560 _scsih_get_state(struct device *dev)
1561 {
1562         struct scsi_device *sdev = to_scsi_device(dev);
1563         struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1564         static struct _raid_device *raid_device;
1565         unsigned long flags;
1566         Mpi2RaidVolPage0_t vol_pg0;
1567         Mpi2ConfigReply_t mpi_reply;
1568         u32 volstate;
1569         enum raid_state state = RAID_STATE_UNKNOWN;
1570         u16 handle = 0;
1571 
1572         spin_lock_irqsave(&ioc->raid_device_lock, flags);
1573         raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1574             sdev->channel);
1575         if (raid_device)
1576                 handle = raid_device->handle;
1577         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1578 
1579         if (!raid_device)
1580                 goto out;
1581 
1582         if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1583              MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1584              sizeof(Mpi2RaidVolPage0_t))) {
1585                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1586                     ioc->name, __FILE__, __LINE__, __func__);
1587                 goto out;
1588         }
1589 
1590         volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1591         if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1592                 state = RAID_STATE_RESYNCING;
1593                 goto out;
1594         }
1595 
1596         switch (vol_pg0.VolumeState) {
1597         case MPI2_RAID_VOL_STATE_OPTIMAL:
1598         case MPI2_RAID_VOL_STATE_ONLINE:
1599                 state = RAID_STATE_ACTIVE;
1600                 break;
1601         case  MPI2_RAID_VOL_STATE_DEGRADED:
1602                 state = RAID_STATE_DEGRADED;
1603                 break;
1604         case MPI2_RAID_VOL_STATE_FAILED:
1605         case MPI2_RAID_VOL_STATE_MISSING:
1606                 state = RAID_STATE_OFFLINE;
1607                 break;
1608         }
1609  out:
1610         raid_set_state(mpt2sas_raid_template, dev, state);
1611 }
1612 
1613 /**
1614  * _scsih_set_level - set raid level
1615  * @sdev: scsi device struct
1616  * @volume_type: volume type
1617  */
1618 static void
1619 _scsih_set_level(struct scsi_device *sdev, u8 volume_type)
1620 {
1621         enum raid_level level = RAID_LEVEL_UNKNOWN;
1622 
1623         switch (volume_type) {
1624         case MPI2_RAID_VOL_TYPE_RAID0:
1625                 level = RAID_LEVEL_0;
1626                 break;
1627         case MPI2_RAID_VOL_TYPE_RAID10:
1628                 level = RAID_LEVEL_10;
1629                 break;
1630         case MPI2_RAID_VOL_TYPE_RAID1E:
1631                 level = RAID_LEVEL_1E;
1632                 break;
1633         case MPI2_RAID_VOL_TYPE_RAID1:
1634                 level = RAID_LEVEL_1;
1635                 break;
1636         }
1637 
1638         raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1639 }
1640 
1641 /**
1642  * _scsih_get_volume_capabilities - volume capabilities
1643  * @ioc: per adapter object
1644  * @sas_device: the raid_device object
1645  *
1646  * Returns 0 for success, else 1
1647  */
1648 static int
1649 _scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1650     struct _raid_device *raid_device)
1651 {
1652         Mpi2RaidVolPage0_t *vol_pg0;
1653         Mpi2RaidPhysDiskPage0_t pd_pg0;
1654         Mpi2SasDevicePage0_t sas_device_pg0;
1655         Mpi2ConfigReply_t mpi_reply;
1656         u16 sz;
1657         u8 num_pds;
1658 
1659         if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1660             &num_pds)) || !num_pds) {
1661                 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1662                     "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1663                     __func__));
1664                 return 1;
1665         }
1666 
1667         raid_device->num_pds = num_pds;
1668         sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1669             sizeof(Mpi2RaidVol0PhysDisk_t));
1670         vol_pg0 = kzalloc(sz, GFP_KERNEL);
1671         if (!vol_pg0) {
1672                 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1673                     "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1674                     __func__));
1675                 return 1;
1676         }
1677 
1678         if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1679              MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1680                 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1681                     "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1682                     __func__));
1683                 kfree(vol_pg0);
1684                 return 1;
1685         }
1686 
1687         raid_device->volume_type = vol_pg0->VolumeType;
1688 
1689         /* figure out what the underlying devices are by
1690          * obtaining the device_info bits for the 1st device
1691          */
1692         if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1693             &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1694             vol_pg0->PhysDisk[0].PhysDiskNum))) {
1695                 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1696                     &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1697                     le16_to_cpu(pd_pg0.DevHandle)))) {
1698                         raid_device->device_info =
1699                             le32_to_cpu(sas_device_pg0.DeviceInfo);
1700                 }
1701         }
1702 
1703         kfree(vol_pg0);
1704         return 0;
1705 }
1706 /**
1707  * _scsih_disable_ddio - Disable direct I/O for all the volumes
1708  * @ioc: per adapter object
1709  */
1710 static void
1711 _scsih_disable_ddio(struct MPT2SAS_ADAPTER *ioc)
1712 {
1713         Mpi2RaidVolPage1_t vol_pg1;
1714         Mpi2ConfigReply_t mpi_reply;
1715         struct _raid_device *raid_device;
1716         u16 handle;
1717         u16 ioc_status;
1718         unsigned long flags;
1719 
1720         handle = 0xFFFF;
1721         while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1722             &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1723                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1724                     MPI2_IOCSTATUS_MASK;
1725                 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1726                         break;
1727                 handle = le16_to_cpu(vol_pg1.DevHandle);
1728                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1729                 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1730                 if (raid_device)
1731                         raid_device->direct_io_enabled = 0;
1732                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1733         }
1734         return;
1735 }
1736 
1737 
1738 /**
1739  * _scsih_get_num_volumes - Get number of volumes in the ioc
1740  * @ioc: per adapter object
1741  */
1742 static u8
1743 _scsih_get_num_volumes(struct MPT2SAS_ADAPTER *ioc)
1744 {
1745         Mpi2RaidVolPage1_t vol_pg1;
1746         Mpi2ConfigReply_t mpi_reply;
1747         u16 handle;
1748         u8 vol_cnt = 0;
1749         u16 ioc_status;
1750 
1751         handle = 0xFFFF;
1752         while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1753             &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1754                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1755                     MPI2_IOCSTATUS_MASK;
1756                 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1757                         break;
1758                 vol_cnt++;
1759                 handle = le16_to_cpu(vol_pg1.DevHandle);
1760         }
1761         return vol_cnt;
1762 }
1763 
1764 
1765 /**
1766  * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O.
1767  * @ioc: per adapter object
1768  * @raid_device: the raid_device object
1769  */
1770 static void
1771 _scsih_init_warpdrive_properties(struct MPT2SAS_ADAPTER *ioc,
1772         struct _raid_device *raid_device)
1773 {
1774         Mpi2RaidVolPage0_t *vol_pg0;
1775         Mpi2RaidPhysDiskPage0_t pd_pg0;
1776         Mpi2ConfigReply_t mpi_reply;
1777         u16 sz;
1778         u8 num_pds, count;
1779         unsigned long stripe_sz, block_sz;
1780         u8 stripe_exp, block_exp;
1781         u64 dev_max_lba;
1782 
1783         if (!ioc->is_warpdrive)
1784                 return;
1785 
1786         if (ioc->mfg_pg10_hide_flag ==  MFG_PAGE10_EXPOSE_ALL_DISKS) {
1787                 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1788                     "globally as drives are exposed\n", ioc->name);
1789                 return;
1790         }
1791         if (_scsih_get_num_volumes(ioc) > 1) {
1792                 _scsih_disable_ddio(ioc);
1793                 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1794                     "globally as number of drives > 1\n", ioc->name);
1795                 return;
1796         }
1797         if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1798             &num_pds)) || !num_pds) {
1799                 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1800                     "Failure in computing number of drives\n", ioc->name);
1801                 return;
1802         }
1803 
1804         sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1805             sizeof(Mpi2RaidVol0PhysDisk_t));
1806         vol_pg0 = kzalloc(sz, GFP_KERNEL);
1807         if (!vol_pg0) {
1808                 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1809                     "Memory allocation failure for RVPG0\n", ioc->name);
1810                 return;
1811         }
1812 
1813         if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1814              MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1815                 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1816                     "Failure in retrieving RVPG0\n", ioc->name);
1817                 kfree(vol_pg0);
1818                 return;
1819         }
1820 
1821         /*
1822          * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
1823          * assumed for WARPDRIVE, disable direct I/O
1824          */
1825         if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
1826                 printk(MPT2SAS_WARN_FMT "WarpDrive : Direct IO is disabled "
1827                     "for the drive with handle(0x%04x): num_mem=%d, "
1828                     "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
1829                     num_pds, MPT_MAX_WARPDRIVE_PDS);
1830                 kfree(vol_pg0);
1831                 return;
1832         }
1833         for (count = 0; count < num_pds; count++) {
1834                 if (mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1835                     &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1836                     vol_pg0->PhysDisk[count].PhysDiskNum) ||
1837                      le16_to_cpu(pd_pg0.DevHandle) ==
1838                     MPT2SAS_INVALID_DEVICE_HANDLE) {
1839                         printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1840                             "disabled for the drive with handle(0x%04x) member"
1841                             "handle retrieval failed for member number=%d\n",
1842                             ioc->name, raid_device->handle,
1843                             vol_pg0->PhysDisk[count].PhysDiskNum);
1844                         goto out_error;
1845                 }
1846                 /* Disable direct I/O if member drive lba exceeds 4 bytes */
1847                 dev_max_lba = le64_to_cpu(pd_pg0.DeviceMaxLBA);
1848                 if (dev_max_lba >> 32) {
1849                         printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1850                             "disabled for the drive with handle(0x%04x) member"
1851                             "handle (0x%04x) unsupported max lba 0x%016llx\n",
1852                             ioc->name, raid_device->handle,
1853                             le16_to_cpu(pd_pg0.DevHandle),
1854                             (unsigned long long)dev_max_lba);
1855                         goto out_error;
1856                 }
1857 
1858                 raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
1859         }
1860 
1861         /*
1862          * Assumption for WD: Direct I/O is not supported if the volume is
1863          * not RAID0
1864          */
1865         if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0) {
1866                 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1867                     "for the drive with handle(0x%04x): type=%d, "
1868                     "s_sz=%uK, blk_size=%u\n", ioc->name,
1869                     raid_device->handle, raid_device->volume_type,
1870                     (le32_to_cpu(vol_pg0->StripeSize) *
1871                     le16_to_cpu(vol_pg0->BlockSize)) / 1024,
1872                     le16_to_cpu(vol_pg0->BlockSize));
1873                 goto out_error;
1874         }
1875 
1876         stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1877         stripe_exp = find_first_bit(&stripe_sz, 32);
1878         if (stripe_exp == 32) {
1879                 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1880                 "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
1881                     ioc->name, raid_device->handle,
1882                     (le32_to_cpu(vol_pg0->StripeSize) *
1883                     le16_to_cpu(vol_pg0->BlockSize)) / 1024);
1884                 goto out_error;
1885         }
1886         raid_device->stripe_exponent = stripe_exp;
1887         block_sz = le16_to_cpu(vol_pg0->BlockSize);
1888         block_exp = find_first_bit(&block_sz, 16);
1889         if (block_exp == 16) {
1890                 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1891                     "for the drive with handle(0x%04x) invalid block sz %u\n",
1892                     ioc->name, raid_device->handle,
1893                     le16_to_cpu(vol_pg0->BlockSize));
1894                 goto out_error;
1895         }
1896         raid_device->block_exponent = block_exp;
1897         raid_device->direct_io_enabled = 1;
1898 
1899         printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is Enabled for the drive"
1900             " with handle(0x%04x)\n", ioc->name, raid_device->handle);
1901         /*
1902          * WARPDRIVE: Though the following fields are not used for direct IO,
1903          * stored for future purpose:
1904          */
1905         raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
1906         raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1907         raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
1908 
1909 
1910         kfree(vol_pg0);
1911         return;
1912 
1913 out_error:
1914         raid_device->direct_io_enabled = 0;
1915         for (count = 0; count < num_pds; count++)
1916                 raid_device->pd_handle[count] = 0;
1917         kfree(vol_pg0);
1918         return;
1919 }
1920 
1921 /**
1922  * _scsih_enable_tlr - setting TLR flags
1923  * @ioc: per adapter object
1924  * @sdev: scsi device struct
1925  *
1926  * Enabling Transaction Layer Retries for tape devices when
1927  * vpd page 0x90 is present
1928  *
1929  */
1930 static void
1931 _scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1932 {
1933         /* only for TAPE */
1934         if (sdev->type != TYPE_TAPE)
1935                 return;
1936 
1937         if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1938                 return;
1939 
1940         sas_enable_tlr(sdev);
1941         sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1942             sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1943         return;
1944 
1945 }
1946 
1947 /**
1948  * _scsih_slave_configure - device configure routine.
1949  * @sdev: scsi device struct
1950  *
1951  * Returns 0 if ok. Any other return is assumed to be an error and
1952  * the device is ignored.
1953  */
1954 static int
1955 _scsih_slave_configure(struct scsi_device *sdev)
1956 {
1957         struct Scsi_Host *shost = sdev->host;
1958         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1959         struct MPT2SAS_DEVICE *sas_device_priv_data;
1960         struct MPT2SAS_TARGET *sas_target_priv_data;
1961         struct _sas_device *sas_device;
1962         struct _raid_device *raid_device;
1963         unsigned long flags;
1964         int qdepth;
1965         u8 ssp_target = 0;
1966         char *ds = "";
1967         char *r_level = "";
1968         u16 handle, volume_handle = 0;
1969         u64 volume_wwid = 0;
1970 
1971         qdepth = 1;
1972         sas_device_priv_data = sdev->hostdata;
1973         sas_device_priv_data->configured_lun = 1;
1974         sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1975         sas_target_priv_data = sas_device_priv_data->sas_target;
1976         handle = sas_target_priv_data->handle;
1977 
1978         /* raid volume handling */
1979         if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1980 
1981                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1982                 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1983                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1984                 if (!raid_device) {
1985                         dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1986                             "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1987                             __LINE__, __func__));
1988                         return 1;
1989                 }
1990 
1991                 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1992                         dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1993                             "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1994                             __LINE__, __func__));
1995                         return 1;
1996                 }
1997                 /*
1998                  * WARPDRIVE: Initialize the required data for Direct IO
1999                  */
2000                 _scsih_init_warpdrive_properties(ioc, raid_device);
2001 
2002                 /* RAID Queue Depth Support
2003                  * IS volume = underlying qdepth of drive type, either
2004                  *    MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
2005                  * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
2006                  */
2007                 if (raid_device->device_info &
2008                     MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2009                         qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2010                         ds = "SSP";
2011                 } else {
2012                         qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2013                          if (raid_device->device_info &
2014                             MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2015                                 ds = "SATA";
2016                         else
2017                                 ds = "STP";
2018                 }
2019 
2020                 switch (raid_device->volume_type) {
2021                 case MPI2_RAID_VOL_TYPE_RAID0:
2022                         r_level = "RAID0";
2023                         break;
2024                 case MPI2_RAID_VOL_TYPE_RAID1E:
2025                         qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2026                         if (ioc->manu_pg10.OEMIdentifier &&
2027                             (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
2028                             MFG10_GF0_R10_DISPLAY) &&
2029                             !(raid_device->num_pds % 2))
2030                                 r_level = "RAID10";
2031                         else
2032                                 r_level = "RAID1E";
2033                         break;
2034                 case MPI2_RAID_VOL_TYPE_RAID1:
2035                         qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2036                         r_level = "RAID1";
2037                         break;
2038                 case MPI2_RAID_VOL_TYPE_RAID10:
2039                         qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2040                         r_level = "RAID10";
2041                         break;
2042                 case MPI2_RAID_VOL_TYPE_UNKNOWN:
2043                 default:
2044                         qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2045                         r_level = "RAIDX";
2046                         break;
2047                 }
2048 
2049                 if (!ioc->hide_ir_msg)
2050                         sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2051                             "wwid(0x%016llx), pd_count(%d), type(%s)\n",
2052                             r_level, raid_device->handle,
2053                             (unsigned long long)raid_device->wwid,
2054                             raid_device->num_pds, ds);
2055                 _scsih_change_queue_depth(sdev, qdepth);
2056                 /* raid transport support */
2057                 if (!ioc->is_warpdrive)
2058                         _scsih_set_level(sdev, raid_device->volume_type);
2059                 return 0;
2060         }
2061 
2062         /* non-raid handling */
2063         if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
2064                 if (mpt2sas_config_get_volume_handle(ioc, handle,
2065                     &volume_handle)) {
2066                         dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2067                             "failure at %s:%d/%s()!\n", ioc->name,
2068                             __FILE__, __LINE__, __func__));
2069                         return 1;
2070                 }
2071                 if (volume_handle && mpt2sas_config_get_volume_wwid(ioc,
2072                     volume_handle, &volume_wwid)) {
2073                         dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2074                             "failure at %s:%d/%s()!\n", ioc->name,
2075                             __FILE__, __LINE__, __func__));
2076                         return 1;
2077                 }
2078         }
2079 
2080         spin_lock_irqsave(&ioc->sas_device_lock, flags);
2081         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2082            sas_device_priv_data->sas_target->sas_address);
2083         if (!sas_device) {
2084                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2085                 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2086                         "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
2087                         __LINE__, __func__));
2088                 return 1;
2089         }
2090         sas_device->volume_handle = volume_handle;
2091         sas_device->volume_wwid = volume_wwid;
2092         if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2093                 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2094                 ssp_target = 1;
2095                 ds = "SSP";
2096         } else {
2097                 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2098                 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
2099                         ds = "STP";
2100                 else if (sas_device->device_info &
2101                     MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2102                         ds = "SATA";
2103         }
2104         sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2105             "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
2106             ds, sas_device->handle,
2107             (unsigned long long)sas_device->sas_address,
2108             sas_device->phy,
2109             (unsigned long long)sas_device->device_name);
2110         sdev_printk(KERN_INFO, sdev, "%s: "
2111             "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
2112             (unsigned long long) sas_device->enclosure_logical_id,
2113             sas_device->slot);
2114 
2115         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2116         if (!ssp_target)
2117                 _scsih_display_sata_capabilities(ioc, handle, sdev);
2118 
2119 
2120         _scsih_change_queue_depth(sdev, qdepth);
2121 
2122         if (ssp_target) {
2123                 sas_read_port_mode_page(sdev);
2124                 _scsih_enable_tlr(ioc, sdev);
2125         }
2126         return 0;
2127 }
2128 
2129 /**
2130  * _scsih_bios_param - fetch head, sector, cylinder info for a disk
2131  * @sdev: scsi device struct
2132  * @bdev: pointer to block device context
2133  * @capacity: device size (in 512 byte sectors)
2134  * @params: three element array to place output:
2135  *              params[0] number of heads (max 255)
2136  *              params[1] number of sectors (max 63)
2137  *              params[2] number of cylinders
2138  *
2139  * Return nothing.
2140  */
2141 static int
2142 _scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2143     sector_t capacity, int params[])
2144 {
2145         int             heads;
2146         int             sectors;
2147         sector_t        cylinders;
2148         ulong           dummy;
2149 
2150         heads = 64;
2151         sectors = 32;
2152 
2153         dummy = heads * sectors;
2154         cylinders = capacity;
2155         sector_div(cylinders, dummy);
2156 
2157         /*
2158          * Handle extended translation size for logical drives
2159          * > 1Gb
2160          */
2161         if ((ulong)capacity >= 0x200000) {
2162                 heads = 255;
2163                 sectors = 63;
2164                 dummy = heads * sectors;
2165                 cylinders = capacity;
2166                 sector_div(cylinders, dummy);
2167         }
2168 
2169         /* return result */
2170         params[0] = heads;
2171         params[1] = sectors;
2172         params[2] = cylinders;
2173 
2174         return 0;
2175 }
2176 
2177 /**
2178  * _scsih_response_code - translation of device response code
2179  * @ioc: per adapter object
2180  * @response_code: response code returned by the device
2181  *
2182  * Return nothing.
2183  */
2184 static void
2185 _scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
2186 {
2187         char *desc;
2188 
2189         switch (response_code) {
2190         case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2191                 desc = "task management request completed";
2192                 break;
2193         case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2194                 desc = "invalid frame";
2195                 break;
2196         case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2197                 desc = "task management request not supported";
2198                 break;
2199         case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2200                 desc = "task management request failed";
2201                 break;
2202         case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2203                 desc = "task management request succeeded";
2204                 break;
2205         case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2206                 desc = "invalid lun";
2207                 break;
2208         case 0xA:
2209                 desc = "overlapped tag attempted";
2210                 break;
2211         case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2212                 desc = "task queued, however not sent to target";
2213                 break;
2214         default:
2215                 desc = "unknown";
2216                 break;
2217         }
2218         printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
2219                 ioc->name, response_code, desc);
2220 }
2221 
2222 /**
2223  * _scsih_tm_done - tm completion routine
2224  * @ioc: per adapter object
2225  * @smid: system request message index
2226  * @msix_index: MSIX table index supplied by the OS
2227  * @reply: reply message frame(lower 32bit addr)
2228  * Context: none.
2229  *
2230  * The callback handler when using scsih_issue_tm.
2231  *
2232  * Return 1 meaning mf should be freed from _base_interrupt
2233  *        0 means the mf is freed from this function.
2234  */
2235 static u8
2236 _scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
2237 {
2238         MPI2DefaultReply_t *mpi_reply;
2239 
2240         if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
2241                 return 1;
2242         if (ioc->tm_cmds.smid != smid)
2243                 return 1;
2244         mpt2sas_base_flush_reply_queues(ioc);
2245         ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
2246         mpi_reply =  mpt2sas_base_get_reply_virt_addr(ioc, reply);
2247         if (mpi_reply) {
2248                 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2249                 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
2250         }
2251         ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
2252         complete(&ioc->tm_cmds.done);
2253         return 1;
2254 }
2255 
2256 /**
2257  * mpt2sas_scsih_set_tm_flag - set per target tm_busy
2258  * @ioc: per adapter object
2259  * @handle: device handle
2260  *
2261  * During taskmangement request, we need to freeze the device queue.
2262  */
2263 void
2264 mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2265 {
2266         struct MPT2SAS_DEVICE *sas_device_priv_data;
2267         struct scsi_device *sdev;
2268         u8 skip = 0;
2269 
2270         shost_for_each_device(sdev, ioc->shost) {
2271                 if (skip)
2272                         continue;
2273                 sas_device_priv_data = sdev->hostdata;
2274                 if (!sas_device_priv_data)
2275                         continue;
2276                 if (sas_device_priv_data->sas_target->handle == handle) {
2277                         sas_device_priv_data->sas_target->tm_busy = 1;
2278                         skip = 1;
2279                         ioc->ignore_loginfos = 1;
2280                 }
2281         }
2282 }
2283 
2284 /**
2285  * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
2286  * @ioc: per adapter object
2287  * @handle: device handle
2288  *
2289  * During taskmangement request, we need to freeze the device queue.
2290  */
2291 void
2292 mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2293 {
2294         struct MPT2SAS_DEVICE *sas_device_priv_data;
2295         struct scsi_device *sdev;
2296         u8 skip = 0;
2297 
2298         shost_for_each_device(sdev, ioc->shost) {
2299                 if (skip)
2300                         continue;
2301                 sas_device_priv_data = sdev->hostdata;
2302                 if (!sas_device_priv_data)
2303                         continue;
2304                 if (sas_device_priv_data->sas_target->handle == handle) {
2305                         sas_device_priv_data->sas_target->tm_busy = 0;
2306                         skip = 1;
2307                         ioc->ignore_loginfos = 0;
2308                 }
2309         }
2310 }
2311 
2312 
2313 /**
2314  * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2315  * @ioc: per adapter struct
2316  * @device_handle: device handle
2317  * @channel: the channel assigned by the OS
2318  * @id: the id assigned by the OS
2319  * @lun: lun number
2320  * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2321  * @smid_task: smid assigned to the task
2322  * @timeout: timeout in seconds
2323  * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
2324  * Context: user
2325  *
2326  * A generic API for sending task management requests to firmware.
2327  *
2328  * The callback index is set inside `ioc->tm_cb_idx`.
2329  *
2330  * Return SUCCESS or FAILED.
2331  */
2332 int
2333 mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2334     uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
2335         enum mutex_type m_type)
2336 {
2337         Mpi2SCSITaskManagementRequest_t *mpi_request;
2338         Mpi2SCSITaskManagementReply_t *mpi_reply;
2339         u16 smid = 0;
2340         u32 ioc_state;
2341         unsigned long timeleft;
2342         struct scsiio_tracker *scsi_lookup = NULL;
2343         int rc;
2344 
2345         if (m_type == TM_MUTEX_ON)
2346                 mutex_lock(&ioc->tm_cmds.mutex);
2347         if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2348                 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2349                     __func__, ioc->name);
2350                 rc = FAILED;
2351                 goto err_out;
2352         }
2353 
2354         if (ioc->shost_recovery || ioc->remove_host ||
2355             ioc->pci_error_recovery) {
2356                 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2357                     __func__, ioc->name);
2358                 rc = FAILED;
2359                 goto err_out;
2360         }
2361 
2362         ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2363         if (ioc_state & MPI2_DOORBELL_USED) {
2364                 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
2365                     "active!\n", ioc->name));
2366                 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2367                     FORCE_BIG_HAMMER);
2368                 rc = (!rc) ? SUCCESS : FAILED;
2369                 goto err_out;
2370         }
2371 
2372         if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2373                 mpt2sas_base_fault_info(ioc, ioc_state &
2374                     MPI2_DOORBELL_DATA_MASK);
2375                 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2376                     FORCE_BIG_HAMMER);
2377                 rc = (!rc) ? SUCCESS : FAILED;
2378                 goto err_out;
2379         }
2380 
2381         smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2382         if (!smid) {
2383                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2384                     ioc->name, __func__);
2385                 rc = FAILED;
2386                 goto err_out;
2387         }
2388 
2389         if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2390                 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2391 
2392         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
2393             " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2394             smid_task));
2395         ioc->tm_cmds.status = MPT2_CMD_PENDING;
2396         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2397         ioc->tm_cmds.smid = smid;
2398         memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2399         memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
2400         mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2401         mpi_request->DevHandle = cpu_to_le16(handle);
2402         mpi_request->TaskType = type;
2403         mpi_request->TaskMID = cpu_to_le16(smid_task);
2404         int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2405         mpt2sas_scsih_set_tm_flag(ioc, handle);
2406         init_completion(&ioc->tm_cmds.done);
2407         mpt2sas_base_put_smid_hi_priority(ioc, smid);
2408         timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2409         if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2410                 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2411                     ioc->name, __func__);
2412                 _debug_dump_mf(mpi_request,
2413                     sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2414                 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
2415                         rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2416                             FORCE_BIG_HAMMER);
2417                         rc = (!rc) ? SUCCESS : FAILED;
2418                         ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2419                         mpt2sas_scsih_clear_tm_flag(ioc, handle);
2420                         goto err_out;
2421                 }
2422         }
2423 
2424         if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2425                 mpi_reply = ioc->tm_cmds.reply;
2426                 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2427                     "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2428                     ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2429                     le32_to_cpu(mpi_reply->IOCLogInfo),
2430                     le32_to_cpu(mpi_reply->TerminationCount)));
2431                 if (ioc->logging_level & MPT_DEBUG_TM) {
2432                         _scsih_response_code(ioc, mpi_reply->ResponseCode);
2433                         if (mpi_reply->IOCStatus)
2434                                 _debug_dump_mf(mpi_request,
2435                                     sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2436                 }
2437         }
2438 
2439         switch (type) {
2440         case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2441                 rc = SUCCESS;
2442                 if (scsi_lookup->scmd == NULL)
2443                         break;
2444                 rc = FAILED;
2445                 break;
2446 
2447         case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2448                 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2449                         rc = FAILED;
2450                 else
2451                         rc = SUCCESS;
2452                 break;
2453 
2454         case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
2455         case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2456                 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2457                         rc = FAILED;
2458                 else
2459                         rc = SUCCESS;
2460                 break;
2461         case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2462                 rc = SUCCESS;
2463                 break;
2464         default:
2465                 rc = FAILED;
2466                 break;
2467         }
2468 
2469         mpt2sas_scsih_clear_tm_flag(ioc, handle);
2470         ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2471         if (m_type == TM_MUTEX_ON)
2472                 mutex_unlock(&ioc->tm_cmds.mutex);
2473 
2474         return rc;
2475 
2476  err_out:
2477         if (m_type == TM_MUTEX_ON)
2478                 mutex_unlock(&ioc->tm_cmds.mutex);
2479         return rc;
2480 }
2481 
2482 /**
2483  * _scsih_tm_display_info - displays info about the device
2484  * @ioc: per adapter struct
2485  * @scmd: pointer to scsi command object
2486  *
2487  * Called by task management callback handlers.
2488  */
2489 static void
2490 _scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2491 {
2492         struct scsi_target *starget = scmd->device->sdev_target;
2493         struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2494         struct _sas_device *sas_device = NULL;
2495         unsigned long flags;
2496         char *device_str = NULL;
2497 
2498         if (!priv_target)
2499                 return;
2500         if (ioc->hide_ir_msg)
2501                 device_str = "WarpDrive";
2502         else
2503                 device_str = "volume";
2504 
2505         scsi_print_command(scmd);
2506         if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2507                 starget_printk(KERN_INFO, starget, "%s handle(0x%04x), "
2508                     "%s wwid(0x%016llx)\n", device_str, priv_target->handle,
2509                     device_str, (unsigned long long)priv_target->sas_address);
2510         } else {
2511                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2512                 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2513                     priv_target->sas_address);
2514                 if (sas_device) {
2515                         if (priv_target->flags &
2516                             MPT_TARGET_FLAGS_RAID_COMPONENT) {
2517                                 starget_printk(KERN_INFO, starget,
2518                                     "volume handle(0x%04x), "
2519                                     "volume wwid(0x%016llx)\n",
2520                                     sas_device->volume_handle,
2521                                    (unsigned long long)sas_device->volume_wwid);
2522                         }
2523                         starget_printk(KERN_INFO, starget,
2524                             "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2525                             sas_device->handle,
2526                             (unsigned long long)sas_device->sas_address,
2527                             sas_device->phy);
2528                         starget_printk(KERN_INFO, starget,
2529                             "enclosure_logical_id(0x%016llx), slot(%d)\n",
2530                            (unsigned long long)sas_device->enclosure_logical_id,
2531                             sas_device->slot);
2532                 }
2533                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2534         }
2535 }
2536 
2537 /**
2538  * _scsih_abort - eh threads main abort routine
2539  * @scmd: pointer to scsi command object
2540  *
2541  * Returns SUCCESS if command aborted else FAILED
2542  */
2543 static int
2544 _scsih_abort(struct scsi_cmnd *scmd)
2545 {
2546         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2547         struct MPT2SAS_DEVICE *sas_device_priv_data;
2548         u16 smid;
2549         u16 handle;
2550         int r;
2551 
2552         sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2553             "scmd(%p)\n", scmd);
2554         _scsih_tm_display_info(ioc, scmd);
2555 
2556         sas_device_priv_data = scmd->device->hostdata;
2557         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2558                 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2559                     "scmd(%p)\n", scmd);
2560                 scmd->result = DID_NO_CONNECT << 16;
2561                 scmd->scsi_done(scmd);
2562                 r = SUCCESS;
2563                 goto out;
2564         }
2565 
2566         /* search for the command */
2567         smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2568         if (!smid) {
2569                 scmd->result = DID_RESET << 16;
2570                 r = SUCCESS;
2571                 goto out;
2572         }
2573 
2574         /* for hidden raid components and volumes this is not supported */
2575         if (sas_device_priv_data->sas_target->flags &
2576             MPT_TARGET_FLAGS_RAID_COMPONENT ||
2577             sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2578                 scmd->result = DID_RESET << 16;
2579                 r = FAILED;
2580                 goto out;
2581         }
2582 
2583         mpt2sas_halt_firmware(ioc);
2584 
2585         handle = sas_device_priv_data->sas_target->handle;
2586         r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2587             scmd->device->id, scmd->device->lun,
2588             MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, TM_MUTEX_ON);
2589 
2590  out:
2591         sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2592             ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2593         return r;
2594 }
2595 
2596 /**
2597  * _scsih_dev_reset - eh threads main device reset routine
2598  * @scmd: pointer to scsi command object
2599  *
2600  * Returns SUCCESS if command aborted else FAILED
2601  */
2602 static int
2603 _scsih_dev_reset(struct scsi_cmnd *scmd)
2604 {
2605         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2606         struct MPT2SAS_DEVICE *sas_device_priv_data;
2607         struct _sas_device *sas_device;
2608         unsigned long flags;
2609         u16     handle;
2610         int r;
2611 
2612         struct scsi_target *starget = scmd->device->sdev_target;
2613 
2614         starget_printk(KERN_INFO, starget, "attempting device reset! "
2615             "scmd(%p)\n", scmd);
2616         _scsih_tm_display_info(ioc, scmd);
2617 
2618         sas_device_priv_data = scmd->device->hostdata;
2619         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2620                 starget_printk(KERN_INFO, starget, "device been deleted! "
2621                     "scmd(%p)\n", scmd);
2622                 scmd->result = DID_NO_CONNECT << 16;
2623                 scmd->scsi_done(scmd);
2624                 r = SUCCESS;
2625                 goto out;
2626         }
2627 
2628         /* for hidden raid components obtain the volume_handle */
2629         handle = 0;
2630         if (sas_device_priv_data->sas_target->flags &
2631             MPT_TARGET_FLAGS_RAID_COMPONENT) {
2632                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2633                 sas_device = _scsih_sas_device_find_by_handle(ioc,
2634                    sas_device_priv_data->sas_target->handle);
2635                 if (sas_device)
2636                         handle = sas_device->volume_handle;
2637                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2638         } else
2639                 handle = sas_device_priv_data->sas_target->handle;
2640 
2641         if (!handle) {
2642                 scmd->result = DID_RESET << 16;
2643                 r = FAILED;
2644                 goto out;
2645         }
2646 
2647         r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2648             scmd->device->id, scmd->device->lun,
2649             MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, TM_MUTEX_ON);
2650 
2651  out:
2652         sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2653             ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2654         return r;
2655 }
2656 
2657 /**
2658  * _scsih_target_reset - eh threads main target reset routine
2659  * @scmd: pointer to scsi command object
2660  *
2661  * Returns SUCCESS if command aborted else FAILED
2662  */
2663 static int
2664 _scsih_target_reset(struct scsi_cmnd *scmd)
2665 {
2666         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2667         struct MPT2SAS_DEVICE *sas_device_priv_data;
2668         struct _sas_device *sas_device;
2669         unsigned long flags;
2670         u16     handle;
2671         int r;
2672         struct scsi_target *starget = scmd->device->sdev_target;
2673 
2674         starget_printk(KERN_INFO, starget, "attempting target reset! "
2675             "scmd(%p)\n", scmd);
2676         _scsih_tm_display_info(ioc, scmd);
2677 
2678         sas_device_priv_data = scmd->device->hostdata;
2679         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2680                 starget_printk(KERN_INFO, starget, "target been deleted! "
2681                     "scmd(%p)\n", scmd);
2682                 scmd->result = DID_NO_CONNECT << 16;
2683                 scmd->scsi_done(scmd);
2684                 r = SUCCESS;
2685                 goto out;
2686         }
2687 
2688         /* for hidden raid components obtain the volume_handle */
2689         handle = 0;
2690         if (sas_device_priv_data->sas_target->flags &
2691             MPT_TARGET_FLAGS_RAID_COMPONENT) {
2692                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2693                 sas_device = _scsih_sas_device_find_by_handle(ioc,
2694                    sas_device_priv_data->sas_target->handle);
2695                 if (sas_device)
2696                         handle = sas_device->volume_handle;
2697                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2698         } else
2699                 handle = sas_device_priv_data->sas_target->handle;
2700 
2701         if (!handle) {
2702                 scmd->result = DID_RESET << 16;
2703                 r = FAILED;
2704                 goto out;
2705         }
2706 
2707         r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2708             scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2709             30, TM_MUTEX_ON);
2710 
2711  out:
2712         starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2713             ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2714         return r;
2715 }
2716 
2717 /**
2718  * _scsih_host_reset - eh threads main host reset routine
2719  * @scmd: pointer to scsi command object
2720  *
2721  * Returns SUCCESS if command aborted else FAILED
2722  */
2723 static int
2724 _scsih_host_reset(struct scsi_cmnd *scmd)
2725 {
2726         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2727         int r, retval;
2728 
2729         printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2730             ioc->name, scmd);
2731         scsi_print_command(scmd);
2732 
2733         if (ioc->is_driver_loading) {
2734                 printk(MPT2SAS_INFO_FMT "Blocking the host reset\n",
2735                                                           ioc->name);
2736                 r = FAILED;
2737                 goto out;
2738         }
2739 
2740         retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2741             FORCE_BIG_HAMMER);
2742         r = (retval < 0) ? FAILED : SUCCESS;
2743 
2744  out:
2745         printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2746             ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2747 
2748         return r;
2749 }
2750 
2751 /**
2752  * _scsih_fw_event_add - insert and queue up fw_event
2753  * @ioc: per adapter object
2754  * @fw_event: object describing the event
2755  * Context: This function will acquire ioc->fw_event_lock.
2756  *
2757  * This adds the firmware event object into link list, then queues it up to
2758  * be processed from user context.
2759  *
2760  * Return nothing.
2761  */
2762 static void
2763 _scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2764 {
2765         unsigned long flags;
2766 
2767         if (ioc->firmware_event_thread == NULL)
2768                 return;
2769 
2770         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2771         list_add_tail(&fw_event->list, &ioc->fw_event_list);
2772         INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2773         queue_delayed_work(ioc->firmware_event_thread,
2774             &fw_event->delayed_work, 0);
2775         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2776 }
2777 
2778 /**
2779  * _scsih_fw_event_free - delete fw_event
2780  * @ioc: per adapter object
2781  * @fw_event: object describing the event
2782  * Context: This function will acquire ioc->fw_event_lock.
2783  *
2784  * This removes firmware event object from link list, frees associated memory.
2785  *
2786  * Return nothing.
2787  */
2788 static void
2789 _scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2790     *fw_event)
2791 {
2792         unsigned long flags;
2793 
2794         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2795         list_del(&fw_event->list);
2796         kfree(fw_event);
2797         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2798 }
2799 
2800 
2801 /**
2802  * _scsih_error_recovery_delete_devices - remove devices not responding
2803  * @ioc: per adapter object
2804  *
2805  * Return nothing.
2806  */
2807 static void
2808 _scsih_error_recovery_delete_devices(struct MPT2SAS_ADAPTER *ioc)
2809 {
2810         struct fw_event_work *fw_event;
2811 
2812         if (ioc->is_driver_loading)
2813                 return;
2814 
2815         fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2816         if (!fw_event)
2817                 return;
2818 
2819         fw_event->event = MPT2SAS_REMOVE_UNRESPONDING_DEVICES;
2820         fw_event->ioc = ioc;
2821         _scsih_fw_event_add(ioc, fw_event);
2822 }
2823 
2824 /**
2825  * mpt2sas_port_enable_complete - port enable completed (fake event)
2826  * @ioc: per adapter object
2827  *
2828  * Return nothing.
2829  */
2830 void
2831 mpt2sas_port_enable_complete(struct MPT2SAS_ADAPTER *ioc)
2832 {
2833         struct fw_event_work *fw_event;
2834 
2835         fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2836         if (!fw_event)
2837                 return;
2838         fw_event->event = MPT2SAS_PORT_ENABLE_COMPLETE;
2839         fw_event->ioc = ioc;
2840         _scsih_fw_event_add(ioc, fw_event);
2841 }
2842 
2843 /**
2844  * _scsih_fw_event_cleanup_queue - cleanup event queue
2845  * @ioc: per adapter object
2846  *
2847  * Walk the firmware event queue, either killing timers, or waiting
2848  * for outstanding events to complete
2849  *
2850  * Return nothing.
2851  */
2852 static void
2853 _scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2854 {
2855         struct fw_event_work *fw_event, *next;
2856 
2857         if (list_empty(&ioc->fw_event_list) ||
2858              !ioc->firmware_event_thread || in_interrupt())
2859                 return;
2860 
2861         list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2862                 if (cancel_delayed_work_sync(&fw_event->delayed_work)) {
2863                         _scsih_fw_event_free(ioc, fw_event);
2864                         continue;
2865                 }
2866         }
2867 }
2868 
2869 /**
2870  * _scsih_ublock_io_all_device - unblock every device
2871  * @ioc: per adapter object
2872  *
2873  * change the device state from block to running
2874  */
2875 static void
2876 _scsih_ublock_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2877 {
2878         struct MPT2SAS_DEVICE *sas_device_priv_data;
2879         struct scsi_device *sdev;
2880 
2881         shost_for_each_device(sdev, ioc->shost) {
2882                 sas_device_priv_data = sdev->hostdata;
2883                 if (!sas_device_priv_data)
2884                         continue;
2885                 if (!sas_device_priv_data->block)
2886                         continue;
2887                 sas_device_priv_data->block = 0;
2888                 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_running, "
2889                     "handle(0x%04x)\n",
2890                     sas_device_priv_data->sas_target->handle));
2891                 scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2892         }
2893 }
2894 /**
2895  * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2896  * @ioc: per adapter object
2897  * @handle: device handle
2898  *
2899  * During device pull we need to appropiately set the sdev state.
2900  */
2901 static void
2902 _scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
2903 {
2904         struct MPT2SAS_DEVICE *sas_device_priv_data;
2905         struct scsi_device *sdev;
2906 
2907         shost_for_each_device(sdev, ioc->shost) {
2908                 sas_device_priv_data = sdev->hostdata;
2909                 if (!sas_device_priv_data)
2910                         continue;
2911                 if (!sas_device_priv_data->block)
2912                         continue;
2913                 if (sas_device_priv_data->sas_target->sas_address ==
2914                                                                 sas_address) {
2915                         dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2916                             MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2917                             "sas address(0x%016llx)\n", ioc->name,
2918                                 (unsigned long long)sas_address));
2919                         sas_device_priv_data->block = 0;
2920                         scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2921                 }
2922         }
2923 }
2924 
2925 /**
2926  * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2927  * @ioc: per adapter object
2928  * @handle: device handle
2929  *
2930  * During device pull we need to appropiately set the sdev state.
2931  */
2932 static void
2933 _scsih_block_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2934 {
2935         struct MPT2SAS_DEVICE *sas_device_priv_data;
2936         struct scsi_device *sdev;
2937 
2938         shost_for_each_device(sdev, ioc->shost) {
2939                 sas_device_priv_data = sdev->hostdata;
2940                 if (!sas_device_priv_data)
2941                         continue;
2942                 if (sas_device_priv_data->block)
2943                         continue;
2944                 sas_device_priv_data->block = 1;
2945                 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_blocked, "
2946                     "handle(0x%04x)\n",
2947                     sas_device_priv_data->sas_target->handle));
2948                 scsi_internal_device_block(sdev);
2949         }
2950 }
2951 
2952 
2953 /**
2954  * _scsih_block_io_device - set the device state to SDEV_BLOCK
2955  * @ioc: per adapter object
2956  * @handle: device handle
2957  *
2958  * During device pull we need to appropiately set the sdev state.
2959  */
2960 static void
2961 _scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2962 {
2963         struct MPT2SAS_DEVICE *sas_device_priv_data;
2964         struct scsi_device *sdev;
2965 
2966         shost_for_each_device(sdev, ioc->shost) {
2967                 sas_device_priv_data = sdev->hostdata;
2968                 if (!sas_device_priv_data)
2969                         continue;
2970                 if (sas_device_priv_data->block)
2971                         continue;
2972                 if (sas_device_priv_data->sas_target->handle == handle) {
2973                         dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2974                             MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2975                             "handle(0x%04x)\n", ioc->name, handle));
2976                         sas_device_priv_data->block = 1;
2977                         scsi_internal_device_block(sdev);
2978                 }
2979         }
2980 }
2981 
2982 /**
2983  * _scsih_block_io_to_children_attached_to_ex
2984  * @ioc: per adapter object
2985  * @sas_expander: the sas_device object
2986  *
2987  * This routine set sdev state to SDEV_BLOCK for all devices
2988  * attached to this expander. This function called when expander is
2989  * pulled.
2990  */
2991 static void
2992 _scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2993     struct _sas_node *sas_expander)
2994 {
2995         struct _sas_port *mpt2sas_port;
2996         struct _sas_device *sas_device;
2997         struct _sas_node *expander_sibling;
2998         unsigned long flags;
2999 
3000         if (!sas_expander)
3001                 return;
3002 
3003         list_for_each_entry(mpt2sas_port,
3004            &sas_expander->sas_port_list, port_list) {
3005                 if (mpt2sas_port->remote_identify.device_type ==
3006                     SAS_END_DEVICE) {
3007                         spin_lock_irqsave(&ioc->sas_device_lock, flags);
3008                         sas_device =
3009                             mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3010                            mpt2sas_port->remote_identify.sas_address);
3011                         if (sas_device)
3012                                 set_bit(sas_device->handle,
3013                                     ioc->blocking_handles);
3014                         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3015                 }
3016         }
3017 
3018         list_for_each_entry(mpt2sas_port,
3019            &sas_expander->sas_port_list, port_list) {
3020 
3021                 if (mpt2sas_port->remote_identify.device_type ==
3022                     SAS_EDGE_EXPANDER_DEVICE ||
3023                     mpt2sas_port->remote_identify.device_type ==
3024                     SAS_FANOUT_EXPANDER_DEVICE) {
3025                         expander_sibling =
3026                             mpt2sas_scsih_expander_find_by_sas_address(
3027                             ioc, mpt2sas_port->remote_identify.sas_address);
3028                         _scsih_block_io_to_children_attached_to_ex(ioc,
3029                             expander_sibling);
3030                 }
3031         }
3032 }
3033 
3034 /**
3035  * _scsih_block_io_to_children_attached_directly
3036  * @ioc: per adapter object
3037  * @event_data: topology change event data
3038  *
3039  * This routine set sdev state to SDEV_BLOCK for all devices
3040  * direct attached during device pull.
3041  */
3042 static void
3043 _scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
3044     Mpi2EventDataSasTopologyChangeList_t *event_data)
3045 {
3046         int i;
3047         u16 handle;
3048         u16 reason_code;
3049         u8 phy_number;
3050 
3051         for (i = 0; i < event_data->NumEntries; i++) {
3052                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3053                 if (!handle)
3054                         continue;
3055                 phy_number = event_data->StartPhyNum + i;
3056                 reason_code = event_data->PHY[i].PhyStatus &
3057                     MPI2_EVENT_SAS_TOPO_RC_MASK;
3058                 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3059                         _scsih_block_io_device(ioc, handle);
3060         }
3061 }
3062 
3063 /**
3064  * _scsih_tm_tr_send - send task management request
3065  * @ioc: per adapter object
3066  * @handle: device handle
3067  * Context: interrupt time.
3068  *
3069  * This code is to initiate the device removal handshake protocol
3070  * with controller firmware.  This function will issue target reset
3071  * using high priority request queue.  It will send a sas iounit
3072  * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
3073  *
3074  * This is designed to send muliple task management request at the same
3075  * time to the fifo. If the fifo is full, we will append the request,
3076  * and process it in a future completion.
3077  */
3078 static void
3079 _scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3080 {
3081         Mpi2SCSITaskManagementRequest_t *mpi_request;
3082         u16 smid;
3083         struct _sas_device *sas_device;
3084         struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
3085         u64 sas_address = 0;
3086         unsigned long flags;
3087         struct _tr_list *delayed_tr;
3088         u32 ioc_state;
3089 
3090         if (ioc->remove_host) {
3091                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3092                     "removed: handle(0x%04x)\n", __func__, ioc->name, handle));
3093                 return;
3094         } else if (ioc->pci_error_recovery) {
3095                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3096                     "error recovery: handle(0x%04x)\n", __func__, ioc->name,
3097                     handle));
3098                 return;
3099         }
3100         ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3101         if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3102                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3103                    "operational: handle(0x%04x)\n", __func__, ioc->name,
3104                    handle));
3105                 return;
3106         }
3107 
3108         /* if PD, then return */
3109         if (test_bit(handle, ioc->pd_handles))
3110                 return;
3111 
3112         spin_lock_irqsave(&ioc->sas_device_lock, flags);
3113         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3114         if (sas_device && sas_device->starget &&
3115              sas_device->starget->hostdata) {
3116                 sas_target_priv_data = sas_device->starget->hostdata;
3117                 sas_target_priv_data->deleted = 1;
3118                 sas_address = sas_device->sas_address;
3119         }
3120         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3121 
3122         if (sas_target_priv_data) {
3123                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "setting delete flag: "
3124                 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, handle,
3125                         (unsigned long long)sas_address));
3126                 _scsih_ublock_io_device(ioc, sas_address);
3127                 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
3128         }
3129 
3130         smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3131         if (!smid) {
3132                 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3133                 if (!delayed_tr)
3134                         return;
3135                 INIT_LIST_HEAD(&delayed_tr->list);
3136                 delayed_tr->handle = handle;
3137                 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3138                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3139                     "DELAYED:tr:handle(0x%04x), (open)\n",
3140                     ioc->name, handle));
3141                 return;
3142         }
3143 
3144         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3145             "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3146             ioc->tm_tr_cb_idx));
3147         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3148         memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3149         mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3150         mpi_request->DevHandle = cpu_to_le16(handle);
3151         mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3152         mpt2sas_base_put_smid_hi_priority(ioc, smid);
3153 }
3154 
3155 
3156 
3157 /**
3158  * _scsih_sas_control_complete - completion routine
3159  * @ioc: per adapter object
3160  * @smid: system request message index
3161  * @msix_index: MSIX table index supplied by the OS
3162  * @reply: reply message frame(lower 32bit addr)
3163  * Context: interrupt time.
3164  *
3165  * This is the sas iounit control completion routine.
3166  * This code is part of the code to initiate the device removal
3167  * handshake protocol with controller firmware.
3168  *
3169  * Return 1 meaning mf should be freed from _base_interrupt
3170  *        0 means the mf is freed from this function.
3171  */
3172 static u8
3173 _scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3174     u8 msix_index, u32 reply)
3175 {
3176         Mpi2SasIoUnitControlReply_t *mpi_reply =
3177             mpt2sas_base_get_reply_virt_addr(ioc, reply);
3178         if (likely(mpi_reply)) {
3179                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3180                 "sc_complete:handle(0x%04x), (open) "
3181                 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3182                 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3183                 le16_to_cpu(mpi_reply->IOCStatus),
3184                 le32_to_cpu(mpi_reply->IOCLogInfo)));
3185         } else {
3186                 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3187                     ioc->name, __FILE__, __LINE__, __func__);
3188         }
3189         return 1;
3190 }
3191 
3192 /**
3193  * _scsih_tm_tr_volume_send - send target reset request for volumes
3194  * @ioc: per adapter object
3195  * @handle: device handle
3196  * Context: interrupt time.
3197  *
3198  * This is designed to send muliple task management request at the same
3199  * time to the fifo. If the fifo is full, we will append the request,
3200  * and process it in a future completion.
3201  */
3202 static void
3203 _scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3204 {
3205         Mpi2SCSITaskManagementRequest_t *mpi_request;
3206         u16 smid;
3207         struct _tr_list *delayed_tr;
3208 
3209         if (ioc->shost_recovery || ioc->remove_host ||
3210             ioc->pci_error_recovery) {
3211                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3212                    "progress!\n", __func__, ioc->name));
3213                 return;
3214         }
3215 
3216         smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3217         if (!smid) {
3218                 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3219                 if (!delayed_tr)
3220                         return;
3221                 INIT_LIST_HEAD(&delayed_tr->list);
3222                 delayed_tr->handle = handle;
3223                 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3224                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3225                     "DELAYED:tr:handle(0x%04x), (open)\n",
3226                     ioc->name, handle));
3227                 return;
3228         }
3229 
3230         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3231             "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3232             ioc->tm_tr_volume_cb_idx));
3233         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3234         memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3235         mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3236         mpi_request->DevHandle = cpu_to_le16(handle);
3237         mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3238         mpt2sas_base_put_smid_hi_priority(ioc, smid);
3239 }
3240 
3241 /**
3242  * _scsih_tm_volume_tr_complete - target reset completion
3243  * @ioc: per adapter object
3244  * @smid: system request message index
3245  * @msix_index: MSIX table index supplied by the OS
3246  * @reply: reply message frame(lower 32bit addr)
3247  * Context: interrupt time.
3248  *
3249  * Return 1 meaning mf should be freed from _base_interrupt
3250  *        0 means the mf is freed from this function.
3251  */
3252 static u8
3253 _scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3254     u8 msix_index, u32 reply)
3255 {
3256         u16 handle;
3257         Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3258         Mpi2SCSITaskManagementReply_t *mpi_reply =
3259             mpt2sas_base_get_reply_virt_addr(ioc, reply);
3260 
3261         if (ioc->shost_recovery || ioc->remove_host ||
3262             ioc->pci_error_recovery) {
3263                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3264                    "progress!\n", __func__, ioc->name));
3265                 return 1;
3266         }
3267         if (unlikely(!mpi_reply)) {
3268                 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3269                     ioc->name, __FILE__, __LINE__, __func__);
3270                 return 1;
3271         }
3272         mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3273         handle = le16_to_cpu(mpi_request_tm->DevHandle);
3274         if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3275                 dewtprintk(ioc, printk("spurious interrupt: "
3276                     "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
3277                     le16_to_cpu(mpi_reply->DevHandle), smid));
3278                 return 0;
3279         }
3280 
3281         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3282             "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3283             "loginfo(0x%08x), completed(%d)\n", ioc->name,
3284             handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3285             le32_to_cpu(mpi_reply->IOCLogInfo),
3286             le32_to_cpu(mpi_reply->TerminationCount)));
3287 
3288         return _scsih_check_for_pending_tm(ioc, smid);
3289 }
3290 
3291 /**
3292  * _scsih_tm_tr_complete -
3293  * @ioc: per adapter object
3294  * @smid: system request message index
3295  * @msix_index: MSIX table index supplied by the OS
3296  * @reply: reply message frame(lower 32bit addr)
3297  * Context: interrupt time.
3298  *
3299  * This is the target reset completion routine.
3300  * This code is part of the code to initiate the device removal
3301  * handshake protocol with controller firmware.
3302  * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
3303  *
3304  * Return 1 meaning mf should be freed from _base_interrupt
3305  *        0 means the mf is freed from this function.
3306  */
3307 static u8
3308 _scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3309     u32 reply)
3310 {
3311         u16 handle;
3312         Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3313         Mpi2SCSITaskManagementReply_t *mpi_reply =
3314             mpt2sas_base_get_reply_virt_addr(ioc, reply);
3315         Mpi2SasIoUnitControlRequest_t *mpi_request;
3316         u16 smid_sas_ctrl;
3317         u32 ioc_state;
3318 
3319         if (ioc->remove_host) {
3320                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3321                    "removed\n", __func__, ioc->name));
3322                 return 1;
3323         } else if (ioc->pci_error_recovery) {
3324                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3325                     "error recovery\n", __func__, ioc->name));
3326                 return 1;
3327         }
3328         ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3329         if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3330                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3331                     "operational\n", __func__, ioc->name));
3332                 return 1;
3333         }
3334         if (unlikely(!mpi_reply)) {
3335                 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3336                     ioc->name, __FILE__, __LINE__, __func__);
3337                 return 1;
3338         }
3339         mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3340         handle = le16_to_cpu(mpi_request_tm->DevHandle);
3341         if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3342                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
3343                     "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
3344                     le16_to_cpu(mpi_reply->DevHandle), smid));
3345                 return 0;
3346         }
3347 
3348         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3349             "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3350             "loginfo(0x%08x), completed(%d)\n", ioc->name,
3351             handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3352             le32_to_cpu(mpi_reply->IOCLogInfo),
3353             le32_to_cpu(mpi_reply->TerminationCount)));
3354 
3355         smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3356         if (!smid_sas_ctrl) {
3357                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3358                     ioc->name, __func__);
3359                 return 1;
3360         }
3361 
3362         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
3363             "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
3364             ioc->tm_sas_control_cb_idx));
3365         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3366         memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3367         mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3368         mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3369         mpi_request->DevHandle = mpi_request_tm->DevHandle;
3370         mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
3371 
3372         return _scsih_check_for_pending_tm(ioc, smid);
3373 }
3374 
3375 /**
3376  * _scsih_check_for_pending_tm - check for pending task management
3377  * @ioc: per adapter object
3378  * @smid: system request message index
3379  *
3380  * This will check delayed target reset list, and feed the
3381  * next reqeust.
3382  *
3383  * Return 1 meaning mf should be freed from _base_interrupt
3384  *        0 means the mf is freed from this function.
3385  */
3386 static u8
3387 _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3388 {
3389         struct _tr_list *delayed_tr;
3390 
3391         if (!list_empty(&ioc->delayed_tr_volume_list)) {
3392                 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3393                     struct _tr_list, list);
3394                 mpt2sas_base_free_smid(ioc, smid);
3395                 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3396                 list_del(&delayed_tr->list);
3397                 kfree(delayed_tr);
3398                 return 0;
3399         }
3400 
3401         if (!list_empty(&ioc->delayed_tr_list)) {
3402                 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3403                     struct _tr_list, list);
3404                 mpt2sas_base_free_smid(ioc, smid);
3405                 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3406                 list_del(&delayed_tr->list);
3407                 kfree(delayed_tr);
3408                 return 0;
3409         }
3410 
3411         return 1;
3412 }
3413 
3414 /**
3415  * _scsih_check_topo_delete_events - sanity check on topo events
3416  * @ioc: per adapter object
3417  * @event_data: the event data payload
3418  *
3419  * This routine added to better handle cable breaker.
3420  *
3421  * This handles the case where driver receives multiple expander
3422  * add and delete events in a single shot.  When there is a delete event
3423  * the routine will void any pending add events waiting in the event queue.
3424  *
3425  * Return nothing.
3426  */
3427 static void
3428 _scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
3429     Mpi2EventDataSasTopologyChangeList_t *event_data)
3430 {
3431         struct fw_event_work *fw_event;
3432         Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3433         u16 expander_handle;
3434         struct _sas_node *sas_expander;
3435         unsigned long flags;
3436         int i, reason_code;
3437         u16 handle;
3438 
3439         for (i = 0 ; i < event_data->NumEntries; i++) {
3440                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3441                 if (!handle)
3442                         continue;
3443                 reason_code = event_data->PHY[i].PhyStatus &
3444                     MPI2_EVENT_SAS_TOPO_RC_MASK;
3445                 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3446                         _scsih_tm_tr_send(ioc, handle);
3447         }
3448 
3449         expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3450         if (expander_handle < ioc->sas_hba.num_phys) {
3451                 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3452                 return;
3453         }
3454         if (event_data->ExpStatus ==
3455             MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3456                 /* put expander attached devices into blocking state */
3457                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3458                 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3459                     expander_handle);
3460                 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3461                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3462                 do {
3463                         handle = find_first_bit(ioc->blocking_handles,
3464                             ioc->facts.MaxDevHandle);
3465                         if (handle < ioc->facts.MaxDevHandle)
3466                                 _scsih_block_io_device(ioc, handle);
3467                 } while (test_and_clear_bit(handle, ioc->blocking_handles));
3468         } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3469                 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3470 
3471         if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3472                 return;
3473 
3474         /* mark ignore flag for pending events */
3475         spin_lock_irqsave(&ioc->fw_event_lock, flags);
3476         list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3477                 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3478                     fw_event->ignore)
3479                         continue;
3480                 local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3481                         fw_event->event_data;
3482                 if (local_event_data->ExpStatus ==
3483                     MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3484                     local_event_data->ExpStatus ==
3485                     MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3486                         if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3487                             expander_handle) {
3488                                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3489                                     "setting ignoring flag\n", ioc->name));
3490                                 fw_event->ignore = 1;
3491                         }
3492                 }
3493         }
3494         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3495 }
3496 
3497 /**
3498  * _scsih_set_volume_delete_flag - setting volume delete flag
3499  * @ioc: per adapter object
3500  * @handle: device handle
3501  *
3502  * This
3503  * Return nothing.
3504  */
3505 static void
3506 _scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3507 {
3508         struct _raid_device *raid_device;
3509         struct MPT2SAS_TARGET *sas_target_priv_data;
3510         unsigned long flags;
3511 
3512         spin_lock_irqsave(&ioc->raid_device_lock, flags);
3513         raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3514         if (raid_device && raid_device->starget &&
3515             raid_device->starget->hostdata) {
3516                 sas_target_priv_data =
3517                     raid_device->starget->hostdata;
3518                 sas_target_priv_data->deleted = 1;
3519                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3520                     "setting delete flag: handle(0x%04x), "
3521                     "wwid(0x%016llx)\n", ioc->name, handle,
3522                     (unsigned long long) raid_device->wwid));
3523         }
3524         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3525 }
3526 
3527 /**
3528  * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3529  * @handle: input handle
3530  * @a: handle for volume a
3531  * @b: handle for volume b
3532  *
3533  * IR firmware only supports two raid volumes.  The purpose of this
3534  * routine is to set the volume handle in either a or b. When the given
3535  * input handle is non-zero, or when a and b have not been set before.
3536  */
3537 static void
3538 _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3539 {
3540         if (!handle || handle == *a || handle == *b)
3541                 return;
3542         if (!*a)
3543                 *a = handle;
3544         else if (!*b)
3545                 *b = handle;
3546 }
3547 
3548 /**
3549  * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3550  * @ioc: per adapter object
3551  * @event_data: the event data payload
3552  * Context: interrupt time.
3553  *
3554  * This routine will send target reset to volume, followed by target
3555  * resets to the PDs. This is called when a PD has been removed, or
3556  * volume has been deleted or removed. When the target reset is sent
3557  * to volume, the PD target resets need to be queued to start upon
3558  * completion of the volume target reset.
3559  *
3560  * Return nothing.
3561  */
3562 static void
3563 _scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3564     Mpi2EventDataIrConfigChangeList_t *event_data)
3565 {
3566         Mpi2EventIrConfigElement_t *element;
3567         int i;
3568         u16 handle, volume_handle, a, b;
3569         struct _tr_list *delayed_tr;
3570 
3571         a = 0;
3572         b = 0;
3573 
3574         if (ioc->is_warpdrive)
3575                 return;
3576 
3577         /* Volume Resets for Deleted or Removed */
3578         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3579         for (i = 0; i < event_data->NumElements; i++, element++) {
3580                 if (element->ReasonCode ==
3581                     MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3582                     element->ReasonCode ==
3583                     MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3584                         volume_handle = le16_to_cpu(element->VolDevHandle);
3585                         _scsih_set_volume_delete_flag(ioc, volume_handle);
3586                         _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3587                 }
3588         }
3589 
3590         /* Volume Resets for UNHIDE events */
3591         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3592         for (i = 0; i < event_data->NumElements; i++, element++) {
3593                 if (le32_to_cpu(event_data->Flags) &
3594                     MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3595                         continue;
3596                 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3597                         volume_handle = le16_to_cpu(element->VolDevHandle);
3598                         _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3599                 }
3600         }
3601 
3602         if (a)
3603                 _scsih_tm_tr_volume_send(ioc, a);
3604         if (b)
3605                 _scsih_tm_tr_volume_send(ioc, b);
3606 
3607         /* PD target resets */
3608         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3609         for (i = 0; i < event_data->NumElements; i++, element++) {
3610                 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3611                         continue;
3612                 handle = le16_to_cpu(element->PhysDiskDevHandle);
3613                 volume_handle = le16_to_cpu(element->VolDevHandle);
3614                 clear_bit(handle, ioc->pd_handles);
3615                 if (!volume_handle)
3616                         _scsih_tm_tr_send(ioc, handle);
3617                 else if (volume_handle == a || volume_handle == b) {
3618                         delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3619                         BUG_ON(!delayed_tr);
3620                         INIT_LIST_HEAD(&delayed_tr->list);
3621                         delayed_tr->handle = handle;
3622                         list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3623                         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3624                             "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3625                             handle));
3626                 } else
3627                         _scsih_tm_tr_send(ioc, handle);
3628         }
3629 }
3630 
3631 
3632 /**
3633  * _scsih_check_volume_delete_events - set delete flag for volumes
3634  * @ioc: per adapter object
3635  * @event_data: the event data payload
3636  * Context: interrupt time.
3637  *
3638  * This will handle the case when the cable connected to entire volume is
3639  * pulled. We will take care of setting the deleted flag so normal IO will
3640  * not be sent.
3641  *
3642  * Return nothing.
3643  */
3644 static void
3645 _scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3646     Mpi2EventDataIrVolume_t *event_data)
3647 {
3648         u32 state;
3649 
3650         if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3651                 return;
3652         state = le32_to_cpu(event_data->NewValue);
3653         if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3654             MPI2_RAID_VOL_STATE_FAILED)
3655                 _scsih_set_volume_delete_flag(ioc,
3656                     le16_to_cpu(event_data->VolDevHandle));
3657 }
3658 
3659 /**
3660  * _scsih_temp_threshold_events - display temperature threshold exceeded events
3661  * @ioc: per adapter object
3662  * @event_data: the temp threshold event data
3663  * Context: interrupt time.
3664  *
3665  * Return nothing.
3666  */
3667 static void
3668 _scsih_temp_threshold_events(struct MPT2SAS_ADAPTER *ioc,
3669         Mpi2EventDataTemperature_t *event_data)
3670 {
3671         if (ioc->temp_sensors_count >= event_data->SensorNum) {
3672                 printk(MPT2SAS_ERR_FMT "Temperature Threshold flags %s%s%s%s"
3673                   " exceeded for Sensor: %d !!!\n", ioc->name,
3674                   ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
3675                   ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
3676                   ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
3677                   ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
3678                   event_data->SensorNum);
3679                 printk(MPT2SAS_ERR_FMT "Current Temp In Celsius: %d\n",
3680                         ioc->name, event_data->CurrentTemperature);
3681         }
3682 }
3683 
3684 /**
3685  * _scsih_flush_running_cmds - completing outstanding commands.
3686  * @ioc: per adapter object
3687  *
3688  * The flushing out of all pending scmd commands following host reset,
3689  * where all IO is dropped to the floor.
3690  *
3691  * Return nothing.
3692  */
3693 static void
3694 _scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3695 {
3696         struct scsi_cmnd *scmd;
3697         u16 smid;
3698         u16 count = 0;
3699 
3700         for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3701                 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3702                 if (!scmd)
3703                         continue;
3704                 count++;
3705                 mpt2sas_base_free_smid(ioc, smid);
3706                 scsi_dma_unmap(scmd);
3707                 if (ioc->pci_error_recovery)
3708                         scmd->result = DID_NO_CONNECT << 16;
3709                 else
3710                         scmd->result = DID_RESET << 16;
3711                 scmd->scsi_done(scmd);
3712         }
3713         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3714             ioc->name, count));
3715 }
3716 
3717 /**
3718  * _scsih_setup_eedp - setup MPI request for EEDP transfer
3719  * @scmd: pointer to scsi command object
3720  * @mpi_request: pointer to the SCSI_IO reqest message frame
3721  *
3722  * Supporting protection 1 and 3.
3723  *
3724  * Returns nothing
3725  */
3726 static void
3727 _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3728 {
3729         u16 eedp_flags;
3730         unsigned char prot_op = scsi_get_prot_op(scmd);
3731         unsigned char prot_type = scsi_get_prot_type(scmd);
3732 
3733         if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3734                 return;
3735 
3736         if (prot_op ==  SCSI_PROT_READ_STRIP)
3737                 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3738         else if (prot_op ==  SCSI_PROT_WRITE_INSERT)
3739                 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3740         else
3741                 return;
3742 
3743         switch (prot_type) {
3744         case SCSI_PROT_DIF_TYPE1:
3745         case SCSI_PROT_DIF_TYPE2:
3746 
3747                 /*
3748                 * enable ref/guard checking
3749                 * auto increment ref tag
3750                 */
3751                 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3752                     MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3753                     MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3754                 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3755                     cpu_to_be32(scsi_get_lba(scmd));
3756                 break;
3757 
3758         case SCSI_PROT_DIF_TYPE3:
3759 
3760                 /*
3761                 * enable guard checking
3762                 */
3763                 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3764                 break;
3765         }
3766         mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3767         mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3768 }
3769 
3770 /**
3771  * _scsih_eedp_error_handling - return sense code for EEDP errors
3772  * @scmd: pointer to scsi command object
3773  * @ioc_status: ioc status
3774  *
3775  * Returns nothing
3776  */
3777 static void
3778 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3779 {
3780         u8 ascq;
3781 
3782         switch (ioc_status) {
3783         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3784                 ascq = 0x01;
3785                 break;
3786         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3787                 ascq = 0x02;
3788                 break;
3789         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3790                 ascq = 0x03;
3791                 break;
3792         default:
3793                 ascq = 0x00;
3794                 break;
3795         }
3796 
3797         scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10, ascq);
3798         scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
3799             SAM_STAT_CHECK_CONDITION;
3800 }
3801 
3802 /**
3803  * _scsih_scsi_direct_io_get - returns direct io flag
3804  * @ioc: per adapter object
3805  * @smid: system request message index
3806  *
3807  * Returns the smid stored scmd pointer.
3808  */
3809 static inline u8
3810 _scsih_scsi_direct_io_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3811 {
3812         return ioc->scsi_lookup[smid - 1].direct_io;
3813 }
3814 
3815 /**
3816  * _scsih_scsi_direct_io_set - sets direct io flag
3817  * @ioc: per adapter object
3818  * @smid: system request message index
3819  * @direct_io: Zero or non-zero value to set in the direct_io flag
3820  *
3821  * Returns Nothing.
3822  */
3823 static inline void
3824 _scsih_scsi_direct_io_set(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
3825 {
3826         ioc->scsi_lookup[smid - 1].direct_io = direct_io;
3827 }
3828 
3829 
3830 /**
3831  * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
3832  * @ioc: per adapter object
3833  * @scmd: pointer to scsi command object
3834  * @raid_device: pointer to raid device data structure
3835  * @mpi_request: pointer to the SCSI_IO reqest message frame
3836  * @smid: system request message index
3837  *
3838  * Returns nothing
3839  */
3840 static void
3841 _scsih_setup_direct_io(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3842         struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
3843         u16 smid)
3844 {
3845         sector_t v_lba, p_lba, stripe_off, column, io_size;
3846         u32 stripe_sz, stripe_exp;
3847         u8 num_pds, cmd = scmd->cmnd[0];
3848 
3849         if (cmd != READ_10 && cmd != WRITE_10 &&
3850             cmd != READ_16 && cmd != WRITE_16)
3851                 return;
3852 
3853         if (cmd == READ_10 || cmd == WRITE_10)
3854                 v_lba = get_unaligned_be32(&mpi_request->CDB.CDB32[2]);
3855         else
3856                 v_lba = get_unaligned_be64(&mpi_request->CDB.CDB32[2]);
3857 
3858         io_size = scsi_bufflen(scmd) >> raid_device->block_exponent;
3859 
3860         if (v_lba + io_size - 1 > raid_device->max_lba)
3861                 return;
3862 
3863         stripe_sz = raid_device->stripe_sz;
3864         stripe_exp = raid_device->stripe_exponent;
3865         stripe_off = v_lba & (stripe_sz - 1);
3866 
3867         /* Return unless IO falls within a stripe */
3868         if (stripe_off + io_size > stripe_sz)
3869                 return;
3870 
3871         num_pds = raid_device->num_pds;
3872         p_lba = v_lba >> stripe_exp;
3873         column = sector_div(p_lba, num_pds);
3874         p_lba = (p_lba << stripe_exp) + stripe_off;
3875 
3876         mpi_request->DevHandle = cpu_to_le16(raid_device->pd_handle[column]);
3877 
3878         if (cmd == READ_10 || cmd == WRITE_10)
3879                 put_unaligned_be32(lower_32_bits(p_lba),
3880                                    &mpi_request->CDB.CDB32[2]);
3881         else
3882                 put_unaligned_be64(p_lba, &mpi_request->CDB.CDB32[2]);
3883 
3884         _scsih_scsi_direct_io_set(ioc, smid, 1);
3885 }
3886 
3887 /**
3888  * _scsih_qcmd - main scsi request entry point
3889  * @scmd: pointer to scsi command object
3890  * @done: function pointer to be invoked on completion
3891  *
3892  * The callback index is set inside `ioc->scsi_io_cb_idx`.
3893  *
3894  * Returns 0 on success.  If there's a failure, return either:
3895  * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3896  * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3897  */
3898 static int
3899 _scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
3900 {
3901         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
3902         struct MPT2SAS_DEVICE *sas_device_priv_data;
3903         struct MPT2SAS_TARGET *sas_target_priv_data;
3904         struct _raid_device *raid_device;
3905         Mpi2SCSIIORequest_t *mpi_request;
3906         u32 mpi_control;
3907         u16 smid;
3908 
3909         sas_device_priv_data = scmd->device->hostdata;
3910         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
3911                 scmd->result = DID_NO_CONNECT << 16;
3912                 scmd->scsi_done(scmd);
3913                 return 0;
3914         }
3915 
3916         if (ioc->pci_error_recovery || ioc->remove_host) {
3917                 scmd->result = DID_NO_CONNECT << 16;
3918                 scmd->scsi_done(scmd);
3919                 return 0;
3920         }
3921 
3922         sas_target_priv_data = sas_device_priv_data->sas_target;
3923         /* invalid device handle */
3924         if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
3925                 scmd->result = DID_NO_CONNECT << 16;
3926                 scmd->scsi_done(scmd);
3927                 return 0;
3928         }
3929 
3930         /* host recovery or link resets sent via IOCTLs */
3931         if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
3932                 return SCSI_MLQUEUE_HOST_BUSY;
3933         /* device busy with task management */
3934         else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3935                 return SCSI_MLQUEUE_DEVICE_BUSY;
3936         /* device has been deleted */
3937         else if (sas_target_priv_data->deleted) {
3938                 scmd->result = DID_NO_CONNECT << 16;
3939                 scmd->scsi_done(scmd);
3940                 return 0;
3941         }
3942 
3943         if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3944                 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3945         else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3946                 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3947         else
3948                 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3949 
3950         /* set tags */
3951         mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3952 
3953         /* Make sure Device is not raid volume.
3954          * We do not expose raid functionality to upper layer for warpdrive.
3955          */
3956         if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
3957             sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
3958                 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3959 
3960         smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
3961         if (!smid) {
3962                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3963                     ioc->name, __func__);
3964                 goto out;
3965         }
3966         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3967         memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
3968         _scsih_setup_eedp(scmd, mpi_request);
3969         if (scmd->cmd_len == 32)
3970                 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
3971         mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3972         if (sas_device_priv_data->sas_target->flags &
3973             MPT_TARGET_FLAGS_RAID_COMPONENT)
3974                 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3975         else
3976         mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3977         mpi_request->DevHandle =
3978             cpu_to_le16(sas_device_priv_data->sas_target->handle);
3979         mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3980         mpi_request->Control = cpu_to_le32(mpi_control);
3981         mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3982         mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3983         mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3984         mpi_request->SenseBufferLowAddress =
3985             mpt2sas_base_get_sense_buffer_dma(ioc, smid);
3986         mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3987         mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3988             MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
3989         mpi_request->VF_ID = 0; /* TODO */
3990         mpi_request->VP_ID = 0;
3991         int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3992             mpi_request->LUN);
3993         memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3994 
3995         if (!mpi_request->DataLength) {
3996                 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3997         } else {
3998                 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3999                         mpt2sas_base_free_smid(ioc, smid);
4000                         goto out;
4001                 }
4002         }
4003 
4004         raid_device = sas_target_priv_data->raid_device;
4005         if (raid_device && raid_device->direct_io_enabled)
4006                 _scsih_setup_direct_io(ioc, scmd, raid_device, mpi_request,
4007                     smid);
4008 
4009         if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
4010                 mpt2sas_base_put_smid_scsi_io(ioc, smid,
4011                     le16_to_cpu(mpi_request->DevHandle));
4012         else
4013                 mpt2sas_base_put_smid_default(ioc, smid);
4014         return 0;
4015 
4016  out:
4017         return SCSI_MLQUEUE_HOST_BUSY;
4018 }
4019 
4020 /**
4021  * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4022  * @sense_buffer: sense data returned by target
4023  * @data: normalized skey/asc/ascq
4024  *
4025  * Return nothing.
4026  */
4027 static void
4028 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4029 {
4030         if ((sense_buffer[0] & 0x7F) >= 0x72) {
4031                 /* descriptor format */
4032                 data->skey = sense_buffer[1] & 0x0F;
4033                 data->asc = sense_buffer[2];
4034                 data->ascq = sense_buffer[3];
4035         } else {
4036                 /* fixed format */
4037                 data->skey = sense_buffer[2] & 0x0F;
4038                 data->asc = sense_buffer[12];
4039                 data->ascq = sense_buffer[13];
4040         }
4041 }
4042 
4043 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4044 /**
4045  * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
4046  * @ioc: per adapter object
4047  * @scmd: pointer to scsi command object
4048  * @mpi_reply: reply mf payload returned from firmware
4049  *
4050  * scsi_status - SCSI Status code returned from target device
4051  * scsi_state - state info associated with SCSI_IO determined by ioc
4052  * ioc_status - ioc supplied status info
4053  *
4054  * Return nothing.
4055  */
4056 static void
4057 _scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4058     Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4059 {
4060         u32 response_info;
4061         u8 *response_bytes;
4062         u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4063             MPI2_IOCSTATUS_MASK;
4064         u8 scsi_state = mpi_reply->SCSIState;
4065         u8 scsi_status = mpi_reply->SCSIStatus;
4066         char *desc_ioc_state = NULL;
4067         char *desc_scsi_status = NULL;
4068         char *desc_scsi_state = ioc->tmp_string;
4069         u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4070         struct _sas_device *sas_device = NULL;
4071         unsigned long flags;
4072         struct scsi_target *starget = scmd->device->sdev_target;
4073         struct MPT2SAS_TARGET *priv_target = starget->hostdata;
4074         char *device_str = NULL;
4075 
4076         if (!priv_target)
4077                 return;
4078 
4079         if (ioc->hide_ir_msg)
4080                 device_str = "WarpDrive";
4081         else
4082                 device_str = "volume";
4083 
4084         if (log_info == 0x31170000)
4085                 return;
4086 
4087         switch (ioc_status) {
4088         case MPI2_IOCSTATUS_SUCCESS:
4089                 desc_ioc_state = "success";
4090                 break;
4091         case MPI2_IOCSTATUS_INVALID_FUNCTION:
4092                 desc_ioc_state = "invalid function";
4093                 break;
4094         case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4095                 desc_ioc_state = "scsi recovered error";
4096                 break;
4097         case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4098                 desc_ioc_state = "scsi invalid dev handle";
4099                 break;
4100         case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4101                 desc_ioc_state = "scsi device not there";
4102                 break;
4103         case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4104                 desc_ioc_state = "scsi data overrun";
4105                 break;
4106         case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4107                 desc_ioc_state = "scsi data underrun";
4108                 break;
4109         case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4110                 desc_ioc_state = "scsi io data error";
4111                 break;
4112         case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4113                 desc_ioc_state = "scsi protocol error";
4114                 break;
4115         case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4116                 desc_ioc_state = "scsi task terminated";
4117                 break;
4118         case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4119                 desc_ioc_state = "scsi residual mismatch";
4120                 break;
4121         case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4122                 desc_ioc_state = "scsi task mgmt failed";
4123                 break;
4124         case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4125                 desc_ioc_state = "scsi ioc terminated";
4126                 break;
4127         case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4128                 desc_ioc_state = "scsi ext terminated";
4129                 break;
4130         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4131                 desc_ioc_state = "eedp guard error";
4132                 break;
4133         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4134                 desc_ioc_state = "eedp ref tag error";
4135                 break;
4136         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4137                 desc_ioc_state = "eedp app tag error";
4138                 break;
4139         default:
4140                 desc_ioc_state = "unknown";
4141                 break;
4142         }
4143 
4144         switch (scsi_status) {
4145         case MPI2_SCSI_STATUS_GOOD:
4146                 desc_scsi_status = "good";
4147                 break;
4148         case MPI2_SCSI_STATUS_CHECK_CONDITION:
4149                 desc_scsi_status = "check condition";
4150                 break;
4151         case MPI2_SCSI_STATUS_CONDITION_MET:
4152                 desc_scsi_status = "condition met";
4153                 break;
4154         case MPI2_SCSI_STATUS_BUSY:
4155                 desc_scsi_status = "busy";
4156                 break;
4157         case MPI2_SCSI_STATUS_INTERMEDIATE:
4158                 desc_scsi_status = "intermediate";
4159                 break;
4160         case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4161                 desc_scsi_status = "intermediate condmet";
4162                 break;
4163         case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4164                 desc_scsi_status = "reservation conflict";
4165                 break;
4166         case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4167                 desc_scsi_status = "command terminated";
4168                 break;
4169         case MPI2_SCSI_STATUS_TASK_SET_FULL:
4170                 desc_scsi_status = "task set full";
4171                 break;
4172         case MPI2_SCSI_STATUS_ACA_ACTIVE:
4173                 desc_scsi_status = "aca active";
4174                 break;
4175         case MPI2_SCSI_STATUS_TASK_ABORTED:
4176                 desc_scsi_status = "task aborted";
4177                 break;
4178         default:
4179                 desc_scsi_status = "unknown";
4180                 break;
4181         }
4182 
4183         desc_scsi_state[0] = '\0';
4184         if (!scsi_state)
4185                 desc_scsi_state = " ";
4186         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4187                 strcat(desc_scsi_state, "response info ");
4188         if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4189                 strcat(desc_scsi_state, "state terminated ");
4190         if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4191                 strcat(desc_scsi_state, "no status ");
4192         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4193                 strcat(desc_scsi_state, "autosense failed ");
4194         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4195                 strcat(desc_scsi_state, "autosense valid ");
4196 
4197         scsi_print_command(scmd);
4198 
4199         if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
4200                 printk(MPT2SAS_WARN_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4201                     device_str, (unsigned long long)priv_target->sas_address);
4202         } else {
4203                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4204                 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4205                     priv_target->sas_address);
4206                 if (sas_device) {
4207                         printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
4208                             "phy(%d)\n", ioc->name, sas_device->sas_address,
4209                             sas_device->phy);
4210                         printk(MPT2SAS_WARN_FMT
4211                             "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
4212                             ioc->name, sas_device->enclosure_logical_id,
4213                             sas_device->slot);
4214                 }
4215                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4216         }
4217 
4218         printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
4219             "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4220             desc_ioc_state, ioc_status, smid);
4221         printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
4222             "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
4223             scsi_get_resid(scmd));
4224         printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
4225             "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4226             le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4227         printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
4228             "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
4229             scsi_status, desc_scsi_state, scsi_state);
4230 
4231         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4232                 struct sense_info data;
4233                 _scsih_normalize_sense(scmd->sense_buffer, &data);
4234                 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
4235                     "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
4236                     data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
4237         }
4238 
4239         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4240                 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4241                 response_bytes = (u8 *)&response_info;
4242                 _scsih_response_code(ioc, response_bytes[0]);
4243         }
4244 }
4245 #endif
4246 
4247 /**
4248  * _scsih_turn_on_pfa_led - illuminate PFA LED
4249  * @ioc: per adapter object
4250  * @handle: device handle
4251  * Context: process
4252  *
4253  * Return nothing.
4254  */
4255 static void
4256 _scsih_turn_on_pfa_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4257 {
4258         Mpi2SepReply_t mpi_reply;
4259         Mpi2SepRequest_t mpi_request;
4260         struct _sas_device *sas_device;
4261 
4262         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4263         if (!sas_device)
4264                 return;
4265 
4266         memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4267         mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4268         mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4269         mpi_request.SlotStatus =
4270             cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4271         mpi_request.DevHandle = cpu_to_le16(handle);
4272         mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4273         if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4274             &mpi_request)) != 0) {
4275                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4276                 __FILE__, __LINE__, __func__);
4277                 return;
4278         }
4279         sas_device->pfa_led_on = 1;
4280 
4281 
4282         if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4283                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
4284                  "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4285                  ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4286                  le32_to_cpu(mpi_reply.IOCLogInfo)));
4287                 return;
4288         }
4289 }
4290 
4291 /**
4292  * _scsih_turn_off_pfa_led - turn off PFA LED
4293  * @ioc: per adapter object
4294  * @sas_device: sas device whose PFA LED has to turned off
4295  * Context: process
4296  *
4297  * Return nothing.
4298  */
4299 static void
4300 _scsih_turn_off_pfa_led(struct MPT2SAS_ADAPTER *ioc,
4301         struct _sas_device *sas_device)
4302 {
4303         Mpi2SepReply_t mpi_reply;
4304         Mpi2SepRequest_t mpi_request;
4305 
4306         memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4307         mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4308         mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4309         mpi_request.SlotStatus = 0;
4310         mpi_request.Slot = cpu_to_le16(sas_device->slot);
4311         mpi_request.DevHandle = 0;
4312         mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
4313         mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
4314         if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4315             &mpi_request)) != 0) {
4316                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4317                 __FILE__, __LINE__, __func__);
4318                 return;
4319         }
4320 
4321         if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4322                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "enclosure_processor: "
4323                     "ioc_status (0x%04x), loginfo(0x%08x)\n", ioc->name,
4324                     le16_to_cpu(mpi_reply.IOCStatus),
4325                     le32_to_cpu(mpi_reply.IOCLogInfo)));
4326                 return;
4327         }
4328 }
4329 
4330 /**
4331  * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
4332  * @ioc: per adapter object
4333  * @handle: device handle
4334  * Context: interrupt.
4335  *
4336  * Return nothing.
4337  */
4338 static void
4339 _scsih_send_event_to_turn_on_pfa_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4340 {
4341         struct fw_event_work *fw_event;
4342 
4343         fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4344         if (!fw_event)
4345                 return;
4346         fw_event->event = MPT2SAS_TURN_ON_PFA_LED;
4347         fw_event->device_handle = handle;
4348         fw_event->ioc = ioc;
4349         _scsih_fw_event_add(ioc, fw_event);
4350 }
4351 
4352 /**
4353  * _scsih_smart_predicted_fault - process smart errors
4354  * @ioc: per adapter object
4355  * @handle: device handle
4356  * Context: interrupt.
4357  *
4358  * Return nothing.
4359  */
4360 static void
4361 _scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4362 {
4363         struct scsi_target *starget;
4364         struct MPT2SAS_TARGET *sas_target_priv_data;
4365         Mpi2EventNotificationReply_t *event_reply;
4366         Mpi2EventDataSasDeviceStatusChange_t *event_data;
4367         struct _sas_device *sas_device;
4368         ssize_t sz;
4369         unsigned long flags;
4370 
4371         /* only handle non-raid devices */
4372         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4373         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4374         if (!sas_device) {
4375                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4376                 return;
4377         }
4378         starget = sas_device->starget;
4379         sas_target_priv_data = starget->hostdata;
4380 
4381         if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4382            ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4383                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4384                 return;
4385         }
4386         starget_printk(KERN_WARNING, starget, "predicted fault\n");
4387         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4388 
4389         if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4390                 _scsih_send_event_to_turn_on_pfa_led(ioc, handle);
4391 
4392         /* insert into event log */
4393         sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4394              sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4395         event_reply = kzalloc(sz, GFP_ATOMIC);
4396         if (!event_reply) {
4397                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4398                     ioc->name, __FILE__, __LINE__, __func__);
4399                 return;
4400         }
4401 
4402         event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4403         event_reply->Event =
4404             cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4405         event_reply->MsgLength = sz/4;
4406         event_reply->EventDataLength =
4407             cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4408         event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4409             event_reply->EventData;
4410         event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4411         event_data->ASC = 0x5D;
4412         event_data->DevHandle = cpu_to_le16(handle);
4413         event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4414         mpt2sas_ctl_add_to_event_log(ioc, event_reply);
4415         kfree(event_reply);
4416 }
4417 
4418 /**
4419  * _scsih_io_done - scsi request callback
4420  * @ioc: per adapter object
4421  * @smid: system request message index
4422  * @msix_index: MSIX table index supplied by the OS
4423  * @reply: reply message frame(lower 32bit addr)
4424  *
4425  * Callback handler when using _scsih_qcmd.
4426  *
4427  * Return 1 meaning mf should be freed from _base_interrupt
4428  *        0 means the mf is freed from this function.
4429  */
4430 static u8
4431 _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4432 {
4433         Mpi2SCSIIORequest_t *mpi_request;
4434         Mpi2SCSIIOReply_t *mpi_reply;
4435         struct scsi_cmnd *scmd;
4436         u16 ioc_status;
4437         u32 xfer_cnt;
4438         u8 scsi_state;
4439         u8 scsi_status;
4440         u32 log_info;
4441         struct MPT2SAS_DEVICE *sas_device_priv_data;
4442         u32 response_code = 0;
4443         unsigned long flags;
4444 
4445         mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4446         scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
4447         if (scmd == NULL)
4448                 return 1;
4449 
4450         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4451 
4452         if (mpi_reply == NULL) {
4453                 scmd->result = DID_OK << 16;
4454                 goto out;
4455         }
4456 
4457         sas_device_priv_data = scmd->device->hostdata;
4458         if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4459              sas_device_priv_data->sas_target->deleted) {
4460                 scmd->result = DID_NO_CONNECT << 16;
4461                 goto out;
4462         }
4463         ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4464         /*
4465          * WARPDRIVE: If direct_io is set then it is directIO,
4466          * the failed direct I/O should be redirected to volume
4467          */
4468         if (_scsih_scsi_direct_io_get(ioc, smid) &&
4469             ((ioc_status & MPI2_IOCSTATUS_MASK)
4470             != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
4471                 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4472                 ioc->scsi_lookup[smid - 1].scmd = scmd;
4473                 _scsih_scsi_direct_io_set(ioc, smid, 0);
4474                 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4475                 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4476                 mpi_request->DevHandle =
4477                     cpu_to_le16(sas_device_priv_data->sas_target->handle);
4478                 mpt2sas_base_put_smid_scsi_io(ioc, smid,
4479                     sas_device_priv_data->sas_target->handle);
4480                 return 0;
4481         }
4482 
4483 
4484         /* turning off TLR */
4485         scsi_state = mpi_reply->SCSIState;
4486         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4487                 response_code =
4488                     le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
4489         if (!sas_device_priv_data->tlr_snoop_check) {
4490                 sas_device_priv_data->tlr_snoop_check++;
4491         /* Make sure Device is not raid volume.
4492          * We do not expose raid functionality to upper layer for warpdrive.
4493          */
4494         if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
4495                 sas_is_tlr_enabled(scmd->device) &&
4496                     response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4497                         sas_disable_tlr(scmd->device);
4498                         sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4499                 }
4500         }
4501 
4502         xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4503         scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4504         if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4505                 log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
4506         else
4507                 log_info = 0;
4508         ioc_status &= MPI2_IOCSTATUS_MASK;
4509         scsi_status = mpi_reply->SCSIStatus;
4510 
4511         if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4512             (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4513              scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4514              scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4515                 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4516         }
4517 
4518         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4519                 struct sense_info data;
4520                 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
4521                     smid);
4522                 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
4523                     le32_to_cpu(mpi_reply->SenseCount));
4524                 memcpy(scmd->sense_buffer, sense_data, sz);
4525                 _scsih_normalize_sense(scmd->sense_buffer, &data);
4526                 /* failure prediction threshold exceeded */
4527                 if (data.asc == 0x5D)
4528                         _scsih_smart_predicted_fault(ioc,
4529                             le16_to_cpu(mpi_reply->DevHandle));
4530         }
4531 
4532         switch (ioc_status) {
4533         case MPI2_IOCSTATUS_BUSY:
4534         case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4535                 scmd->result = SAM_STAT_BUSY;
4536                 break;
4537 
4538         case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4539                 scmd->result = DID_NO_CONNECT << 16;
4540                 break;
4541 
4542         case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4543                 if (sas_device_priv_data->block) {
4544                         scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4545                         goto out;
4546                 }
4547                 if (log_info == 0x32010081) {
4548                         scmd->result = DID_RESET << 16;
4549                         break;
4550                 }
4551                 scmd->result = DID_SOFT_ERROR << 16;
4552                 break;
4553         case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4554         case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4555                 scmd->result = DID_RESET << 16;
4556                 break;
4557 
4558         case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4559                 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4560                         scmd->result = DID_SOFT_ERROR << 16;
4561                 else
4562                         scmd->result = (DID_OK << 16) | scsi_status;
4563                 break;
4564 
4565         case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4566                 scmd->result = (DID_OK << 16) | scsi_status;
4567 
4568                 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4569                         break;
4570 
4571                 if (xfer_cnt < scmd->underflow) {
4572                         if (scsi_status == SAM_STAT_BUSY)
4573                                 scmd->result = SAM_STAT_BUSY;
4574                         else
4575                                 scmd->result = DID_SOFT_ERROR << 16;
4576                 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4577                      MPI2_SCSI_STATE_NO_SCSI_STATUS))
4578                         scmd->result = DID_SOFT_ERROR << 16;
4579                 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4580                         scmd->result = DID_RESET << 16;
4581                 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4582                         mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4583                         mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4584                         scmd->result = (DRIVER_SENSE << 24) |
4585                             SAM_STAT_CHECK_CONDITION;
4586                         scmd->sense_buffer[0] = 0x70;
4587                         scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4588                         scmd->sense_buffer[12] = 0x20;
4589                         scmd->sense_buffer[13] = 0;
4590                 }
4591                 break;
4592 
4593         case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4594                 scsi_set_resid(scmd, 0);
4595         case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4596         case MPI2_IOCSTATUS_SUCCESS:
4597                 scmd->result = (DID_OK << 16) | scsi_status;
4598                 if (response_code ==
4599                     MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4600                     (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4601                      MPI2_SCSI_STATE_NO_SCSI_STATUS)))
4602                         scmd->result = DID_SOFT_ERROR << 16;
4603                 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4604                         scmd->result = DID_RESET << 16;
4605                 break;
4606 
4607         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4608         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4609         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4610                 _scsih_eedp_error_handling(scmd, ioc_status);
4611                 break;
4612         case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4613         case MPI2_IOCSTATUS_INVALID_FUNCTION:
4614         case MPI2_IOCSTATUS_INVALID_SGL:
4615         case MPI2_IOCSTATUS_INTERNAL_ERROR:
4616         case MPI2_IOCSTATUS_INVALID_FIELD:
4617         case MPI2_IOCSTATUS_INVALID_STATE:
4618         case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4619         case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4620         default:
4621                 scmd->result = DID_SOFT_ERROR << 16;
4622                 break;
4623 
4624         }
4625 
4626 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4627         if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4628                 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4629 #endif
4630 
4631  out:
4632         scsi_dma_unmap(scmd);
4633         scmd->scsi_done(scmd);
4634         return 1;
4635 }
4636 
4637 /**
4638  * _scsih_sas_host_refresh - refreshing sas host object contents
4639  * @ioc: per adapter object
4640  * Context: user
4641  *
4642  * During port enable, fw will send topology events for every device. Its
4643  * possible that the handles may change from the previous setting, so this
4644  * code keeping handles updating if changed.
4645  *
4646  * Return nothing.
4647  */
4648 static void
4649 _scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
4650 {
4651         u16 sz;
4652         u16 ioc_status;
4653         int i;
4654         Mpi2ConfigReply_t mpi_reply;
4655         Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4656         u16 attached_handle;
4657         u8 link_rate;
4658 
4659         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4660             "updating handles for sas_host(0x%016llx)\n",
4661             ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4662 
4663         sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4664             * sizeof(Mpi2SasIOUnit0PhyData_t));
4665         sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4666         if (!sas_iounit_pg0) {
4667                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4668                     ioc->name, __FILE__, __LINE__, __func__);
4669                 return;
4670         }
4671 
4672         if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4673             sas_iounit_pg0, sz)) != 0)
4674                 goto out;
4675         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4676         if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4677                 goto out;
4678         for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4679                 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4680                 if (i == 0)
4681                         ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4682                             PhyData[0].ControllerDevHandle);
4683                 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4684                 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4685                     AttachedDevHandle);
4686                 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4687                         link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4688                 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4689                     attached_handle, i, link_rate);
4690         }
4691  out:
4692         kfree(sas_iounit_pg0);
4693 }
4694 
4695 /**
4696  * _scsih_sas_host_add - create sas host object
4697  * @ioc: per adapter object
4698  *
4699  * Creating host side data object, stored in ioc->sas_hba
4700  *
4701  * Return nothing.
4702  */
4703 static void
4704 _scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4705 {
4706         int i;
4707         Mpi2ConfigReply_t mpi_reply;
4708         Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4709         Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4710         Mpi2SasPhyPage0_t phy_pg0;
4711         Mpi2SasDevicePage0_t sas_device_pg0;
4712         Mpi2SasEnclosurePage0_t enclosure_pg0;
4713         u16 ioc_status;
4714         u16 sz;
4715         u16 device_missing_delay;
4716 
4717         mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4718         if (!ioc->sas_hba.num_phys) {
4719                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4720                     ioc->name, __FILE__, __LINE__, __func__);
4721                 return;
4722         }
4723 
4724         /* sas_iounit page 0 */
4725         sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4726             sizeof(Mpi2SasIOUnit0PhyData_t));
4727         sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4728         if (!sas_iounit_pg0) {
4729                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4730                     ioc->name, __FILE__, __LINE__, __func__);
4731                 return;
4732         }
4733         if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4734             sas_iounit_pg0, sz))) {
4735                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4736                     ioc->name, __FILE__, __LINE__, __func__);
4737                 goto out;
4738         }
4739         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4740             MPI2_IOCSTATUS_MASK;
4741         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4742                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4743                     ioc->name, __FILE__, __LINE__, __func__);
4744                 goto out;
4745         }
4746 
4747         /* sas_iounit page 1 */
4748         sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4749             sizeof(Mpi2SasIOUnit1PhyData_t));
4750         sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4751         if (!sas_iounit_pg1) {
4752                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4753                     ioc->name, __FILE__, __LINE__, __func__);
4754                 goto out;
4755         }
4756         if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4757             sas_iounit_pg1, sz))) {
4758                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4759                     ioc->name, __FILE__, __LINE__, __func__);
4760                 goto out;
4761         }
4762         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4763             MPI2_IOCSTATUS_MASK;
4764         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4765                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4766                     ioc->name, __FILE__, __LINE__, __func__);
4767                 goto out;
4768         }
4769 
4770         ioc->io_missing_delay =
4771             le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4772         device_missing_delay =
4773             le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4774         if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4775                 ioc->device_missing_delay = (device_missing_delay &
4776                     MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4777         else
4778                 ioc->device_missing_delay = device_missing_delay &
4779                     MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4780 
4781         ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4782         ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4783             sizeof(struct _sas_phy), GFP_KERNEL);
4784         if (!ioc->sas_hba.phy) {
4785                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4786                     ioc->name, __FILE__, __LINE__, __func__);
4787                 goto out;
4788         }
4789         for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4790                 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4791                     i))) {
4792                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4793                             ioc->name, __FILE__, __LINE__, __func__);
4794                         goto out;
4795                 }
4796                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4797                     MPI2_IOCSTATUS_MASK;
4798                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4799                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4800                             ioc->name, __FILE__, __LINE__, __func__);
4801                         goto out;
4802                 }
4803 
4804                 if (i == 0)
4805                         ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4806                             PhyData[0].ControllerDevHandle);
4807                 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4808                 ioc->sas_hba.phy[i].phy_id = i;
4809                 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4810                     phy_pg0, ioc->sas_hba.parent_dev);
4811         }
4812         if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4813             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
4814                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4815                     ioc->name, __FILE__, __LINE__, __func__);
4816                 goto out;
4817         }
4818         ioc->sas_hba.enclosure_handle =
4819             le16_to_cpu(sas_device_pg0.EnclosureHandle);
4820         ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4821         printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4822             "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4823             (unsigned long long) ioc->sas_hba.sas_address,
4824             ioc->sas_hba.num_phys) ;
4825 
4826         if (ioc->sas_hba.enclosure_handle) {
4827                 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4828                     &enclosure_pg0,
4829                    MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4830                    ioc->sas_hba.enclosure_handle))) {
4831                         ioc->sas_hba.enclosure_logical_id =
4832                             le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4833                 }
4834         }
4835 
4836  out:
4837         kfree(sas_iounit_pg1);
4838         kfree(sas_iounit_pg0);
4839 }
4840 
4841 /**
4842  * _scsih_expander_add -  creating expander object
4843  * @ioc: per adapter object
4844  * @handle: expander handle
4845  *
4846  * Creating expander object, stored in ioc->sas_expander_list.
4847  *
4848  * Return 0 for success, else error.
4849  */
4850 static int
4851 _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4852 {
4853         struct _sas_node *sas_expander;
4854         Mpi2ConfigReply_t mpi_reply;
4855         Mpi2ExpanderPage0_t expander_pg0;
4856         Mpi2ExpanderPage1_t expander_pg1;
4857         Mpi2SasEnclosurePage0_t enclosure_pg0;
4858         u32 ioc_status;
4859         u16 parent_handle;
4860         u64 sas_address, sas_address_parent = 0;
4861         int i;
4862         unsigned long flags;
4863         struct _sas_port *mpt2sas_port = NULL;
4864         int rc = 0;
4865 
4866         if (!handle)
4867                 return -1;
4868 
4869         if (ioc->shost_recovery || ioc->pci_error_recovery)
4870                 return -1;
4871 
4872         if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4873             MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4874                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4875                     ioc->name, __FILE__, __LINE__, __func__);
4876                 return -1;
4877         }
4878 
4879         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4880             MPI2_IOCSTATUS_MASK;
4881         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4882                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4883                     ioc->name, __FILE__, __LINE__, __func__);
4884                 return -1;
4885         }
4886 
4887         /* handle out of order topology events */
4888         parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
4889         if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4890             != 0) {
4891                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4892                     ioc->name, __FILE__, __LINE__, __func__);
4893                 return -1;
4894         }
4895         if (sas_address_parent != ioc->sas_hba.sas_address) {
4896                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4897                 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4898                     sas_address_parent);
4899                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4900                 if (!sas_expander) {
4901                         rc = _scsih_expander_add(ioc, parent_handle);
4902                         if (rc != 0)
4903                                 return rc;
4904                 }
4905         }
4906 
4907         spin_lock_irqsave(&ioc->sas_node_lock, flags);
4908         sas_address = le64_to_cpu(expander_pg0.SASAddress);
4909         sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4910             sas_address);
4911         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4912 
4913         if (sas_expander)
4914                 return 0;
4915 
4916         sas_expander = kzalloc(sizeof(struct _sas_node),
4917             GFP_KERNEL);
4918         if (!sas_expander) {
4919                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4920                     ioc->name, __FILE__, __LINE__, __func__);
4921                 return -1;
4922         }
4923 
4924         sas_expander->handle = handle;
4925         sas_expander->num_phys = expander_pg0.NumPhys;
4926         sas_expander->sas_address_parent = sas_address_parent;
4927         sas_expander->sas_address = sas_address;
4928 
4929         printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4930             " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
4931             handle, parent_handle, (unsigned long long)
4932             sas_expander->sas_address, sas_expander->num_phys);
4933 
4934         if (!sas_expander->num_phys)
4935                 goto out_fail;
4936         sas_expander->phy = kcalloc(sas_expander->num_phys,
4937             sizeof(struct _sas_phy), GFP_KERNEL);
4938         if (!sas_expander->phy) {
4939                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4940                     ioc->name, __FILE__, __LINE__, __func__);
4941                 rc = -1;
4942                 goto out_fail;
4943         }
4944 
4945         INIT_LIST_HEAD(&sas_expander->sas_port_list);
4946         mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
4947             sas_address_parent);
4948         if (!mpt2sas_port) {
4949                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4950                     ioc->name, __FILE__, __LINE__, __func__);
4951                 rc = -1;
4952                 goto out_fail;
4953         }
4954         sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4955 
4956         for (i = 0 ; i < sas_expander->num_phys ; i++) {
4957                 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4958                     &expander_pg1, i, handle))) {
4959                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4960                             ioc->name, __FILE__, __LINE__, __func__);
4961                         rc = -1;
4962                         goto out_fail;
4963                 }
4964                 sas_expander->phy[i].handle = handle;
4965                 sas_expander->phy[i].phy_id = i;
4966 
4967                 if ((mpt2sas_transport_add_expander_phy(ioc,
4968                     &sas_expander->phy[i], expander_pg1,
4969                     sas_expander->parent_dev))) {
4970                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4971                             ioc->name, __FILE__, __LINE__, __func__);
4972                         rc = -1;
4973                         goto out_fail;
4974                 }
4975         }
4976 
4977         if (sas_expander->enclosure_handle) {
4978                 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4979                     &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4980                    sas_expander->enclosure_handle))) {
4981                         sas_expander->enclosure_logical_id =
4982                             le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4983                 }
4984         }
4985 
4986         _scsih_expander_node_add(ioc, sas_expander);
4987          return 0;
4988 
4989  out_fail:
4990 
4991         if (mpt2sas_port)
4992                 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
4993                     sas_address_parent);
4994         kfree(sas_expander);
4995         return rc;
4996 }
4997 
4998 /**
4999  * _scsih_done -  scsih callback handler.
5000  * @ioc: per adapter object
5001  * @smid: system request message index
5002  * @msix_index: MSIX table index supplied by the OS
5003  * @reply: reply message frame(lower 32bit addr)
5004  *
5005  * Callback handler when sending internal generated message frames.
5006  * The callback index passed is `ioc->scsih_cb_idx`
5007  *
5008  * Return 1 meaning mf should be freed from _base_interrupt
5009  *        0 means the mf is freed from this function.
5010  */
5011 static u8
5012 _scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
5013 {
5014         MPI2DefaultReply_t *mpi_reply;
5015 
5016         mpi_reply =  mpt2sas_base_get_reply_virt_addr(ioc, reply);
5017         if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
5018                 return 1;
5019         if (ioc->scsih_cmds.smid != smid)
5020                 return 1;
5021         ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
5022         if (mpi_reply) {
5023                 memcpy(ioc->scsih_cmds.reply, mpi_reply,
5024                     mpi_reply->MsgLength*4);
5025                 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
5026         }
5027         ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
5028         complete(&ioc->scsih_cmds.done);
5029         return 1;
5030 }
5031 
5032 /**
5033  * mpt2sas_expander_remove - removing expander object
5034  * @ioc: per adapter object
5035  * @sas_address: expander sas_address
5036  *
5037  * Return nothing.
5038  */
5039 void
5040 mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
5041 {
5042         struct _sas_node *sas_expander;
5043         unsigned long flags;
5044 
5045         if (ioc->shost_recovery)
5046                 return;
5047 
5048         spin_lock_irqsave(&ioc->sas_node_lock, flags);
5049         sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
5050             sas_address);
5051         if (sas_expander)
5052                 list_del(&sas_expander->list);
5053         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5054         if (sas_expander)
5055                 _scsih_expander_node_remove(ioc, sas_expander);
5056 }
5057 
5058 /**
5059  * _scsih_check_access_status - check access flags
5060  * @ioc: per adapter object
5061  * @sas_address: sas address
5062  * @handle: sas device handle
5063  * @access_flags: errors returned during discovery of the device
5064  *
5065  * Return 0 for success, else failure
5066  */
5067 static u8
5068 _scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5069    u16 handle, u8 access_status)
5070 {
5071         u8 rc = 1;
5072         char *desc = NULL;
5073 
5074         switch (access_status) {
5075         case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5076         case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5077                 rc = 0;
5078                 break;
5079         case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5080                 desc = "sata capability failed";
5081                 break;
5082         case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5083                 desc = "sata affiliation conflict";
5084                 break;
5085         case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5086                 desc = "route not addressable";
5087                 break;
5088         case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5089                 desc = "smp error not addressable";
5090                 break;
5091         case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5092                 desc = "device blocked";
5093                 break;
5094         case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5095         case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5096         case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5097         case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5098         case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5099         case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5100         case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5101         case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5102         case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5103         case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5104         case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5105         case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5106                 desc = "sata initialization failed";
5107                 break;
5108         default:
5109                 desc = "unknown";
5110                 break;
5111         }
5112 
5113         if (!rc)
5114                 return 0;
5115 
5116         printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
5117             "handle(0x%04x)\n", ioc->name, desc,
5118             (unsigned long long)sas_address, handle);
5119         return rc;
5120 }
5121 
5122 static void
5123 _scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5124 {
5125         Mpi2ConfigReply_t mpi_reply;
5126         Mpi2SasDevicePage0_t sas_device_pg0;
5127         struct _sas_device *sas_device;
5128         u32 ioc_status;
5129         unsigned long flags;
5130         u64 sas_address;
5131         struct scsi_target *starget;
5132         struct MPT2SAS_TARGET *sas_target_priv_data;
5133         u32 device_info;
5134 
5135 
5136         if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5137             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5138                 return;
5139 
5140         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5141         if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5142                 return;
5143 
5144         /* check if this is end device */
5145         device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5146         if (!(_scsih_is_end_device(device_info)))
5147                 return;
5148 
5149         spin_lock_irqsave(&ioc->sas_device_lock, flags);
5150         sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5151         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5152             sas_address);
5153 
5154         if (!sas_device) {
5155                 printk(MPT2SAS_ERR_FMT "device is not present "
5156                     "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
5157                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5158                 return;
5159         }
5160 
5161         if (unlikely(sas_device->handle != handle)) {
5162                 starget = sas_device->starget;
5163                 sas_target_priv_data = starget->hostdata;
5164                 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
5165                    " to (0x%04x)!!!\n", sas_device->handle, handle);
5166                 sas_target_priv_data->handle = handle;
5167                 sas_device->handle = handle;
5168         }
5169 
5170         /* check if device is present */
5171         if (!(le16_to_cpu(sas_device_pg0.Flags) &
5172             MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5173                 printk(MPT2SAS_ERR_FMT "device is not present "
5174                     "handle(0x%04x), flags!!!\n", ioc->name, handle);
5175                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5176                 return;
5177         }
5178 
5179         /* check if there were any issues with discovery */
5180         if (_scsih_check_access_status(ioc, sas_address, handle,
5181             sas_device_pg0.AccessStatus)) {
5182                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5183                 return;
5184         }
5185         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5186         _scsih_ublock_io_device(ioc, sas_address);
5187 
5188 }
5189 
5190 /**
5191  * _scsih_add_device -  creating sas device object
5192  * @ioc: per adapter object
5193  * @handle: sas device handle
5194  * @phy_num: phy number end device attached to
5195  * @is_pd: is this hidden raid component
5196  *
5197  * Creating end device object, stored in ioc->sas_device_list.
5198  *
5199  * Returns 0 for success, non-zero for failure.
5200  */
5201 static int
5202 _scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
5203 {
5204         Mpi2ConfigReply_t mpi_reply;
5205         Mpi2SasDevicePage0_t sas_device_pg0;
5206         Mpi2SasEnclosurePage0_t enclosure_pg0;
5207         struct _sas_device *sas_device;
5208         u32 ioc_status;
5209         __le64 sas_address;
5210         u32 device_info;
5211         unsigned long flags;
5212 
5213         if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5214             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5215                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5216                     ioc->name, __FILE__, __LINE__, __func__);
5217                 return -1;
5218         }
5219 
5220         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5221             MPI2_IOCSTATUS_MASK;
5222         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5223                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5224                     ioc->name, __FILE__, __LINE__, __func__);
5225                 return -1;
5226         }
5227 
5228         sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5229 
5230         /* check if device is present */
5231         if (!(le16_to_cpu(sas_device_pg0.Flags) &
5232             MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5233                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5234                     ioc->name, __FILE__, __LINE__, __func__);
5235                 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
5236                     ioc->name, le16_to_cpu(sas_device_pg0.Flags));
5237                 return -1;
5238         }
5239 
5240         /* check if there were any issues with discovery */
5241         if (_scsih_check_access_status(ioc, sas_address, handle,
5242             sas_device_pg0.AccessStatus))
5243                 return -1;
5244 
5245         /* check if this is end device */
5246         device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5247         if (!(_scsih_is_end_device(device_info))) {
5248                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5249                     ioc->name, __FILE__, __LINE__, __func__);
5250                 return -1;
5251         }
5252 
5253 
5254         spin_lock_irqsave(&ioc->sas_device_lock, flags);
5255         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5256             sas_address);
5257         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5258 
5259         if (sas_device)
5260                 return 0;
5261 
5262         sas_device = kzalloc(sizeof(struct _sas_device),
5263             GFP_KERNEL);
5264         if (!sas_device) {
5265                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5266                     ioc->name, __FILE__, __LINE__, __func__);
5267                 return -1;
5268         }
5269 
5270         sas_device->handle = handle;
5271         if (_scsih_get_sas_address(ioc, le16_to_cpu
5272                 (sas_device_pg0.ParentDevHandle),
5273                 &sas_device->sas_address_parent) != 0)
5274                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5275                     ioc->name, __FILE__, __LINE__, __func__);
5276         sas_device->enclosure_handle =
5277             le16_to_cpu(sas_device_pg0.EnclosureHandle);
5278         sas_device->slot =
5279             le16_to_cpu(sas_device_pg0.Slot);
5280         sas_device->device_info = device_info;
5281         sas_device->sas_address = sas_address;
5282         sas_device->phy = sas_device_pg0.PhyNum;
5283 
5284         /* get enclosure_logical_id */
5285         if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
5286            ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5287            sas_device->enclosure_handle)))
5288                 sas_device->enclosure_logical_id =
5289                     le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5290 
5291         /* get device name */
5292         sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5293 
5294         if (ioc->wait_for_discovery_to_complete)
5295                 _scsih_sas_device_init_add(ioc, sas_device);
5296         else
5297                 _scsih_sas_device_add(ioc, sas_device);
5298 
5299         return 0;
5300 }
5301 
5302 /**
5303  * _scsih_remove_device -  removing sas device object
5304  * @ioc: per adapter object
5305  * @sas_device_delete: the sas_device object
5306  *
5307  * Return nothing.
5308  */
5309 static void
5310 _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
5311     struct _sas_device *sas_device)
5312 {
5313         struct MPT2SAS_TARGET *sas_target_priv_data;
5314 
5315         if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
5316                 (sas_device->pfa_led_on)) {
5317                 _scsih_turn_off_pfa_led(ioc, sas_device);
5318                 sas_device->pfa_led_on = 0;
5319         }
5320 
5321         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
5322             "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5323                 sas_device->handle, (unsigned long long)
5324             sas_device->sas_address));
5325 
5326         if (sas_device->starget && sas_device->starget->hostdata) {
5327                 sas_target_priv_data = sas_device->starget->hostdata;
5328                 sas_target_priv_data->deleted = 1;
5329                 _scsih_ublock_io_device(ioc, sas_device->sas_address);
5330                 sas_target_priv_data->handle =
5331                      MPT2SAS_INVALID_DEVICE_HANDLE;
5332         }
5333 
5334         if (!ioc->hide_drives)
5335                 mpt2sas_transport_port_remove(ioc,
5336                     sas_device->sas_address,
5337                     sas_device->sas_address_parent);
5338 
5339         printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
5340             "(0x%016llx)\n", ioc->name, sas_device->handle,
5341             (unsigned long long) sas_device->sas_address);
5342 
5343         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
5344             "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5345             sas_device->handle, (unsigned long long)
5346             sas_device->sas_address));
5347         kfree(sas_device);
5348 }
5349 /**
5350  * _scsih_device_remove_by_handle - removing device object by handle
5351  * @ioc: per adapter object
5352  * @handle: device handle
5353  *
5354  * Return nothing.
5355  */
5356 static void
5357 _scsih_device_remove_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5358 {
5359         struct _sas_device *sas_device;
5360         unsigned long flags;
5361 
5362         if (ioc->shost_recovery)
5363                 return;
5364 
5365         spin_lock_irqsave(&ioc->sas_device_lock, flags);
5366         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5367         if (sas_device)
5368                 list_del(&sas_device->list);
5369         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5370         if (sas_device)
5371                 _scsih_remove_device(ioc, sas_device);
5372 }
5373 
5374 /**
5375  * mpt2sas_device_remove_by_sas_address - removing device object by sas address
5376  * @ioc: per adapter object
5377  * @sas_address: device sas_address
5378  *
5379  * Return nothing.
5380  */
5381 void
5382 mpt2sas_device_remove_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
5383         u64 sas_address)
5384 {
5385         struct _sas_device *sas_device;
5386         unsigned long flags;
5387 
5388         if (ioc->shost_recovery)
5389                 return;
5390 
5391         spin_lock_irqsave(&ioc->sas_device_lock, flags);
5392         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5393             sas_address);
5394         if (sas_device)
5395                 list_del(&sas_device->list);
5396         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5397         if (sas_device)
5398                 _scsih_remove_device(ioc, sas_device);
5399 }
5400 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5401 /**
5402  * _scsih_sas_topology_change_event_debug - debug for topology event
5403  * @ioc: per adapter object
5404  * @event_data: event data payload
5405  * Context: user.
5406  */
5407 static void
5408 _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5409     Mpi2EventDataSasTopologyChangeList_t *event_data)
5410 {
5411         int i;
5412         u16 handle;
5413         u16 reason_code;
5414         u8 phy_number;
5415         char *status_str = NULL;
5416         u8 link_rate, prev_link_rate;
5417 
5418         switch (event_data->ExpStatus) {
5419         case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5420                 status_str = "add";
5421                 break;
5422         case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5423                 status_str = "remove";
5424                 break;
5425         case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
5426         case 0:
5427                 status_str =  "responding";
5428                 break;
5429         case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5430                 status_str = "remove delay";
5431                 break;
5432         default:
5433                 status_str = "unknown status";
5434                 break;
5435         }
5436         printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
5437             ioc->name, status_str);
5438         printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
5439             "start_phy(%02d), count(%d)\n",
5440             le16_to_cpu(event_data->ExpanderDevHandle),
5441             le16_to_cpu(event_data->EnclosureHandle),
5442             event_data->StartPhyNum, event_data->NumEntries);
5443         for (i = 0; i < event_data->NumEntries; i++) {
5444                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5445                 if (!handle)
5446                         continue;
5447                 phy_number = event_data->StartPhyNum + i;
5448                 reason_code = event_data->PHY[i].PhyStatus &
5449                     MPI2_EVENT_SAS_TOPO_RC_MASK;
5450                 switch (reason_code) {
5451                 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5452                         status_str = "target add";
5453                         break;
5454                 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5455                         status_str = "target remove";
5456                         break;
5457                 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
5458                         status_str = "delay target remove";
5459                         break;
5460                 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5461                         status_str = "link rate change";
5462                         break;
5463                 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
5464                         status_str = "target responding";
5465                         break;
5466                 default:
5467                         status_str = "unknown";
5468                         break;
5469                 }
5470                 link_rate = event_data->PHY[i].LinkRate >> 4;
5471                 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5472                 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
5473                     " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5474                     handle, status_str, link_rate, prev_link_rate);
5475 
5476         }
5477 }
5478 #endif
5479 
5480 /**
5481  * _scsih_sas_topology_change_event - handle topology changes
5482  * @ioc: per adapter object
5483  * @fw_event: The fw_event_work object
5484  * Context: user.
5485  *
5486  */
5487 static void
5488 _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
5489     struct fw_event_work *fw_event)
5490 {
5491         int i;
5492         u16 parent_handle, handle;
5493         u16 reason_code;
5494         u8 phy_number, max_phys;
5495         struct _sas_node *sas_expander;
5496         u64 sas_address;
5497         unsigned long flags;
5498         u8 link_rate, prev_link_rate;
5499         Mpi2EventDataSasTopologyChangeList_t *event_data =
5500                 (Mpi2EventDataSasTopologyChangeList_t *)
5501                 fw_event->event_data;
5502 
5503 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5504         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5505                 _scsih_sas_topology_change_event_debug(ioc, event_data);
5506 #endif
5507 
5508         if (ioc->remove_host || ioc->pci_error_recovery)
5509                 return;
5510 
5511         if (!ioc->sas_hba.num_phys)
5512                 _scsih_sas_host_add(ioc);
5513         else
5514                 _scsih_sas_host_refresh(ioc);
5515 
5516         if (fw_event->ignore) {
5517                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
5518                     "event\n", ioc->name));
5519                 return;
5520         }
5521 
5522         parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5523 
5524         /* handle expander add */
5525         if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5526                 if (_scsih_expander_add(ioc, parent_handle) != 0)
5527                         return;
5528 
5529         spin_lock_irqsave(&ioc->sas_node_lock, flags);
5530         sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
5531             parent_handle);
5532         if (sas_expander) {
5533                 sas_address = sas_expander->sas_address;
5534                 max_phys = sas_expander->num_phys;
5535         } else if (parent_handle < ioc->sas_hba.num_phys) {
5536                 sas_address = ioc->sas_hba.sas_address;
5537                 max_phys = ioc->sas_hba.num_phys;
5538         } else {
5539         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5540                 return;
5541         }
5542         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5543 
5544         /* handle siblings events */
5545         for (i = 0; i < event_data->NumEntries; i++) {
5546                 if (fw_event->ignore) {
5547                         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
5548                             "expander event\n", ioc->name));
5549                         return;
5550                 }
5551                 if (ioc->shost_recovery || ioc->remove_host ||
5552                     ioc->pci_error_recovery)
5553                         return;
5554                 phy_number = event_data->StartPhyNum + i;
5555                 if (phy_number >= max_phys)
5556                         continue;
5557                 reason_code = event_data->PHY[i].PhyStatus &
5558                     MPI2_EVENT_SAS_TOPO_RC_MASK;
5559                 if ((event_data->PHY[i].PhyStatus &
5560                     MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5561                     MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
5562                         continue;
5563                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5564                 if (!handle)
5565                         continue;
5566                 link_rate = event_data->PHY[i].LinkRate >> 4;
5567                 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5568                 switch (reason_code) {
5569                 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5570 
5571                         if (ioc->shost_recovery)
5572                                 break;
5573 
5574                         if (link_rate == prev_link_rate)
5575                                 break;
5576 
5577                         mpt2sas_transport_update_links(ioc, sas_address,
5578                             handle, phy_number, link_rate);
5579 
5580                         if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5581                                 break;
5582 
5583                         _scsih_check_device(ioc, handle);
5584                         break;
5585                 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5586 
5587                         if (ioc->shost_recovery)
5588                                 break;
5589 
5590                         mpt2sas_transport_update_links(ioc, sas_address,
5591                             handle, phy_number, link_rate);
5592 
5593                         _scsih_add_device(ioc, handle, phy_number, 0);
5594                         break;
5595                 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5596 
5597                         _scsih_device_remove_by_handle(ioc, handle);
5598                         break;
5599                 }
5600         }
5601 
5602         /* handle expander removal */
5603         if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5604             sas_expander)
5605                 mpt2sas_expander_remove(ioc, sas_address);
5606 
5607 }
5608 
5609 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5610 /**
5611  * _scsih_sas_device_status_change_event_debug - debug for device event
5612  * @event_data: event data payload
5613  * Context: user.
5614  *
5615  * Return nothing.
5616  */
5617 static void
5618 _scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5619     Mpi2EventDataSasDeviceStatusChange_t *event_data)
5620 {
5621         char *reason_str = NULL;
5622 
5623         switch (event_data->ReasonCode) {
5624         case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5625                 reason_str = "smart data";
5626                 break;
5627         case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5628                 reason_str = "unsupported device discovered";
5629                 break;
5630         case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5631                 reason_str = "internal device reset";
5632                 break;
5633         case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5634                 reason_str = "internal task abort";
5635                 break;
5636         case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5637                 reason_str = "internal task abort set";
5638                 break;
5639         case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5640                 reason_str = "internal clear task set";
5641                 break;
5642         case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5643                 reason_str = "internal query task";
5644                 break;
5645         case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5646                 reason_str = "sata init failure";
5647                 break;
5648         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5649                 reason_str = "internal device reset complete";
5650                 break;
5651         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5652                 reason_str = "internal task abort complete";
5653                 break;
5654         case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5655                 reason_str = "internal async notification";
5656                 break;
5657         case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5658                 reason_str = "expander reduced functionality";
5659                 break;
5660         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5661                 reason_str = "expander reduced functionality complete";
5662                 break;
5663         default:
5664                 reason_str = "unknown reason";
5665                 break;
5666         }
5667         printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
5668             "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5669             ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5670             (unsigned long long)le64_to_cpu(event_data->SASAddress),
5671             le16_to_cpu(event_data->TaskTag));
5672         if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
5673                 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
5674                     event_data->ASC, event_data->ASCQ);
5675         printk(KERN_INFO "\n");
5676 }
5677 #endif
5678 
5679 /**
5680  * _scsih_sas_device_status_change_event - handle device status change
5681  * @ioc: per adapter object
5682  * @fw_event: The fw_event_work object
5683  * Context: user.
5684  *
5685  * Return nothing.
5686  */
5687 static void
5688 _scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5689     struct fw_event_work *fw_event)
5690 {
5691         struct MPT2SAS_TARGET *target_priv_data;
5692         struct _sas_device *sas_device;
5693         u64 sas_address;
5694         unsigned long flags;
5695         Mpi2EventDataSasDeviceStatusChange_t *event_data =
5696                 (Mpi2EventDataSasDeviceStatusChange_t *)
5697                 fw_event->event_data;
5698 
5699 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5700         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5701                 _scsih_sas_device_status_change_event_debug(ioc,
5702                      event_data);
5703 #endif
5704 
5705         /* In MPI Revision K (0xC), the internal device reset complete was
5706          * implemented, so avoid setting tm_busy flag for older firmware.
5707          */
5708         if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5709                 return;
5710 
5711         if (event_data->ReasonCode !=
5712             MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
5713            event_data->ReasonCode !=
5714             MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
5715                 return;
5716 
5717         spin_lock_irqsave(&ioc->sas_device_lock, flags);
5718         sas_address = le64_to_cpu(event_data->SASAddress);
5719         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5720             sas_address);
5721 
5722         if (!sas_device || !sas_device->starget) {
5723                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5724                 return;
5725         }
5726 
5727         target_priv_data = sas_device->starget->hostdata;
5728         if (!target_priv_data) {
5729                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5730                 return;
5731         }
5732 
5733         if (event_data->ReasonCode ==
5734             MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5735                 target_priv_data->tm_busy = 1;
5736         else
5737                 target_priv_data->tm_busy = 0;
5738         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5739 }
5740 
5741 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5742 /**
5743  * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5744  * @ioc: per adapter object
5745  * @event_data: event data payload
5746  * Context: user.
5747  *
5748  * Return nothing.
5749  */
5750 static void
5751 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5752     Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5753 {
5754         char *reason_str = NULL;
5755 
5756         switch (event_data->ReasonCode) {
5757         case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5758                 reason_str = "enclosure add";
5759                 break;
5760         case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5761                 reason_str = "enclosure remove";
5762                 break;
5763         default:
5764                 reason_str = "unknown reason";
5765                 break;
5766         }
5767 
5768         printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
5769             "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5770             " number slots(%d)\n", ioc->name, reason_str,
5771             le16_to_cpu(event_data->EnclosureHandle),
5772             (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5773             le16_to_cpu(event_data->StartSlot));
5774 }
5775 #endif
5776 
5777 /**
5778  * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5779  * @ioc: per adapter object
5780  * @fw_event: The fw_event_work object
5781  * Context: user.
5782  *
5783  * Return nothing.
5784  */
5785 static void
5786 _scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5787     struct fw_event_work *fw_event)
5788 {
5789 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5790         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5791                 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
5792                      (Mpi2EventDataSasEnclDevStatusChange_t *)
5793                      fw_event->event_data);
5794 #endif
5795 }
5796 
5797 /**
5798  * _scsih_sas_broadcast_primitive_event - handle broadcast events
5799  * @ioc: per adapter object
5800  * @fw_event: The fw_event_work object
5801  * Context: user.
5802  *
5803  * Return nothing.
5804  */
5805 static void
5806 _scsih_sas_broadcast_primitive_event(struct MPT2SAS_ADAPTER *ioc,
5807     struct fw_event_work *fw_event)
5808 {
5809         struct scsi_cmnd *scmd;
5810         struct scsi_device *sdev;
5811         u16 smid, handle;
5812         u32 lun;
5813         struct MPT2SAS_DEVICE *sas_device_priv_data;
5814         u32 termination_count;
5815         u32 query_count;
5816         Mpi2SCSITaskManagementReply_t *mpi_reply;
5817         Mpi2EventDataSasBroadcastPrimitive_t *event_data =
5818                 (Mpi2EventDataSasBroadcastPrimitive_t *)
5819                 fw_event->event_data;
5820         u16 ioc_status;
5821         unsigned long flags;
5822         int r;
5823         u8 max_retries = 0;
5824         u8 task_abort_retries;
5825 
5826         mutex_lock(&ioc->tm_cmds.mutex);
5827         pr_info(MPT2SAS_FMT
5828                 "%s: enter: phy number(%d), width(%d)\n",
5829                 ioc->name, __func__, event_data->PhyNum,
5830                 event_data->PortWidth);
5831 
5832         _scsih_block_io_all_device(ioc);
5833 
5834         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5835         mpi_reply = ioc->tm_cmds.reply;
5836 broadcast_aen_retry:
5837 
5838         /* sanity checks for retrying this loop */
5839         if (max_retries++ == 5) {
5840                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: giving up\n",
5841                     ioc->name, __func__));
5842                 goto out;
5843         } else if (max_retries > 1)
5844                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %d retry\n",
5845                     ioc->name, __func__, max_retries - 1));
5846 
5847         termination_count = 0;
5848         query_count = 0;
5849         for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
5850                 if (ioc->shost_recovery)
5851                         goto out;
5852                 scmd = _scsih_scsi_lookup_get(ioc, smid);
5853                 if (!scmd)
5854                         continue;
5855                 sdev = scmd->device;
5856                 sas_device_priv_data = sdev->hostdata;
5857                 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5858                         continue;
5859                  /* skip hidden raid components */
5860                 if (sas_device_priv_data->sas_target->flags &
5861                     MPT_TARGET_FLAGS_RAID_COMPONENT)
5862                         continue;
5863                  /* skip volumes */
5864                 if (sas_device_priv_data->sas_target->flags &
5865                     MPT_TARGET_FLAGS_VOLUME)
5866                         continue;
5867 
5868                 handle = sas_device_priv_data->sas_target->handle;
5869                 lun = sas_device_priv_data->lun;
5870                 query_count++;
5871 
5872                 if (ioc->shost_recovery)
5873                         goto out;
5874 
5875                 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5876                 r = mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5877                     MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30,
5878                     TM_MUTEX_OFF);
5879                 if (r == FAILED) {
5880                         sdev_printk(KERN_WARNING, sdev,
5881                             "mpt2sas_scsih_issue_tm: FAILED when sending "
5882                             "QUERY_TASK: scmd(%p)\n", scmd);
5883                         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5884                         goto broadcast_aen_retry;
5885                 }
5886                 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5887                     & MPI2_IOCSTATUS_MASK;
5888                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5889                         sdev_printk(KERN_WARNING, sdev, "query task: FAILED "
5890                             "with IOCSTATUS(0x%04x), scmd(%p)\n", ioc_status,
5891                             scmd);
5892                         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5893                         goto broadcast_aen_retry;
5894                 }
5895 
5896                 /* see if IO is still owned by IOC and target */
5897                 if (mpi_reply->ResponseCode ==
5898                      MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5899                      mpi_reply->ResponseCode ==
5900                      MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
5901                         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5902                         continue;
5903                 }
5904                 task_abort_retries = 0;
5905  tm_retry:
5906                 if (task_abort_retries++ == 60) {
5907                         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
5908                             "%s: ABORT_TASK: giving up\n", ioc->name,
5909                             __func__));
5910                         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5911                         goto broadcast_aen_retry;
5912                 }
5913 
5914                 if (ioc->shost_recovery)
5915                         goto out_no_lock;
5916 
5917                 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5918                     sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
5919                     TM_MUTEX_OFF);
5920                 if (r == FAILED) {
5921                         sdev_printk(KERN_WARNING, sdev,
5922                             "mpt2sas_scsih_issue_tm: ABORT_TASK: FAILED : "
5923                             "scmd(%p)\n", scmd);
5924                         goto tm_retry;
5925                 }
5926 
5927                 if (task_abort_retries > 1)
5928                         sdev_printk(KERN_WARNING, sdev,
5929                             "mpt2sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5930                             " scmd(%p)\n",
5931                             task_abort_retries - 1, scmd);
5932 
5933                 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
5934                 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5935         }
5936 
5937         if (ioc->broadcast_aen_pending) {
5938                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: loop back due to"
5939                      " pending AEN\n", ioc->name, __func__));
5940                  ioc->broadcast_aen_pending = 0;
5941                  goto broadcast_aen_retry;
5942         }
5943 
5944  out:
5945         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5946  out_no_lock:
5947 
5948         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
5949             "%s - exit, query_count = %d termination_count = %d\n",
5950             ioc->name, __func__, query_count, termination_count));
5951 
5952         ioc->broadcast_aen_busy = 0;
5953         if (!ioc->shost_recovery)
5954                 _scsih_ublock_io_all_device(ioc);
5955         mutex_unlock(&ioc->tm_cmds.mutex);
5956 }
5957 
5958 /**
5959  * _scsih_sas_discovery_event - handle discovery events
5960  * @ioc: per adapter object
5961  * @fw_event: The fw_event_work object
5962  * Context: user.
5963  *
5964  * Return nothing.
5965  */
5966 static void
5967 _scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5968     struct fw_event_work *fw_event)
5969 {
5970         Mpi2EventDataSasDiscovery_t *event_data =
5971                 (Mpi2EventDataSasDiscovery_t *)
5972                 fw_event->event_data;
5973 
5974 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5975         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
5976                 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
5977                     (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5978                     "start" : "stop");
5979         if (event_data->DiscoveryStatus)
5980                 printk("discovery_status(0x%08x)",
5981                     le32_to_cpu(event_data->DiscoveryStatus));
5982         printk("\n");
5983         }
5984 #endif
5985 
5986         if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5987             !ioc->sas_hba.num_phys) {
5988                 if (disable_discovery > 0 && ioc->shost_recovery) {
5989                         /* Wait for the reset to complete */
5990                         while (ioc->shost_recovery)
5991                                 ssleep(1);
5992                 }
5993                 _scsih_sas_host_add(ioc);
5994         }
5995 }
5996 
5997 /**
5998  * _scsih_reprobe_lun - reprobing lun
5999  * @sdev: scsi device struct
6000  * @no_uld_attach: sdev->no_uld_attach flag setting
6001  *
6002  **/
6003 static void
6004 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
6005 {
6006         int rc;
6007 
6008         sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6009         sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6010             sdev->no_uld_attach ? "hidding" : "exposing");
6011         rc = scsi_device_reprobe(sdev);
6012 }
6013 
6014 /**
6015  * _scsih_sas_volume_add - add new volume
6016  * @ioc: per adapter object
6017  * @element: IR config element data
6018  * Context: user.
6019  *
6020  * Return nothing.
6021  */
6022 static void
6023 _scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
6024     Mpi2EventIrConfigElement_t *element)
6025 {
6026         struct _raid_device *raid_device;
6027         unsigned long flags;
6028         u64 wwid;
6029         u16 handle = le16_to_cpu(element->VolDevHandle);
6030         int rc;
6031 
6032         mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6033         if (!wwid) {
6034                 printk(MPT2SAS_ERR_FMT
6035                     "failure at %s:%d/%s()!\n", ioc->name,
6036                     __FILE__, __LINE__, __func__);
6037                 return;
6038         }
6039 
6040         spin_lock_irqsave(&ioc->raid_device_lock, flags);
6041         raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6042         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6043 
6044         if (raid_device)
6045                 return;
6046 
6047         raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6048         if (!raid_device) {
6049                 printk(MPT2SAS_ERR_FMT
6050                     "failure at %s:%d/%s()!\n", ioc->name,
6051                     __FILE__, __LINE__, __func__);
6052                 return;
6053         }
6054 
6055         raid_device->id = ioc->sas_id++;
6056         raid_device->channel = RAID_CHANNEL;
6057         raid_device->handle = handle;
6058         raid_device->wwid = wwid;
6059         _scsih_raid_device_add(ioc, raid_device);
6060         if (!ioc->wait_for_discovery_to_complete) {
6061                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6062                     raid_device->id, 0);
6063                 if (rc)
6064                         _scsih_raid_device_remove(ioc, raid_device);
6065         } else {
6066                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6067                 _scsih_determine_boot_device(ioc, raid_device, 1);
6068                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6069         }
6070 }
6071 
6072 /**
6073  * _scsih_sas_volume_delete - delete volume
6074  * @ioc: per adapter object
6075  * @handle: volume device handle
6076  * Context: user.
6077  *
6078  * Return nothing.
6079  */
6080 static void
6081 _scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
6082 {
6083         struct _raid_device *raid_device;
6084         unsigned long flags;
6085         struct MPT2SAS_TARGET *sas_target_priv_data;
6086         struct scsi_target *starget = NULL;
6087 
6088         spin_lock_irqsave(&ioc->raid_device_lock, flags);
6089         raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6090         if (raid_device) {
6091                 if (raid_device->starget) {
6092                         starget = raid_device->starget;
6093                         sas_target_priv_data = starget->hostdata;
6094                         sas_target_priv_data->deleted = 1;
6095                 }
6096                 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6097                     "(0x%016llx)\n", ioc->name,  raid_device->handle,
6098                     (unsigned long long) raid_device->wwid);
6099                 list_del(&raid_device->list);
6100                 kfree(raid_device);
6101         }
6102         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6103         if (starget)
6104                 scsi_remove_target(&starget->dev);
6105 }
6106 
6107 /**
6108  * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6109  * @ioc: per adapter object
6110  * @element: IR config element data
6111  * Context: user.
6112  *
6113  * Return nothing.
6114  */
6115 static void
6116 _scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
6117     Mpi2EventIrConfigElement_t *element)
6118 {
6119         struct _sas_device *sas_device;
6120         struct scsi_target *starget = NULL;
6121         struct MPT2SAS_TARGET *sas_target_priv_data;
6122         unsigned long flags;
6123         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6124 
6125         spin_lock_irqsave(&ioc->sas_device_lock, flags);
6126         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6127         if (sas_device) {
6128                 sas_device->volume_handle = 0;
6129                 sas_device->volume_wwid = 0;
6130                 clear_bit(handle, ioc->pd_handles);
6131                 if (sas_device->starget && sas_device->starget->hostdata) {
6132                         starget = sas_device->starget;
6133                         sas_target_priv_data = starget->hostdata;
6134                         sas_target_priv_data->flags &=
6135                             ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6136                 }
6137         }
6138         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6139         if (!sas_device)
6140                 return;
6141 
6142         /* exposing raid component */
6143         if (starget)
6144                 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
6145 }
6146 
6147 /**
6148  * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6149  * @ioc: per adapter object
6150  * @element: IR config element data
6151  * Context: user.
6152  *
6153  * Return nothing.
6154  */
6155 static void
6156 _scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
6157     Mpi2EventIrConfigElement_t *element)
6158 {
6159         struct _sas_device *sas_device;
6160         struct scsi_target *starget = NULL;
6161         struct MPT2SAS_TARGET *sas_target_priv_data;
6162         unsigned long flags;
6163         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6164         u16 volume_handle = 0;
6165         u64 volume_wwid = 0;
6166 
6167         mpt2sas_config_get_volume_handle(ioc, handle, &volume_handle);
6168         if (volume_handle)
6169                 mpt2sas_config_get_volume_wwid(ioc, volume_handle,
6170                     &volume_wwid);
6171 
6172         spin_lock_irqsave(&ioc->sas_device_lock, flags);
6173         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6174         if (sas_device) {
6175                 set_bit(handle, ioc->pd_handles);
6176                 if (sas_device->starget && sas_device->starget->hostdata) {
6177                         starget = sas_device->starget;
6178                         sas_target_priv_data = starget->hostdata;
6179                         sas_target_priv_data->flags |=
6180                             MPT_TARGET_FLAGS_RAID_COMPONENT;
6181                         sas_device->volume_handle = volume_handle;
6182                         sas_device->volume_wwid = volume_wwid;
6183                 }
6184         }
6185         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6186         if (!sas_device)
6187                 return;
6188 
6189         /* hiding raid component */
6190         if (starget)
6191                 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
6192 }
6193 
6194 /**
6195  * _scsih_sas_pd_delete - delete pd component
6196  * @ioc: per adapter object
6197  * @element: IR config element data
6198  * Context: user.
6199  *
6200  * Return nothing.
6201  */
6202 static void
6203 _scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
6204     Mpi2EventIrConfigElement_t *element)
6205 {
6206         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6207 
6208         _scsih_device_remove_by_handle(ioc, handle);
6209 }
6210 
6211 /**
6212  * _scsih_sas_pd_add - remove pd component
6213  * @ioc: per adapter object
6214  * @element: IR config element data
6215  * Context: user.
6216  *
6217  * Return nothing.
6218  */
6219 static void
6220 _scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
6221     Mpi2EventIrConfigElement_t *element)
6222 {
6223         struct _sas_device *sas_device;
6224         unsigned long flags;
6225         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6226         Mpi2ConfigReply_t mpi_reply;
6227         Mpi2SasDevicePage0_t sas_device_pg0;
6228         u32 ioc_status;
6229         u64 sas_address;
6230         u16 parent_handle;
6231 
6232         set_bit(handle, ioc->pd_handles);
6233 
6234         spin_lock_irqsave(&ioc->sas_device_lock, flags);
6235         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6236         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6237         if (sas_device)
6238                 return;
6239 
6240         if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6241             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6242                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6243                     ioc->name, __FILE__, __LINE__, __func__);
6244                 return;
6245         }
6246 
6247         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6248             MPI2_IOCSTATUS_MASK;
6249         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6250                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6251                     ioc->name, __FILE__, __LINE__, __func__);
6252                 return;
6253         }
6254 
6255         parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6256         if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6257                 mpt2sas_transport_update_links(ioc, sas_address, handle,
6258                     sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6259 
6260         _scsih_add_device(ioc, handle, 0, 1);
6261 }
6262 
6263 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6264 /**
6265  * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6266  * @ioc: per adapter object
6267  * @event_data: event data payload
6268  * Context: user.
6269  *
6270  * Return nothing.
6271  */
6272 static void
6273 _scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
6274     Mpi2EventDataIrConfigChangeList_t *event_data)
6275 {
6276         Mpi2EventIrConfigElement_t *element;
6277         u8 element_type;
6278         int i;
6279         char *reason_str = NULL, *element_str = NULL;
6280 
6281         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6282 
6283         printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
6284             ioc->name, (le32_to_cpu(event_data->Flags) &
6285             MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6286             "foreign" : "native", event_data->NumElements);
6287         for (i = 0; i < event_data->NumElements; i++, element++) {
6288                 switch (element->ReasonCode) {
6289                 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6290                         reason_str = "add";
6291                         break;
6292                 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6293                         reason_str = "remove";
6294                         break;
6295                 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6296                         reason_str = "no change";
6297                         break;
6298                 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6299                         reason_str = "hide";
6300                         break;
6301                 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6302                         reason_str = "unhide";
6303                         break;
6304                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6305                         reason_str = "volume_created";
6306                         break;
6307                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6308                         reason_str = "volume_deleted";
6309                         break;
6310                 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6311                         reason_str = "pd_created";
6312                         break;
6313                 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6314                         reason_str = "pd_deleted";
6315                         break;
6316                 default:
6317                         reason_str = "unknown reason";
6318                         break;
6319                 }
6320                 element_type = le16_to_cpu(element->ElementFlags) &
6321                     MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6322                 switch (element_type) {
6323                 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6324                         element_str = "volume";
6325                         break;
6326                 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6327                         element_str = "phys disk";
6328                         break;
6329                 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6330                         element_str = "hot spare";
6331                         break;
6332                 default:
6333                         element_str = "unknown element";
6334                         break;
6335                 }
6336                 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
6337                     "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6338                     reason_str, le16_to_cpu(element->VolDevHandle),
6339                     le16_to_cpu(element->PhysDiskDevHandle),
6340                     element->PhysDiskNum);
6341         }
6342 }
6343 #endif
6344 
6345 /**
6346  * _scsih_sas_ir_config_change_event - handle ir configuration change events
6347  * @ioc: per adapter object
6348  * @fw_event: The fw_event_work object
6349  * Context: user.
6350  *
6351  * Return nothing.
6352  */
6353 static void
6354 _scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
6355     struct fw_event_work *fw_event)
6356 {
6357         Mpi2EventIrConfigElement_t *element;
6358         int i;
6359         u8 foreign_config;
6360         Mpi2EventDataIrConfigChangeList_t *event_data =
6361                 (Mpi2EventDataIrConfigChangeList_t *)
6362                 fw_event->event_data;
6363 
6364 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6365         if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6366             && !ioc->hide_ir_msg)
6367                 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6368 
6369 #endif
6370 
6371         if (ioc->shost_recovery)
6372                 return;
6373 
6374         foreign_config = (le32_to_cpu(event_data->Flags) &
6375             MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
6376 
6377         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6378         for (i = 0; i < event_data->NumElements; i++, element++) {
6379 
6380                 switch (element->ReasonCode) {
6381                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6382                 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6383                         if (!foreign_config)
6384                                 _scsih_sas_volume_add(ioc, element);
6385                         break;
6386                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6387                 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6388                         if (!foreign_config)
6389                                 _scsih_sas_volume_delete(ioc,
6390                                     le16_to_cpu(element->VolDevHandle));
6391                         break;
6392                 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6393                         if (!ioc->is_warpdrive)
6394                                 _scsih_sas_pd_hide(ioc, element);
6395                         break;
6396                 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6397                         if (!ioc->is_warpdrive)
6398                                 _scsih_sas_pd_expose(ioc, element);
6399                         break;
6400                 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6401                         if (!ioc->is_warpdrive)
6402                                 _scsih_sas_pd_add(ioc, element);
6403                         break;
6404                 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6405                         if (!ioc->is_warpdrive)
6406                                 _scsih_sas_pd_delete(ioc, element);
6407                         break;
6408                 }
6409         }
6410 }
6411 
6412 /**
6413  * _scsih_sas_ir_volume_event - IR volume event
6414  * @ioc: per adapter object
6415  * @fw_event: The fw_event_work object
6416  * Context: user.
6417  *
6418  * Return nothing.
6419  */
6420 static void
6421 _scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
6422     struct fw_event_work *fw_event)
6423 {
6424         u64 wwid;
6425         unsigned long flags;
6426         struct _raid_device *raid_device;
6427         u16 handle;
6428         u32 state;
6429         int rc;
6430         Mpi2EventDataIrVolume_t *event_data =
6431                 (Mpi2EventDataIrVolume_t *)
6432                 fw_event->event_data;
6433 
6434         if (ioc->shost_recovery)
6435                 return;
6436 
6437         if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6438                 return;
6439 
6440         handle = le16_to_cpu(event_data->VolDevHandle);
6441         state = le32_to_cpu(event_data->NewValue);
6442         if (!ioc->hide_ir_msg)
6443                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6444                     "old(0x%08x), new(0x%08x)\n", ioc->name, __func__,  handle,
6445                     le32_to_cpu(event_data->PreviousValue), state));
6446 
6447         switch (state) {
6448         case MPI2_RAID_VOL_STATE_MISSING:
6449         case MPI2_RAID_VOL_STATE_FAILED:
6450                 _scsih_sas_volume_delete(ioc, handle);
6451                 break;
6452 
6453         case MPI2_RAID_VOL_STATE_ONLINE:
6454         case MPI2_RAID_VOL_STATE_DEGRADED:
6455         case MPI2_RAID_VOL_STATE_OPTIMAL:
6456 
6457                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6458                 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6459                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6460 
6461                 if (raid_device)
6462                         break;
6463 
6464                 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6465                 if (!wwid) {
6466                         printk(MPT2SAS_ERR_FMT
6467                             "failure at %s:%d/%s()!\n", ioc->name,
6468                             __FILE__, __LINE__, __func__);
6469                         break;
6470                 }
6471 
6472                 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6473                 if (!raid_device) {
6474                         printk(MPT2SAS_ERR_FMT
6475                             "failure at %s:%d/%s()!\n", ioc->name,
6476                             __FILE__, __LINE__, __func__);
6477                         break;
6478                 }
6479 
6480                 raid_device->id = ioc->sas_id++;
6481                 raid_device->channel = RAID_CHANNEL;
6482                 raid_device->handle = handle;
6483                 raid_device->wwid = wwid;
6484                 _scsih_raid_device_add(ioc, raid_device);
6485                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6486                     raid_device->id, 0);
6487                 if (rc)
6488                         _scsih_raid_device_remove(ioc, raid_device);
6489                 break;
6490 
6491         case MPI2_RAID_VOL_STATE_INITIALIZING:
6492         default:
6493                 break;
6494         }
6495 }
6496 
6497 /**
6498  * _scsih_sas_ir_physical_disk_event - PD event
6499  * @ioc: per adapter object
6500  * @fw_event: The fw_event_work object
6501  * Context: user.
6502  *
6503  * Return nothing.
6504  */
6505 static void
6506 _scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
6507     struct fw_event_work *fw_event)
6508 {
6509         u16 handle, parent_handle;
6510         u32 state;
6511         struct _sas_device *sas_device;
6512         unsigned long flags;
6513         Mpi2ConfigReply_t mpi_reply;
6514         Mpi2SasDevicePage0_t sas_device_pg0;
6515         u32 ioc_status;
6516         Mpi2EventDataIrPhysicalDisk_t *event_data =
6517                 (Mpi2EventDataIrPhysicalDisk_t *)
6518                 fw_event->event_data;
6519         u64 sas_address;
6520 
6521         if (ioc->shost_recovery)
6522                 return;
6523 
6524         if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6525                 return;
6526 
6527         handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6528         state = le32_to_cpu(event_data->NewValue);
6529 
6530         if (!ioc->hide_ir_msg)
6531                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6532                     "old(0x%08x), new(0x%08x)\n", ioc->name, __func__,  handle,
6533                     le32_to_cpu(event_data->PreviousValue), state));
6534 
6535         switch (state) {
6536         case MPI2_RAID_PD_STATE_ONLINE:
6537         case MPI2_RAID_PD_STATE_DEGRADED:
6538         case MPI2_RAID_PD_STATE_REBUILDING:
6539         case MPI2_RAID_PD_STATE_OPTIMAL:
6540         case MPI2_RAID_PD_STATE_HOT_SPARE:
6541 
6542                 if (!ioc->is_warpdrive)
6543                         set_bit(handle, ioc->pd_handles);
6544 
6545                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6546                 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6547                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6548 
6549                 if (sas_device)
6550                         return;
6551 
6552                 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6553                     &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6554                     handle))) {
6555                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6556                             ioc->name, __FILE__, __LINE__, __func__);
6557                         return;
6558                 }
6559 
6560                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6561                     MPI2_IOCSTATUS_MASK;
6562                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6563                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6564                             ioc->name, __FILE__, __LINE__, __func__);
6565                         return;
6566                 }
6567 
6568                 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6569                 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6570                         mpt2sas_transport_update_links(ioc, sas_address, handle,
6571                             sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6572 
6573                 _scsih_add_device(ioc, handle, 0, 1);
6574 
6575                 break;
6576 
6577         case MPI2_RAID_PD_STATE_OFFLINE:
6578         case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6579         case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
6580         default:
6581                 break;
6582         }
6583 }
6584 
6585 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6586 /**
6587  * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6588  * @ioc: per adapter object
6589  * @event_data: event data payload
6590  * Context: user.
6591  *
6592  * Return nothing.
6593  */
6594 static void
6595 _scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
6596     Mpi2EventDataIrOperationStatus_t *event_data)
6597 {
6598         char *reason_str = NULL;
6599 
6600         switch (event_data->RAIDOperation) {
6601         case MPI2_EVENT_IR_RAIDOP_RESYNC:
6602                 reason_str = "resync";
6603                 break;
6604         case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6605                 reason_str = "online capacity expansion";
6606                 break;
6607         case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6608                 reason_str = "consistency check";
6609                 break;
6610         case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6611                 reason_str = "background init";
6612                 break;
6613         case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6614                 reason_str = "make data consistent";
6615                 break;
6616         }
6617 
6618         if (!reason_str)
6619                 return;
6620 
6621         printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
6622             "\thandle(0x%04x), percent complete(%d)\n",
6623             ioc->name, reason_str,
6624             le16_to_cpu(event_data->VolDevHandle),
6625             event_data->PercentComplete);
6626 }
6627 #endif
6628 
6629 /**
6630  * _scsih_sas_ir_operation_status_event - handle RAID operation events
6631  * @ioc: per adapter object
6632  * @fw_event: The fw_event_work object
6633  * Context: user.
6634  *
6635  * Return nothing.
6636  */
6637 static void
6638 _scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
6639     struct fw_event_work *fw_event)
6640 {
6641         Mpi2EventDataIrOperationStatus_t *event_data =
6642                 (Mpi2EventDataIrOperationStatus_t *)
6643                 fw_event->event_data;
6644         static struct _raid_device *raid_device;
6645         unsigned long flags;
6646         u16 handle;
6647 
6648 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6649         if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6650             && !ioc->hide_ir_msg)
6651                 _scsih_sas_ir_operation_status_event_debug(ioc,
6652                      event_data);
6653 #endif
6654 
6655         /* code added for raid transport support */
6656         if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6657 
6658                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6659                 handle = le16_to_cpu(event_data->VolDevHandle);
6660                 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6661                 if (raid_device)
6662                         raid_device->percent_complete =
6663                             event_data->PercentComplete;
6664                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6665         }
6666 }
6667 
6668 /**
6669  * _scsih_prep_device_scan - initialize parameters prior to device scan
6670  * @ioc: per adapter object
6671  *
6672  * Set the deleted flag prior to device scan.  If the device is found during
6673  * the scan, then we clear the deleted flag.
6674  */
6675 static void
6676 _scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
6677 {
6678         struct MPT2SAS_DEVICE *sas_device_priv_data;
6679         struct scsi_device *sdev;
6680 
6681         shost_for_each_device(sdev, ioc->shost) {
6682                 sas_device_priv_data = sdev->hostdata;
6683                 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6684                         sas_device_priv_data->sas_target->deleted = 1;
6685         }
6686 }
6687 
6688 /**
6689  * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6690  * @ioc: per adapter object
6691  * @sas_address: sas address
6692  * @slot: enclosure slot id
6693  * @handle: device handle
6694  *
6695  * After host reset, find out whether devices are still responding.
6696  * Used in _scsi_remove_unresponsive_sas_devices.
6697  *
6698  * Return nothing.
6699  */
6700 static void
6701 _scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6702     u16 slot, u16 handle)
6703 {
6704         struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
6705         struct scsi_target *starget;
6706         struct _sas_device *sas_device;
6707         unsigned long flags;
6708 
6709         spin_lock_irqsave(&ioc->sas_device_lock, flags);
6710         list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6711                 if (sas_device->sas_address == sas_address &&
6712                     sas_device->slot == slot) {
6713                         sas_device->responding = 1;
6714                         starget = sas_device->starget;
6715                         if (starget && starget->hostdata) {
6716                                 sas_target_priv_data = starget->hostdata;
6717                                 sas_target_priv_data->tm_busy = 0;
6718                                 sas_target_priv_data->deleted = 0;
6719                         } else
6720                                 sas_target_priv_data = NULL;
6721                         if (starget)
6722                                 starget_printk(KERN_INFO, starget,
6723                                     "handle(0x%04x), sas_addr(0x%016llx), "
6724                                     "enclosure logical id(0x%016llx), "
6725                                     "slot(%d)\n", handle,
6726                                     (unsigned long long)sas_device->sas_address,
6727                                     (unsigned long long)
6728                                     sas_device->enclosure_logical_id,
6729                                     sas_device->slot);
6730                         if (sas_device->handle == handle)
6731                                 goto out;
6732                         printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6733                             sas_device->handle);
6734                         sas_device->handle = handle;
6735                         if (sas_target_priv_data)
6736                                 sas_target_priv_data->handle = handle;
6737                         goto out;
6738                 }
6739         }
6740  out:
6741         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6742 }
6743 
6744 /**
6745  * _scsih_search_responding_sas_devices -
6746  * @ioc: per adapter object
6747  *
6748  * After host reset, find out whether devices are still responding.
6749  * If not remove.
6750  *
6751  * Return nothing.
6752  */
6753 static void
6754 _scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6755 {
6756         Mpi2SasDevicePage0_t sas_device_pg0;
6757         Mpi2ConfigReply_t mpi_reply;
6758         u16 ioc_status;
6759         __le64 sas_address;
6760         u16 handle;
6761         u32 device_info;
6762         u16 slot;
6763 
6764         printk(MPT2SAS_INFO_FMT "search for end-devices: start\n", ioc->name);
6765 
6766         if (list_empty(&ioc->sas_device_list))
6767                 goto out;
6768 
6769         handle = 0xFFFF;
6770         while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6771             &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6772             handle))) {
6773                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6774                     MPI2_IOCSTATUS_MASK;
6775                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6776                         break;
6777                 handle = le16_to_cpu(sas_device_pg0.DevHandle);
6778                 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6779                 if (!(_scsih_is_end_device(device_info)))
6780                         continue;
6781                 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
6782                 slot = le16_to_cpu(sas_device_pg0.Slot);
6783                 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
6784                     handle);
6785         }
6786 out:
6787         printk(MPT2SAS_INFO_FMT "search for end-devices: complete\n",
6788             ioc->name);
6789 }
6790 
6791 /**
6792  * _scsih_mark_responding_raid_device - mark a raid_device as responding
6793  * @ioc: per adapter object
6794  * @wwid: world wide identifier for raid volume
6795  * @handle: device handle
6796  *
6797  * After host reset, find out whether devices are still responding.
6798  * Used in _scsi_remove_unresponsive_raid_devices.
6799  *
6800  * Return nothing.
6801  */
6802 static void
6803 _scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6804     u16 handle)
6805 {
6806         struct MPT2SAS_TARGET *sas_target_priv_data;
6807         struct scsi_target *starget;
6808         struct _raid_device *raid_device;
6809         unsigned long flags;
6810 
6811         spin_lock_irqsave(&ioc->raid_device_lock, flags);
6812         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6813                 if (raid_device->wwid == wwid && raid_device->starget) {
6814                         starget = raid_device->starget;
6815                         if (starget && starget->hostdata) {
6816                                 sas_target_priv_data = starget->hostdata;
6817                                 sas_target_priv_data->deleted = 0;
6818                         } else
6819                                 sas_target_priv_data = NULL;
6820                         raid_device->responding = 1;
6821                         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6822                         starget_printk(KERN_INFO, raid_device->starget,
6823                             "handle(0x%04x), wwid(0x%016llx)\n", handle,
6824                             (unsigned long long)raid_device->wwid);
6825                         /*
6826                          * WARPDRIVE: The handles of the PDs might have changed
6827                          * across the host reset so re-initialize the
6828                          * required data for Direct IO
6829                          */
6830                         _scsih_init_warpdrive_properties(ioc, raid_device);
6831                         spin_lock_irqsave(&ioc->raid_device_lock, flags);
6832                         if (raid_device->handle == handle) {
6833                                 spin_unlock_irqrestore(&ioc->raid_device_lock,
6834                                     flags);
6835                                 return;
6836                         }
6837                         printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6838                             raid_device->handle);
6839                         raid_device->handle = handle;
6840                         if (sas_target_priv_data)
6841                                 sas_target_priv_data->handle = handle;
6842                         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6843                         return;
6844                 }
6845         }
6846 
6847         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6848 }
6849 
6850 /**
6851  * _scsih_search_responding_raid_devices -
6852  * @ioc: per adapter object
6853  *
6854  * After host reset, find out whether devices are still responding.
6855  * If not remove.
6856  *
6857  * Return nothing.
6858  */
6859 static void
6860 _scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6861 {
6862         Mpi2RaidVolPage1_t volume_pg1;
6863         Mpi2RaidVolPage0_t volume_pg0;
6864         Mpi2RaidPhysDiskPage0_t pd_pg0;
6865         Mpi2ConfigReply_t mpi_reply;
6866         u16 ioc_status;
6867         u16 handle;
6868         u8 phys_disk_num;
6869 
6870         if (!ioc->ir_firmware)
6871                 return;
6872 
6873         printk(MPT2SAS_INFO_FMT "search for raid volumes: start\n",
6874             ioc->name);
6875 
6876         if (list_empty(&ioc->raid_device_list))
6877                 goto out;
6878 
6879         handle = 0xFFFF;
6880         while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6881             &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6882                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6883                     MPI2_IOCSTATUS_MASK;
6884                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6885                         break;
6886                 handle = le16_to_cpu(volume_pg1.DevHandle);
6887 
6888                 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6889                     &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6890                      sizeof(Mpi2RaidVolPage0_t)))
6891                         continue;
6892 
6893                 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6894                     volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6895                     volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6896                         _scsih_mark_responding_raid_device(ioc,
6897                             le64_to_cpu(volume_pg1.WWID), handle);
6898         }
6899 
6900         /* refresh the pd_handles */
6901         if (!ioc->is_warpdrive) {
6902                 phys_disk_num = 0xFF;
6903                 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6904                 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6905                     &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6906                     phys_disk_num))) {
6907                         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6908                             MPI2_IOCSTATUS_MASK;
6909                         if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6910                                 break;
6911                         phys_disk_num = pd_pg0.PhysDiskNum;
6912                         handle = le16_to_cpu(pd_pg0.DevHandle);
6913                         set_bit(handle, ioc->pd_handles);
6914                 }