Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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