Version:  2.0.40 2.2.26 2.4.37 3.2 3.3 3.4 3.5 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

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