Version:  2.0.40 2.2.26 2.4.37 3.1 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

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