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