ANDROID: video: adf: Avoid directly referencing user pointers
[firefly-linux-kernel-4.4.55.git] / drivers / scsi / mpt3sas / mpt3sas_scsih.c
1 /*
2  * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3  *
4  * This code is based on drivers/scsi/mpt3sas/mpt3sas_scsih.c
5  * Copyright (C) 2012-2014  LSI Corporation
6  * Copyright (C) 2013-2014 Avago Technologies
7  *  (mailto: MPT-FusionLinux.pdl@avagotech.com)
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * NO WARRANTY
20  * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21  * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22  * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24  * solely responsible for determining the appropriateness of using and
25  * distributing the Program and assumes all risks associated with its
26  * exercise of rights under this Agreement, including but not limited to
27  * the risks and costs of program errors, damage to or loss of data,
28  * programs or equipment, and unavailability or interruption of operations.
29
30  * DISCLAIMER OF LIABILITY
31  * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36  * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37  * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38
39  * You should have received a copy of the GNU General Public License
40  * along with this program; if not, write to the Free Software
41  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
42  * USA.
43  */
44
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/init.h>
48 #include <linux/errno.h>
49 #include <linux/blkdev.h>
50 #include <linux/sched.h>
51 #include <linux/workqueue.h>
52 #include <linux/delay.h>
53 #include <linux/pci.h>
54 #include <linux/interrupt.h>
55 #include <linux/aer.h>
56 #include <linux/raid_class.h>
57 #include <asm/unaligned.h>
58
59 #include "mpt3sas_base.h"
60
61 #define RAID_CHANNEL 1
62 /* forward proto's */
63 static void _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
64         struct _sas_node *sas_expander);
65 static void _firmware_event_work(struct work_struct *work);
66
67 static void _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
68         struct _sas_device *sas_device);
69 static int _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle,
70         u8 retry_count, u8 is_pd);
71
72 static u8 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid);
73
74 /* global parameters */
75 LIST_HEAD(mpt3sas_ioc_list);
76 /* global ioc lock for list operations */
77 DEFINE_SPINLOCK(gioc_lock);
78
79 MODULE_AUTHOR(MPT3SAS_AUTHOR);
80 MODULE_DESCRIPTION(MPT3SAS_DESCRIPTION);
81 MODULE_LICENSE("GPL");
82 MODULE_VERSION(MPT3SAS_DRIVER_VERSION);
83 MODULE_ALIAS("mpt2sas");
84
85 /* local parameters */
86 static u8 scsi_io_cb_idx = -1;
87 static u8 tm_cb_idx = -1;
88 static u8 ctl_cb_idx = -1;
89 static u8 base_cb_idx = -1;
90 static u8 port_enable_cb_idx = -1;
91 static u8 transport_cb_idx = -1;
92 static u8 scsih_cb_idx = -1;
93 static u8 config_cb_idx = -1;
94 static int mpt2_ids;
95 static int mpt3_ids;
96
97 static u8 tm_tr_cb_idx = -1 ;
98 static u8 tm_tr_volume_cb_idx = -1 ;
99 static u8 tm_sas_control_cb_idx = -1;
100
101 /* command line options */
102 static u32 logging_level;
103 MODULE_PARM_DESC(logging_level,
104         " bits for enabling additional logging info (default=0)");
105
106
107 static ushort max_sectors = 0xFFFF;
108 module_param(max_sectors, ushort, 0);
109 MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767  default=32767");
110
111
112 static int missing_delay[2] = {-1, -1};
113 module_param_array(missing_delay, int, NULL, 0);
114 MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
115
116 /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
117 #define MPT3SAS_MAX_LUN (16895)
118 static u64 max_lun = MPT3SAS_MAX_LUN;
119 module_param(max_lun, ullong, 0);
120 MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
121
122 static ushort hbas_to_enumerate;
123 module_param(hbas_to_enumerate, ushort, 0);
124 MODULE_PARM_DESC(hbas_to_enumerate,
125                 " 0 - enumerates both SAS 2.0 & SAS 3.0 generation HBAs\n \
126                   1 - enumerates only SAS 2.0 generation HBAs\n \
127                   2 - enumerates only SAS 3.0 generation HBAs (default=0)");
128
129 /* diag_buffer_enable is bitwise
130  * bit 0 set = TRACE
131  * bit 1 set = SNAPSHOT
132  * bit 2 set = EXTENDED
133  *
134  * Either bit can be set, or both
135  */
136 static int diag_buffer_enable = -1;
137 module_param(diag_buffer_enable, int, 0);
138 MODULE_PARM_DESC(diag_buffer_enable,
139         " post diag buffers (TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
140 static int disable_discovery = -1;
141 module_param(disable_discovery, int, 0);
142 MODULE_PARM_DESC(disable_discovery, " disable discovery ");
143
144
145 /* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
146 static int prot_mask = -1;
147 module_param(prot_mask, int, 0);
148 MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
149
150
151 /* raid transport support */
152 struct raid_template *mpt3sas_raid_template;
153 struct raid_template *mpt2sas_raid_template;
154
155
156 /**
157  * struct sense_info - common structure for obtaining sense keys
158  * @skey: sense key
159  * @asc: additional sense code
160  * @ascq: additional sense code qualifier
161  */
162 struct sense_info {
163         u8 skey;
164         u8 asc;
165         u8 ascq;
166 };
167
168 #define MPT3SAS_PROCESS_TRIGGER_DIAG (0xFFFB)
169 #define MPT3SAS_TURN_ON_PFA_LED (0xFFFC)
170 #define MPT3SAS_PORT_ENABLE_COMPLETE (0xFFFD)
171 #define MPT3SAS_ABRT_TASK_SET (0xFFFE)
172 #define MPT3SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
173 /**
174  * struct fw_event_work - firmware event struct
175  * @list: link list framework
176  * @work: work object (ioc->fault_reset_work_q)
177  * @cancel_pending_work: flag set during reset handling
178  * @ioc: per adapter object
179  * @device_handle: device handle
180  * @VF_ID: virtual function id
181  * @VP_ID: virtual port id
182  * @ignore: flag meaning this event has been marked to ignore
183  * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
184  * @event_data: reply event data payload follows
185  *
186  * This object stored on ioc->fw_event_list.
187  */
188 struct fw_event_work {
189         struct list_head        list;
190         struct work_struct      work;
191         u8                      cancel_pending_work;
192         struct delayed_work     delayed_work;
193
194         struct MPT3SAS_ADAPTER *ioc;
195         u16                     device_handle;
196         u8                      VF_ID;
197         u8                      VP_ID;
198         u8                      ignore;
199         u16                     event;
200         struct kref             refcount;
201         char                    event_data[0] __aligned(4);
202 };
203
204 static void fw_event_work_free(struct kref *r)
205 {
206         kfree(container_of(r, struct fw_event_work, refcount));
207 }
208
209 static void fw_event_work_get(struct fw_event_work *fw_work)
210 {
211         kref_get(&fw_work->refcount);
212 }
213
214 static void fw_event_work_put(struct fw_event_work *fw_work)
215 {
216         kref_put(&fw_work->refcount, fw_event_work_free);
217 }
218
219 static struct fw_event_work *alloc_fw_event_work(int len)
220 {
221         struct fw_event_work *fw_event;
222
223         fw_event = kzalloc(sizeof(*fw_event) + len, GFP_ATOMIC);
224         if (!fw_event)
225                 return NULL;
226
227         kref_init(&fw_event->refcount);
228         return fw_event;
229 }
230
231 /**
232  * struct _scsi_io_transfer - scsi io transfer
233  * @handle: sas device handle (assigned by firmware)
234  * @is_raid: flag set for hidden raid components
235  * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
236  * @data_length: data transfer length
237  * @data_dma: dma pointer to data
238  * @sense: sense data
239  * @lun: lun number
240  * @cdb_length: cdb length
241  * @cdb: cdb contents
242  * @timeout: timeout for this command
243  * @VF_ID: virtual function id
244  * @VP_ID: virtual port id
245  * @valid_reply: flag set for reply message
246  * @sense_length: sense length
247  * @ioc_status: ioc status
248  * @scsi_state: scsi state
249  * @scsi_status: scsi staus
250  * @log_info: log information
251  * @transfer_length: data length transfer when there is a reply message
252  *
253  * Used for sending internal scsi commands to devices within this module.
254  * Refer to _scsi_send_scsi_io().
255  */
256 struct _scsi_io_transfer {
257         u16     handle;
258         u8      is_raid;
259         enum dma_data_direction dir;
260         u32     data_length;
261         dma_addr_t data_dma;
262         u8      sense[SCSI_SENSE_BUFFERSIZE];
263         u32     lun;
264         u8      cdb_length;
265         u8      cdb[32];
266         u8      timeout;
267         u8      VF_ID;
268         u8      VP_ID;
269         u8      valid_reply;
270   /* the following bits are only valid when 'valid_reply = 1' */
271         u32     sense_length;
272         u16     ioc_status;
273         u8      scsi_state;
274         u8      scsi_status;
275         u32     log_info;
276         u32     transfer_length;
277 };
278
279 /**
280  * _scsih_set_debug_level - global setting of ioc->logging_level.
281  *
282  * Note: The logging levels are defined in mpt3sas_debug.h.
283  */
284 static int
285 _scsih_set_debug_level(const char *val, struct kernel_param *kp)
286 {
287         int ret = param_set_int(val, kp);
288         struct MPT3SAS_ADAPTER *ioc;
289
290         if (ret)
291                 return ret;
292
293         pr_info("setting logging_level(0x%08x)\n", logging_level);
294         spin_lock(&gioc_lock);
295         list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
296                 ioc->logging_level = logging_level;
297         spin_unlock(&gioc_lock);
298         return 0;
299 }
300 module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
301         &logging_level, 0644);
302
303 /**
304  * _scsih_srch_boot_sas_address - search based on sas_address
305  * @sas_address: sas address
306  * @boot_device: boot device object from bios page 2
307  *
308  * Returns 1 when there's a match, 0 means no match.
309  */
310 static inline int
311 _scsih_srch_boot_sas_address(u64 sas_address,
312         Mpi2BootDeviceSasWwid_t *boot_device)
313 {
314         return (sas_address == le64_to_cpu(boot_device->SASAddress)) ?  1 : 0;
315 }
316
317 /**
318  * _scsih_srch_boot_device_name - search based on device name
319  * @device_name: device name specified in INDENTIFY fram
320  * @boot_device: boot device object from bios page 2
321  *
322  * Returns 1 when there's a match, 0 means no match.
323  */
324 static inline int
325 _scsih_srch_boot_device_name(u64 device_name,
326         Mpi2BootDeviceDeviceName_t *boot_device)
327 {
328         return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
329 }
330
331 /**
332  * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
333  * @enclosure_logical_id: enclosure logical id
334  * @slot_number: slot number
335  * @boot_device: boot device object from bios page 2
336  *
337  * Returns 1 when there's a match, 0 means no match.
338  */
339 static inline int
340 _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
341         Mpi2BootDeviceEnclosureSlot_t *boot_device)
342 {
343         return (enclosure_logical_id == le64_to_cpu(boot_device->
344             EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
345             SlotNumber)) ? 1 : 0;
346 }
347
348 /**
349  * _scsih_is_boot_device - search for matching boot device.
350  * @sas_address: sas address
351  * @device_name: device name specified in INDENTIFY fram
352  * @enclosure_logical_id: enclosure logical id
353  * @slot_number: slot number
354  * @form: specifies boot device form
355  * @boot_device: boot device object from bios page 2
356  *
357  * Returns 1 when there's a match, 0 means no match.
358  */
359 static int
360 _scsih_is_boot_device(u64 sas_address, u64 device_name,
361         u64 enclosure_logical_id, u16 slot, u8 form,
362         Mpi2BiosPage2BootDevice_t *boot_device)
363 {
364         int rc = 0;
365
366         switch (form) {
367         case MPI2_BIOSPAGE2_FORM_SAS_WWID:
368                 if (!sas_address)
369                         break;
370                 rc = _scsih_srch_boot_sas_address(
371                     sas_address, &boot_device->SasWwid);
372                 break;
373         case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
374                 if (!enclosure_logical_id)
375                         break;
376                 rc = _scsih_srch_boot_encl_slot(
377                     enclosure_logical_id,
378                     slot, &boot_device->EnclosureSlot);
379                 break;
380         case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
381                 if (!device_name)
382                         break;
383                 rc = _scsih_srch_boot_device_name(
384                     device_name, &boot_device->DeviceName);
385                 break;
386         case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
387                 break;
388         }
389
390         return rc;
391 }
392
393 /**
394  * _scsih_get_sas_address - set the sas_address for given device handle
395  * @handle: device handle
396  * @sas_address: sas address
397  *
398  * Returns 0 success, non-zero when failure
399  */
400 static int
401 _scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
402         u64 *sas_address)
403 {
404         Mpi2SasDevicePage0_t sas_device_pg0;
405         Mpi2ConfigReply_t mpi_reply;
406         u32 ioc_status;
407
408         *sas_address = 0;
409
410         if (handle <= ioc->sas_hba.num_phys) {
411                 *sas_address = ioc->sas_hba.sas_address;
412                 return 0;
413         }
414
415         if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
416             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
417                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
418                 __FILE__, __LINE__, __func__);
419                 return -ENXIO;
420         }
421
422         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
423         if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
424                 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
425                 return 0;
426         }
427
428         /* we hit this becuase the given parent handle doesn't exist */
429         if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
430                 return -ENXIO;
431
432         /* else error case */
433         pr_err(MPT3SAS_FMT
434                 "handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
435                 ioc->name, handle, ioc_status,
436              __FILE__, __LINE__, __func__);
437         return -EIO;
438 }
439
440 /**
441  * _scsih_determine_boot_device - determine boot device.
442  * @ioc: per adapter object
443  * @device: either sas_device or raid_device object
444  * @is_raid: [flag] 1 = raid object, 0 = sas object
445  *
446  * Determines whether this device should be first reported device to
447  * to scsi-ml or sas transport, this purpose is for persistent boot device.
448  * There are primary, alternate, and current entries in bios page 2. The order
449  * priority is primary, alternate, then current.  This routine saves
450  * the corresponding device object and is_raid flag in the ioc object.
451  * The saved data to be used later in _scsih_probe_boot_devices().
452  */
453 static void
454 _scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc,
455         void *device, u8 is_raid)
456 {
457         struct _sas_device *sas_device;
458         struct _raid_device *raid_device;
459         u64 sas_address;
460         u64 device_name;
461         u64 enclosure_logical_id;
462         u16 slot;
463
464          /* only process this function when driver loads */
465         if (!ioc->is_driver_loading)
466                 return;
467
468          /* no Bios, return immediately */
469         if (!ioc->bios_pg3.BiosVersion)
470                 return;
471
472         if (!is_raid) {
473                 sas_device = device;
474                 sas_address = sas_device->sas_address;
475                 device_name = sas_device->device_name;
476                 enclosure_logical_id = sas_device->enclosure_logical_id;
477                 slot = sas_device->slot;
478         } else {
479                 raid_device = device;
480                 sas_address = raid_device->wwid;
481                 device_name = 0;
482                 enclosure_logical_id = 0;
483                 slot = 0;
484         }
485
486         if (!ioc->req_boot_device.device) {
487                 if (_scsih_is_boot_device(sas_address, device_name,
488                     enclosure_logical_id, slot,
489                     (ioc->bios_pg2.ReqBootDeviceForm &
490                     MPI2_BIOSPAGE2_FORM_MASK),
491                     &ioc->bios_pg2.RequestedBootDevice)) {
492                         dinitprintk(ioc, pr_info(MPT3SAS_FMT
493                            "%s: req_boot_device(0x%016llx)\n",
494                             ioc->name, __func__,
495                             (unsigned long long)sas_address));
496                         ioc->req_boot_device.device = device;
497                         ioc->req_boot_device.is_raid = is_raid;
498                 }
499         }
500
501         if (!ioc->req_alt_boot_device.device) {
502                 if (_scsih_is_boot_device(sas_address, device_name,
503                     enclosure_logical_id, slot,
504                     (ioc->bios_pg2.ReqAltBootDeviceForm &
505                     MPI2_BIOSPAGE2_FORM_MASK),
506                     &ioc->bios_pg2.RequestedAltBootDevice)) {
507                         dinitprintk(ioc, pr_info(MPT3SAS_FMT
508                            "%s: req_alt_boot_device(0x%016llx)\n",
509                             ioc->name, __func__,
510                             (unsigned long long)sas_address));
511                         ioc->req_alt_boot_device.device = device;
512                         ioc->req_alt_boot_device.is_raid = is_raid;
513                 }
514         }
515
516         if (!ioc->current_boot_device.device) {
517                 if (_scsih_is_boot_device(sas_address, device_name,
518                     enclosure_logical_id, slot,
519                     (ioc->bios_pg2.CurrentBootDeviceForm &
520                     MPI2_BIOSPAGE2_FORM_MASK),
521                     &ioc->bios_pg2.CurrentBootDevice)) {
522                         dinitprintk(ioc, pr_info(MPT3SAS_FMT
523                            "%s: current_boot_device(0x%016llx)\n",
524                             ioc->name, __func__,
525                             (unsigned long long)sas_address));
526                         ioc->current_boot_device.device = device;
527                         ioc->current_boot_device.is_raid = is_raid;
528                 }
529         }
530 }
531
532 static struct _sas_device *
533 __mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
534                 struct MPT3SAS_TARGET *tgt_priv)
535 {
536         struct _sas_device *ret;
537
538         assert_spin_locked(&ioc->sas_device_lock);
539
540         ret = tgt_priv->sdev;
541         if (ret)
542                 sas_device_get(ret);
543
544         return ret;
545 }
546
547 static struct _sas_device *
548 mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
549                 struct MPT3SAS_TARGET *tgt_priv)
550 {
551         struct _sas_device *ret;
552         unsigned long flags;
553
554         spin_lock_irqsave(&ioc->sas_device_lock, flags);
555         ret = __mpt3sas_get_sdev_from_target(ioc, tgt_priv);
556         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
557
558         return ret;
559 }
560
561
562 struct _sas_device *
563 __mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
564                                         u64 sas_address)
565 {
566         struct _sas_device *sas_device;
567
568         assert_spin_locked(&ioc->sas_device_lock);
569
570         list_for_each_entry(sas_device, &ioc->sas_device_list, list)
571                 if (sas_device->sas_address == sas_address)
572                         goto found_device;
573
574         list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
575                 if (sas_device->sas_address == sas_address)
576                         goto found_device;
577
578         return NULL;
579
580 found_device:
581         sas_device_get(sas_device);
582         return sas_device;
583 }
584
585 /**
586  * mpt3sas_get_sdev_by_addr - sas device search
587  * @ioc: per adapter object
588  * @sas_address: sas address
589  * Context: Calling function should acquire ioc->sas_device_lock
590  *
591  * This searches for sas_device based on sas_address, then return sas_device
592  * object.
593  */
594 struct _sas_device *
595 mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
596         u64 sas_address)
597 {
598         struct _sas_device *sas_device;
599         unsigned long flags;
600
601         spin_lock_irqsave(&ioc->sas_device_lock, flags);
602         sas_device = __mpt3sas_get_sdev_by_addr(ioc,
603                         sas_address);
604         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
605
606         return sas_device;
607 }
608
609 static struct _sas_device *
610 __mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
611 {
612         struct _sas_device *sas_device;
613
614         assert_spin_locked(&ioc->sas_device_lock);
615
616         list_for_each_entry(sas_device, &ioc->sas_device_list, list)
617                 if (sas_device->handle == handle)
618                         goto found_device;
619
620         list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
621                 if (sas_device->handle == handle)
622                         goto found_device;
623
624         return NULL;
625
626 found_device:
627         sas_device_get(sas_device);
628         return sas_device;
629 }
630
631 /**
632  * mpt3sas_get_sdev_by_handle - sas device search
633  * @ioc: per adapter object
634  * @handle: sas device handle (assigned by firmware)
635  * Context: Calling function should acquire ioc->sas_device_lock
636  *
637  * This searches for sas_device based on sas_address, then return sas_device
638  * object.
639  */
640 static struct _sas_device *
641 mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
642 {
643         struct _sas_device *sas_device;
644         unsigned long flags;
645
646         spin_lock_irqsave(&ioc->sas_device_lock, flags);
647         sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
648         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
649
650         return sas_device;
651 }
652
653 /**
654  * _scsih_sas_device_remove - remove sas_device from list.
655  * @ioc: per adapter object
656  * @sas_device: the sas_device object
657  * Context: This function will acquire ioc->sas_device_lock.
658  *
659  * If sas_device is on the list, remove it and decrement its reference count.
660  */
661 static void
662 _scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
663         struct _sas_device *sas_device)
664 {
665         unsigned long flags;
666
667         if (!sas_device)
668                 return;
669         pr_info(MPT3SAS_FMT
670             "removing handle(0x%04x), sas_addr(0x%016llx)\n",
671             ioc->name, sas_device->handle,
672             (unsigned long long) sas_device->sas_address);
673
674         if (sas_device->enclosure_handle != 0)
675                 pr_info(MPT3SAS_FMT
676                    "removing enclosure logical id(0x%016llx), slot(%d)\n",
677                    ioc->name, (unsigned long long)
678                    sas_device->enclosure_logical_id, sas_device->slot);
679
680         if (sas_device->connector_name[0] != '\0')
681                 pr_info(MPT3SAS_FMT
682                    "removing enclosure level(0x%04x), connector name( %s)\n",
683                    ioc->name, sas_device->enclosure_level,
684                    sas_device->connector_name);
685
686         /*
687          * The lock serializes access to the list, but we still need to verify
688          * that nobody removed the entry while we were waiting on the lock.
689          */
690         spin_lock_irqsave(&ioc->sas_device_lock, flags);
691         if (!list_empty(&sas_device->list)) {
692                 list_del_init(&sas_device->list);
693                 sas_device_put(sas_device);
694         }
695         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
696 }
697
698 /**
699  * _scsih_device_remove_by_handle - removing device object by handle
700  * @ioc: per adapter object
701  * @handle: device handle
702  *
703  * Return nothing.
704  */
705 static void
706 _scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
707 {
708         struct _sas_device *sas_device;
709         unsigned long flags;
710
711         if (ioc->shost_recovery)
712                 return;
713
714         spin_lock_irqsave(&ioc->sas_device_lock, flags);
715         sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
716         if (sas_device) {
717                 list_del_init(&sas_device->list);
718                 sas_device_put(sas_device);
719         }
720         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
721         if (sas_device) {
722                 _scsih_remove_device(ioc, sas_device);
723                 sas_device_put(sas_device);
724         }
725 }
726
727 /**
728  * mpt3sas_device_remove_by_sas_address - removing device object by sas address
729  * @ioc: per adapter object
730  * @sas_address: device sas_address
731  *
732  * Return nothing.
733  */
734 void
735 mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
736         u64 sas_address)
737 {
738         struct _sas_device *sas_device;
739         unsigned long flags;
740
741         if (ioc->shost_recovery)
742                 return;
743
744         spin_lock_irqsave(&ioc->sas_device_lock, flags);
745         sas_device = __mpt3sas_get_sdev_by_addr(ioc, sas_address);
746         if (sas_device) {
747                 list_del_init(&sas_device->list);
748                 sas_device_put(sas_device);
749         }
750         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
751         if (sas_device) {
752                 _scsih_remove_device(ioc, sas_device);
753                 sas_device_put(sas_device);
754         }
755 }
756
757 /**
758  * _scsih_sas_device_add - insert sas_device to the list.
759  * @ioc: per adapter object
760  * @sas_device: the sas_device object
761  * Context: This function will acquire ioc->sas_device_lock.
762  *
763  * Adding new object to the ioc->sas_device_list.
764  */
765 static void
766 _scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
767         struct _sas_device *sas_device)
768 {
769         unsigned long flags;
770
771         dewtprintk(ioc, pr_info(MPT3SAS_FMT
772                 "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
773                 ioc->name, __func__, sas_device->handle,
774                 (unsigned long long)sas_device->sas_address));
775
776         if (sas_device->enclosure_handle != 0)
777                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
778                     "%s: enclosure logical id(0x%016llx), slot( %d)\n",
779                     ioc->name, __func__, (unsigned long long)
780                     sas_device->enclosure_logical_id, sas_device->slot));
781
782         if (sas_device->connector_name[0] != '\0')
783                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
784                     "%s: enclosure level(0x%04x), connector name( %s)\n",
785                     ioc->name, __func__,
786                     sas_device->enclosure_level, sas_device->connector_name));
787
788         spin_lock_irqsave(&ioc->sas_device_lock, flags);
789         sas_device_get(sas_device);
790         list_add_tail(&sas_device->list, &ioc->sas_device_list);
791         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
792
793         if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
794              sas_device->sas_address_parent)) {
795                 _scsih_sas_device_remove(ioc, sas_device);
796         } else if (!sas_device->starget) {
797                 /*
798                  * When asyn scanning is enabled, its not possible to remove
799                  * devices while scanning is turned on due to an oops in
800                  * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
801                  */
802                 if (!ioc->is_driver_loading) {
803                         mpt3sas_transport_port_remove(ioc,
804                             sas_device->sas_address,
805                             sas_device->sas_address_parent);
806                         _scsih_sas_device_remove(ioc, sas_device);
807                 }
808         }
809 }
810
811 /**
812  * _scsih_sas_device_init_add - insert sas_device to the list.
813  * @ioc: per adapter object
814  * @sas_device: the sas_device object
815  * Context: This function will acquire ioc->sas_device_lock.
816  *
817  * Adding new object at driver load time to the ioc->sas_device_init_list.
818  */
819 static void
820 _scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
821         struct _sas_device *sas_device)
822 {
823         unsigned long flags;
824
825         dewtprintk(ioc, pr_info(MPT3SAS_FMT
826                 "%s: handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
827                 __func__, sas_device->handle,
828                 (unsigned long long)sas_device->sas_address));
829
830         if (sas_device->enclosure_handle != 0)
831                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
832                     "%s: enclosure logical id(0x%016llx), slot( %d)\n",
833                     ioc->name, __func__, (unsigned long long)
834                     sas_device->enclosure_logical_id, sas_device->slot));
835
836         if (sas_device->connector_name[0] != '\0')
837                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
838                     "%s: enclosure level(0x%04x), connector name( %s)\n",
839                     ioc->name, __func__, sas_device->enclosure_level,
840                     sas_device->connector_name));
841
842         spin_lock_irqsave(&ioc->sas_device_lock, flags);
843         sas_device_get(sas_device);
844         list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
845         _scsih_determine_boot_device(ioc, sas_device, 0);
846         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
847 }
848
849 /**
850  * _scsih_raid_device_find_by_id - raid device search
851  * @ioc: per adapter object
852  * @id: sas device target id
853  * @channel: sas device channel
854  * Context: Calling function should acquire ioc->raid_device_lock
855  *
856  * This searches for raid_device based on target id, then return raid_device
857  * object.
858  */
859 static struct _raid_device *
860 _scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
861 {
862         struct _raid_device *raid_device, *r;
863
864         r = NULL;
865         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
866                 if (raid_device->id == id && raid_device->channel == channel) {
867                         r = raid_device;
868                         goto out;
869                 }
870         }
871
872  out:
873         return r;
874 }
875
876 /**
877  * mpt3sas_raid_device_find_by_handle - raid device search
878  * @ioc: per adapter object
879  * @handle: sas device handle (assigned by firmware)
880  * Context: Calling function should acquire ioc->raid_device_lock
881  *
882  * This searches for raid_device based on handle, then return raid_device
883  * object.
884  */
885 struct _raid_device *
886 mpt3sas_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
887 {
888         struct _raid_device *raid_device, *r;
889
890         r = NULL;
891         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
892                 if (raid_device->handle != handle)
893                         continue;
894                 r = raid_device;
895                 goto out;
896         }
897
898  out:
899         return r;
900 }
901
902 /**
903  * _scsih_raid_device_find_by_wwid - raid device search
904  * @ioc: per adapter object
905  * @handle: sas device handle (assigned by firmware)
906  * Context: Calling function should acquire ioc->raid_device_lock
907  *
908  * This searches for raid_device based on wwid, then return raid_device
909  * object.
910  */
911 static struct _raid_device *
912 _scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
913 {
914         struct _raid_device *raid_device, *r;
915
916         r = NULL;
917         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
918                 if (raid_device->wwid != wwid)
919                         continue;
920                 r = raid_device;
921                 goto out;
922         }
923
924  out:
925         return r;
926 }
927
928 /**
929  * _scsih_raid_device_add - add raid_device object
930  * @ioc: per adapter object
931  * @raid_device: raid_device object
932  *
933  * This is added to the raid_device_list link list.
934  */
935 static void
936 _scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
937         struct _raid_device *raid_device)
938 {
939         unsigned long flags;
940
941         dewtprintk(ioc, pr_info(MPT3SAS_FMT
942                 "%s: handle(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
943             raid_device->handle, (unsigned long long)raid_device->wwid));
944
945         spin_lock_irqsave(&ioc->raid_device_lock, flags);
946         list_add_tail(&raid_device->list, &ioc->raid_device_list);
947         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
948 }
949
950 /**
951  * _scsih_raid_device_remove - delete raid_device object
952  * @ioc: per adapter object
953  * @raid_device: raid_device object
954  *
955  */
956 static void
957 _scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
958         struct _raid_device *raid_device)
959 {
960         unsigned long flags;
961
962         spin_lock_irqsave(&ioc->raid_device_lock, flags);
963         list_del(&raid_device->list);
964         kfree(raid_device);
965         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
966 }
967
968 /**
969  * mpt3sas_scsih_expander_find_by_handle - expander device search
970  * @ioc: per adapter object
971  * @handle: expander handle (assigned by firmware)
972  * Context: Calling function should acquire ioc->sas_device_lock
973  *
974  * This searches for expander device based on handle, then returns the
975  * sas_node object.
976  */
977 struct _sas_node *
978 mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
979 {
980         struct _sas_node *sas_expander, *r;
981
982         r = NULL;
983         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
984                 if (sas_expander->handle != handle)
985                         continue;
986                 r = sas_expander;
987                 goto out;
988         }
989  out:
990         return r;
991 }
992
993 /**
994  * mpt3sas_scsih_expander_find_by_sas_address - expander device search
995  * @ioc: per adapter object
996  * @sas_address: sas address
997  * Context: Calling function should acquire ioc->sas_node_lock.
998  *
999  * This searches for expander device based on sas_address, then returns the
1000  * sas_node object.
1001  */
1002 struct _sas_node *
1003 mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
1004         u64 sas_address)
1005 {
1006         struct _sas_node *sas_expander, *r;
1007
1008         r = NULL;
1009         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
1010                 if (sas_expander->sas_address != sas_address)
1011                         continue;
1012                 r = sas_expander;
1013                 goto out;
1014         }
1015  out:
1016         return r;
1017 }
1018
1019 /**
1020  * _scsih_expander_node_add - insert expander device to the list.
1021  * @ioc: per adapter object
1022  * @sas_expander: the sas_device object
1023  * Context: This function will acquire ioc->sas_node_lock.
1024  *
1025  * Adding new object to the ioc->sas_expander_list.
1026  *
1027  * Return nothing.
1028  */
1029 static void
1030 _scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
1031         struct _sas_node *sas_expander)
1032 {
1033         unsigned long flags;
1034
1035         spin_lock_irqsave(&ioc->sas_node_lock, flags);
1036         list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
1037         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1038 }
1039
1040 /**
1041  * _scsih_is_end_device - determines if device is an end device
1042  * @device_info: bitfield providing information about the device.
1043  * Context: none
1044  *
1045  * Returns 1 if end device.
1046  */
1047 static int
1048 _scsih_is_end_device(u32 device_info)
1049 {
1050         if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
1051                 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
1052                 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
1053                 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
1054                 return 1;
1055         else
1056                 return 0;
1057 }
1058
1059 /**
1060  * _scsih_scsi_lookup_get - returns scmd entry
1061  * @ioc: per adapter object
1062  * @smid: system request message index
1063  *
1064  * Returns the smid stored scmd pointer.
1065  */
1066 static struct scsi_cmnd *
1067 _scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1068 {
1069         return ioc->scsi_lookup[smid - 1].scmd;
1070 }
1071
1072 /**
1073  * _scsih_scsi_lookup_get_clear - returns scmd entry
1074  * @ioc: per adapter object
1075  * @smid: system request message index
1076  *
1077  * Returns the smid stored scmd pointer.
1078  * Then will derefrence the stored scmd pointer.
1079  */
1080 static inline struct scsi_cmnd *
1081 _scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1082 {
1083         unsigned long flags;
1084         struct scsi_cmnd *scmd;
1085
1086         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1087         scmd = ioc->scsi_lookup[smid - 1].scmd;
1088         ioc->scsi_lookup[smid - 1].scmd = NULL;
1089         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1090
1091         return scmd;
1092 }
1093
1094 /**
1095  * _scsih_scsi_lookup_find_by_scmd - scmd lookup
1096  * @ioc: per adapter object
1097  * @smid: system request message index
1098  * @scmd: pointer to scsi command object
1099  * Context: This function will acquire ioc->scsi_lookup_lock.
1100  *
1101  * This will search for a scmd pointer in the scsi_lookup array,
1102  * returning the revelent smid.  A returned value of zero means invalid.
1103  */
1104 static u16
1105 _scsih_scsi_lookup_find_by_scmd(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd
1106         *scmd)
1107 {
1108         u16 smid;
1109         unsigned long   flags;
1110         int i;
1111
1112         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1113         smid = 0;
1114         for (i = 0; i < ioc->scsiio_depth; i++) {
1115                 if (ioc->scsi_lookup[i].scmd == scmd) {
1116                         smid = ioc->scsi_lookup[i].smid;
1117                         goto out;
1118                 }
1119         }
1120  out:
1121         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1122         return smid;
1123 }
1124
1125 /**
1126  * _scsih_scsi_lookup_find_by_target - search for matching channel:id
1127  * @ioc: per adapter object
1128  * @id: target id
1129  * @channel: channel
1130  * Context: This function will acquire ioc->scsi_lookup_lock.
1131  *
1132  * This will search for a matching channel:id in the scsi_lookup array,
1133  * returning 1 if found.
1134  */
1135 static u8
1136 _scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
1137         int channel)
1138 {
1139         u8 found;
1140         unsigned long   flags;
1141         int i;
1142
1143         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1144         found = 0;
1145         for (i = 0 ; i < ioc->scsiio_depth; i++) {
1146                 if (ioc->scsi_lookup[i].scmd &&
1147                     (ioc->scsi_lookup[i].scmd->device->id == id &&
1148                     ioc->scsi_lookup[i].scmd->device->channel == channel)) {
1149                         found = 1;
1150                         goto out;
1151                 }
1152         }
1153  out:
1154         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1155         return found;
1156 }
1157
1158 /**
1159  * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
1160  * @ioc: per adapter object
1161  * @id: target id
1162  * @lun: lun number
1163  * @channel: channel
1164  * Context: This function will acquire ioc->scsi_lookup_lock.
1165  *
1166  * This will search for a matching channel:id:lun in the scsi_lookup array,
1167  * returning 1 if found.
1168  */
1169 static u8
1170 _scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
1171         unsigned int lun, int channel)
1172 {
1173         u8 found;
1174         unsigned long   flags;
1175         int i;
1176
1177         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1178         found = 0;
1179         for (i = 0 ; i < ioc->scsiio_depth; i++) {
1180                 if (ioc->scsi_lookup[i].scmd &&
1181                     (ioc->scsi_lookup[i].scmd->device->id == id &&
1182                     ioc->scsi_lookup[i].scmd->device->channel == channel &&
1183                     ioc->scsi_lookup[i].scmd->device->lun == lun)) {
1184                         found = 1;
1185                         goto out;
1186                 }
1187         }
1188  out:
1189         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1190         return found;
1191 }
1192
1193 /**
1194  * scsih_change_queue_depth - setting device queue depth
1195  * @sdev: scsi device struct
1196  * @qdepth: requested queue depth
1197  *
1198  * Returns queue depth.
1199  */
1200 int
1201 scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1202 {
1203         struct Scsi_Host *shost = sdev->host;
1204         int max_depth;
1205         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1206         struct MPT3SAS_DEVICE *sas_device_priv_data;
1207         struct MPT3SAS_TARGET *sas_target_priv_data;
1208         struct _sas_device *sas_device;
1209         unsigned long flags;
1210
1211         max_depth = shost->can_queue;
1212
1213         /* limit max device queue for SATA to 32 */
1214         sas_device_priv_data = sdev->hostdata;
1215         if (!sas_device_priv_data)
1216                 goto not_sata;
1217         sas_target_priv_data = sas_device_priv_data->sas_target;
1218         if (!sas_target_priv_data)
1219                 goto not_sata;
1220         if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1221                 goto not_sata;
1222
1223         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1224         sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1225         if (sas_device) {
1226                 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1227                         max_depth = MPT3SAS_SATA_QUEUE_DEPTH;
1228
1229                 sas_device_put(sas_device);
1230         }
1231         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1232
1233  not_sata:
1234
1235         if (!sdev->tagged_supported)
1236                 max_depth = 1;
1237         if (qdepth > max_depth)
1238                 qdepth = max_depth;
1239         return scsi_change_queue_depth(sdev, qdepth);
1240 }
1241
1242 /**
1243  * scsih_target_alloc - target add routine
1244  * @starget: scsi target struct
1245  *
1246  * Returns 0 if ok. Any other return is assumed to be an error and
1247  * the device is ignored.
1248  */
1249 int
1250 scsih_target_alloc(struct scsi_target *starget)
1251 {
1252         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1253         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1254         struct MPT3SAS_TARGET *sas_target_priv_data;
1255         struct _sas_device *sas_device;
1256         struct _raid_device *raid_device;
1257         unsigned long flags;
1258         struct sas_rphy *rphy;
1259
1260         sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1261                                        GFP_KERNEL);
1262         if (!sas_target_priv_data)
1263                 return -ENOMEM;
1264
1265         starget->hostdata = sas_target_priv_data;
1266         sas_target_priv_data->starget = starget;
1267         sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
1268
1269         /* RAID volumes */
1270         if (starget->channel == RAID_CHANNEL) {
1271                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1272                 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1273                     starget->channel);
1274                 if (raid_device) {
1275                         sas_target_priv_data->handle = raid_device->handle;
1276                         sas_target_priv_data->sas_address = raid_device->wwid;
1277                         sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1278                         sas_target_priv_data->raid_device = raid_device;
1279                         if (ioc->is_warpdrive)
1280                                 raid_device->starget = starget;
1281                 }
1282                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1283                 return 0;
1284         }
1285
1286         /* sas/sata devices */
1287         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1288         rphy = dev_to_rphy(starget->dev.parent);
1289         sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1290            rphy->identify.sas_address);
1291
1292         if (sas_device) {
1293                 sas_target_priv_data->handle = sas_device->handle;
1294                 sas_target_priv_data->sas_address = sas_device->sas_address;
1295                 sas_target_priv_data->sdev = sas_device;
1296                 sas_device->starget = starget;
1297                 sas_device->id = starget->id;
1298                 sas_device->channel = starget->channel;
1299                 if (test_bit(sas_device->handle, ioc->pd_handles))
1300                         sas_target_priv_data->flags |=
1301                             MPT_TARGET_FLAGS_RAID_COMPONENT;
1302                 if (sas_device->fast_path)
1303                         sas_target_priv_data->flags |= MPT_TARGET_FASTPATH_IO;
1304         }
1305         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1306
1307         return 0;
1308 }
1309
1310 /**
1311  * scsih_target_destroy - target destroy routine
1312  * @starget: scsi target struct
1313  *
1314  * Returns nothing.
1315  */
1316 void
1317 scsih_target_destroy(struct scsi_target *starget)
1318 {
1319         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1320         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1321         struct MPT3SAS_TARGET *sas_target_priv_data;
1322         struct _sas_device *sas_device;
1323         struct _raid_device *raid_device;
1324         unsigned long flags;
1325         struct sas_rphy *rphy;
1326
1327         sas_target_priv_data = starget->hostdata;
1328         if (!sas_target_priv_data)
1329                 return;
1330
1331         if (starget->channel == RAID_CHANNEL) {
1332                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1333                 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1334                     starget->channel);
1335                 if (raid_device) {
1336                         raid_device->starget = NULL;
1337                         raid_device->sdev = NULL;
1338                 }
1339                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1340                 goto out;
1341         }
1342
1343         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1344         rphy = dev_to_rphy(starget->dev.parent);
1345         sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1346         if (sas_device && (sas_device->starget == starget) &&
1347             (sas_device->id == starget->id) &&
1348             (sas_device->channel == starget->channel))
1349                 sas_device->starget = NULL;
1350
1351         if (sas_device) {
1352                 /*
1353                  * Corresponding get() is in _scsih_target_alloc()
1354                  */
1355                 sas_target_priv_data->sdev = NULL;
1356                 sas_device_put(sas_device);
1357
1358                 sas_device_put(sas_device);
1359         }
1360         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1361
1362  out:
1363         kfree(sas_target_priv_data);
1364         starget->hostdata = NULL;
1365 }
1366
1367 /**
1368  * scsih_slave_alloc - device add routine
1369  * @sdev: scsi device struct
1370  *
1371  * Returns 0 if ok. Any other return is assumed to be an error and
1372  * the device is ignored.
1373  */
1374 int
1375 scsih_slave_alloc(struct scsi_device *sdev)
1376 {
1377         struct Scsi_Host *shost;
1378         struct MPT3SAS_ADAPTER *ioc;
1379         struct MPT3SAS_TARGET *sas_target_priv_data;
1380         struct MPT3SAS_DEVICE *sas_device_priv_data;
1381         struct scsi_target *starget;
1382         struct _raid_device *raid_device;
1383         struct _sas_device *sas_device;
1384         unsigned long flags;
1385
1386         sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
1387                                        GFP_KERNEL);
1388         if (!sas_device_priv_data)
1389                 return -ENOMEM;
1390
1391         sas_device_priv_data->lun = sdev->lun;
1392         sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1393
1394         starget = scsi_target(sdev);
1395         sas_target_priv_data = starget->hostdata;
1396         sas_target_priv_data->num_luns++;
1397         sas_device_priv_data->sas_target = sas_target_priv_data;
1398         sdev->hostdata = sas_device_priv_data;
1399         if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1400                 sdev->no_uld_attach = 1;
1401
1402         shost = dev_to_shost(&starget->dev);
1403         ioc = shost_priv(shost);
1404         if (starget->channel == RAID_CHANNEL) {
1405                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1406                 raid_device = _scsih_raid_device_find_by_id(ioc,
1407                     starget->id, starget->channel);
1408                 if (raid_device)
1409                         raid_device->sdev = sdev; /* raid is single lun */
1410                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1411         }
1412
1413         if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1414                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1415                 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1416                                         sas_target_priv_data->sas_address);
1417                 if (sas_device && (sas_device->starget == NULL)) {
1418                         sdev_printk(KERN_INFO, sdev,
1419                         "%s : sas_device->starget set to starget @ %d\n",
1420                              __func__, __LINE__);
1421                         sas_device->starget = starget;
1422                 }
1423
1424                 if (sas_device)
1425                         sas_device_put(sas_device);
1426
1427                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1428         }
1429
1430         return 0;
1431 }
1432
1433 /**
1434  * scsih_slave_destroy - device destroy routine
1435  * @sdev: scsi device struct
1436  *
1437  * Returns nothing.
1438  */
1439 void
1440 scsih_slave_destroy(struct scsi_device *sdev)
1441 {
1442         struct MPT3SAS_TARGET *sas_target_priv_data;
1443         struct scsi_target *starget;
1444         struct Scsi_Host *shost;
1445         struct MPT3SAS_ADAPTER *ioc;
1446         struct _sas_device *sas_device;
1447         unsigned long flags;
1448
1449         if (!sdev->hostdata)
1450                 return;
1451
1452         starget = scsi_target(sdev);
1453         sas_target_priv_data = starget->hostdata;
1454         sas_target_priv_data->num_luns--;
1455
1456         shost = dev_to_shost(&starget->dev);
1457         ioc = shost_priv(shost);
1458
1459         if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1460                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1461                 sas_device = __mpt3sas_get_sdev_from_target(ioc,
1462                                 sas_target_priv_data);
1463                 if (sas_device && !sas_target_priv_data->num_luns)
1464                         sas_device->starget = NULL;
1465
1466                 if (sas_device)
1467                         sas_device_put(sas_device);
1468                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1469         }
1470
1471         kfree(sdev->hostdata);
1472         sdev->hostdata = NULL;
1473 }
1474
1475 /**
1476  * _scsih_display_sata_capabilities - sata capabilities
1477  * @ioc: per adapter object
1478  * @handle: device handle
1479  * @sdev: scsi device struct
1480  */
1481 static void
1482 _scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
1483         u16 handle, struct scsi_device *sdev)
1484 {
1485         Mpi2ConfigReply_t mpi_reply;
1486         Mpi2SasDevicePage0_t sas_device_pg0;
1487         u32 ioc_status;
1488         u16 flags;
1489         u32 device_info;
1490
1491         if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1492             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
1493                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1494                     ioc->name, __FILE__, __LINE__, __func__);
1495                 return;
1496         }
1497
1498         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1499             MPI2_IOCSTATUS_MASK;
1500         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1501                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1502                     ioc->name, __FILE__, __LINE__, __func__);
1503                 return;
1504         }
1505
1506         flags = le16_to_cpu(sas_device_pg0.Flags);
1507         device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1508
1509         sdev_printk(KERN_INFO, sdev,
1510             "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1511             "sw_preserve(%s)\n",
1512             (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1513             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1514             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1515             "n",
1516             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1517             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1518             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1519 }
1520
1521 /*
1522  * raid transport support -
1523  * Enabled for SLES11 and newer, in older kernels the driver will panic when
1524  * unloading the driver followed by a load - I beleive that the subroutine
1525  * raid_class_release() is not cleaning up properly.
1526  */
1527
1528 /**
1529  * scsih_is_raid - return boolean indicating device is raid volume
1530  * @dev the device struct object
1531  */
1532 int
1533 scsih_is_raid(struct device *dev)
1534 {
1535         struct scsi_device *sdev = to_scsi_device(dev);
1536         struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1537
1538         if (ioc->is_warpdrive)
1539                 return 0;
1540         return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1541 }
1542
1543 /**
1544  * scsih_get_resync - get raid volume resync percent complete
1545  * @dev the device struct object
1546  */
1547 void
1548 scsih_get_resync(struct device *dev)
1549 {
1550         struct scsi_device *sdev = to_scsi_device(dev);
1551         struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1552         static struct _raid_device *raid_device;
1553         unsigned long flags;
1554         Mpi2RaidVolPage0_t vol_pg0;
1555         Mpi2ConfigReply_t mpi_reply;
1556         u32 volume_status_flags;
1557         u8 percent_complete;
1558         u16 handle;
1559
1560         percent_complete = 0;
1561         handle = 0;
1562         if (ioc->is_warpdrive)
1563                 goto out;
1564
1565         spin_lock_irqsave(&ioc->raid_device_lock, flags);
1566         raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1567             sdev->channel);
1568         if (raid_device) {
1569                 handle = raid_device->handle;
1570                 percent_complete = raid_device->percent_complete;
1571         }
1572         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1573
1574         if (!handle)
1575                 goto out;
1576
1577         if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1578              MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1579              sizeof(Mpi2RaidVolPage0_t))) {
1580                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1581                     ioc->name, __FILE__, __LINE__, __func__);
1582                 percent_complete = 0;
1583                 goto out;
1584         }
1585
1586         volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1587         if (!(volume_status_flags &
1588             MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1589                 percent_complete = 0;
1590
1591  out:
1592         if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
1593                 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1594         if (ioc->hba_mpi_version_belonged == MPI25_VERSION)
1595                 raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
1596 }
1597
1598 /**
1599  * scsih_get_state - get raid volume level
1600  * @dev the device struct object
1601  */
1602 void
1603 scsih_get_state(struct device *dev)
1604 {
1605         struct scsi_device *sdev = to_scsi_device(dev);
1606         struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1607         static struct _raid_device *raid_device;
1608         unsigned long flags;
1609         Mpi2RaidVolPage0_t vol_pg0;
1610         Mpi2ConfigReply_t mpi_reply;
1611         u32 volstate;
1612         enum raid_state state = RAID_STATE_UNKNOWN;
1613         u16 handle = 0;
1614
1615         spin_lock_irqsave(&ioc->raid_device_lock, flags);
1616         raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1617             sdev->channel);
1618         if (raid_device)
1619                 handle = raid_device->handle;
1620         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1621
1622         if (!raid_device)
1623                 goto out;
1624
1625         if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1626              MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1627              sizeof(Mpi2RaidVolPage0_t))) {
1628                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1629                     ioc->name, __FILE__, __LINE__, __func__);
1630                 goto out;
1631         }
1632
1633         volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1634         if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1635                 state = RAID_STATE_RESYNCING;
1636                 goto out;
1637         }
1638
1639         switch (vol_pg0.VolumeState) {
1640         case MPI2_RAID_VOL_STATE_OPTIMAL:
1641         case MPI2_RAID_VOL_STATE_ONLINE:
1642                 state = RAID_STATE_ACTIVE;
1643                 break;
1644         case  MPI2_RAID_VOL_STATE_DEGRADED:
1645                 state = RAID_STATE_DEGRADED;
1646                 break;
1647         case MPI2_RAID_VOL_STATE_FAILED:
1648         case MPI2_RAID_VOL_STATE_MISSING:
1649                 state = RAID_STATE_OFFLINE;
1650                 break;
1651         }
1652  out:
1653         if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
1654                 raid_set_state(mpt2sas_raid_template, dev, state);
1655         if (ioc->hba_mpi_version_belonged == MPI25_VERSION)
1656                 raid_set_state(mpt3sas_raid_template, dev, state);
1657 }
1658
1659 /**
1660  * _scsih_set_level - set raid level
1661  * @sdev: scsi device struct
1662  * @volume_type: volume type
1663  */
1664 static void
1665 _scsih_set_level(struct MPT3SAS_ADAPTER *ioc,
1666         struct scsi_device *sdev, u8 volume_type)
1667 {
1668         enum raid_level level = RAID_LEVEL_UNKNOWN;
1669
1670         switch (volume_type) {
1671         case MPI2_RAID_VOL_TYPE_RAID0:
1672                 level = RAID_LEVEL_0;
1673                 break;
1674         case MPI2_RAID_VOL_TYPE_RAID10:
1675                 level = RAID_LEVEL_10;
1676                 break;
1677         case MPI2_RAID_VOL_TYPE_RAID1E:
1678                 level = RAID_LEVEL_1E;
1679                 break;
1680         case MPI2_RAID_VOL_TYPE_RAID1:
1681                 level = RAID_LEVEL_1;
1682                 break;
1683         }
1684
1685         if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
1686                 raid_set_level(mpt2sas_raid_template,
1687                                &sdev->sdev_gendev, level);
1688         if (ioc->hba_mpi_version_belonged == MPI25_VERSION)
1689                 raid_set_level(mpt3sas_raid_template,
1690                                &sdev->sdev_gendev, level);
1691 }
1692
1693
1694 /**
1695  * _scsih_get_volume_capabilities - volume capabilities
1696  * @ioc: per adapter object
1697  * @sas_device: the raid_device object
1698  *
1699  * Returns 0 for success, else 1
1700  */
1701 static int
1702 _scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
1703         struct _raid_device *raid_device)
1704 {
1705         Mpi2RaidVolPage0_t *vol_pg0;
1706         Mpi2RaidPhysDiskPage0_t pd_pg0;
1707         Mpi2SasDevicePage0_t sas_device_pg0;
1708         Mpi2ConfigReply_t mpi_reply;
1709         u16 sz;
1710         u8 num_pds;
1711
1712         if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
1713             &num_pds)) || !num_pds) {
1714                 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1715                     "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1716                     __func__));
1717                 return 1;
1718         }
1719
1720         raid_device->num_pds = num_pds;
1721         sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1722             sizeof(Mpi2RaidVol0PhysDisk_t));
1723         vol_pg0 = kzalloc(sz, GFP_KERNEL);
1724         if (!vol_pg0) {
1725                 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1726                     "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1727                     __func__));
1728                 return 1;
1729         }
1730
1731         if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1732              MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1733                 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1734                     "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1735                     __func__));
1736                 kfree(vol_pg0);
1737                 return 1;
1738         }
1739
1740         raid_device->volume_type = vol_pg0->VolumeType;
1741
1742         /* figure out what the underlying devices are by
1743          * obtaining the device_info bits for the 1st device
1744          */
1745         if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1746             &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1747             vol_pg0->PhysDisk[0].PhysDiskNum))) {
1748                 if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1749                     &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1750                     le16_to_cpu(pd_pg0.DevHandle)))) {
1751                         raid_device->device_info =
1752                             le32_to_cpu(sas_device_pg0.DeviceInfo);
1753                 }
1754         }
1755
1756         kfree(vol_pg0);
1757         return 0;
1758 }
1759
1760 /**
1761  * _scsih_enable_tlr - setting TLR flags
1762  * @ioc: per adapter object
1763  * @sdev: scsi device struct
1764  *
1765  * Enabling Transaction Layer Retries for tape devices when
1766  * vpd page 0x90 is present
1767  *
1768  */
1769 static void
1770 _scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
1771 {
1772
1773         /* only for TAPE */
1774         if (sdev->type != TYPE_TAPE)
1775                 return;
1776
1777         if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1778                 return;
1779
1780         sas_enable_tlr(sdev);
1781         sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1782             sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1783         return;
1784
1785 }
1786
1787 /**
1788  * scsih_slave_configure - device configure routine.
1789  * @sdev: scsi device struct
1790  *
1791  * Returns 0 if ok. Any other return is assumed to be an error and
1792  * the device is ignored.
1793  */
1794 int
1795 scsih_slave_configure(struct scsi_device *sdev)
1796 {
1797         struct Scsi_Host *shost = sdev->host;
1798         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1799         struct MPT3SAS_DEVICE *sas_device_priv_data;
1800         struct MPT3SAS_TARGET *sas_target_priv_data;
1801         struct _sas_device *sas_device;
1802         struct _raid_device *raid_device;
1803         unsigned long flags;
1804         int qdepth;
1805         u8 ssp_target = 0;
1806         char *ds = "";
1807         char *r_level = "";
1808         u16 handle, volume_handle = 0;
1809         u64 volume_wwid = 0;
1810
1811         qdepth = 1;
1812         sas_device_priv_data = sdev->hostdata;
1813         sas_device_priv_data->configured_lun = 1;
1814         sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1815         sas_target_priv_data = sas_device_priv_data->sas_target;
1816         handle = sas_target_priv_data->handle;
1817
1818         /* raid volume handling */
1819         if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1820
1821                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1822                 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
1823                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1824                 if (!raid_device) {
1825                         dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1826                             "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1827                             __LINE__, __func__));
1828                         return 1;
1829                 }
1830
1831                 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1832                         dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1833                             "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1834                             __LINE__, __func__));
1835                         return 1;
1836                 }
1837
1838                 /*
1839                  * WARPDRIVE: Initialize the required data for Direct IO
1840                  */
1841                 mpt3sas_init_warpdrive_properties(ioc, raid_device);
1842
1843                 /* RAID Queue Depth Support
1844                  * IS volume = underlying qdepth of drive type, either
1845                  *    MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
1846                  * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
1847                  */
1848                 if (raid_device->device_info &
1849                     MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1850                         qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1851                         ds = "SSP";
1852                 } else {
1853                         qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1854                          if (raid_device->device_info &
1855                             MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1856                                 ds = "SATA";
1857                         else
1858                                 ds = "STP";
1859                 }
1860
1861                 switch (raid_device->volume_type) {
1862                 case MPI2_RAID_VOL_TYPE_RAID0:
1863                         r_level = "RAID0";
1864                         break;
1865                 case MPI2_RAID_VOL_TYPE_RAID1E:
1866                         qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1867                         if (ioc->manu_pg10.OEMIdentifier &&
1868                             (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
1869                             MFG10_GF0_R10_DISPLAY) &&
1870                             !(raid_device->num_pds % 2))
1871                                 r_level = "RAID10";
1872                         else
1873                                 r_level = "RAID1E";
1874                         break;
1875                 case MPI2_RAID_VOL_TYPE_RAID1:
1876                         qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1877                         r_level = "RAID1";
1878                         break;
1879                 case MPI2_RAID_VOL_TYPE_RAID10:
1880                         qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1881                         r_level = "RAID10";
1882                         break;
1883                 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1884                 default:
1885                         qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1886                         r_level = "RAIDX";
1887                         break;
1888                 }
1889
1890                 if (!ioc->hide_ir_msg)
1891                         sdev_printk(KERN_INFO, sdev,
1892                            "%s: handle(0x%04x), wwid(0x%016llx),"
1893                             " pd_count(%d), type(%s)\n",
1894                             r_level, raid_device->handle,
1895                             (unsigned long long)raid_device->wwid,
1896                             raid_device->num_pds, ds);
1897
1898                 scsih_change_queue_depth(sdev, qdepth);
1899
1900                 /* raid transport support */
1901                 if (!ioc->is_warpdrive)
1902                         _scsih_set_level(ioc, sdev, raid_device->volume_type);
1903                 return 0;
1904         }
1905
1906         /* non-raid handling */
1907         if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
1908                 if (mpt3sas_config_get_volume_handle(ioc, handle,
1909                     &volume_handle)) {
1910                         dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1911                             "failure at %s:%d/%s()!\n", ioc->name,
1912                             __FILE__, __LINE__, __func__));
1913                         return 1;
1914                 }
1915                 if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
1916                     volume_handle, &volume_wwid)) {
1917                         dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1918                             "failure at %s:%d/%s()!\n", ioc->name,
1919                             __FILE__, __LINE__, __func__));
1920                         return 1;
1921                 }
1922         }
1923
1924         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1925         sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1926            sas_device_priv_data->sas_target->sas_address);
1927         if (!sas_device) {
1928                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1929                 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1930                     "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1931                     __func__));
1932                 return 1;
1933         }
1934
1935         sas_device->volume_handle = volume_handle;
1936         sas_device->volume_wwid = volume_wwid;
1937         if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1938                 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1939                 ssp_target = 1;
1940                 ds = "SSP";
1941         } else {
1942                 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1943                 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
1944                         ds = "STP";
1945                 else if (sas_device->device_info &
1946                     MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1947                         ds = "SATA";
1948         }
1949
1950         sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
1951             "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
1952             ds, handle, (unsigned long long)sas_device->sas_address,
1953             sas_device->phy, (unsigned long long)sas_device->device_name);
1954         if (sas_device->enclosure_handle != 0)
1955                 sdev_printk(KERN_INFO, sdev,
1956                      "%s: enclosure_logical_id(0x%016llx), slot(%d)\n",
1957                      ds, (unsigned long long)
1958                      sas_device->enclosure_logical_id, sas_device->slot);
1959         if (sas_device->connector_name[0] != '\0')
1960                 sdev_printk(KERN_INFO, sdev,
1961                      "%s: enclosure level(0x%04x), connector name( %s)\n",
1962                      ds, sas_device->enclosure_level,
1963                      sas_device->connector_name);
1964
1965         sas_device_put(sas_device);
1966         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1967
1968         if (!ssp_target)
1969                 _scsih_display_sata_capabilities(ioc, handle, sdev);
1970
1971
1972         scsih_change_queue_depth(sdev, qdepth);
1973
1974         if (ssp_target) {
1975                 sas_read_port_mode_page(sdev);
1976                 _scsih_enable_tlr(ioc, sdev);
1977         }
1978
1979         return 0;
1980 }
1981
1982 /**
1983  * scsih_bios_param - fetch head, sector, cylinder info for a disk
1984  * @sdev: scsi device struct
1985  * @bdev: pointer to block device context
1986  * @capacity: device size (in 512 byte sectors)
1987  * @params: three element array to place output:
1988  *              params[0] number of heads (max 255)
1989  *              params[1] number of sectors (max 63)
1990  *              params[2] number of cylinders
1991  *
1992  * Return nothing.
1993  */
1994 int
1995 scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1996         sector_t capacity, int params[])
1997 {
1998         int             heads;
1999         int             sectors;
2000         sector_t        cylinders;
2001         ulong           dummy;
2002
2003         heads = 64;
2004         sectors = 32;
2005
2006         dummy = heads * sectors;
2007         cylinders = capacity;
2008         sector_div(cylinders, dummy);
2009
2010         /*
2011          * Handle extended translation size for logical drives
2012          * > 1Gb
2013          */
2014         if ((ulong)capacity >= 0x200000) {
2015                 heads = 255;
2016                 sectors = 63;
2017                 dummy = heads * sectors;
2018                 cylinders = capacity;
2019                 sector_div(cylinders, dummy);
2020         }
2021
2022         /* return result */
2023         params[0] = heads;
2024         params[1] = sectors;
2025         params[2] = cylinders;
2026
2027         return 0;
2028 }
2029
2030 /**
2031  * _scsih_response_code - translation of device response code
2032  * @ioc: per adapter object
2033  * @response_code: response code returned by the device
2034  *
2035  * Return nothing.
2036  */
2037 static void
2038 _scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
2039 {
2040         char *desc;
2041
2042         switch (response_code) {
2043         case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2044                 desc = "task management request completed";
2045                 break;
2046         case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2047                 desc = "invalid frame";
2048                 break;
2049         case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2050                 desc = "task management request not supported";
2051                 break;
2052         case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2053                 desc = "task management request failed";
2054                 break;
2055         case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2056                 desc = "task management request succeeded";
2057                 break;
2058         case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2059                 desc = "invalid lun";
2060                 break;
2061         case 0xA:
2062                 desc = "overlapped tag attempted";
2063                 break;
2064         case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2065                 desc = "task queued, however not sent to target";
2066                 break;
2067         default:
2068                 desc = "unknown";
2069                 break;
2070         }
2071         pr_warn(MPT3SAS_FMT "response_code(0x%01x): %s\n",
2072                 ioc->name, response_code, desc);
2073 }
2074
2075 /**
2076  * _scsih_tm_done - tm completion routine
2077  * @ioc: per adapter object
2078  * @smid: system request message index
2079  * @msix_index: MSIX table index supplied by the OS
2080  * @reply: reply message frame(lower 32bit addr)
2081  * Context: none.
2082  *
2083  * The callback handler when using scsih_issue_tm.
2084  *
2085  * Return 1 meaning mf should be freed from _base_interrupt
2086  *        0 means the mf is freed from this function.
2087  */
2088 static u8
2089 _scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
2090 {
2091         MPI2DefaultReply_t *mpi_reply;
2092
2093         if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
2094                 return 1;
2095         if (ioc->tm_cmds.smid != smid)
2096                 return 1;
2097         mpt3sas_base_flush_reply_queues(ioc);
2098         ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
2099         mpi_reply =  mpt3sas_base_get_reply_virt_addr(ioc, reply);
2100         if (mpi_reply) {
2101                 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2102                 ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
2103         }
2104         ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
2105         complete(&ioc->tm_cmds.done);
2106         return 1;
2107 }
2108
2109 /**
2110  * mpt3sas_scsih_set_tm_flag - set per target tm_busy
2111  * @ioc: per adapter object
2112  * @handle: device handle
2113  *
2114  * During taskmangement request, we need to freeze the device queue.
2115  */
2116 void
2117 mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2118 {
2119         struct MPT3SAS_DEVICE *sas_device_priv_data;
2120         struct scsi_device *sdev;
2121         u8 skip = 0;
2122
2123         shost_for_each_device(sdev, ioc->shost) {
2124                 if (skip)
2125                         continue;
2126                 sas_device_priv_data = sdev->hostdata;
2127                 if (!sas_device_priv_data)
2128                         continue;
2129                 if (sas_device_priv_data->sas_target->handle == handle) {
2130                         sas_device_priv_data->sas_target->tm_busy = 1;
2131                         skip = 1;
2132                         ioc->ignore_loginfos = 1;
2133                 }
2134         }
2135 }
2136
2137 /**
2138  * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
2139  * @ioc: per adapter object
2140  * @handle: device handle
2141  *
2142  * During taskmangement request, we need to freeze the device queue.
2143  */
2144 void
2145 mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2146 {
2147         struct MPT3SAS_DEVICE *sas_device_priv_data;
2148         struct scsi_device *sdev;
2149         u8 skip = 0;
2150
2151         shost_for_each_device(sdev, ioc->shost) {
2152                 if (skip)
2153                         continue;
2154                 sas_device_priv_data = sdev->hostdata;
2155                 if (!sas_device_priv_data)
2156                         continue;
2157                 if (sas_device_priv_data->sas_target->handle == handle) {
2158                         sas_device_priv_data->sas_target->tm_busy = 0;
2159                         skip = 1;
2160                         ioc->ignore_loginfos = 0;
2161                 }
2162         }
2163 }
2164
2165 /**
2166  * mpt3sas_scsih_issue_tm - main routine for sending tm requests
2167  * @ioc: per adapter struct
2168  * @device_handle: device handle
2169  * @channel: the channel assigned by the OS
2170  * @id: the id assigned by the OS
2171  * @lun: lun number
2172  * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2173  * @smid_task: smid assigned to the task
2174  * @timeout: timeout in seconds
2175  * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
2176  * Context: user
2177  *
2178  * A generic API for sending task management requests to firmware.
2179  *
2180  * The callback index is set inside `ioc->tm_cb_idx`.
2181  *
2182  * Return SUCCESS or FAILED.
2183  */
2184 int
2185 mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
2186         uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
2187         enum mutex_type m_type)
2188 {
2189         Mpi2SCSITaskManagementRequest_t *mpi_request;
2190         Mpi2SCSITaskManagementReply_t *mpi_reply;
2191         u16 smid = 0;
2192         u32 ioc_state;
2193         unsigned long timeleft;
2194         struct scsiio_tracker *scsi_lookup = NULL;
2195         int rc;
2196         u16 msix_task = 0;
2197
2198         if (m_type == TM_MUTEX_ON)
2199                 mutex_lock(&ioc->tm_cmds.mutex);
2200         if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
2201                 pr_info(MPT3SAS_FMT "%s: tm_cmd busy!!!\n",
2202                     __func__, ioc->name);
2203                 rc = FAILED;
2204                 goto err_out;
2205         }
2206
2207         if (ioc->shost_recovery || ioc->remove_host ||
2208             ioc->pci_error_recovery) {
2209                 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
2210                     __func__, ioc->name);
2211                 rc = FAILED;
2212                 goto err_out;
2213         }
2214
2215         ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
2216         if (ioc_state & MPI2_DOORBELL_USED) {
2217                 dhsprintk(ioc, pr_info(MPT3SAS_FMT
2218                         "unexpected doorbell active!\n", ioc->name));
2219                 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2220                     FORCE_BIG_HAMMER);
2221                 rc = (!rc) ? SUCCESS : FAILED;
2222                 goto err_out;
2223         }
2224
2225         if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2226                 mpt3sas_base_fault_info(ioc, ioc_state &
2227                     MPI2_DOORBELL_DATA_MASK);
2228                 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2229                     FORCE_BIG_HAMMER);
2230                 rc = (!rc) ? SUCCESS : FAILED;
2231                 goto err_out;
2232         }
2233
2234         smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2235         if (!smid) {
2236                 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2237                     ioc->name, __func__);
2238                 rc = FAILED;
2239                 goto err_out;
2240         }
2241
2242         if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2243                 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2244
2245         dtmprintk(ioc, pr_info(MPT3SAS_FMT
2246                 "sending tm: handle(0x%04x), task_type(0x%02x), smid(%d)\n",
2247                 ioc->name, handle, type, smid_task));
2248         ioc->tm_cmds.status = MPT3_CMD_PENDING;
2249         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2250         ioc->tm_cmds.smid = smid;
2251         memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2252         memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
2253         mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2254         mpi_request->DevHandle = cpu_to_le16(handle);
2255         mpi_request->TaskType = type;
2256         mpi_request->TaskMID = cpu_to_le16(smid_task);
2257         int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2258         mpt3sas_scsih_set_tm_flag(ioc, handle);
2259         init_completion(&ioc->tm_cmds.done);
2260         if ((type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK) &&
2261                         (scsi_lookup->msix_io < ioc->reply_queue_count))
2262                 msix_task = scsi_lookup->msix_io;
2263         else
2264                 msix_task = 0;
2265         mpt3sas_base_put_smid_hi_priority(ioc, smid, msix_task);
2266         timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2267         if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
2268                 pr_err(MPT3SAS_FMT "%s: timeout\n",
2269                     ioc->name, __func__);
2270                 _debug_dump_mf(mpi_request,
2271                     sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2272                 if (!(ioc->tm_cmds.status & MPT3_CMD_RESET)) {
2273                         rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2274                             FORCE_BIG_HAMMER);
2275                         rc = (!rc) ? SUCCESS : FAILED;
2276                         ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2277                         mpt3sas_scsih_clear_tm_flag(ioc, handle);
2278                         goto err_out;
2279                 }
2280         }
2281
2282         if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
2283                 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
2284                 mpi_reply = ioc->tm_cmds.reply;
2285                 dtmprintk(ioc, pr_info(MPT3SAS_FMT "complete tm: " \
2286                     "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2287                     ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2288                     le32_to_cpu(mpi_reply->IOCLogInfo),
2289                     le32_to_cpu(mpi_reply->TerminationCount)));
2290                 if (ioc->logging_level & MPT_DEBUG_TM) {
2291                         _scsih_response_code(ioc, mpi_reply->ResponseCode);
2292                         if (mpi_reply->IOCStatus)
2293                                 _debug_dump_mf(mpi_request,
2294                                     sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2295                 }
2296         }
2297
2298         switch (type) {
2299         case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2300                 rc = SUCCESS;
2301                 if (scsi_lookup->scmd == NULL)
2302                         break;
2303                 rc = FAILED;
2304                 break;
2305
2306         case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2307                 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2308                         rc = FAILED;
2309                 else
2310                         rc = SUCCESS;
2311                 break;
2312         case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
2313         case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2314                 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2315                         rc = FAILED;
2316                 else
2317                         rc = SUCCESS;
2318                 break;
2319         case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2320                 rc = SUCCESS;
2321                 break;
2322         default:
2323                 rc = FAILED;
2324                 break;
2325         }
2326
2327         mpt3sas_scsih_clear_tm_flag(ioc, handle);
2328         ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2329         if (m_type == TM_MUTEX_ON)
2330                 mutex_unlock(&ioc->tm_cmds.mutex);
2331
2332         return rc;
2333
2334  err_out:
2335         if (m_type == TM_MUTEX_ON)
2336                 mutex_unlock(&ioc->tm_cmds.mutex);
2337         return rc;
2338 }
2339
2340 /**
2341  * _scsih_tm_display_info - displays info about the device
2342  * @ioc: per adapter struct
2343  * @scmd: pointer to scsi command object
2344  *
2345  * Called by task management callback handlers.
2346  */
2347 static void
2348 _scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2349 {
2350         struct scsi_target *starget = scmd->device->sdev_target;
2351         struct MPT3SAS_TARGET *priv_target = starget->hostdata;
2352         struct _sas_device *sas_device = NULL;
2353         unsigned long flags;
2354         char *device_str = NULL;
2355
2356         if (!priv_target)
2357                 return;
2358         if (ioc->hide_ir_msg)
2359                 device_str = "WarpDrive";
2360         else
2361                 device_str = "volume";
2362
2363         scsi_print_command(scmd);
2364         if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2365                 starget_printk(KERN_INFO, starget,
2366                         "%s handle(0x%04x), %s wwid(0x%016llx)\n",
2367                         device_str, priv_target->handle,
2368                     device_str, (unsigned long long)priv_target->sas_address);
2369         } else {
2370                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2371                 sas_device = __mpt3sas_get_sdev_from_target(ioc, priv_target);
2372                 if (sas_device) {
2373                         if (priv_target->flags &
2374                             MPT_TARGET_FLAGS_RAID_COMPONENT) {
2375                                 starget_printk(KERN_INFO, starget,
2376                                     "volume handle(0x%04x), "
2377                                     "volume wwid(0x%016llx)\n",
2378                                     sas_device->volume_handle,
2379                                    (unsigned long long)sas_device->volume_wwid);
2380                         }
2381                         starget_printk(KERN_INFO, starget,
2382                             "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2383                             sas_device->handle,
2384                             (unsigned long long)sas_device->sas_address,
2385                             sas_device->phy);
2386                         if (sas_device->enclosure_handle != 0)
2387                                 starget_printk(KERN_INFO, starget,
2388                                  "enclosure_logical_id(0x%016llx), slot(%d)\n",
2389                                  (unsigned long long)
2390                                  sas_device->enclosure_logical_id,
2391                                  sas_device->slot);
2392                         if (sas_device->connector_name)
2393                                 starget_printk(KERN_INFO, starget,
2394                                 "enclosure level(0x%04x),connector name(%s)\n",
2395                                  sas_device->enclosure_level,
2396                                  sas_device->connector_name);
2397
2398                         sas_device_put(sas_device);
2399                 }
2400                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2401         }
2402 }
2403
2404 /**
2405  * scsih_abort - eh threads main abort routine
2406  * @scmd: pointer to scsi command object
2407  *
2408  * Returns SUCCESS if command aborted else FAILED
2409  */
2410 int
2411 scsih_abort(struct scsi_cmnd *scmd)
2412 {
2413         struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2414         struct MPT3SAS_DEVICE *sas_device_priv_data;
2415         u16 smid;
2416         u16 handle;
2417         int r;
2418
2419         sdev_printk(KERN_INFO, scmd->device,
2420                 "attempting task abort! scmd(%p)\n", scmd);
2421         _scsih_tm_display_info(ioc, scmd);
2422
2423         sas_device_priv_data = scmd->device->hostdata;
2424         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2425                 sdev_printk(KERN_INFO, scmd->device,
2426                         "device been deleted! scmd(%p)\n", scmd);
2427                 scmd->result = DID_NO_CONNECT << 16;
2428                 scmd->scsi_done(scmd);
2429                 r = SUCCESS;
2430                 goto out;
2431         }
2432
2433         /* search for the command */
2434         smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2435         if (!smid) {
2436                 scmd->result = DID_RESET << 16;
2437                 r = SUCCESS;
2438                 goto out;
2439         }
2440
2441         /* for hidden raid components and volumes this is not supported */
2442         if (sas_device_priv_data->sas_target->flags &
2443             MPT_TARGET_FLAGS_RAID_COMPONENT ||
2444             sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2445                 scmd->result = DID_RESET << 16;
2446                 r = FAILED;
2447                 goto out;
2448         }
2449
2450         mpt3sas_halt_firmware(ioc);
2451
2452         handle = sas_device_priv_data->sas_target->handle;
2453         r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2454             scmd->device->id, scmd->device->lun,
2455             MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, TM_MUTEX_ON);
2456
2457  out:
2458         sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2459             ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2460         return r;
2461 }
2462
2463 /**
2464  * scsih_dev_reset - eh threads main device reset routine
2465  * @scmd: pointer to scsi command object
2466  *
2467  * Returns SUCCESS if command aborted else FAILED
2468  */
2469 int
2470 scsih_dev_reset(struct scsi_cmnd *scmd)
2471 {
2472         struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2473         struct MPT3SAS_DEVICE *sas_device_priv_data;
2474         struct _sas_device *sas_device = NULL;
2475         u16     handle;
2476         int r;
2477
2478         struct scsi_target *starget = scmd->device->sdev_target;
2479         struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
2480
2481         sdev_printk(KERN_INFO, scmd->device,
2482                 "attempting device reset! scmd(%p)\n", scmd);
2483         _scsih_tm_display_info(ioc, scmd);
2484
2485         sas_device_priv_data = scmd->device->hostdata;
2486         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2487                 sdev_printk(KERN_INFO, scmd->device,
2488                         "device been deleted! scmd(%p)\n", scmd);
2489                 scmd->result = DID_NO_CONNECT << 16;
2490                 scmd->scsi_done(scmd);
2491                 r = SUCCESS;
2492                 goto out;
2493         }
2494
2495         /* for hidden raid components obtain the volume_handle */
2496         handle = 0;
2497         if (sas_device_priv_data->sas_target->flags &
2498             MPT_TARGET_FLAGS_RAID_COMPONENT) {
2499                 sas_device = mpt3sas_get_sdev_from_target(ioc,
2500                                 target_priv_data);
2501                 if (sas_device)
2502                         handle = sas_device->volume_handle;
2503         } else
2504                 handle = sas_device_priv_data->sas_target->handle;
2505
2506         if (!handle) {
2507                 scmd->result = DID_RESET << 16;
2508                 r = FAILED;
2509                 goto out;
2510         }
2511
2512         r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2513             scmd->device->id, scmd->device->lun,
2514             MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, TM_MUTEX_ON);
2515
2516  out:
2517         sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2518             ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2519
2520         if (sas_device)
2521                 sas_device_put(sas_device);
2522
2523         return r;
2524 }
2525
2526 /**
2527  * scsih_target_reset - eh threads main target reset routine
2528  * @scmd: pointer to scsi command object
2529  *
2530  * Returns SUCCESS if command aborted else FAILED
2531  */
2532 int
2533 scsih_target_reset(struct scsi_cmnd *scmd)
2534 {
2535         struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2536         struct MPT3SAS_DEVICE *sas_device_priv_data;
2537         struct _sas_device *sas_device = NULL;
2538         u16     handle;
2539         int r;
2540         struct scsi_target *starget = scmd->device->sdev_target;
2541         struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
2542
2543         starget_printk(KERN_INFO, starget, "attempting target reset! scmd(%p)\n",
2544                 scmd);
2545         _scsih_tm_display_info(ioc, scmd);
2546
2547         sas_device_priv_data = scmd->device->hostdata;
2548         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2549                 starget_printk(KERN_INFO, starget, "target been deleted! scmd(%p)\n",
2550                         scmd);
2551                 scmd->result = DID_NO_CONNECT << 16;
2552                 scmd->scsi_done(scmd);
2553                 r = SUCCESS;
2554                 goto out;
2555         }
2556
2557         /* for hidden raid components obtain the volume_handle */
2558         handle = 0;
2559         if (sas_device_priv_data->sas_target->flags &
2560             MPT_TARGET_FLAGS_RAID_COMPONENT) {
2561                 sas_device = mpt3sas_get_sdev_from_target(ioc,
2562                                 target_priv_data);
2563                 if (sas_device)
2564                         handle = sas_device->volume_handle;
2565         } else
2566                 handle = sas_device_priv_data->sas_target->handle;
2567
2568         if (!handle) {
2569                 scmd->result = DID_RESET << 16;
2570                 r = FAILED;
2571                 goto out;
2572         }
2573
2574         r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2575             scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2576             30, TM_MUTEX_ON);
2577
2578  out:
2579         starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2580             ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2581
2582         if (sas_device)
2583                 sas_device_put(sas_device);
2584
2585         return r;
2586 }
2587
2588
2589 /**
2590  * scsih_host_reset - eh threads main host reset routine
2591  * @scmd: pointer to scsi command object
2592  *
2593  * Returns SUCCESS if command aborted else FAILED
2594  */
2595 int
2596 scsih_host_reset(struct scsi_cmnd *scmd)
2597 {
2598         struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2599         int r, retval;
2600
2601         pr_info(MPT3SAS_FMT "attempting host reset! scmd(%p)\n",
2602             ioc->name, scmd);
2603         scsi_print_command(scmd);
2604
2605         if (ioc->is_driver_loading) {
2606                 pr_info(MPT3SAS_FMT "Blocking the host reset\n",
2607                     ioc->name);
2608                 r = FAILED;
2609                 goto out;
2610         }
2611
2612         retval = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2613             FORCE_BIG_HAMMER);
2614         r = (retval < 0) ? FAILED : SUCCESS;
2615 out:
2616         pr_info(MPT3SAS_FMT "host reset: %s scmd(%p)\n",
2617             ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2618
2619         return r;
2620 }
2621
2622 /**
2623  * _scsih_fw_event_add - insert and queue up fw_event
2624  * @ioc: per adapter object
2625  * @fw_event: object describing the event
2626  * Context: This function will acquire ioc->fw_event_lock.
2627  *
2628  * This adds the firmware event object into link list, then queues it up to
2629  * be processed from user context.
2630  *
2631  * Return nothing.
2632  */
2633 static void
2634 _scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2635 {
2636         unsigned long flags;
2637
2638         if (ioc->firmware_event_thread == NULL)
2639                 return;
2640
2641         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2642         fw_event_work_get(fw_event);
2643         INIT_LIST_HEAD(&fw_event->list);
2644         list_add_tail(&fw_event->list, &ioc->fw_event_list);
2645         INIT_WORK(&fw_event->work, _firmware_event_work);
2646         fw_event_work_get(fw_event);
2647         queue_work(ioc->firmware_event_thread, &fw_event->work);
2648         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2649 }
2650
2651 /**
2652  * _scsih_fw_event_del_from_list - delete fw_event from the list
2653  * @ioc: per adapter object
2654  * @fw_event: object describing the event
2655  * Context: This function will acquire ioc->fw_event_lock.
2656  *
2657  * If the fw_event is on the fw_event_list, remove it and do a put.
2658  *
2659  * Return nothing.
2660  */
2661 static void
2662 _scsih_fw_event_del_from_list(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
2663         *fw_event)
2664 {
2665         unsigned long flags;
2666
2667         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2668         if (!list_empty(&fw_event->list)) {
2669                 list_del_init(&fw_event->list);
2670                 fw_event_work_put(fw_event);
2671         }
2672         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2673 }
2674
2675
2676  /**
2677  * mpt3sas_send_trigger_data_event - send event for processing trigger data
2678  * @ioc: per adapter object
2679  * @event_data: trigger event data
2680  *
2681  * Return nothing.
2682  */
2683 void
2684 mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
2685         struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
2686 {
2687         struct fw_event_work *fw_event;
2688         u16 sz;
2689
2690         if (ioc->is_driver_loading)
2691                 return;
2692         sz = sizeof(*event_data);
2693         fw_event = alloc_fw_event_work(sz);
2694         if (!fw_event)
2695                 return;
2696         fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
2697         fw_event->ioc = ioc;
2698         memcpy(fw_event->event_data, event_data, sizeof(*event_data));
2699         _scsih_fw_event_add(ioc, fw_event);
2700         fw_event_work_put(fw_event);
2701 }
2702
2703 /**
2704  * _scsih_error_recovery_delete_devices - remove devices not responding
2705  * @ioc: per adapter object
2706  *
2707  * Return nothing.
2708  */
2709 static void
2710 _scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
2711 {
2712         struct fw_event_work *fw_event;
2713
2714         if (ioc->is_driver_loading)
2715                 return;
2716         fw_event = alloc_fw_event_work(0);
2717         if (!fw_event)
2718                 return;
2719         fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
2720         fw_event->ioc = ioc;
2721         _scsih_fw_event_add(ioc, fw_event);
2722         fw_event_work_put(fw_event);
2723 }
2724
2725 /**
2726  * mpt3sas_port_enable_complete - port enable completed (fake event)
2727  * @ioc: per adapter object
2728  *
2729  * Return nothing.
2730  */
2731 void
2732 mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
2733 {
2734         struct fw_event_work *fw_event;
2735
2736         fw_event = alloc_fw_event_work(0);
2737         if (!fw_event)
2738                 return;
2739         fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
2740         fw_event->ioc = ioc;
2741         _scsih_fw_event_add(ioc, fw_event);
2742         fw_event_work_put(fw_event);
2743 }
2744
2745 static struct fw_event_work *dequeue_next_fw_event(struct MPT3SAS_ADAPTER *ioc)
2746 {
2747         unsigned long flags;
2748         struct fw_event_work *fw_event = NULL;
2749
2750         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2751         if (!list_empty(&ioc->fw_event_list)) {
2752                 fw_event = list_first_entry(&ioc->fw_event_list,
2753                                 struct fw_event_work, list);
2754                 list_del_init(&fw_event->list);
2755         }
2756         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2757
2758         return fw_event;
2759 }
2760
2761 /**
2762  * _scsih_fw_event_cleanup_queue - cleanup event queue
2763  * @ioc: per adapter object
2764  *
2765  * Walk the firmware event queue, either killing timers, or waiting
2766  * for outstanding events to complete
2767  *
2768  * Return nothing.
2769  */
2770 static void
2771 _scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
2772 {
2773         struct fw_event_work *fw_event;
2774
2775         if (list_empty(&ioc->fw_event_list) ||
2776              !ioc->firmware_event_thread || in_interrupt())
2777                 return;
2778
2779         while ((fw_event = dequeue_next_fw_event(ioc))) {
2780                 /*
2781                  * Wait on the fw_event to complete. If this returns 1, then
2782                  * the event was never executed, and we need a put for the
2783                  * reference the delayed_work had on the fw_event.
2784                  *
2785                  * If it did execute, we wait for it to finish, and the put will
2786                  * happen from _firmware_event_work()
2787                  */
2788                 if (cancel_delayed_work_sync(&fw_event->delayed_work))
2789                         fw_event_work_put(fw_event);
2790
2791                 fw_event_work_put(fw_event);
2792         }
2793 }
2794
2795 /**
2796  * _scsih_internal_device_block - block the sdev device
2797  * @sdev: per device object
2798  * @sas_device_priv_data : per device driver private data
2799  *
2800  * make sure device is blocked without error, if not
2801  * print an error
2802  */
2803 static void
2804 _scsih_internal_device_block(struct scsi_device *sdev,
2805                         struct MPT3SAS_DEVICE *sas_device_priv_data)
2806 {
2807         int r = 0;
2808
2809         sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
2810             sas_device_priv_data->sas_target->handle);
2811         sas_device_priv_data->block = 1;
2812
2813         r = scsi_internal_device_block(sdev);
2814         if (r == -EINVAL)
2815                 sdev_printk(KERN_WARNING, sdev,
2816                     "device_block failed with return(%d) for handle(0x%04x)\n",
2817                     sas_device_priv_data->sas_target->handle, r);
2818 }
2819
2820 /**
2821  * _scsih_internal_device_unblock - unblock the sdev device
2822  * @sdev: per device object
2823  * @sas_device_priv_data : per device driver private data
2824  * make sure device is unblocked without error, if not retry
2825  * by blocking and then unblocking
2826  */
2827
2828 static void
2829 _scsih_internal_device_unblock(struct scsi_device *sdev,
2830                         struct MPT3SAS_DEVICE *sas_device_priv_data)
2831 {
2832         int r = 0;
2833
2834         sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
2835             "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
2836         sas_device_priv_data->block = 0;
2837         r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2838         if (r == -EINVAL) {
2839                 /* The device has been set to SDEV_RUNNING by SD layer during
2840                  * device addition but the request queue is still stopped by
2841                  * our earlier block call. We need to perform a block again
2842                  * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */
2843
2844                 sdev_printk(KERN_WARNING, sdev,
2845                     "device_unblock failed with return(%d) for handle(0x%04x) "
2846                     "performing a block followed by an unblock\n",
2847                     sas_device_priv_data->sas_target->handle, r);
2848                 sas_device_priv_data->block = 1;
2849                 r = scsi_internal_device_block(sdev);
2850                 if (r)
2851                         sdev_printk(KERN_WARNING, sdev, "retried device_block "
2852                             "failed with return(%d) for handle(0x%04x)\n",
2853                             sas_device_priv_data->sas_target->handle, r);
2854
2855                 sas_device_priv_data->block = 0;
2856                 r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2857                 if (r)
2858                         sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
2859                             " failed with return(%d) for handle(0x%04x)\n",
2860                             sas_device_priv_data->sas_target->handle, r);
2861         }
2862 }
2863
2864 /**
2865  * _scsih_ublock_io_all_device - unblock every device
2866  * @ioc: per adapter object
2867  *
2868  * change the device state from block to running
2869  */
2870 static void
2871 _scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2872 {
2873         struct MPT3SAS_DEVICE *sas_device_priv_data;
2874         struct scsi_device *sdev;
2875
2876         shost_for_each_device(sdev, ioc->shost) {
2877                 sas_device_priv_data = sdev->hostdata;
2878                 if (!sas_device_priv_data)
2879                         continue;
2880                 if (!sas_device_priv_data->block)
2881                         continue;
2882
2883                 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2884                         "device_running, handle(0x%04x)\n",
2885                     sas_device_priv_data->sas_target->handle));
2886                 _scsih_internal_device_unblock(sdev, sas_device_priv_data);
2887         }
2888 }
2889
2890
2891 /**
2892  * _scsih_ublock_io_device - prepare device to be deleted
2893  * @ioc: per adapter object
2894  * @sas_addr: sas address
2895  *
2896  * unblock then put device in offline state
2897  */
2898 static void
2899 _scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
2900 {
2901         struct MPT3SAS_DEVICE *sas_device_priv_data;
2902         struct scsi_device *sdev;
2903
2904         shost_for_each_device(sdev, ioc->shost) {
2905                 sas_device_priv_data = sdev->hostdata;
2906                 if (!sas_device_priv_data)
2907                         continue;
2908                 if (sas_device_priv_data->sas_target->sas_address
2909                     != sas_address)
2910                         continue;
2911                 if (sas_device_priv_data->block)
2912                         _scsih_internal_device_unblock(sdev,
2913                                 sas_device_priv_data);
2914         }
2915 }
2916
2917 /**
2918  * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2919  * @ioc: per adapter object
2920  * @handle: device handle
2921  *
2922  * During device pull we need to appropiately set the sdev state.
2923  */
2924 static void
2925 _scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2926 {
2927         struct MPT3SAS_DEVICE *sas_device_priv_data;
2928         struct scsi_device *sdev;
2929
2930         shost_for_each_device(sdev, ioc->shost) {
2931                 sas_device_priv_data = sdev->hostdata;
2932                 if (!sas_device_priv_data)
2933                         continue;
2934                 if (sas_device_priv_data->block)
2935                         continue;
2936                 _scsih_internal_device_block(sdev, sas_device_priv_data);
2937         }
2938 }
2939
2940 /**
2941  * _scsih_block_io_device - set the device state to SDEV_BLOCK
2942  * @ioc: per adapter object
2943  * @handle: device handle
2944  *
2945  * During device pull we need to appropiately set the sdev state.
2946  */
2947 static void
2948 _scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2949 {
2950         struct MPT3SAS_DEVICE *sas_device_priv_data;
2951         struct scsi_device *sdev;
2952         struct _sas_device *sas_device;
2953
2954         sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
2955         if (!sas_device)
2956                 return;
2957
2958         shost_for_each_device(sdev, ioc->shost) {
2959                 sas_device_priv_data = sdev->hostdata;
2960                 if (!sas_device_priv_data)
2961                         continue;
2962                 if (sas_device_priv_data->sas_target->handle != handle)
2963                         continue;
2964                 if (sas_device_priv_data->block)
2965                         continue;
2966                 if (sas_device->pend_sas_rphy_add)
2967                         continue;
2968                 _scsih_internal_device_block(sdev, sas_device_priv_data);
2969         }
2970
2971         sas_device_put(sas_device);
2972 }
2973
2974 /**
2975  * _scsih_block_io_to_children_attached_to_ex
2976  * @ioc: per adapter object
2977  * @sas_expander: the sas_device object
2978  *
2979  * This routine set sdev state to SDEV_BLOCK for all devices
2980  * attached to this expander. This function called when expander is
2981  * pulled.
2982  */
2983 static void
2984 _scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
2985         struct _sas_node *sas_expander)
2986 {
2987         struct _sas_port *mpt3sas_port;
2988         struct _sas_device *sas_device;
2989         struct _sas_node *expander_sibling;
2990         unsigned long flags;
2991
2992         if (!sas_expander)
2993                 return;
2994
2995         list_for_each_entry(mpt3sas_port,
2996            &sas_expander->sas_port_list, port_list) {
2997                 if (mpt3sas_port->remote_identify.device_type ==
2998                     SAS_END_DEVICE) {
2999                         spin_lock_irqsave(&ioc->sas_device_lock, flags);
3000                         sas_device = __mpt3sas_get_sdev_by_addr(ioc,
3001                             mpt3sas_port->remote_identify.sas_address);
3002                         if (sas_device) {
3003                                 set_bit(sas_device->handle,
3004                                                 ioc->blocking_handles);
3005                                 sas_device_put(sas_device);
3006                         }
3007                         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3008                 }
3009         }
3010
3011         list_for_each_entry(mpt3sas_port,
3012            &sas_expander->sas_port_list, port_list) {
3013
3014                 if (mpt3sas_port->remote_identify.device_type ==
3015                     SAS_EDGE_EXPANDER_DEVICE ||
3016                     mpt3sas_port->remote_identify.device_type ==
3017                     SAS_FANOUT_EXPANDER_DEVICE) {
3018                         expander_sibling =
3019                             mpt3sas_scsih_expander_find_by_sas_address(
3020                             ioc, mpt3sas_port->remote_identify.sas_address);
3021                         _scsih_block_io_to_children_attached_to_ex(ioc,
3022                             expander_sibling);
3023                 }
3024         }
3025 }
3026
3027 /**
3028  * _scsih_block_io_to_children_attached_directly
3029  * @ioc: per adapter object
3030  * @event_data: topology change event data
3031  *
3032  * This routine set sdev state to SDEV_BLOCK for all devices
3033  * direct attached during device pull.
3034  */
3035 static void
3036 _scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
3037         Mpi2EventDataSasTopologyChangeList_t *event_data)
3038 {
3039         int i;
3040         u16 handle;
3041         u16 reason_code;
3042
3043         for (i = 0; i < event_data->NumEntries; i++) {
3044                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3045                 if (!handle)
3046                         continue;
3047                 reason_code = event_data->PHY[i].PhyStatus &
3048                     MPI2_EVENT_SAS_TOPO_RC_MASK;
3049                 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3050                         _scsih_block_io_device(ioc, handle);
3051         }
3052 }
3053
3054 /**
3055  * _scsih_tm_tr_send - send task management request
3056  * @ioc: per adapter object
3057  * @handle: device handle
3058  * Context: interrupt time.
3059  *
3060  * This code is to initiate the device removal handshake protocol
3061  * with controller firmware.  This function will issue target reset
3062  * using high priority request queue.  It will send a sas iounit
3063  * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
3064  *
3065  * This is designed to send muliple task management request at the same
3066  * time to the fifo. If the fifo is full, we will append the request,
3067  * and process it in a future completion.
3068  */
3069 static void
3070 _scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3071 {
3072         Mpi2SCSITaskManagementRequest_t *mpi_request;
3073         u16 smid;
3074         struct _sas_device *sas_device = NULL;
3075         struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
3076         u64 sas_address = 0;
3077         unsigned long flags;
3078         struct _tr_list *delayed_tr;
3079         u32 ioc_state;
3080
3081         if (ioc->remove_host) {
3082                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3083                         "%s: host has been removed: handle(0x%04x)\n",
3084                         __func__, ioc->name, handle));
3085                 return;
3086         } else if (ioc->pci_error_recovery) {
3087                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3088                         "%s: host in pci error recovery: handle(0x%04x)\n",
3089                         __func__, ioc->name,
3090                     handle));
3091                 return;
3092         }
3093         ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3094         if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3095                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3096                         "%s: host is not operational: handle(0x%04x)\n",
3097                         __func__, ioc->name,
3098                    handle));
3099                 return;
3100         }
3101
3102         /* if PD, then return */
3103         if (test_bit(handle, ioc->pd_handles))
3104                 return;
3105
3106         spin_lock_irqsave(&ioc->sas_device_lock, flags);
3107         sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
3108         if (sas_device && sas_device->starget &&
3109             sas_device->starget->hostdata) {
3110                 sas_target_priv_data = sas_device->starget->hostdata;
3111                 sas_target_priv_data->deleted = 1;
3112                 sas_address = sas_device->sas_address;
3113         }
3114         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3115
3116         if (sas_target_priv_data) {
3117                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3118                         "setting delete flag: handle(0x%04x), sas_addr(0x%016llx)\n",
3119                         ioc->name, handle,
3120                     (unsigned long long)sas_address));
3121                 if (sas_device->enclosure_handle != 0)
3122                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3123                          "setting delete flag:enclosure logical id(0x%016llx),"
3124                          " slot(%d)\n", ioc->name, (unsigned long long)
3125                           sas_device->enclosure_logical_id,
3126                           sas_device->slot));
3127                 if (sas_device->connector_name)
3128                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3129                          "setting delete flag: enclosure level(0x%04x),"
3130                          " connector name( %s)\n", ioc->name,
3131                           sas_device->enclosure_level,
3132                           sas_device->connector_name));
3133                 _scsih_ublock_io_device(ioc, sas_address);
3134                 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
3135         }
3136
3137         smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3138         if (!smid) {
3139                 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3140                 if (!delayed_tr)
3141                         goto out;
3142                 INIT_LIST_HEAD(&delayed_tr->list);
3143                 delayed_tr->handle = handle;
3144                 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3145                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3146                     "DELAYED:tr:handle(0x%04x), (open)\n",
3147                     ioc->name, handle));
3148                 goto out;
3149         }
3150
3151         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3152                 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3153                 ioc->name, handle, smid,
3154             ioc->tm_tr_cb_idx));
3155         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3156         memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3157         mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3158         mpi_request->DevHandle = cpu_to_le16(handle);
3159         mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3160         mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
3161         mpt3sas_trigger_master(ioc, MASTER_TRIGGER_DEVICE_REMOVAL);
3162
3163 out:
3164         if (sas_device)
3165                 sas_device_put(sas_device);
3166 }
3167
3168 /**
3169  * _scsih_tm_tr_complete -
3170  * @ioc: per adapter object
3171  * @smid: system request message index
3172  * @msix_index: MSIX table index supplied by the OS
3173  * @reply: reply message frame(lower 32bit addr)
3174  * Context: interrupt time.
3175  *
3176  * This is the target reset completion routine.
3177  * This code is part of the code to initiate the device removal
3178  * handshake protocol with controller firmware.
3179  * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
3180  *
3181  * Return 1 meaning mf should be freed from _base_interrupt
3182  *        0 means the mf is freed from this function.
3183  */
3184 static u8
3185 _scsih_tm_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3186         u32 reply)
3187 {
3188         u16 handle;
3189         Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3190         Mpi2SCSITaskManagementReply_t *mpi_reply =
3191             mpt3sas_base_get_reply_virt_addr(ioc, reply);
3192         Mpi2SasIoUnitControlRequest_t *mpi_request;
3193         u16 smid_sas_ctrl;
3194         u32 ioc_state;
3195
3196         if (ioc->remove_host) {
3197                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3198                         "%s: host has been removed\n", __func__, ioc->name));
3199                 return 1;
3200         } else if (ioc->pci_error_recovery) {
3201                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3202                         "%s: host in pci error recovery\n", __func__,
3203                         ioc->name));
3204                 return 1;
3205         }
3206         ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3207         if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3208                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3209                         "%s: host is not operational\n", __func__, ioc->name));
3210                 return 1;
3211         }
3212         if (unlikely(!mpi_reply)) {
3213                 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3214                     ioc->name, __FILE__, __LINE__, __func__);
3215                 return 1;
3216         }
3217         mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3218         handle = le16_to_cpu(mpi_request_tm->DevHandle);
3219         if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3220                 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3221                         "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3222                         ioc->name, handle,
3223                     le16_to_cpu(mpi_reply->DevHandle), smid));
3224                 return 0;
3225         }
3226
3227         mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
3228         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3229             "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3230             "loginfo(0x%08x), completed(%d)\n", ioc->name,
3231             handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3232             le32_to_cpu(mpi_reply->IOCLogInfo),
3233             le32_to_cpu(mpi_reply->TerminationCount)));
3234
3235         smid_sas_ctrl = mpt3sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3236         if (!smid_sas_ctrl) {
3237                 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3238                     ioc->name, __func__);
3239                 return 1;
3240         }
3241
3242         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3243                 "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3244                 ioc->name, handle, smid_sas_ctrl,
3245             ioc->tm_sas_control_cb_idx));
3246         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3247         memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3248         mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3249         mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3250         mpi_request->DevHandle = mpi_request_tm->DevHandle;
3251         mpt3sas_base_put_smid_default(ioc, smid_sas_ctrl);
3252
3253         return _scsih_check_for_pending_tm(ioc, smid);
3254 }
3255
3256
3257 /**
3258  * _scsih_sas_control_complete - completion routine
3259  * @ioc: per adapter object
3260  * @smid: system request message index
3261  * @msix_index: MSIX table index supplied by the OS
3262  * @reply: reply message frame(lower 32bit addr)
3263  * Context: interrupt time.
3264  *
3265  * This is the sas iounit control completion routine.
3266  * This code is part of the code to initiate the device removal
3267  * handshake protocol with controller firmware.
3268  *
3269  * Return 1 meaning mf should be freed from _base_interrupt
3270  *        0 means the mf is freed from this function.
3271  */
3272 static u8
3273 _scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3274         u8 msix_index, u32 reply)
3275 {
3276         Mpi2SasIoUnitControlReply_t *mpi_reply =
3277             mpt3sas_base_get_reply_virt_addr(ioc, reply);
3278
3279         if (likely(mpi_reply)) {
3280                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3281                 "sc_complete:handle(0x%04x), (open) "
3282                 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3283                 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3284                 le16_to_cpu(mpi_reply->IOCStatus),
3285                 le32_to_cpu(mpi_reply->IOCLogInfo)));
3286         } else {
3287                 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3288                     ioc->name, __FILE__, __LINE__, __func__);
3289         }
3290         return 1;
3291 }
3292
3293 /**
3294  * _scsih_tm_tr_volume_send - send target reset request for volumes
3295  * @ioc: per adapter object
3296  * @handle: device handle
3297  * Context: interrupt time.
3298  *
3299  * This is designed to send muliple task management request at the same
3300  * time to the fifo. If the fifo is full, we will append the request,
3301  * and process it in a future completion.
3302  */
3303 static void
3304 _scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3305 {
3306         Mpi2SCSITaskManagementRequest_t *mpi_request;
3307         u16 smid;
3308         struct _tr_list *delayed_tr;
3309
3310         if (ioc->shost_recovery || ioc->remove_host ||
3311             ioc->pci_error_recovery) {
3312                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3313                         "%s: host reset in progress!\n",
3314                         __func__, ioc->name));
3315                 return;
3316         }
3317
3318         smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3319         if (!smid) {
3320                 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3321                 if (!delayed_tr)
3322                         return;
3323                 INIT_LIST_HEAD(&delayed_tr->list);
3324                 delayed_tr->handle = handle;
3325                 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3326                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3327                     "DELAYED:tr:handle(0x%04x), (open)\n",
3328                     ioc->name, handle));
3329                 return;
3330         }
3331
3332         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3333                 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3334                 ioc->name, handle, smid,
3335             ioc->tm_tr_volume_cb_idx));
3336         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3337         memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3338         mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3339         mpi_request->DevHandle = cpu_to_le16(handle);
3340         mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3341         mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
3342 }
3343
3344 /**
3345  * _scsih_tm_volume_tr_complete - target reset completion
3346  * @ioc: per adapter object
3347  * @smid: system request message index
3348  * @msix_index: MSIX table index supplied by the OS
3349  * @reply: reply message frame(lower 32bit addr)
3350  * Context: interrupt time.
3351  *
3352  * Return 1 meaning mf should be freed from _base_interrupt
3353  *        0 means the mf is freed from this function.
3354  */
3355 static u8
3356 _scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3357         u8 msix_index, u32 reply)
3358 {
3359         u16 handle;
3360         Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3361         Mpi2SCSITaskManagementReply_t *mpi_reply =
3362             mpt3sas_base_get_reply_virt_addr(ioc, reply);
3363
3364         if (ioc->shost_recovery || ioc->remove_host ||
3365             ioc->pci_error_recovery) {
3366                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3367                         "%s: host reset in progress!\n",
3368                         __func__, ioc->name));
3369                 return 1;
3370         }
3371         if (unlikely(!mpi_reply)) {
3372                 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3373                     ioc->name, __FILE__, __LINE__, __func__);
3374                 return 1;
3375         }
3376
3377         mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3378         handle = le16_to_cpu(mpi_request_tm->DevHandle);
3379         if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3380                 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3381                         "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3382                         ioc->name, handle,
3383                     le16_to_cpu(mpi_reply->DevHandle), smid));
3384                 return 0;
3385         }
3386
3387         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3388             "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3389             "loginfo(0x%08x), completed(%d)\n", ioc->name,
3390             handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3391             le32_to_cpu(mpi_reply->IOCLogInfo),
3392             le32_to_cpu(mpi_reply->TerminationCount)));
3393
3394         return _scsih_check_for_pending_tm(ioc, smid);
3395 }
3396
3397
3398 /**
3399  * _scsih_check_for_pending_tm - check for pending task management
3400  * @ioc: per adapter object
3401  * @smid: system request message index
3402  *
3403  * This will check delayed target reset list, and feed the
3404  * next reqeust.
3405  *
3406  * Return 1 meaning mf should be freed from _base_interrupt
3407  *        0 means the mf is freed from this function.
3408  */
3409 static u8
3410 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3411 {
3412         struct _tr_list *delayed_tr;
3413
3414         if (!list_empty(&ioc->delayed_tr_volume_list)) {
3415                 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3416                     struct _tr_list, list);
3417                 mpt3sas_base_free_smid(ioc, smid);
3418                 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3419                 list_del(&delayed_tr->list);
3420                 kfree(delayed_tr);
3421                 return 0;
3422         }
3423
3424         if (!list_empty(&ioc->delayed_tr_list)) {
3425                 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3426                     struct _tr_list, list);
3427                 mpt3sas_base_free_smid(ioc, smid);
3428                 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3429                 list_del(&delayed_tr->list);
3430                 kfree(delayed_tr);
3431                 return 0;
3432         }
3433
3434         return 1;
3435 }
3436
3437 /**
3438  * _scsih_check_topo_delete_events - sanity check on topo events
3439  * @ioc: per adapter object
3440  * @event_data: the event data payload
3441  *
3442  * This routine added to better handle cable breaker.
3443  *
3444  * This handles the case where driver receives multiple expander
3445  * add and delete events in a single shot.  When there is a delete event
3446  * the routine will void any pending add events waiting in the event queue.
3447  *
3448  * Return nothing.
3449  */
3450 static void
3451 _scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
3452         Mpi2EventDataSasTopologyChangeList_t *event_data)
3453 {
3454         struct fw_event_work *fw_event;
3455         Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3456         u16 expander_handle;
3457         struct _sas_node *sas_expander;
3458         unsigned long flags;
3459         int i, reason_code;
3460         u16 handle;
3461
3462         for (i = 0 ; i < event_data->NumEntries; i++) {
3463                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3464                 if (!handle)
3465                         continue;
3466                 reason_code = event_data->PHY[i].PhyStatus &
3467                     MPI2_EVENT_SAS_TOPO_RC_MASK;
3468                 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3469                         _scsih_tm_tr_send(ioc, handle);
3470         }
3471
3472         expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3473         if (expander_handle < ioc->sas_hba.num_phys) {
3474                 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3475                 return;
3476         }
3477         if (event_data->ExpStatus ==
3478             MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3479                 /* put expander attached devices into blocking state */
3480                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3481                 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
3482                     expander_handle);
3483                 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3484                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3485                 do {
3486                         handle = find_first_bit(ioc->blocking_handles,
3487                             ioc->facts.MaxDevHandle);
3488                         if (handle < ioc->facts.MaxDevHandle)
3489                                 _scsih_block_io_device(ioc, handle);
3490                 } while (test_and_clear_bit(handle, ioc->blocking_handles));
3491         } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3492                 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3493
3494         if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3495                 return;
3496
3497         /* mark ignore flag for pending events */
3498         spin_lock_irqsave(&ioc->fw_event_lock, flags);
3499         list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3500                 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3501                     fw_event->ignore)
3502                         continue;
3503                 local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3504                                    fw_event->event_data;
3505                 if (local_event_data->ExpStatus ==
3506                     MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3507                     local_event_data->ExpStatus ==
3508                     MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3509                         if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3510                             expander_handle) {
3511                                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3512                                     "setting ignoring flag\n", ioc->name));
3513                                 fw_event->ignore = 1;
3514                         }
3515                 }
3516         }
3517         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3518 }
3519
3520 /**
3521  * _scsih_set_volume_delete_flag - setting volume delete flag
3522  * @ioc: per adapter object
3523  * @handle: device handle
3524  *
3525  * This returns nothing.
3526  */
3527 static void
3528 _scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3529 {
3530         struct _raid_device *raid_device;
3531         struct MPT3SAS_TARGET *sas_target_priv_data;
3532         unsigned long flags;
3533
3534         spin_lock_irqsave(&ioc->raid_device_lock, flags);
3535         raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
3536         if (raid_device && raid_device->starget &&
3537             raid_device->starget->hostdata) {
3538                 sas_target_priv_data =
3539                     raid_device->starget->hostdata;
3540                 sas_target_priv_data->deleted = 1;
3541                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3542                     "setting delete flag: handle(0x%04x), "
3543                     "wwid(0x%016llx)\n", ioc->name, handle,
3544                     (unsigned long long) raid_device->wwid));
3545         }
3546         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3547 }
3548
3549 /**
3550  * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3551  * @handle: input handle
3552  * @a: handle for volume a
3553  * @b: handle for volume b
3554  *
3555  * IR firmware only supports two raid volumes.  The purpose of this
3556  * routine is to set the volume handle in either a or b. When the given
3557  * input handle is non-zero, or when a and b have not been set before.
3558  */
3559 static void
3560 _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3561 {
3562         if (!handle || handle == *a || handle == *b)
3563                 return;
3564         if (!*a)
3565                 *a = handle;
3566         else if (!*b)
3567                 *b = handle;
3568 }
3569
3570 /**
3571  * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3572  * @ioc: per adapter object
3573  * @event_data: the event data payload
3574  * Context: interrupt time.
3575  *
3576  * This routine will send target reset to volume, followed by target
3577  * resets to the PDs. This is called when a PD has been removed, or
3578  * volume has been deleted or removed. When the target reset is sent
3579  * to volume, the PD target resets need to be queued to start upon
3580  * completion of the volume target reset.
3581  *
3582  * Return nothing.
3583  */
3584 static void
3585 _scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
3586         Mpi2EventDataIrConfigChangeList_t *event_data)
3587 {
3588         Mpi2EventIrConfigElement_t *element;
3589         int i;
3590         u16 handle, volume_handle, a, b;
3591         struct _tr_list *delayed_tr;
3592
3593         a = 0;
3594         b = 0;
3595
3596         if (ioc->is_warpdrive)
3597                 return;
3598
3599         /* Volume Resets for Deleted or Removed */
3600         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3601         for (i = 0; i < event_data->NumElements; i++, element++) {
3602                 if (le32_to_cpu(event_data->Flags) &
3603                     MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3604                         continue;
3605                 if (element->ReasonCode ==
3606                     MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3607                     element->ReasonCode ==
3608                     MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3609                         volume_handle = le16_to_cpu(element->VolDevHandle);
3610                         _scsih_set_volume_delete_flag(ioc, volume_handle);
3611                         _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3612                 }
3613         }
3614
3615         /* Volume Resets for UNHIDE events */
3616         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3617         for (i = 0; i < event_data->NumElements; i++, element++) {
3618                 if (le32_to_cpu(event_data->Flags) &
3619                     MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3620                         continue;
3621                 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3622                         volume_handle = le16_to_cpu(element->VolDevHandle);
3623                         _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3624                 }
3625         }
3626
3627         if (a)
3628                 _scsih_tm_tr_volume_send(ioc, a);
3629         if (b)
3630                 _scsih_tm_tr_volume_send(ioc, b);
3631
3632         /* PD target resets */
3633         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3634         for (i = 0; i < event_data->NumElements; i++, element++) {
3635                 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3636                         continue;
3637                 handle = le16_to_cpu(element->PhysDiskDevHandle);
3638                 volume_handle = le16_to_cpu(element->VolDevHandle);
3639                 clear_bit(handle, ioc->pd_handles);
3640                 if (!volume_handle)
3641                         _scsih_tm_tr_send(ioc, handle);
3642                 else if (volume_handle == a || volume_handle == b) {
3643                         delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3644                         BUG_ON(!delayed_tr);
3645                         INIT_LIST_HEAD(&delayed_tr->list);
3646                         delayed_tr->handle = handle;
3647                         list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3648                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3649                             "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3650                             handle));
3651                 } else
3652                         _scsih_tm_tr_send(ioc, handle);
3653         }
3654 }
3655
3656
3657 /**
3658  * _scsih_check_volume_delete_events - set delete flag for volumes
3659  * @ioc: per adapter object
3660  * @event_data: the event data payload
3661  * Context: interrupt time.
3662  *
3663  * This will handle the case when the cable connected to entire volume is
3664  * pulled. We will take care of setting the deleted flag so normal IO will
3665  * not be sent.
3666  *
3667  * Return nothing.
3668  */
3669 static void
3670 _scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
3671         Mpi2EventDataIrVolume_t *event_data)
3672 {
3673         u32 state;
3674
3675         if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3676                 return;
3677         state = le32_to_cpu(event_data->NewValue);
3678         if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3679             MPI2_RAID_VOL_STATE_FAILED)
3680                 _scsih_set_volume_delete_flag(ioc,
3681                     le16_to_cpu(event_data->VolDevHandle));
3682 }
3683
3684 /**
3685  * _scsih_temp_threshold_events - display temperature threshold exceeded events
3686  * @ioc: per adapter object
3687  * @event_data: the temp threshold event data
3688  * Context: interrupt time.
3689  *
3690  * Return nothing.
3691  */
3692 static void
3693 _scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
3694         Mpi2EventDataTemperature_t *event_data)
3695 {
3696         if (ioc->temp_sensors_count >= event_data->SensorNum) {
3697                 pr_err(MPT3SAS_FMT "Temperature Threshold flags %s%s%s%s"
3698                   " exceeded for Sensor: %d !!!\n", ioc->name,
3699                   ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
3700                   ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
3701                   ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
3702                   ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
3703                   event_data->SensorNum);
3704                 pr_err(MPT3SAS_FMT "Current Temp In Celsius: %d\n",
3705                         ioc->name, event_data->CurrentTemperature);
3706         }
3707 }
3708
3709 /**
3710  * _scsih_flush_running_cmds - completing outstanding commands.
3711  * @ioc: per adapter object
3712  *
3713  * The flushing out of all pending scmd commands following host reset,
3714  * where all IO is dropped to the floor.
3715  *
3716  * Return nothing.
3717  */
3718 static void
3719 _scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
3720 {
3721         struct scsi_cmnd *scmd;
3722         u16 smid;
3723         u16 count = 0;
3724
3725         for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3726                 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3727                 if (!scmd)
3728                         continue;
3729                 count++;
3730                 mpt3sas_base_free_smid(ioc, smid);
3731                 scsi_dma_unmap(scmd);
3732                 if (ioc->pci_error_recovery)
3733                         scmd->result = DID_NO_CONNECT << 16;
3734                 else
3735                         scmd->result = DID_RESET << 16;
3736                 scmd->scsi_done(scmd);
3737         }
3738         dtmprintk(ioc, pr_info(MPT3SAS_FMT "completing %d cmds\n",
3739             ioc->name, count));
3740 }
3741
3742 /**
3743  * _scsih_setup_eedp - setup MPI request for EEDP transfer
3744  * @ioc: per adapter object
3745  * @scmd: pointer to scsi command object
3746  * @mpi_request: pointer to the SCSI_IO reqest message frame
3747  *
3748  * Supporting protection 1 and 3.
3749  *
3750  * Returns nothing
3751  */
3752 static void
3753 _scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3754         Mpi2SCSIIORequest_t *mpi_request)
3755 {
3756         u16 eedp_flags;
3757         unsigned char prot_op = scsi_get_prot_op(scmd);
3758         unsigned char prot_type = scsi_get_prot_type(scmd);
3759         Mpi25SCSIIORequest_t *mpi_request_3v =
3760            (Mpi25SCSIIORequest_t *)mpi_request;
3761
3762         if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3763                 return;
3764
3765         if (prot_op ==  SCSI_PROT_READ_STRIP)
3766                 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3767         else if (prot_op ==  SCSI_PROT_WRITE_INSERT)
3768                 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3769         else
3770                 return;
3771
3772         switch (prot_type) {
3773         case SCSI_PROT_DIF_TYPE1:
3774         case SCSI_PROT_DIF_TYPE2:
3775
3776                 /*
3777                 * enable ref/guard checking
3778                 * auto increment ref tag
3779                 */
3780                 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3781                     MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3782                     MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3783                 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3784                     cpu_to_be32(scsi_get_lba(scmd));
3785                 break;
3786
3787         case SCSI_PROT_DIF_TYPE3:
3788
3789                 /*
3790                 * enable guard checking
3791                 */
3792                 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3793
3794                 break;
3795         }
3796
3797         mpi_request_3v->EEDPBlockSize =
3798             cpu_to_le16(scmd->device->sector_size);
3799         mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3800 }
3801
3802 /**
3803  * _scsih_eedp_error_handling - return sense code for EEDP errors
3804  * @scmd: pointer to scsi command object
3805  * @ioc_status: ioc status
3806  *
3807  * Returns nothing
3808  */
3809 static void
3810 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3811 {
3812         u8 ascq;
3813
3814         switch (ioc_status) {
3815         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3816                 ascq = 0x01;
3817                 break;
3818         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3819                 ascq = 0x02;
3820                 break;
3821         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3822                 ascq = 0x03;
3823                 break;
3824         default:
3825                 ascq = 0x00;
3826                 break;
3827         }
3828         scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10,
3829             ascq);
3830         scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
3831             SAM_STAT_CHECK_CONDITION;
3832 }
3833
3834
3835
3836 /**
3837  * scsih_qcmd - main scsi request entry point
3838  * @scmd: pointer to scsi command object
3839  * @done: function pointer to be invoked on completion
3840  *
3841  * The callback index is set inside `ioc->scsi_io_cb_idx`.
3842  *
3843  * Returns 0 on success.  If there's a failure, return either:
3844  * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3845  * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3846  */
3847 int
3848 scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
3849 {
3850         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3851         struct MPT3SAS_DEVICE *sas_device_priv_data;
3852         struct MPT3SAS_TARGET *sas_target_priv_data;
3853         struct _raid_device *raid_device;
3854         Mpi2SCSIIORequest_t *mpi_request;
3855         u32 mpi_control;
3856         u16 smid;
3857         u16 handle;
3858
3859         if (ioc->logging_level & MPT_DEBUG_SCSI)
3860                 scsi_print_command(scmd);
3861
3862         sas_device_priv_data = scmd->device->hostdata;
3863         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
3864                 scmd->result = DID_NO_CONNECT << 16;
3865                 scmd->scsi_done(scmd);
3866                 return 0;
3867         }
3868
3869         if (ioc->pci_error_recovery || ioc->remove_host) {
3870                 scmd->result = DID_NO_CONNECT << 16;
3871                 scmd->scsi_done(scmd);
3872                 return 0;
3873         }
3874
3875         sas_target_priv_data = sas_device_priv_data->sas_target;
3876
3877         /* invalid device handle */
3878         handle = sas_target_priv_data->handle;
3879         if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
3880                 scmd->result = DID_NO_CONNECT << 16;
3881                 scmd->scsi_done(scmd);
3882                 return 0;
3883         }
3884
3885
3886         /* host recovery or link resets sent via IOCTLs */
3887         if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
3888                 return SCSI_MLQUEUE_HOST_BUSY;
3889
3890         /* device has been deleted */
3891         else if (sas_target_priv_data->deleted) {
3892                 scmd->result = DID_NO_CONNECT << 16;
3893                 scmd->scsi_done(scmd);
3894                 return 0;
3895         /* device busy with task managment */
3896         } else if (sas_target_priv_data->tm_busy ||
3897             sas_device_priv_data->block)
3898                 return SCSI_MLQUEUE_DEVICE_BUSY;
3899
3900         if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3901                 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3902         else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3903                 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3904         else
3905                 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3906
3907         /* set tags */
3908         mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3909
3910         /* Make sure Device is not raid volume.
3911          * We do not expose raid functionality to upper layer for warpdrive.
3912          */
3913         if (!ioc->is_warpdrive && !scsih_is_raid(&scmd->device->sdev_gendev)
3914             && sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
3915                 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3916
3917         smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
3918         if (!smid) {
3919                 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3920                     ioc->name, __func__);
3921                 goto out;
3922         }
3923         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3924         memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
3925         _scsih_setup_eedp(ioc, scmd, mpi_request);
3926
3927         if (scmd->cmd_len == 32)
3928                 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
3929         mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3930         if (sas_device_priv_data->sas_target->flags &
3931             MPT_TARGET_FLAGS_RAID_COMPONENT)
3932                 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3933         else
3934                 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3935         mpi_request->DevHandle = cpu_to_le16(handle);
3936         mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3937         mpi_request->Control = cpu_to_le32(mpi_control);
3938         mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3939         mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3940         mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3941         mpi_request->SenseBufferLowAddress =
3942             mpt3sas_base_get_sense_buffer_dma(ioc, smid);
3943         mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3944         int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3945             mpi_request->LUN);
3946         memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3947
3948         if (mpi_request->DataLength) {
3949                 if (ioc->build_sg_scmd(ioc, scmd, smid)) {
3950                         mpt3sas_base_free_smid(ioc, smid);
3951                         goto out;
3952                 }
3953         } else
3954                 ioc->build_zero_len_sge(ioc, &mpi_request->SGL);
3955
3956         raid_device = sas_target_priv_data->raid_device;
3957         if (raid_device && raid_device->direct_io_enabled)
3958                 mpt3sas_setup_direct_io(ioc, scmd, raid_device, mpi_request,
3959                     smid);
3960
3961         if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
3962                 if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
3963                         mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
3964                             MPI25_SCSIIO_IOFLAGS_FAST_PATH);
3965                         mpt3sas_base_put_smid_fast_path(ioc, smid, handle);
3966                 } else
3967                         mpt3sas_base_put_smid_scsi_io(ioc, smid,
3968                             le16_to_cpu(mpi_request->DevHandle));
3969         } else
3970                 mpt3sas_base_put_smid_default(ioc, smid);
3971         return 0;
3972
3973  out:
3974         return SCSI_MLQUEUE_HOST_BUSY;
3975 }
3976
3977 /**
3978  * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3979  * @sense_buffer: sense data returned by target
3980  * @data: normalized skey/asc/ascq
3981  *
3982  * Return nothing.
3983  */
3984 static void
3985 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3986 {
3987         if ((sense_buffer[0] & 0x7F) >= 0x72) {
3988                 /* descriptor format */
3989                 data->skey = sense_buffer[1] & 0x0F;
3990                 data->asc = sense_buffer[2];
3991                 data->ascq = sense_buffer[3];
3992         } else {
3993                 /* fixed format */
3994                 data->skey = sense_buffer[2] & 0x0F;
3995                 data->asc = sense_buffer[12];
3996                 data->ascq = sense_buffer[13];
3997         }
3998 }
3999
4000 /**
4001  * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
4002  * @ioc: per adapter object
4003  * @scmd: pointer to scsi command object
4004  * @mpi_reply: reply mf payload returned from firmware
4005  *
4006  * scsi_status - SCSI Status code returned from target device
4007  * scsi_state - state info associated with SCSI_IO determined by ioc
4008  * ioc_status - ioc supplied status info
4009  *
4010  * Return nothing.
4011  */
4012 static void
4013 _scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4014         Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4015 {
4016         u32 response_info;
4017         u8 *response_bytes;
4018         u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4019             MPI2_IOCSTATUS_MASK;
4020         u8 scsi_state = mpi_reply->SCSIState;
4021         u8 scsi_status = mpi_reply->SCSIStatus;
4022         char *desc_ioc_state = NULL;
4023         char *desc_scsi_status = NULL;
4024         char *desc_scsi_state = ioc->tmp_string;
4025         u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4026         struct _sas_device *sas_device = NULL;
4027         struct scsi_target *starget = scmd->device->sdev_target;
4028         struct MPT3SAS_TARGET *priv_target = starget->hostdata;
4029         char *device_str = NULL;
4030
4031         if (!priv_target)
4032                 return;
4033         if (ioc->hide_ir_msg)
4034                 device_str = "WarpDrive";
4035         else
4036                 device_str = "volume";
4037
4038         if (log_info == 0x31170000)
4039                 return;
4040
4041         switch (ioc_status) {
4042         case MPI2_IOCSTATUS_SUCCESS:
4043                 desc_ioc_state = "success";
4044                 break;
4045         case MPI2_IOCSTATUS_INVALID_FUNCTION:
4046                 desc_ioc_state = "invalid function";
4047                 break;
4048         case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4049                 desc_ioc_state = "scsi recovered error";
4050                 break;
4051         case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4052                 desc_ioc_state = "scsi invalid dev handle";
4053                 break;
4054         case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4055                 desc_ioc_state = "scsi device not there";
4056                 break;
4057         case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4058                 desc_ioc_state = "scsi data overrun";
4059                 break;
4060         case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4061                 desc_ioc_state = "scsi data underrun";
4062                 break;
4063         case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4064                 desc_ioc_state = "scsi io data error";
4065                 break;
4066         case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4067                 desc_ioc_state = "scsi protocol error";
4068                 break;
4069         case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4070                 desc_ioc_state = "scsi task terminated";
4071                 break;
4072         case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4073                 desc_ioc_state = "scsi residual mismatch";
4074                 break;
4075         case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4076                 desc_ioc_state = "scsi task mgmt failed";
4077                 break;
4078         case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4079                 desc_ioc_state = "scsi ioc terminated";
4080                 break;
4081         case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4082                 desc_ioc_state = "scsi ext terminated";
4083                 break;
4084         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4085                 desc_ioc_state = "eedp guard error";
4086                 break;
4087         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4088                 desc_ioc_state = "eedp ref tag error";
4089                 break;
4090         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4091                 desc_ioc_state = "eedp app tag error";
4092                 break;
4093         default:
4094                 desc_ioc_state = "unknown";
4095                 break;
4096         }
4097
4098         switch (scsi_status) {
4099         case MPI2_SCSI_STATUS_GOOD:
4100                 desc_scsi_status = "good";
4101                 break;
4102         case MPI2_SCSI_STATUS_CHECK_CONDITION:
4103                 desc_scsi_status = "check condition";
4104                 break;
4105         case MPI2_SCSI_STATUS_CONDITION_MET:
4106                 desc_scsi_status = "condition met";
4107                 break;
4108         case MPI2_SCSI_STATUS_BUSY:
4109                 desc_scsi_status = "busy";
4110                 break;
4111         case MPI2_SCSI_STATUS_INTERMEDIATE:
4112                 desc_scsi_status = "intermediate";
4113                 break;
4114         case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4115                 desc_scsi_status = "intermediate condmet";
4116                 break;
4117         case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4118                 desc_scsi_status = "reservation conflict";
4119                 break;
4120         case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4121                 desc_scsi_status = "command terminated";
4122                 break;
4123         case MPI2_SCSI_STATUS_TASK_SET_FULL:
4124                 desc_scsi_status = "task set full";
4125                 break;
4126         case MPI2_SCSI_STATUS_ACA_ACTIVE:
4127                 desc_scsi_status = "aca active";
4128                 break;
4129         case MPI2_SCSI_STATUS_TASK_ABORTED:
4130                 desc_scsi_status = "task aborted";
4131                 break;
4132         default:
4133                 desc_scsi_status = "unknown";
4134                 break;
4135         }
4136
4137         desc_scsi_state[0] = '\0';
4138         if (!scsi_state)
4139                 desc_scsi_state = " ";
4140         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4141                 strcat(desc_scsi_state, "response info ");
4142         if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4143                 strcat(desc_scsi_state, "state terminated ");
4144         if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4145                 strcat(desc_scsi_state, "no status ");
4146         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4147                 strcat(desc_scsi_state, "autosense failed ");
4148         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4149                 strcat(desc_scsi_state, "autosense valid ");
4150
4151         scsi_print_command(scmd);
4152
4153         if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
4154                 pr_warn(MPT3SAS_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4155                     device_str, (unsigned long long)priv_target->sas_address);
4156         } else {
4157                 sas_device = mpt3sas_get_sdev_from_target(ioc, priv_target);
4158                 if (sas_device) {
4159                         pr_warn(MPT3SAS_FMT
4160                                 "\tsas_address(0x%016llx), phy(%d)\n",
4161                                 ioc->name, (unsigned long long)
4162                             sas_device->sas_address, sas_device->phy);
4163                         if (sas_device->enclosure_handle != 0)
4164                                 pr_warn(MPT3SAS_FMT
4165                                   "\tenclosure_logical_id(0x%016llx),"
4166                                   "slot(%d)\n", ioc->name,
4167                                   (unsigned long long)
4168                                   sas_device->enclosure_logical_id,
4169                                   sas_device->slot);
4170                         if (sas_device->connector_name[0])
4171                                 pr_warn(MPT3SAS_FMT
4172                                   "\tenclosure level(0x%04x),"
4173                                   " connector name( %s)\n", ioc->name,
4174                                   sas_device->enclosure_level,
4175                                   sas_device->connector_name);
4176
4177                         sas_device_put(sas_device);
4178                 }
4179         }
4180
4181         pr_warn(MPT3SAS_FMT
4182                 "\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
4183                 ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4184             desc_ioc_state, ioc_status, smid);
4185         pr_warn(MPT3SAS_FMT
4186                 "\trequest_len(%d), underflow(%d), resid(%d)\n",
4187                 ioc->name, scsi_bufflen(scmd), scmd->underflow,
4188             scsi_get_resid(scmd));
4189         pr_warn(MPT3SAS_FMT
4190                 "\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
4191                 ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4192             le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4193         pr_warn(MPT3SAS_FMT
4194                 "\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
4195                 ioc->name, desc_scsi_status,
4196             scsi_status, desc_scsi_state, scsi_state);
4197
4198         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4199                 struct sense_info data;
4200                 _scsih_normalize_sense(scmd->sense_buffer, &data);
4201                 pr_warn(MPT3SAS_FMT
4202                         "\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
4203                         ioc->name, data.skey,
4204                     data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
4205         }
4206
4207         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4208                 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4209                 response_bytes = (u8 *)&response_info;
4210                 _scsih_response_code(ioc, response_bytes[0]);
4211         }
4212 }
4213
4214 /**
4215  * _scsih_turn_on_pfa_led - illuminate PFA LED
4216  * @ioc: per adapter object
4217  * @handle: device handle
4218  * Context: process
4219  *
4220  * Return nothing.
4221  */
4222 static void
4223 _scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4224 {
4225         Mpi2SepReply_t mpi_reply;
4226         Mpi2SepRequest_t mpi_request;
4227         struct _sas_device *sas_device;
4228
4229         sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
4230         if (!sas_device)
4231                 return;
4232
4233         memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4234         mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4235         mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4236         mpi_request.SlotStatus =
4237             cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4238         mpi_request.DevHandle = cpu_to_le16(handle);
4239         mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4240         if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4241             &mpi_request)) != 0) {
4242                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4243                 __FILE__, __LINE__, __func__);
4244                 goto out;
4245         }
4246         sas_device->pfa_led_on = 1;
4247
4248         if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4249                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
4250                         "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4251                         ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4252                     le32_to_cpu(mpi_reply.IOCLogInfo)));
4253                 goto out;
4254         }
4255 out:
4256         sas_device_put(sas_device);
4257 }
4258
4259 /**
4260  * _scsih_turn_off_pfa_led - turn off Fault LED
4261  * @ioc: per adapter object
4262  * @sas_device: sas device whose PFA LED has to turned off
4263  * Context: process
4264  *
4265  * Return nothing.
4266  */
4267 static void
4268 _scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
4269         struct _sas_device *sas_device)
4270 {
4271         Mpi2SepReply_t mpi_reply;
4272         Mpi2SepRequest_t mpi_request;
4273
4274         memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4275         mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4276         mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4277         mpi_request.SlotStatus = 0;
4278         mpi_request.Slot = cpu_to_le16(sas_device->slot);
4279         mpi_request.DevHandle = 0;
4280         mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
4281         mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
4282         if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4283                 &mpi_request)) != 0) {
4284                 printk(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4285                 __FILE__, __LINE__, __func__);
4286                 return;
4287         }
4288
4289         if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4290                 dewtprintk(ioc, printk(MPT3SAS_FMT
4291                  "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4292                  ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4293                  le32_to_cpu(mpi_reply.IOCLogInfo)));
4294                 return;
4295         }
4296 }
4297
4298 /**
4299  * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
4300  * @ioc: per adapter object
4301  * @handle: device handle
4302  * Context: interrupt.
4303  *
4304  * Return nothing.
4305  */
4306 static void
4307 _scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4308 {
4309         struct fw_event_work *fw_event;
4310
4311         fw_event = alloc_fw_event_work(0);
4312         if (!fw_event)
4313                 return;
4314         fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
4315         fw_event->device_handle = handle;
4316         fw_event->ioc = ioc;
4317         _scsih_fw_event_add(ioc, fw_event);
4318         fw_event_work_put(fw_event);
4319 }
4320
4321 /**
4322  * _scsih_smart_predicted_fault - process smart errors
4323  * @ioc: per adapter object
4324  * @handle: device handle
4325  * Context: interrupt.
4326  *
4327  * Return nothing.
4328  */
4329 static void
4330 _scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4331 {
4332         struct scsi_target *starget;
4333         struct MPT3SAS_TARGET *sas_target_priv_data;
4334         Mpi2EventNotificationReply_t *event_reply;
4335         Mpi2EventDataSasDeviceStatusChange_t *event_data;
4336         struct _sas_device *sas_device;
4337         ssize_t sz;
4338         unsigned long flags;
4339
4340         /* only handle non-raid devices */
4341         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4342         sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
4343         if (!sas_device)
4344                 goto out_unlock;
4345
4346         starget = sas_device->starget;
4347         sas_target_priv_data = starget->hostdata;
4348
4349         if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4350            ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)))
4351                 goto out_unlock;
4352
4353         if (sas_device->enclosure_handle != 0)
4354                 starget_printk(KERN_INFO, starget, "predicted fault, "
4355                         "enclosure logical id(0x%016llx), slot(%d)\n",
4356                         (unsigned long long)sas_device->enclosure_logical_id,
4357                         sas_device->slot);
4358         if (sas_device->connector_name[0] != '\0')
4359                 starget_printk(KERN_WARNING, starget, "predicted fault, "
4360                         "enclosure level(0x%04x), connector name( %s)\n",
4361                         sas_device->enclosure_level,
4362                         sas_device->connector_name);
4363         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4364
4365         if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4366                 _scsih_send_event_to_turn_on_pfa_led(ioc, handle);
4367
4368         /* insert into event log */
4369         sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4370              sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4371         event_reply = kzalloc(sz, GFP_KERNEL);
4372         if (!event_reply) {
4373                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4374                     ioc->name, __FILE__, __LINE__, __func__);
4375                 goto out;
4376         }
4377
4378         event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4379         event_reply->Event =
4380             cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4381         event_reply->MsgLength = sz/4;
4382         event_reply->EventDataLength =
4383             cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4384         event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4385             event_reply->EventData;
4386         event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4387         event_data->ASC = 0x5D;
4388         event_data->DevHandle = cpu_to_le16(handle);
4389         event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4390         mpt3sas_ctl_add_to_event_log(ioc, event_reply);
4391         kfree(event_reply);
4392 out:
4393         if (sas_device)
4394                 sas_device_put(sas_device);
4395         return;
4396
4397 out_unlock:
4398         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4399         goto out;
4400 }
4401
4402 /**
4403  * _scsih_io_done - scsi request callback
4404  * @ioc: per adapter object
4405  * @smid: system request message index
4406  * @msix_index: MSIX table index supplied by the OS
4407  * @reply: reply message frame(lower 32bit addr)
4408  *
4409  * Callback handler when using _scsih_qcmd.
4410  *
4411  * Return 1 meaning mf should be freed from _base_interrupt
4412  *        0 means the mf is freed from this function.
4413  */
4414 static u8
4415 _scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4416 {
4417         Mpi2SCSIIORequest_t *mpi_request;
4418         Mpi2SCSIIOReply_t *mpi_reply;
4419         struct scsi_cmnd *scmd;
4420         u16 ioc_status;
4421         u32 xfer_cnt;
4422         u8 scsi_state;
4423         u8 scsi_status;
4424         u32 log_info;
4425         struct MPT3SAS_DEVICE *sas_device_priv_data;
4426         u32 response_code = 0;
4427         unsigned long flags;
4428
4429         mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
4430         scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
4431         if (scmd == NULL)
4432                 return 1;
4433
4434         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4435
4436         if (mpi_reply == NULL) {
4437                 scmd->result = DID_OK << 16;
4438                 goto out;
4439         }
4440
4441         sas_device_priv_data = scmd->device->hostdata;
4442         if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4443              sas_device_priv_data->sas_target->deleted) {
4444                 scmd->result = DID_NO_CONNECT << 16;
4445                 goto out;
4446         }
4447         ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4448
4449         /*
4450          * WARPDRIVE: If direct_io is set then it is directIO,
4451          * the failed direct I/O should be redirected to volume
4452          */
4453         if (mpt3sas_scsi_direct_io_get(ioc, smid) &&
4454              ((ioc_status & MPI2_IOCSTATUS_MASK)
4455               != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
4456                 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4457                 ioc->scsi_lookup[smid - 1].scmd = scmd;
4458                 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4459                 mpt3sas_scsi_direct_io_set(ioc, smid, 0);
4460                 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4461                 mpi_request->DevHandle =
4462                     cpu_to_le16(sas_device_priv_data->sas_target->handle);
4463                 mpt3sas_base_put_smid_scsi_io(ioc, smid,
4464                     sas_device_priv_data->sas_target->handle);
4465                 return 0;
4466         }
4467         /* turning off TLR */
4468         scsi_state = mpi_reply->SCSIState;
4469         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4470                 response_code =
4471                     le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
4472         if (!sas_device_priv_data->tlr_snoop_check) {
4473                 sas_device_priv_data->tlr_snoop_check++;
4474                 if (!ioc->is_warpdrive &&
4475                     !scsih_is_raid(&scmd->device->sdev_gendev) &&
4476                     sas_is_tlr_enabled(scmd->device) &&
4477                     response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4478                         sas_disable_tlr(scmd->device);
4479                         sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4480                 }
4481         }
4482
4483         xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4484         scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4485         if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4486                 log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
4487         else
4488                 log_info = 0;
4489         ioc_status &= MPI2_IOCSTATUS_MASK;
4490         scsi_status = mpi_reply->SCSIStatus;
4491
4492         if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4493             (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4494              scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4495              scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4496                 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4497         }
4498
4499         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4500                 struct sense_info data;
4501                 const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
4502                     smid);
4503                 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
4504                     le32_to_cpu(mpi_reply->SenseCount));
4505                 memcpy(scmd->sense_buffer, sense_data, sz);
4506                 _scsih_normalize_sense(scmd->sense_buffer, &data);
4507                 /* failure prediction threshold exceeded */
4508                 if (data.asc == 0x5D)
4509                         _scsih_smart_predicted_fault(ioc,
4510                             le16_to_cpu(mpi_reply->DevHandle));
4511                 mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);
4512
4513                 if ((ioc->logging_level & MPT_DEBUG_REPLY) &&
4514                      ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
4515                      (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
4516                      (scmd->sense_buffer[2] == HARDWARE_ERROR)))
4517                         _scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
4518         }
4519         switch (ioc_status) {
4520         case MPI2_IOCSTATUS_BUSY:
4521         case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4522                 scmd->result = SAM_STAT_BUSY;
4523                 break;
4524
4525         case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4526                 scmd->result = DID_NO_CONNECT << 16;
4527                 break;
4528
4529         case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4530                 if (sas_device_priv_data->block) {
4531                         scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4532                         goto out;
4533                 }
4534                 if (log_info == 0x31110630) {
4535                         if (scmd->retries > 2) {
4536                                 scmd->result = DID_NO_CONNECT << 16;
4537                                 scsi_device_set_state(scmd->device,
4538                                     SDEV_OFFLINE);
4539                         } else {
4540                                 scmd->result = DID_SOFT_ERROR << 16;
4541                                 scmd->device->expecting_cc_ua = 1;
4542                         }
4543                         break;
4544                 } else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
4545                         scmd->result = DID_RESET << 16;
4546                         break;
4547                 }
4548                 scmd->result = DID_SOFT_ERROR << 16;
4549                 break;
4550         case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4551         case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4552                 scmd->result = DID_RESET << 16;
4553                 break;
4554
4555         case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4556                 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4557                         scmd->result = DID_SOFT_ERROR << 16;
4558                 else
4559                         scmd->result = (DID_OK << 16) | scsi_status;
4560                 break;
4561
4562         case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4563                 scmd->result = (DID_OK << 16) | scsi_status;
4564
4565                 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4566                         break;
4567
4568                 if (xfer_cnt < scmd->underflow) {
4569                         if (scsi_status == SAM_STAT_BUSY)
4570                                 scmd->result = SAM_STAT_BUSY;
4571                         else
4572                                 scmd->result = DID_SOFT_ERROR << 16;
4573                 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4574                      MPI2_SCSI_STATE_NO_SCSI_STATUS))
4575                         scmd->result = DID_SOFT_ERROR << 16;
4576                 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4577                         scmd->result = DID_RESET << 16;
4578                 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4579                         mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4580                         mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4581                         scmd->result = (DRIVER_SENSE << 24) |
4582                             SAM_STAT_CHECK_CONDITION;
4583                         scmd->sense_buffer[0] = 0x70;
4584                         scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4585                         scmd->sense_buffer[12] = 0x20;
4586                         scmd->sense_buffer[13] = 0;
4587                 }
4588                 break;
4589
4590         case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4591                 scsi_set_resid(scmd, 0);
4592         case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4593         case MPI2_IOCSTATUS_SUCCESS:
4594                 scmd->result = (DID_OK << 16) | scsi_status;
4595                 if (response_code ==
4596                     MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4597                     (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4598                      MPI2_SCSI_STATE_NO_SCSI_STATUS)))
4599                         scmd->result = DID_SOFT_ERROR << 16;
4600                 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4601                         scmd->result = DID_RESET << 16;
4602                 break;
4603
4604         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4605         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4606         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4607                 _scsih_eedp_error_handling(scmd, ioc_status);
4608                 break;
4609
4610         case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4611         case MPI2_IOCSTATUS_INVALID_FUNCTION:
4612         case MPI2_IOCSTATUS_INVALID_SGL:
4613         case MPI2_IOCSTATUS_INTERNAL_ERROR:
4614         case MPI2_IOCSTATUS_INVALID_FIELD:
4615         case MPI2_IOCSTATUS_INVALID_STATE:
4616         case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4617         case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4618         default:
4619                 scmd->result = DID_SOFT_ERROR << 16;
4620                 break;
4621
4622         }
4623
4624         if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4625                 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4626
4627  out:
4628
4629         scsi_dma_unmap(scmd);
4630
4631         scmd->scsi_done(scmd);
4632         return 1;
4633 }
4634
4635 /**
4636  * _scsih_sas_host_refresh - refreshing sas host object contents
4637  * @ioc: per adapter object
4638  * Context: user
4639  *
4640  * During port enable, fw will send topology events for every device. Its
4641  * possible that the handles may change from the previous setting, so this
4642  * code keeping handles updating if changed.
4643  *
4644  * Return nothing.
4645  */
4646 static void
4647 _scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
4648 {
4649         u16 sz;
4650         u16 ioc_status;
4651         int i;
4652         Mpi2ConfigReply_t mpi_reply;
4653         Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4654         u16 attached_handle;
4655         u8 link_rate;
4656
4657         dtmprintk(ioc, pr_info(MPT3SAS_FMT
4658             "updating handles for sas_host(0x%016llx)\n",
4659             ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4660
4661         sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4662             * sizeof(Mpi2SasIOUnit0PhyData_t));
4663         sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4664         if (!sas_iounit_pg0) {
4665                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4666                     ioc->name, __FILE__, __LINE__, __func__);
4667                 return;
4668         }
4669
4670         if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4671             sas_iounit_pg0, sz)) != 0)
4672                 goto out;
4673         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4674         if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4675                 goto out;
4676         for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4677                 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4678                 if (i == 0)
4679                         ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4680                             PhyData[0].ControllerDevHandle);
4681                 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4682                 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4683                     AttachedDevHandle);
4684                 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4685                         link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4686                 mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4687                     attached_handle, i, link_rate);
4688         }
4689  out:
4690         kfree(sas_iounit_pg0);
4691 }
4692
4693 /**
4694  * _scsih_sas_host_add - create sas host object
4695  * @ioc: per adapter object
4696  *
4697  * Creating host side data object, stored in ioc->sas_hba
4698  *
4699  * Return nothing.
4700  */
4701 static void
4702 _scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
4703 {
4704         int i;
4705         Mpi2ConfigReply_t mpi_reply;
4706         Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4707         Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4708         Mpi2SasPhyPage0_t phy_pg0;
4709         Mpi2SasDevicePage0_t sas_device_pg0;
4710         Mpi2SasEnclosurePage0_t enclosure_pg0;
4711         u16 ioc_status;
4712         u16 sz;
4713         u8 device_missing_delay;
4714
4715         mpt3sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4716         if (!ioc->sas_hba.num_phys) {
4717                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4718                     ioc->name, __FILE__, __LINE__, __func__);
4719                 return;
4720         }
4721
4722         /* sas_iounit page 0 */
4723         sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4724             sizeof(Mpi2SasIOUnit0PhyData_t));
4725         sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4726         if (!sas_iounit_pg0) {
4727                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4728                     ioc->name, __FILE__, __LINE__, __func__);
4729                 return;
4730         }
4731         if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4732             sas_iounit_pg0, sz))) {
4733                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4734                     ioc->name, __FILE__, __LINE__, __func__);
4735                 goto out;
4736         }
4737         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4738             MPI2_IOCSTATUS_MASK;
4739         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4740                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4741                     ioc->name, __FILE__, __LINE__, __func__);
4742                 goto out;
4743         }
4744
4745         /* sas_iounit page 1 */
4746         sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4747             sizeof(Mpi2SasIOUnit1PhyData_t));
4748         sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4749         if (!sas_iounit_pg1) {
4750                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4751                     ioc->name, __FILE__, __LINE__, __func__);
4752                 goto out;
4753         }
4754         if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4755             sas_iounit_pg1, sz))) {
4756                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4757                     ioc->name, __FILE__, __LINE__, __func__);
4758                 goto out;
4759         }
4760         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4761             MPI2_IOCSTATUS_MASK;
4762         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4763                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4764                     ioc->name, __FILE__, __LINE__, __func__);
4765                 goto out;
4766         }
4767
4768         ioc->io_missing_delay =
4769             sas_iounit_pg1->IODeviceMissingDelay;
4770         device_missing_delay =
4771             sas_iounit_pg1->ReportDeviceMissingDelay;
4772         if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4773                 ioc->device_missing_delay = (device_missing_delay &
4774                     MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4775         else
4776                 ioc->device_missing_delay = device_missing_delay &
4777                     MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4778
4779         ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4780         ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4781             sizeof(struct _sas_phy), GFP_KERNEL);
4782         if (!ioc->sas_hba.phy) {
4783                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4784                     ioc->name, __FILE__, __LINE__, __func__);
4785                 goto out;
4786         }
4787         for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4788                 if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4789                     i))) {
4790                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4791                             ioc->name, __FILE__, __LINE__, __func__);
4792                         goto out;
4793                 }
4794                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4795                     MPI2_IOCSTATUS_MASK;
4796                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4797                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4798                             ioc->name, __FILE__, __LINE__, __func__);
4799                         goto out;
4800                 }
4801
4802                 if (i == 0)
4803                         ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4804                             PhyData[0].ControllerDevHandle);
4805                 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4806                 ioc->sas_hba.phy[i].phy_id = i;
4807                 mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4808                     phy_pg0, ioc->sas_hba.parent_dev);
4809         }
4810         if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4811             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
4812                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4813                     ioc->name, __FILE__, __LINE__, __func__);
4814                 goto out;
4815         }
4816         ioc->sas_hba.enclosure_handle =
4817             le16_to_cpu(sas_device_pg0.EnclosureHandle);
4818         ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4819         pr_info(MPT3SAS_FMT
4820                 "host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
4821                 ioc->name, ioc->sas_hba.handle,
4822             (unsigned long long) ioc->sas_hba.sas_address,
4823             ioc->sas_hba.num_phys) ;
4824
4825         if (ioc->sas_hba.enclosure_handle) {
4826                 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4827                     &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4828                    ioc->sas_hba.enclosure_handle)))
4829                         ioc->sas_hba.enclosure_logical_id =
4830                             le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4831         }
4832
4833  out:
4834         kfree(sas_iounit_pg1);
4835         kfree(sas_iounit_pg0);
4836 }
4837
4838 /**
4839  * _scsih_expander_add -  creating expander object
4840  * @ioc: per adapter object
4841  * @handle: expander handle
4842  *
4843  * Creating expander object, stored in ioc->sas_expander_list.
4844  *
4845  * Return 0 for success, else error.
4846  */
4847 static int
4848 _scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4849 {
4850         struct _sas_node *sas_expander;
4851         Mpi2ConfigReply_t mpi_reply;
4852         Mpi2ExpanderPage0_t expander_pg0;
4853         Mpi2ExpanderPage1_t expander_pg1;
4854         Mpi2SasEnclosurePage0_t enclosure_pg0;
4855         u32 ioc_status;
4856         u16 parent_handle;
4857         u64 sas_address, sas_address_parent = 0;
4858         int i;
4859         unsigned long flags;
4860         struct _sas_port *mpt3sas_port = NULL;
4861
4862         int rc = 0;
4863
4864         if (!handle)
4865                 return -1;
4866
4867         if (ioc->shost_recovery || ioc->pci_error_recovery)
4868                 return -1;
4869
4870         if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4871             MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4872                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4873                     ioc->name, __FILE__, __LINE__, __func__);
4874                 return -1;
4875         }
4876
4877         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4878             MPI2_IOCSTATUS_MASK;
4879         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4880                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4881                     ioc->name, __FILE__, __LINE__, __func__);
4882                 return -1;
4883         }
4884
4885         /* handle out of order topology events */
4886         parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
4887         if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4888             != 0) {
4889                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4890                     ioc->name, __FILE__, __LINE__, __func__);
4891                 return -1;
4892         }
4893         if (sas_address_parent != ioc->sas_hba.sas_address) {
4894                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4895                 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4896                     sas_address_parent);
4897                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4898                 if (!sas_expander) {
4899                         rc = _scsih_expander_add(ioc, parent_handle);
4900                         if (rc != 0)
4901                                 return rc;
4902                 }
4903         }
4904
4905         spin_lock_irqsave(&ioc->sas_node_lock, flags);
4906         sas_address = le64_to_cpu(expander_pg0.SASAddress);
4907         sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4908             sas_address);
4909         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4910
4911         if (sas_expander)
4912                 return 0;
4913
4914         sas_expander = kzalloc(sizeof(struct _sas_node),
4915             GFP_KERNEL);
4916         if (!sas_expander) {
4917                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4918                     ioc->name, __FILE__, __LINE__, __func__);
4919                 return -1;
4920         }
4921
4922         sas_expander->handle = handle;
4923         sas_expander->num_phys = expander_pg0.NumPhys;
4924         sas_expander->sas_address_parent = sas_address_parent;
4925         sas_expander->sas_address = sas_address;
4926
4927         pr_info(MPT3SAS_FMT "expander_add: handle(0x%04x)," \
4928             " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
4929             handle, parent_handle, (unsigned long long)
4930             sas_expander->sas_address, sas_expander->num_phys);
4931
4932         if (!sas_expander->num_phys)
4933                 goto out_fail;
4934         sas_expander->phy = kcalloc(sas_expander->num_phys,
4935             sizeof(struct _sas_phy), GFP_KERNEL);
4936         if (!sas_expander->phy) {
4937                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4938                     ioc->name, __FILE__, __LINE__, __func__);
4939                 rc = -1;
4940                 goto out_fail;
4941         }
4942
4943         INIT_LIST_HEAD(&sas_expander->sas_port_list);
4944         mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
4945             sas_address_parent);
4946         if (!mpt3sas_port) {
4947                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4948                     ioc->name, __FILE__, __LINE__, __func__);
4949                 rc = -1;
4950                 goto out_fail;
4951         }
4952         sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
4953
4954         for (i = 0 ; i < sas_expander->num_phys ; i++) {
4955                 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
4956                     &expander_pg1, i, handle))) {
4957                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4958                             ioc->name, __FILE__, __LINE__, __func__);
4959                         rc = -1;
4960                         goto out_fail;
4961                 }
4962                 sas_expander->phy[i].handle = handle;
4963                 sas_expander->phy[i].phy_id = i;
4964
4965                 if ((mpt3sas_transport_add_expander_phy(ioc,
4966                     &sas_expander->phy[i], expander_pg1,
4967                     sas_expander->parent_dev))) {
4968                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4969                             ioc->name, __FILE__, __LINE__, __func__);
4970                         rc = -1;
4971                         goto out_fail;
4972                 }
4973         }
4974
4975         if (sas_expander->enclosure_handle) {
4976                 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4977                     &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4978                    sas_expander->enclosure_handle)))
4979                         sas_expander->enclosure_logical_id =
4980                             le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4981         }
4982
4983         _scsih_expander_node_add(ioc, sas_expander);
4984          return 0;
4985
4986  out_fail:
4987
4988         if (mpt3sas_port)
4989                 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
4990                     sas_address_parent);
4991         kfree(sas_expander);
4992         return rc;
4993 }
4994
4995 /**
4996  * mpt3sas_expander_remove - removing expander object
4997  * @ioc: per adapter object
4998  * @sas_address: expander sas_address
4999  *
5000  * Return nothing.
5001  */
5002 void
5003 mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
5004 {
5005         struct _sas_node *sas_expander;
5006         unsigned long flags;
5007
5008         if (ioc->shost_recovery)
5009                 return;
5010
5011         spin_lock_irqsave(&ioc->sas_node_lock, flags);
5012         sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5013             sas_address);
5014         if (sas_expander)
5015                 list_del(&sas_expander->list);
5016         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5017         if (sas_expander)
5018                 _scsih_expander_node_remove(ioc, sas_expander);
5019 }
5020
5021 /**
5022  * _scsih_done -  internal SCSI_IO callback handler.
5023  * @ioc: per adapter object
5024  * @smid: system request message index
5025  * @msix_index: MSIX table index supplied by the OS
5026  * @reply: reply message frame(lower 32bit addr)
5027  *
5028  * Callback handler when sending internal generated SCSI_IO.
5029  * The callback index passed is `ioc->scsih_cb_idx`
5030  *
5031  * Return 1 meaning mf should be freed from _base_interrupt
5032  *        0 means the mf is freed from this function.
5033  */
5034 static u8
5035 _scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
5036 {
5037         MPI2DefaultReply_t *mpi_reply;
5038
5039         mpi_reply =  mpt3sas_base_get_reply_virt_addr(ioc, reply);
5040         if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
5041                 return 1;
5042         if (ioc->scsih_cmds.smid != smid)
5043                 return 1;
5044         ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
5045         if (mpi_reply) {
5046                 memcpy(ioc->scsih_cmds.reply, mpi_reply,
5047                     mpi_reply->MsgLength*4);
5048                 ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
5049         }
5050         ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
5051         complete(&ioc->scsih_cmds.done);
5052         return 1;
5053 }
5054
5055
5056
5057
5058 #define MPT3_MAX_LUNS (255)
5059
5060
5061 /**
5062  * _scsih_check_access_status - check access flags
5063  * @ioc: per adapter object
5064  * @sas_address: sas address
5065  * @handle: sas device handle
5066  * @access_flags: errors returned during discovery of the device
5067  *
5068  * Return 0 for success, else failure
5069  */
5070 static u8
5071 _scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
5072         u16 handle, u8 access_status)
5073 {
5074         u8 rc = 1;
5075         char *desc = NULL;
5076
5077         switch (access_status) {
5078         case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5079         case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5080                 rc = 0;
5081                 break;
5082         case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5083                 desc = "sata capability failed";
5084                 break;
5085         case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5086                 desc = "sata affiliation conflict";
5087                 break;
5088         case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5089                 desc = "route not addressable";
5090                 break;
5091         case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5092                 desc = "smp error not addressable";
5093                 break;
5094         case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5095                 desc = "device blocked";
5096                 break;
5097         case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5098         case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5099         case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5100         case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5101         case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5102         case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5103         case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5104         case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5105         case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5106         case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5107         case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5108         case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5109                 desc = "sata initialization failed";
5110                 break;
5111         default:
5112                 desc = "unknown";
5113                 break;
5114         }
5115
5116         if (!rc)
5117                 return 0;
5118
5119         pr_err(MPT3SAS_FMT
5120                 "discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
5121                 ioc->name, desc, (unsigned long long)sas_address, handle);
5122         return rc;
5123 }
5124
5125 /**
5126  * _scsih_check_device - checking device responsiveness
5127  * @ioc: per adapter object
5128  * @parent_sas_address: sas address of parent expander or sas host
5129  * @handle: attached device handle
5130  * @phy_numberv: phy number
5131  * @link_rate: new link rate
5132  *
5133  * Returns nothing.
5134  */
5135 static void
5136 _scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
5137         u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
5138 {
5139         Mpi2ConfigReply_t mpi_reply;
5140         Mpi2SasDevicePage0_t sas_device_pg0;
5141         struct _sas_device *sas_device;
5142         u32 ioc_status;
5143         unsigned long flags;
5144         u64 sas_address;
5145         struct scsi_target *starget;
5146         struct MPT3SAS_TARGET *sas_target_priv_data;
5147         u32 device_info;
5148
5149
5150         if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5151             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5152                 return;
5153
5154         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5155         if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5156                 return;
5157
5158         /* wide port handling ~ we need only handle device once for the phy that
5159          * is matched in sas device page zero
5160          */
5161         if (phy_number != sas_device_pg0.PhyNum)
5162                 return;
5163
5164         /* check if this is end device */
5165         device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5166         if (!(_scsih_is_end_device(device_info)))
5167                 return;
5168
5169         spin_lock_irqsave(&ioc->sas_device_lock, flags);
5170         sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5171         sas_device = __mpt3sas_get_sdev_by_addr(ioc,
5172             sas_address);
5173
5174         if (!sas_device)
5175                 goto out_unlock;
5176
5177         if (unlikely(sas_device->handle != handle)) {
5178                 starget = sas_device->starget;
5179                 sas_target_priv_data = starget->hostdata;
5180                 starget_printk(KERN_INFO, starget,
5181                         "handle changed from(0x%04x) to (0x%04x)!!!\n",
5182                         sas_device->handle, handle);
5183                 sas_target_priv_data->handle = handle;
5184                 sas_device->handle = handle;
5185                 if (sas_device_pg0.Flags &
5186                      MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5187                         sas_device->enclosure_level =
5188                                 le16_to_cpu(sas_device_pg0.EnclosureLevel);
5189                         memcpy(&sas_device->connector_name[0],
5190                                 &sas_device_pg0.ConnectorName[0], 4);
5191                 } else {
5192                         sas_device->enclosure_level = 0;
5193                         sas_device->connector_name[0] = '\0';
5194                 }
5195         }
5196
5197         /* check if device is present */
5198         if (!(le16_to_cpu(sas_device_pg0.Flags) &
5199             MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5200                 pr_err(MPT3SAS_FMT
5201                         "device is not present handle(0x%04x), flags!!!\n",
5202                         ioc->name, handle);
5203                 goto out_unlock;
5204         }
5205
5206         /* check if there were any issues with discovery */
5207         if (_scsih_check_access_status(ioc, sas_address, handle,
5208             sas_device_pg0.AccessStatus))
5209                 goto out_unlock;
5210
5211         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5212         _scsih_ublock_io_device(ioc, sas_address);
5213
5214         if (sas_device)
5215                 sas_device_put(sas_device);
5216         return;
5217
5218 out_unlock:
5219         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5220         if (sas_device)
5221                 sas_device_put(sas_device);
5222 }
5223
5224 /**
5225  * _scsih_add_device -  creating sas device object
5226  * @ioc: per adapter object
5227  * @handle: sas device handle
5228  * @phy_num: phy number end device attached to
5229  * @is_pd: is this hidden raid component
5230  *
5231  * Creating end device object, stored in ioc->sas_device_list.
5232  *
5233  * Returns 0 for success, non-zero for failure.
5234  */
5235 static int
5236 _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phy_num,
5237         u8 is_pd)
5238 {
5239         Mpi2ConfigReply_t mpi_reply;
5240         Mpi2SasDevicePage0_t sas_device_pg0;
5241         Mpi2SasEnclosurePage0_t enclosure_pg0;
5242         struct _sas_device *sas_device;
5243         u32 ioc_status;
5244         u64 sas_address;
5245         u32 device_info;
5246
5247         if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5248             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5249                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5250                     ioc->name, __FILE__, __LINE__, __func__);
5251                 return -1;
5252         }
5253
5254         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5255             MPI2_IOCSTATUS_MASK;
5256         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5257                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5258                     ioc->name, __FILE__, __LINE__, __func__);
5259                 return -1;
5260         }
5261
5262         /* check if this is end device */
5263         device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5264         if (!(_scsih_is_end_device(device_info)))
5265                 return -1;
5266         sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5267
5268         /* check if device is present */
5269         if (!(le16_to_cpu(sas_device_pg0.Flags) &
5270             MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5271                 pr_err(MPT3SAS_FMT "device is not present handle(0x04%x)!!!\n",
5272                         ioc->name, handle);
5273                 return -1;
5274         }
5275
5276         /* check if there were any issues with discovery */
5277         if (_scsih_check_access_status(ioc, sas_address, handle,
5278             sas_device_pg0.AccessStatus))
5279                 return -1;
5280
5281         sas_device = mpt3sas_get_sdev_by_addr(ioc,
5282                                         sas_address);
5283         if (sas_device) {
5284                 sas_device_put(sas_device);
5285                 return -1;
5286         }
5287
5288         sas_device = kzalloc(sizeof(struct _sas_device),
5289             GFP_KERNEL);
5290         if (!sas_device) {
5291                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5292                     ioc->name, __FILE__, __LINE__, __func__);
5293                 return 0;
5294         }
5295
5296         kref_init(&sas_device->refcount);
5297         sas_device->handle = handle;
5298         if (_scsih_get_sas_address(ioc,
5299             le16_to_cpu(sas_device_pg0.ParentDevHandle),
5300             &sas_device->sas_address_parent) != 0)
5301                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5302                     ioc->name, __FILE__, __LINE__, __func__);
5303         sas_device->enclosure_handle =
5304             le16_to_cpu(sas_device_pg0.EnclosureHandle);
5305         if (sas_device->enclosure_handle != 0)
5306                 sas_device->slot =
5307                     le16_to_cpu(sas_device_pg0.Slot);
5308         sas_device->device_info = device_info;
5309         sas_device->sas_address = sas_address;
5310         sas_device->phy = sas_device_pg0.PhyNum;
5311         sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
5312             MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
5313
5314         if (sas_device_pg0.Flags & MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5315                 sas_device->enclosure_level =
5316                         le16_to_cpu(sas_device_pg0.EnclosureLevel);
5317                 memcpy(&sas_device->connector_name[0],
5318                         &sas_device_pg0.ConnectorName[0], 4);
5319         } else {
5320                 sas_device->enclosure_level = 0;
5321                 sas_device->connector_name[0] = '\0';
5322         }
5323         /* get enclosure_logical_id */
5324         if (sas_device->enclosure_handle && !(mpt3sas_config_get_enclosure_pg0(
5325            ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5326            sas_device->enclosure_handle)))
5327                 sas_device->enclosure_logical_id =
5328                     le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5329
5330         /* get device name */
5331         sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5332
5333         if (ioc->wait_for_discovery_to_complete)
5334                 _scsih_sas_device_init_add(ioc, sas_device);
5335         else
5336                 _scsih_sas_device_add(ioc, sas_device);
5337
5338         sas_device_put(sas_device);
5339         return 0;
5340 }
5341
5342 /**
5343  * _scsih_remove_device -  removing sas device object
5344  * @ioc: per adapter object
5345  * @sas_device_delete: the sas_device object
5346  *
5347  * Return nothing.
5348  */
5349 static void
5350 _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
5351         struct _sas_device *sas_device)
5352 {
5353         struct MPT3SAS_TARGET *sas_target_priv_data;
5354
5355         if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
5356              (sas_device->pfa_led_on)) {
5357                 _scsih_turn_off_pfa_led(ioc, sas_device);
5358                 sas_device->pfa_led_on = 0;
5359         }
5360         dewtprintk(ioc, pr_info(MPT3SAS_FMT
5361                 "%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
5362                 ioc->name, __func__,
5363             sas_device->handle, (unsigned long long)
5364             sas_device->sas_address));
5365         if (sas_device->enclosure_handle != 0)
5366                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5367                     "%s: enter: enclosure logical id(0x%016llx), slot(%d)\n",
5368                     ioc->name, __func__,
5369                     (unsigned long long)sas_device->enclosure_logical_id,
5370                     sas_device->slot));
5371         if (sas_device->connector_name[0] != '\0')
5372                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5373                   "%s: enter: enclosure level(0x%04x), connector name( %s)\n",
5374                   ioc->name, __func__,
5375                   sas_device->enclosure_level,
5376                   sas_device->connector_name));
5377
5378         if (sas_device->starget && sas_device->starget->hostdata) {
5379                 sas_target_priv_data = sas_device->starget->hostdata;
5380                 sas_target_priv_data->deleted = 1;
5381                 _scsih_ublock_io_device(ioc, sas_device->sas_address);
5382                 sas_target_priv_data->handle =
5383                      MPT3SAS_INVALID_DEVICE_HANDLE;
5384         }
5385
5386         if (!ioc->hide_drives)
5387                 mpt3sas_transport_port_remove(ioc,
5388                     sas_device->sas_address,
5389                     sas_device->sas_address_parent);
5390
5391         pr_info(MPT3SAS_FMT
5392                 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
5393                 ioc->name, sas_device->handle,
5394             (unsigned long long) sas_device->sas_address);
5395         if (sas_device->enclosure_handle != 0)
5396                 pr_info(MPT3SAS_FMT
5397                   "removing : enclosure logical id(0x%016llx), slot(%d)\n",
5398                   ioc->name,
5399                   (unsigned long long)sas_device->enclosure_logical_id,
5400                   sas_device->slot);
5401         if (sas_device->connector_name[0] != '\0')
5402                 pr_info(MPT3SAS_FMT
5403                   "removing enclosure level(0x%04x), connector name( %s)\n",
5404                   ioc->name, sas_device->enclosure_level,
5405                   sas_device->connector_name);
5406
5407         dewtprintk(ioc, pr_info(MPT3SAS_FMT
5408                 "%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
5409                 ioc->name, __func__,
5410                 sas_device->handle, (unsigned long long)
5411                 sas_device->sas_address));
5412         if (sas_device->enclosure_handle != 0)
5413                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5414                     "%s: exit: enclosure logical id(0x%016llx), slot(%d)\n",
5415                     ioc->name, __func__,
5416                     (unsigned long long)sas_device->enclosure_logical_id,
5417                     sas_device->slot));
5418         if (sas_device->connector_name[0] != '\0')
5419                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5420                     "%s: exit: enclosure level(0x%04x), connector name(%s)\n",
5421                     ioc->name, __func__, sas_device->enclosure_level,
5422                     sas_device->connector_name));
5423 }
5424
5425 /**
5426  * _scsih_sas_topology_change_event_debug - debug for topology event
5427  * @ioc: per adapter object
5428  * @event_data: event data payload
5429  * Context: user.
5430  */
5431 static void
5432 _scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5433         Mpi2EventDataSasTopologyChangeList_t *event_data)
5434 {
5435         int i;
5436         u16 handle;
5437         u16 reason_code;
5438         u8 phy_number;
5439         char *status_str = NULL;
5440         u8 link_rate, prev_link_rate;
5441
5442         switch (event_data->ExpStatus) {
5443         case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5444                 status_str = "add";
5445                 break;
5446         case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5447                 status_str = "remove";
5448                 break;
5449         case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
5450         case 0:
5451                 status_str =  "responding";
5452                 break;
5453         case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5454                 status_str = "remove delay";
5455                 break;
5456         default:
5457                 status_str = "unknown status";
5458                 break;
5459         }
5460         pr_info(MPT3SAS_FMT "sas topology change: (%s)\n",
5461             ioc->name, status_str);
5462         pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
5463             "start_phy(%02d), count(%d)\n",
5464             le16_to_cpu(event_data->ExpanderDevHandle),
5465             le16_to_cpu(event_data->EnclosureHandle),
5466             event_data->StartPhyNum, event_data->NumEntries);
5467         for (i = 0; i < event_data->NumEntries; i++) {
5468                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5469                 if (!handle)
5470                         continue;
5471                 phy_number = event_data->StartPhyNum + i;
5472                 reason_code = event_data->PHY[i].PhyStatus &
5473                     MPI2_EVENT_SAS_TOPO_RC_MASK;
5474                 switch (reason_code) {
5475                 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5476                         status_str = "target add";
5477                         break;
5478                 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5479                         status_str = "target remove";
5480                         break;
5481                 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
5482                         status_str = "delay target remove";
5483                         break;
5484                 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5485                         status_str = "link rate change";
5486                         break;
5487                 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
5488                         status_str = "target responding";
5489                         break;
5490                 default:
5491                         status_str = "unknown";
5492                         break;
5493                 }
5494                 link_rate = event_data->PHY[i].LinkRate >> 4;
5495                 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5496                 pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
5497                     " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5498                     handle, status_str, link_rate, prev_link_rate);
5499
5500         }
5501 }
5502
5503 /**
5504  * _scsih_sas_topology_change_event - handle topology changes
5505  * @ioc: per adapter object
5506  * @fw_event: The fw_event_work object
5507  * Context: user.
5508  *
5509  */
5510 static int
5511 _scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
5512         struct fw_event_work *fw_event)
5513 {
5514         int i;
5515         u16 parent_handle, handle;
5516         u16 reason_code;
5517         u8 phy_number, max_phys;
5518         struct _sas_node *sas_expander;
5519         u64 sas_address;
5520         unsigned long flags;
5521         u8 link_rate, prev_link_rate;
5522         Mpi2EventDataSasTopologyChangeList_t *event_data =
5523                 (Mpi2EventDataSasTopologyChangeList_t *)
5524                 fw_event->event_data;
5525
5526         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5527                 _scsih_sas_topology_change_event_debug(ioc, event_data);
5528
5529         if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
5530                 return 0;
5531
5532         if (!ioc->sas_hba.num_phys)
5533                 _scsih_sas_host_add(ioc);
5534         else
5535                 _scsih_sas_host_refresh(ioc);
5536
5537         if (fw_event->ignore) {
5538                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5539                         "ignoring expander event\n", ioc->name));
5540                 return 0;
5541         }
5542
5543         parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5544
5545         /* handle expander add */
5546         if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5547                 if (_scsih_expander_add(ioc, parent_handle) != 0)
5548                         return 0;
5549
5550         spin_lock_irqsave(&ioc->sas_node_lock, flags);
5551         sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
5552             parent_handle);
5553         if (sas_expander) {
5554                 sas_address = sas_expander->sas_address;
5555                 max_phys = sas_expander->num_phys;
5556         } else if (parent_handle < ioc->sas_hba.num_phys) {
5557                 sas_address = ioc->sas_hba.sas_address;
5558                 max_phys = ioc->sas_hba.num_phys;
5559         } else {
5560                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5561                 return 0;
5562         }
5563         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5564
5565         /* handle siblings events */
5566         for (i = 0; i < event_data->NumEntries; i++) {
5567                 if (fw_event->ignore) {
5568                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
5569                                 "ignoring expander event\n", ioc->name));
5570                         return 0;
5571                 }
5572                 if (ioc->remove_host || ioc->pci_error_recovery)
5573                         return 0;
5574                 phy_number = event_data->StartPhyNum + i;
5575                 if (phy_number >= max_phys)
5576                         continue;
5577                 reason_code = event_data->PHY[i].PhyStatus &
5578                     MPI2_EVENT_SAS_TOPO_RC_MASK;
5579                 if ((event_data->PHY[i].PhyStatus &
5580                     MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5581                     MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
5582                                 continue;
5583                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5584                 if (!handle)
5585                         continue;
5586                 link_rate = event_data->PHY[i].LinkRate >> 4;
5587                 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5588                 switch (reason_code) {
5589                 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5590
5591                         if (ioc->shost_recovery)
5592                                 break;
5593
5594                         if (link_rate == prev_link_rate)
5595                                 break;
5596
5597                         mpt3sas_transport_update_links(ioc, sas_address,
5598                             handle, phy_number, link_rate);
5599
5600                         if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5601                                 break;
5602
5603                         _scsih_check_device(ioc, sas_address, handle,
5604                             phy_number, link_rate);
5605
5606
5607                 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5608
5609                         if (ioc->shost_recovery)
5610                                 break;
5611
5612                         mpt3sas_transport_update_links(ioc, sas_address,
5613                             handle, phy_number, link_rate);
5614
5615                         _scsih_add_device(ioc, handle, phy_number, 0);
5616
5617                         break;
5618                 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5619
5620                         _scsih_device_remove_by_handle(ioc, handle);
5621                         break;
5622                 }
5623         }
5624
5625         /* handle expander removal */
5626         if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5627             sas_expander)
5628                 mpt3sas_expander_remove(ioc, sas_address);
5629
5630         return 0;
5631 }
5632
5633 /**
5634  * _scsih_sas_device_status_change_event_debug - debug for device event
5635  * @event_data: event data payload
5636  * Context: user.
5637  *
5638  * Return nothing.
5639  */
5640 static void
5641 _scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5642         Mpi2EventDataSasDeviceStatusChange_t *event_data)
5643 {
5644         char *reason_str = NULL;
5645
5646         switch (event_data->ReasonCode) {
5647         case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5648                 reason_str = "smart data";
5649                 break;
5650         case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5651                 reason_str = "unsupported device discovered";
5652                 break;
5653         case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5654                 reason_str = "internal device reset";
5655                 break;
5656         case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5657                 reason_str = "internal task abort";
5658                 break;
5659         case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5660                 reason_str = "internal task abort set";
5661                 break;
5662         case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5663                 reason_str = "internal clear task set";
5664                 break;
5665         case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5666                 reason_str = "internal query task";
5667                 break;
5668         case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5669                 reason_str = "sata init failure";
5670                 break;
5671         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5672                 reason_str = "internal device reset complete";
5673                 break;
5674         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5675                 reason_str = "internal task abort complete";
5676                 break;
5677         case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5678                 reason_str = "internal async notification";
5679                 break;
5680         case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5681                 reason_str = "expander reduced functionality";
5682                 break;
5683         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5684                 reason_str = "expander reduced functionality complete";
5685                 break;
5686         default:
5687                 reason_str = "unknown reason";
5688                 break;
5689         }
5690         pr_info(MPT3SAS_FMT "device status change: (%s)\n"
5691             "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5692             ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5693             (unsigned long long)le64_to_cpu(event_data->SASAddress),
5694             le16_to_cpu(event_data->TaskTag));
5695         if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
5696                 pr_info(MPT3SAS_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
5697                     event_data->ASC, event_data->ASCQ);
5698         pr_info("\n");
5699 }
5700
5701 /**
5702  * _scsih_sas_device_status_change_event - handle device status change
5703  * @ioc: per adapter object
5704  * @fw_event: The fw_event_work object
5705  * Context: user.
5706  *
5707  * Return nothing.
5708  */
5709 static void
5710 _scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5711         struct fw_event_work *fw_event)
5712 {
5713         struct MPT3SAS_TARGET *target_priv_data;
5714         struct _sas_device *sas_device;
5715         u64 sas_address;
5716         unsigned long flags;
5717         Mpi2EventDataSasDeviceStatusChange_t *event_data =
5718                 (Mpi2EventDataSasDeviceStatusChange_t *)
5719                 fw_event->event_data;
5720
5721         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5722                 _scsih_sas_device_status_change_event_debug(ioc,
5723                      event_data);
5724
5725         /* In MPI Revision K (0xC), the internal device reset complete was
5726          * implemented, so avoid setting tm_busy flag for older firmware.
5727          */
5728         if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5729                 return;
5730
5731         if (event_data->ReasonCode !=
5732             MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
5733            event_data->ReasonCode !=
5734             MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
5735                 return;
5736
5737         spin_lock_irqsave(&ioc->sas_device_lock, flags);
5738         sas_address = le64_to_cpu(event_data->SASAddress);
5739         sas_device = __mpt3sas_get_sdev_by_addr(ioc,
5740             sas_address);
5741
5742         if (!sas_device || !sas_device->starget)
5743                 goto out;
5744
5745         target_priv_data = sas_device->starget->hostdata;
5746         if (!target_priv_data)
5747                 goto out;
5748
5749         if (event_data->ReasonCode ==
5750             MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5751                 target_priv_data->tm_busy = 1;
5752         else
5753                 target_priv_data->tm_busy = 0;
5754
5755 out:
5756         if (sas_device)
5757                 sas_device_put(sas_device);
5758
5759         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5760
5761 }
5762
5763 /**
5764  * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
5765  * event
5766  * @ioc: per adapter object
5767  * @event_data: event data payload
5768  * Context: user.
5769  *
5770  * Return nothing.
5771  */
5772 static void
5773 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5774         Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5775 {
5776         char *reason_str = NULL;
5777
5778         switch (event_data->ReasonCode) {
5779         case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5780                 reason_str = "enclosure add";
5781                 break;
5782         case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5783                 reason_str = "enclosure remove";
5784                 break;
5785         default:
5786                 reason_str = "unknown reason";
5787                 break;
5788         }
5789
5790         pr_info(MPT3SAS_FMT "enclosure status change: (%s)\n"
5791             "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5792             " number slots(%d)\n", ioc->name, reason_str,
5793             le16_to_cpu(event_data->EnclosureHandle),
5794             (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5795             le16_to_cpu(event_data->StartSlot));
5796 }
5797
5798 /**
5799  * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5800  * @ioc: per adapter object
5801  * @fw_event: The fw_event_work object
5802  * Context: user.
5803  *
5804  * Return nothing.
5805  */
5806 static void
5807 _scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5808         struct fw_event_work *fw_event)
5809 {
5810         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5811                 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
5812                      (Mpi2EventDataSasEnclDevStatusChange_t *)
5813                      fw_event->event_data);
5814 }
5815
5816 /**
5817  * _scsih_sas_broadcast_primitive_event - handle broadcast events
5818  * @ioc: per adapter object
5819  * @fw_event: The fw_event_work object
5820  * Context: user.
5821  *
5822  * Return nothing.
5823  */
5824 static void
5825 _scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
5826         struct fw_event_work *fw_event)
5827 {
5828         struct scsi_cmnd *scmd;
5829         struct scsi_device *sdev;
5830         u16 smid, handle;
5831         u32 lun;
5832         struct MPT3SAS_DEVICE *sas_device_priv_data;
5833         u32 termination_count;
5834         u32 query_count;
5835         Mpi2SCSITaskManagementReply_t *mpi_reply;
5836         Mpi2EventDataSasBroadcastPrimitive_t *event_data =
5837                 (Mpi2EventDataSasBroadcastPrimitive_t *)
5838                 fw_event->event_data;
5839         u16 ioc_status;
5840         unsigned long flags;
5841         int r;
5842         u8 max_retries = 0;
5843         u8 task_abort_retries;
5844
5845         mutex_lock(&ioc->tm_cmds.mutex);
5846         pr_info(MPT3SAS_FMT
5847                 "%s: enter: phy number(%d), width(%d)\n",
5848                 ioc->name, __func__, event_data->PhyNum,
5849              event_data->PortWidth);
5850
5851         _scsih_block_io_all_device(ioc);
5852
5853         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5854         mpi_reply = ioc->tm_cmds.reply;
5855  broadcast_aen_retry:
5856
5857         /* sanity checks for retrying this loop */
5858         if (max_retries++ == 5) {
5859                 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: giving up\n",
5860                     ioc->name, __func__));
5861                 goto out;
5862         } else if (max_retries > 1)
5863                 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: %d retry\n",
5864                     ioc->name, __func__, max_retries - 1));
5865
5866         termination_count = 0;
5867         query_count = 0;
5868         for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
5869                 if (ioc->shost_recovery)
5870                         goto out;
5871                 scmd = _scsih_scsi_lookup_get(ioc, smid);
5872                 if (!scmd)
5873                         continue;
5874                 sdev = scmd->device;
5875                 sas_device_priv_data = sdev->hostdata;
5876                 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5877                         continue;
5878                  /* skip hidden raid components */
5879                 if (sas_device_priv_data->sas_target->flags &
5880                     MPT_TARGET_FLAGS_RAID_COMPONENT)
5881                         continue;
5882                  /* skip volumes */
5883                 if (sas_device_priv_data->sas_target->flags &
5884                     MPT_TARGET_FLAGS_VOLUME)
5885                         continue;
5886
5887                 handle = sas_device_priv_data->sas_target->handle;
5888                 lun = sas_device_priv_data->lun;
5889                 query_count++;
5890
5891                 if (ioc->shost_recovery)
5892                         goto out;
5893
5894                 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5895                 r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5896                     MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30,
5897                     TM_MUTEX_OFF);
5898                 if (r == FAILED) {
5899                         sdev_printk(KERN_WARNING, sdev,
5900                             "mpt3sas_scsih_issue_tm: FAILED when sending "
5901                             "QUERY_TASK: scmd(%p)\n", scmd);
5902                         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5903                         goto broadcast_aen_retry;
5904                 }
5905                 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5906                     & MPI2_IOCSTATUS_MASK;
5907                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5908                         sdev_printk(KERN_WARNING, sdev,
5909                                 "query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
5910                                 ioc_status, scmd);
5911                         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5912                         goto broadcast_aen_retry;
5913                 }
5914
5915                 /* see if IO is still owned by IOC and target */
5916                 if (mpi_reply->ResponseCode ==
5917                      MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5918                      mpi_reply->ResponseCode ==
5919                      MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
5920                         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5921                         continue;
5922                 }
5923                 task_abort_retries = 0;
5924  tm_retry:
5925                 if (task_abort_retries++ == 60) {
5926                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
5927                             "%s: ABORT_TASK: giving up\n", ioc->name,
5928                             __func__));
5929                         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5930                         goto broadcast_aen_retry;
5931                 }
5932
5933                 if (ioc->shost_recovery)
5934                         goto out_no_lock;
5935
5936                 r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5937                     sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
5938                     TM_MUTEX_OFF);
5939                 if (r == FAILED) {
5940                         sdev_printk(KERN_WARNING, sdev,
5941                             "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
5942                             "scmd(%p)\n", scmd);
5943                         goto tm_retry;
5944                 }
5945
5946                 if (task_abort_retries > 1)
5947                         sdev_printk(KERN_WARNING, sdev,
5948                             "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5949                             " scmd(%p)\n",
5950                             task_abort_retries - 1, scmd);
5951
5952                 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
5953                 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5954         }
5955
5956         if (ioc->broadcast_aen_pending) {
5957                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5958                         "%s: loop back due to pending AEN\n",
5959                         ioc->name, __func__));
5960                  ioc->broadcast_aen_pending = 0;
5961                  goto broadcast_aen_retry;
5962         }
5963
5964  out:
5965         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5966  out_no_lock:
5967
5968         dewtprintk(ioc, pr_info(MPT3SAS_FMT
5969             "%s - exit, query_count = %d termination_count = %d\n",
5970             ioc->name, __func__, query_count, termination_count));
5971
5972         ioc->broadcast_aen_busy = 0;
5973         if (!ioc->shost_recovery)
5974                 _scsih_ublock_io_all_device(ioc);
5975         mutex_unlock(&ioc->tm_cmds.mutex);
5976 }
5977
5978 /**
5979  * _scsih_sas_discovery_event - handle discovery events
5980  * @ioc: per adapter object
5981  * @fw_event: The fw_event_work object
5982  * Context: user.
5983  *
5984  * Return nothing.
5985  */
5986 static void
5987 _scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
5988         struct fw_event_work *fw_event)
5989 {
5990         Mpi2EventDataSasDiscovery_t *event_data =
5991                 (Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
5992
5993         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
5994                 pr_info(MPT3SAS_FMT "discovery event: (%s)", ioc->name,
5995                     (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5996                     "start" : "stop");
5997                 if (event_data->DiscoveryStatus)
5998                         pr_info("discovery_status(0x%08x)",
5999                             le32_to_cpu(event_data->DiscoveryStatus));
6000                 pr_info("\n");
6001         }
6002
6003         if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
6004             !ioc->sas_hba.num_phys) {
6005                 if (disable_discovery > 0 && ioc->shost_recovery) {
6006                         /* Wait for the reset to complete */
6007                         while (ioc->shost_recovery)
6008                                 ssleep(1);
6009                 }
6010                 _scsih_sas_host_add(ioc);
6011         }
6012 }
6013
6014 /**
6015  * _scsih_ir_fastpath - turn on fastpath for IR physdisk
6016  * @ioc: per adapter object
6017  * @handle: device handle for physical disk
6018  * @phys_disk_num: physical disk number
6019  *
6020  * Return 0 for success, else failure.
6021  */
6022 static int
6023 _scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
6024 {
6025         Mpi2RaidActionRequest_t *mpi_request;
6026         Mpi2RaidActionReply_t *mpi_reply;
6027         u16 smid;
6028         u8 issue_reset = 0;
6029         int rc = 0;
6030         u16 ioc_status;
6031         u32 log_info;
6032
6033         if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
6034                 return rc;
6035
6036         mutex_lock(&ioc->scsih_cmds.mutex);
6037
6038         if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
6039                 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
6040                     ioc->name, __func__);
6041                 rc = -EAGAIN;
6042                 goto out;
6043         }
6044         ioc->scsih_cmds.status = MPT3_CMD_PENDING;
6045
6046         smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6047         if (!smid) {
6048                 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
6049                     ioc->name, __func__);
6050                 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6051                 rc = -EAGAIN;
6052                 goto out;
6053         }
6054
6055         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
6056         ioc->scsih_cmds.smid = smid;
6057         memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6058
6059         mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6060         mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
6061         mpi_request->PhysDiskNum = phys_disk_num;
6062
6063         dewtprintk(ioc, pr_info(MPT3SAS_FMT "IR RAID_ACTION: turning fast "\
6064             "path on for handle(0x%04x), phys_disk_num (0x%02x)\n", ioc->name,
6065             handle, phys_disk_num));
6066
6067         init_completion(&ioc->scsih_cmds.done);
6068         mpt3sas_base_put_smid_default(ioc, smid);
6069         wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6070
6071         if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
6072                 pr_err(MPT3SAS_FMT "%s: timeout\n",
6073                     ioc->name, __func__);
6074                 if (!(ioc->scsih_cmds.status & MPT3_CMD_RESET))
6075                         issue_reset = 1;
6076                 rc = -EFAULT;
6077                 goto out;
6078         }
6079
6080         if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
6081
6082                 mpi_reply = ioc->scsih_cmds.reply;
6083                 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
6084                 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
6085                         log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
6086                 else
6087                         log_info = 0;
6088                 ioc_status &= MPI2_IOCSTATUS_MASK;
6089                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6090                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
6091                             "IR RAID_ACTION: failed: ioc_status(0x%04x), "
6092                             "loginfo(0x%08x)!!!\n", ioc->name, ioc_status,
6093                             log_info));
6094                         rc = -EFAULT;
6095                 } else
6096                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
6097                             "IR RAID_ACTION: completed successfully\n",
6098                             ioc->name));
6099         }
6100
6101  out:
6102         ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6103         mutex_unlock(&ioc->scsih_cmds.mutex);
6104
6105         if (issue_reset)
6106                 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
6107                     FORCE_BIG_HAMMER);
6108         return rc;
6109 }
6110
6111 /**
6112  * _scsih_reprobe_lun - reprobing lun
6113  * @sdev: scsi device struct
6114  * @no_uld_attach: sdev->no_uld_attach flag setting
6115  *
6116  **/
6117 static void
6118 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
6119 {
6120         int rc;
6121         sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6122         sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6123             sdev->no_uld_attach ? "hidding" : "exposing");
6124         rc = scsi_device_reprobe(sdev);
6125 }
6126
6127 /**
6128  * _scsih_sas_volume_add - add new volume
6129  * @ioc: per adapter object
6130  * @element: IR config element data
6131  * Context: user.
6132  *
6133  * Return nothing.
6134  */
6135 static void
6136 _scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
6137         Mpi2EventIrConfigElement_t *element)
6138 {
6139         struct _raid_device *raid_device;
6140         unsigned long flags;
6141         u64 wwid;
6142         u16 handle = le16_to_cpu(element->VolDevHandle);
6143         int rc;
6144
6145         mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6146         if (!wwid) {
6147                 pr_err(MPT3SAS_FMT
6148                     "failure at %s:%d/%s()!\n", ioc->name,
6149                     __FILE__, __LINE__, __func__);
6150                 return;
6151         }
6152
6153         spin_lock_irqsave(&ioc->raid_device_lock, flags);
6154         raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6155         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6156
6157         if (raid_device)
6158                 return;
6159
6160         raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6161         if (!raid_device) {
6162                 pr_err(MPT3SAS_FMT
6163                     "failure at %s:%d/%s()!\n", ioc->name,
6164                     __FILE__, __LINE__, __func__);
6165                 return;
6166         }
6167
6168         raid_device->id = ioc->sas_id++;
6169         raid_device->channel = RAID_CHANNEL;
6170         raid_device->handle = handle;
6171         raid_device->wwid = wwid;
6172         _scsih_raid_device_add(ioc, raid_device);
6173         if (!ioc->wait_for_discovery_to_complete) {
6174                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6175                     raid_device->id, 0);
6176                 if (rc)
6177                         _scsih_raid_device_remove(ioc, raid_device);
6178         } else {
6179                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6180                 _scsih_determine_boot_device(ioc, raid_device, 1);
6181                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6182         }
6183 }
6184
6185 /**
6186  * _scsih_sas_volume_delete - delete volume
6187  * @ioc: per adapter object
6188  * @handle: volume device handle
6189  * Context: user.
6190  *
6191  * Return nothing.
6192  */
6193 static void
6194 _scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
6195 {
6196         struct _raid_device *raid_device;
6197         unsigned long flags;
6198         struct MPT3SAS_TARGET *sas_target_priv_data;
6199         struct scsi_target *starget = NULL;
6200
6201         spin_lock_irqsave(&ioc->raid_device_lock, flags);
6202         raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
6203         if (raid_device) {
6204                 if (raid_device->starget) {
6205                         starget = raid_device->starget;
6206                         sas_target_priv_data = starget->hostdata;
6207                         sas_target_priv_data->deleted = 1;
6208                 }
6209                 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
6210                         ioc->name,  raid_device->handle,
6211                     (unsigned long long) raid_device->wwid);
6212                 list_del(&raid_device->list);
6213                 kfree(raid_device);
6214         }
6215         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6216         if (starget)
6217                 scsi_remove_target(&starget->dev);
6218 }
6219
6220 /**
6221  * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6222  * @ioc: per adapter object
6223  * @element: IR config element data
6224  * Context: user.
6225  *
6226  * Return nothing.
6227  */
6228 static void
6229 _scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
6230         Mpi2EventIrConfigElement_t *element)
6231 {
6232         struct _sas_device *sas_device;
6233         struct scsi_target *starget = NULL;
6234         struct MPT3SAS_TARGET *sas_target_priv_data;
6235         unsigned long flags;
6236         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6237
6238         spin_lock_irqsave(&ioc->sas_device_lock, flags);
6239         sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
6240         if (sas_device) {
6241                 sas_device->volume_handle = 0;
6242                 sas_device->volume_wwid = 0;
6243                 clear_bit(handle, ioc->pd_handles);
6244                 if (sas_device->starget && sas_device->starget->hostdata) {
6245                         starget = sas_device->starget;
6246                         sas_target_priv_data = starget->hostdata;
6247                         sas_target_priv_data->flags &=
6248                             ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6249                 }
6250         }
6251         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6252         if (!sas_device)
6253                 return;
6254
6255         /* exposing raid component */
6256         if (starget)
6257                 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
6258
6259         sas_device_put(sas_device);
6260 }
6261
6262 /**
6263  * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6264  * @ioc: per adapter object
6265  * @element: IR config element data
6266  * Context: user.
6267  *
6268  * Return nothing.
6269  */
6270 static void
6271 _scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
6272         Mpi2EventIrConfigElement_t *element)
6273 {
6274         struct _sas_device *sas_device;
6275         struct scsi_target *starget = NULL;
6276         struct MPT3SAS_TARGET *sas_target_priv_data;
6277         unsigned long flags;
6278         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6279         u16 volume_handle = 0;
6280         u64 volume_wwid = 0;
6281
6282         mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
6283         if (volume_handle)
6284                 mpt3sas_config_get_volume_wwid(ioc, volume_handle,
6285                     &volume_wwid);
6286
6287         spin_lock_irqsave(&ioc->sas_device_lock, flags);
6288         sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
6289         if (sas_device) {
6290                 set_bit(handle, ioc->pd_handles);
6291                 if (sas_device->starget && sas_device->starget->hostdata) {
6292                         starget = sas_device->starget;
6293                         sas_target_priv_data = starget->hostdata;
6294                         sas_target_priv_data->flags |=
6295                             MPT_TARGET_FLAGS_RAID_COMPONENT;
6296                         sas_device->volume_handle = volume_handle;
6297                         sas_device->volume_wwid = volume_wwid;
6298                 }
6299         }
6300         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6301         if (!sas_device)
6302                 return;
6303
6304         /* hiding raid component */
6305         _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6306
6307         if (starget)
6308                 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
6309
6310         sas_device_put(sas_device);
6311 }
6312
6313 /**
6314  * _scsih_sas_pd_delete - delete pd component
6315  * @ioc: per adapter object
6316  * @element: IR config element data
6317  * Context: user.
6318  *
6319  * Return nothing.
6320  */
6321 static void
6322 _scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
6323         Mpi2EventIrConfigElement_t *element)
6324 {
6325         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6326
6327         _scsih_device_remove_by_handle(ioc, handle);
6328 }
6329
6330 /**
6331  * _scsih_sas_pd_add - remove pd component
6332  * @ioc: per adapter object
6333  * @element: IR config element data
6334  * Context: user.
6335  *
6336  * Return nothing.
6337  */
6338 static void
6339 _scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
6340         Mpi2EventIrConfigElement_t *element)
6341 {
6342         struct _sas_device *sas_device;
6343         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6344         Mpi2ConfigReply_t mpi_reply;
6345         Mpi2SasDevicePage0_t sas_device_pg0;
6346         u32 ioc_status;
6347         u64 sas_address;
6348         u16 parent_handle;
6349
6350         set_bit(handle, ioc->pd_handles);
6351
6352         sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6353         if (sas_device) {
6354                 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6355                 sas_device_put(sas_device);
6356                 return;
6357         }
6358
6359         if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6360             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6361                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6362                     ioc->name, __FILE__, __LINE__, __func__);
6363                 return;
6364         }
6365
6366         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6367             MPI2_IOCSTATUS_MASK;
6368         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6369                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6370                     ioc->name, __FILE__, __LINE__, __func__);
6371                 return;
6372         }
6373
6374         parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6375         if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6376                 mpt3sas_transport_update_links(ioc, sas_address, handle,
6377                     sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6378
6379         _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6380         _scsih_add_device(ioc, handle, 0, 1);
6381 }
6382
6383 /**
6384  * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6385  * @ioc: per adapter object
6386  * @event_data: event data payload
6387  * Context: user.
6388  *
6389  * Return nothing.
6390  */
6391 static void
6392 _scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
6393         Mpi2EventDataIrConfigChangeList_t *event_data)
6394 {
6395         Mpi2EventIrConfigElement_t *element;
6396         u8 element_type;
6397         int i;
6398         char *reason_str = NULL, *element_str = NULL;
6399
6400         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6401
6402         pr_info(MPT3SAS_FMT "raid config change: (%s), elements(%d)\n",
6403             ioc->name, (le32_to_cpu(event_data->Flags) &
6404             MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6405             "foreign" : "native", event_data->NumElements);
6406         for (i = 0; i < event_data->NumElements; i++, element++) {
6407                 switch (element->ReasonCode) {
6408                 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6409                         reason_str = "add";
6410                         break;
6411                 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6412                         reason_str = "remove";
6413                         break;
6414                 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6415                         reason_str = "no change";
6416                         break;
6417                 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6418                         reason_str = "hide";
6419                         break;
6420                 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6421                         reason_str = "unhide";
6422                         break;
6423                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6424                         reason_str = "volume_created";
6425                         break;
6426                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6427                         reason_str = "volume_deleted";
6428                         break;
6429                 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6430                         reason_str = "pd_created";
6431                         break;
6432                 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6433                         reason_str = "pd_deleted";
6434                         break;
6435                 default:
6436                         reason_str = "unknown reason";
6437                         break;
6438                 }
6439                 element_type = le16_to_cpu(element->ElementFlags) &
6440                     MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6441                 switch (element_type) {
6442                 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6443                         element_str = "volume";
6444                         break;
6445                 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6446                         element_str = "phys disk";
6447                         break;
6448                 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6449                         element_str = "hot spare";
6450                         break;
6451                 default:
6452                         element_str = "unknown element";
6453                         break;
6454                 }
6455                 pr_info("\t(%s:%s), vol handle(0x%04x), " \
6456                     "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6457                     reason_str, le16_to_cpu(element->VolDevHandle),
6458                     le16_to_cpu(element->PhysDiskDevHandle),
6459                     element->PhysDiskNum);
6460         }
6461 }
6462
6463 /**
6464  * _scsih_sas_ir_config_change_event - handle ir configuration change events
6465  * @ioc: per adapter object
6466  * @fw_event: The fw_event_work object
6467  * Context: user.
6468  *
6469  * Return nothing.
6470  */
6471 static void
6472 _scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
6473         struct fw_event_work *fw_event)
6474 {
6475         Mpi2EventIrConfigElement_t *element;
6476         int i;
6477         u8 foreign_config;
6478         Mpi2EventDataIrConfigChangeList_t *event_data =
6479                 (Mpi2EventDataIrConfigChangeList_t *)
6480                 fw_event->event_data;
6481
6482         if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
6483              (!ioc->hide_ir_msg))
6484                 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6485
6486         foreign_config = (le32_to_cpu(event_data->Flags) &
6487             MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
6488
6489         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6490         if (ioc->shost_recovery &&
6491             ioc->hba_mpi_version_belonged != MPI2_VERSION) {
6492                 for (i = 0; i < event_data->NumElements; i++, element++) {
6493                         if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
6494                                 _scsih_ir_fastpath(ioc,
6495                                         le16_to_cpu(element->PhysDiskDevHandle),
6496                                         element->PhysDiskNum);
6497                 }
6498                 return;
6499         }
6500
6501         for (i = 0; i < event_data->NumElements; i++, element++) {
6502
6503                 switch (element->ReasonCode) {
6504                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6505                 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6506                         if (!foreign_config)
6507                                 _scsih_sas_volume_add(ioc, element);
6508                         break;
6509                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6510                 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6511                         if (!foreign_config)
6512                                 _scsih_sas_volume_delete(ioc,
6513                                     le16_to_cpu(element->VolDevHandle));
6514                         break;
6515                 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6516                         if (!ioc->is_warpdrive)
6517                                 _scsih_sas_pd_hide(ioc, element);
6518                         break;
6519                 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6520                         if (!ioc->is_warpdrive)
6521                                 _scsih_sas_pd_expose(ioc, element);
6522                         break;
6523                 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6524                         if (!ioc->is_warpdrive)
6525                                 _scsih_sas_pd_add(ioc, element);
6526                         break;
6527                 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6528                         if (!ioc->is_warpdrive)
6529                                 _scsih_sas_pd_delete(ioc, element);
6530                         break;
6531                 }
6532         }
6533 }
6534
6535 /**
6536  * _scsih_sas_ir_volume_event - IR volume event
6537  * @ioc: per adapter object
6538  * @fw_event: The fw_event_work object
6539  * Context: user.
6540  *
6541  * Return nothing.
6542  */
6543 static void
6544 _scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
6545         struct fw_event_work *fw_event)
6546 {
6547         u64 wwid;
6548         unsigned long flags;
6549         struct _raid_device *raid_device;
6550         u16 handle;
6551         u32 state;
6552         int rc;
6553         Mpi2EventDataIrVolume_t *event_data =
6554                 (Mpi2EventDataIrVolume_t *) fw_event->event_data;
6555
6556         if (ioc->shost_recovery)
6557                 return;
6558
6559         if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6560                 return;
6561
6562         handle = le16_to_cpu(event_data->VolDevHandle);
6563         state = le32_to_cpu(event_data->NewValue);
6564         if (!ioc->hide_ir_msg)
6565                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6566                     "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6567                     ioc->name, __func__,  handle,
6568                     le32_to_cpu(event_data->PreviousValue), state));
6569         switch (state) {
6570         case MPI2_RAID_VOL_STATE_MISSING:
6571         case MPI2_RAID_VOL_STATE_FAILED:
6572                 _scsih_sas_volume_delete(ioc, handle);
6573                 break;
6574
6575         case MPI2_RAID_VOL_STATE_ONLINE:
6576         case MPI2_RAID_VOL_STATE_DEGRADED:
6577         case MPI2_RAID_VOL_STATE_OPTIMAL:
6578
6579                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6580                 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
6581                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6582
6583                 if (raid_device)
6584                         break;
6585
6586                 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6587                 if (!wwid) {
6588                         pr_err(MPT3SAS_FMT
6589                             "failure at %s:%d/%s()!\n", ioc->name,
6590                             __FILE__, __LINE__, __func__);
6591                         break;
6592                 }
6593
6594                 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6595                 if (!raid_device) {
6596                         pr_err(MPT3SAS_FMT
6597                             "failure at %s:%d/%s()!\n", ioc->name,
6598                             __FILE__, __LINE__, __func__);
6599                         break;
6600                 }
6601
6602                 raid_device->id = ioc->sas_id++;
6603                 raid_device->channel = RAID_CHANNEL;
6604                 raid_device->handle = handle;
6605                 raid_device->wwid = wwid;
6606                 _scsih_raid_device_add(ioc, raid_device);
6607                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6608                     raid_device->id, 0);
6609                 if (rc)
6610                         _scsih_raid_device_remove(ioc, raid_device);
6611                 break;
6612
6613         case MPI2_RAID_VOL_STATE_INITIALIZING:
6614         default:
6615                 break;
6616         }
6617 }
6618
6619 /**
6620  * _scsih_sas_ir_physical_disk_event - PD event
6621  * @ioc: per adapter object
6622  * @fw_event: The fw_event_work object
6623  * Context: user.
6624  *
6625  * Return nothing.
6626  */
6627 static void
6628 _scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
6629         struct fw_event_work *fw_event)
6630 {
6631         u16 handle, parent_handle;
6632         u32 state;
6633         struct _sas_device *sas_device;
6634         Mpi2ConfigReply_t mpi_reply;
6635         Mpi2SasDevicePage0_t sas_device_pg0;
6636         u32 ioc_status;
6637         Mpi2EventDataIrPhysicalDisk_t *event_data =
6638                 (Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
6639         u64 sas_address;
6640
6641         if (ioc->shost_recovery)
6642                 return;
6643
6644         if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6645                 return;
6646
6647         handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6648         state = le32_to_cpu(event_data->NewValue);
6649
6650         if (!ioc->hide_ir_msg)
6651                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6652                     "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6653                     ioc->name, __func__,  handle,
6654                     le32_to_cpu(event_data->PreviousValue), state));
6655
6656         switch (state) {
6657         case MPI2_RAID_PD_STATE_ONLINE:
6658         case MPI2_RAID_PD_STATE_DEGRADED:
6659         case MPI2_RAID_PD_STATE_REBUILDING:
6660         case MPI2_RAID_PD_STATE_OPTIMAL:
6661         case MPI2_RAID_PD_STATE_HOT_SPARE:
6662
6663                 if (!ioc->is_warpdrive)
6664                         set_bit(handle, ioc->pd_handles);
6665
6666                 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6667                 if (sas_device) {
6668                         sas_device_put(sas_device);
6669                         return;
6670                 }
6671
6672                 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6673                     &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6674                     handle))) {
6675                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6676                             ioc->name, __FILE__, __LINE__, __func__);
6677                         return;
6678                 }
6679
6680                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6681                     MPI2_IOCSTATUS_MASK;
6682                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6683                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6684                             ioc->name, __FILE__, __LINE__, __func__);
6685                         return;
6686                 }
6687
6688                 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6689                 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6690                         mpt3sas_transport_update_links(ioc, sas_address, handle,
6691                             sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6692
6693                 _scsih_add_device(ioc, handle, 0, 1);
6694
6695                 break;
6696
6697         case MPI2_RAID_PD_STATE_OFFLINE:
6698         case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6699         case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
6700         default:
6701                 break;
6702         }
6703 }
6704
6705 /**
6706  * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6707  * @ioc: per adapter object
6708  * @event_data: event data payload
6709  * Context: user.
6710  *
6711  * Return nothing.
6712  */
6713 static void
6714 _scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
6715         Mpi2EventDataIrOperationStatus_t *event_data)
6716 {
6717         char *reason_str = NULL;
6718
6719         switch (event_data->RAIDOperation) {
6720         case MPI2_EVENT_IR_RAIDOP_RESYNC:
6721                 reason_str = "resync";
6722                 break;
6723         case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6724                 reason_str = "online capacity expansion";
6725                 break;
6726         case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6727                 reason_str = "consistency check";
6728                 break;
6729         case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6730                 reason_str = "background init";
6731                 break;
6732         case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6733                 reason_str = "make data consistent";
6734                 break;
6735         }
6736
6737         if (!reason_str)
6738                 return;
6739
6740         pr_info(MPT3SAS_FMT "raid operational status: (%s)" \
6741             "\thandle(0x%04x), percent complete(%d)\n",
6742             ioc->name, reason_str,
6743             le16_to_cpu(event_data->VolDevHandle),
6744             event_data->PercentComplete);
6745 }
6746
6747 /**
6748  * _scsih_sas_ir_operation_status_event - handle RAID operation events
6749  * @ioc: per adapter object
6750  * @fw_event: The fw_event_work object
6751  * Context: user.
6752  *
6753  * Return nothing.
6754  */
6755 static void
6756 _scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
6757         struct fw_event_work *fw_event)
6758 {
6759         Mpi2EventDataIrOperationStatus_t *event_data =
6760                 (Mpi2EventDataIrOperationStatus_t *)
6761                 fw_event->event_data;
6762         static struct _raid_device *raid_device;
6763         unsigned long flags;
6764         u16 handle;
6765
6766         if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
6767             (!ioc->hide_ir_msg))
6768                 _scsih_sas_ir_operation_status_event_debug(ioc,
6769                      event_data);
6770
6771         /* code added for raid transport support */
6772         if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6773
6774                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6775                 handle = le16_to_cpu(event_data->VolDevHandle);
6776                 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
6777                 if (raid_device)
6778                         raid_device->percent_complete =
6779                             event_data->PercentComplete;
6780                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6781         }
6782 }
6783
6784 /**
6785  * _scsih_prep_device_scan - initialize parameters prior to device scan
6786  * @ioc: per adapter object
6787  *
6788  * Set the deleted flag prior to device scan.  If the device is found during
6789  * the scan, then we clear the deleted flag.
6790  */
6791 static void
6792 _scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
6793 {
6794         struct MPT3SAS_DEVICE *sas_device_priv_data;
6795         struct scsi_device *sdev;
6796
6797         shost_for_each_device(sdev, ioc->shost) {
6798                 sas_device_priv_data = sdev->hostdata;
6799                 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6800                         sas_device_priv_data->sas_target->deleted = 1;
6801         }
6802 }
6803
6804 /**
6805  * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6806  * @ioc: per adapter object
6807  * @sas_device_pg0: SAS Device page 0
6808  *
6809  * After host reset, find out whether devices are still responding.
6810  * Used in _scsih_remove_unresponsive_sas_devices.
6811  *
6812  * Return nothing.
6813  */
6814 static void
6815 _scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
6816 Mpi2SasDevicePage0_t *sas_device_pg0)
6817 {
6818         struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
6819         struct scsi_target *starget;
6820         struct _sas_device *sas_device;
6821         unsigned long flags;
6822
6823         spin_lock_irqsave(&ioc->sas_device_lock, flags);
6824         list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6825                 if ((sas_device->sas_address == sas_device_pg0->SASAddress) &&
6826                         (sas_device->slot == sas_device_pg0->Slot)) {
6827                         sas_device->responding = 1;
6828                         starget = sas_device->starget;
6829                         if (starget && starget->hostdata) {
6830                                 sas_target_priv_data = starget->hostdata;
6831                                 sas_target_priv_data->tm_busy = 0;
6832                                 sas_target_priv_data->deleted = 0;
6833                         } else
6834                                 sas_target_priv_data = NULL;
6835                         if (starget) {
6836                                 starget_printk(KERN_INFO, starget,
6837                                     "handle(0x%04x), sas_addr(0x%016llx)\n",
6838                                     sas_device_pg0->DevHandle,
6839                                     (unsigned long long)
6840                                     sas_device->sas_address);
6841
6842                                 if (sas_device->enclosure_handle != 0)
6843                                         starget_printk(KERN_INFO, starget,
6844                                          "enclosure logical id(0x%016llx),"
6845                                          " slot(%d)\n",
6846                                          (unsigned long long)
6847                                          sas_device->enclosure_logical_id,
6848                                          sas_device->slot);
6849                         }
6850                         if (sas_device_pg0->Flags &
6851                               MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
6852                                 sas_device->enclosure_level =
6853                                    le16_to_cpu(sas_device_pg0->EnclosureLevel);
6854                                 memcpy(&sas_device->connector_name[0],
6855                                         &sas_device_pg0->ConnectorName[0], 4);
6856                         } else {
6857                                 sas_device->enclosure_level = 0;
6858                                 sas_device->connector_name[0] = '\0';
6859                         }
6860
6861                         if (sas_device->handle == sas_device_pg0->DevHandle)
6862                                 goto out;
6863                         pr_info("\thandle changed from(0x%04x)!!!\n",
6864                             sas_device->handle);
6865                         sas_device->handle = sas_device_pg0->DevHandle;
6866                         if (sas_target_priv_data)
6867                                 sas_target_priv_data->handle =
6868                                         sas_device_pg0->DevHandle;
6869                         goto out;
6870                 }
6871         }
6872  out:
6873         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6874 }
6875
6876 /**
6877  * _scsih_search_responding_sas_devices -
6878  * @ioc: per adapter object
6879  *
6880  * After host reset, find out whether devices are still responding.
6881  * If not remove.
6882  *
6883  * Return nothing.
6884  */
6885 static void
6886 _scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
6887 {
6888         Mpi2SasDevicePage0_t sas_device_pg0;
6889         Mpi2ConfigReply_t mpi_reply;
6890         u16 ioc_status;
6891         u16 handle;
6892         u32 device_info;
6893
6894         pr_info(MPT3SAS_FMT "search for end-devices: start\n", ioc->name);
6895
6896         if (list_empty(&ioc->sas_device_list))
6897                 goto out;
6898
6899         handle = 0xFFFF;
6900         while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6901             &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6902             handle))) {
6903                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6904                     MPI2_IOCSTATUS_MASK;
6905                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6906                         break;
6907                 handle = sas_device_pg0.DevHandle =
6908                                 le16_to_cpu(sas_device_pg0.DevHandle);
6909                 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6910                 if (!(_scsih_is_end_device(device_info)))
6911                         continue;
6912                 sas_device_pg0.SASAddress =
6913                                 le64_to_cpu(sas_device_pg0.SASAddress);
6914                 sas_device_pg0.Slot = le16_to_cpu(sas_device_pg0.Slot);
6915                 _scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
6916         }
6917
6918  out:
6919         pr_info(MPT3SAS_FMT "search for end-devices: complete\n",
6920             ioc->name);
6921 }
6922
6923 /**
6924  * _scsih_mark_responding_raid_device - mark a raid_device as responding
6925  * @ioc: per adapter object
6926  * @wwid: world wide identifier for raid volume
6927  * @handle: device handle
6928  *
6929  * After host reset, find out whether devices are still responding.
6930  * Used in _scsih_remove_unresponsive_raid_devices.
6931  *
6932  * Return nothing.
6933  */
6934 static void
6935 _scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
6936         u16 handle)
6937 {
6938         struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
6939         struct scsi_target *starget;
6940         struct _raid_device *raid_device;
6941         unsigned long flags;
6942
6943         spin_lock_irqsave(&ioc->raid_device_lock, flags);
6944         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6945                 if (raid_device->wwid == wwid && raid_device->starget) {
6946                         starget = raid_device->starget;
6947                         if (starget && starget->hostdata) {
6948                                 sas_target_priv_data = starget->hostdata;
6949                                 sas_target_priv_data->deleted = 0;
6950                         } else
6951                                 sas_target_priv_data = NULL;
6952                         raid_device->responding = 1;
6953                         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6954                         starget_printk(KERN_INFO, raid_device->starget,
6955                             "handle(0x%04x), wwid(0x%016llx)\n", handle,
6956                             (unsigned long long)raid_device->wwid);
6957
6958                         /*
6959                          * WARPDRIVE: The handles of the PDs might have changed
6960                          * across the host reset so re-initialize the
6961                          * required data for Direct IO
6962                          */
6963                         mpt3sas_init_warpdrive_properties(ioc, raid_device);
6964                         spin_lock_irqsave(&ioc->raid_device_lock, flags);
6965                         if (raid_device->handle == handle) {
6966                                 spin_unlock_irqrestore(&ioc->raid_device_lock,
6967                                     flags);
6968                                 return;
6969                         }
6970                         pr_info("\thandle changed from(0x%04x)!!!\n",
6971                             raid_device->handle);
6972                         raid_device->handle = handle;
6973                         if (sas_target_priv_data)
6974                                 sas_target_priv_data->handle = handle;
6975                         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6976                         return;
6977                 }
6978         }
6979         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6980 }
6981
6982 /**
6983  * _scsih_search_responding_raid_devices -
6984  * @ioc: per adapter object
6985  *
6986  * After host reset, find out whether devices are still responding.
6987  * If not remove.
6988  *
6989  * Return nothing.
6990  */
6991 static void
6992 _scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
6993 {
6994         Mpi2RaidVolPage1_t volume_pg1;
6995         Mpi2RaidVolPage0_t volume_pg0;
6996         Mpi2RaidPhysDiskPage0_t pd_pg0;
6997         Mpi2ConfigReply_t mpi_reply;
6998         u16 ioc_status;
6999         u16 handle;
7000         u8 phys_disk_num;
7001
7002         if (!ioc->ir_firmware)
7003                 return;
7004
7005         pr_info(MPT3SAS_FMT "search for raid volumes: start\n",
7006             ioc->name);
7007
7008         if (list_empty(&ioc->raid_device_list))
7009                 goto out;
7010
7011         handle = 0xFFFF;
7012         while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7013             &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7014                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7015                     MPI2_IOCSTATUS_MASK;
7016                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7017                         break;
7018                 handle = le16_to_cpu(volume_pg1.DevHandle);
7019
7020                 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7021                     &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7022                      sizeof(Mpi2RaidVolPage0_t)))
7023                         continue;
7024
7025                 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7026                     volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7027                     volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
7028                         _scsih_mark_responding_raid_device(ioc,
7029                             le64_to_cpu(volume_pg1.WWID), handle);
7030         }
7031
7032         /* refresh the pd_handles */
7033         if (!ioc->is_warpdrive) {
7034                 phys_disk_num = 0xFF;
7035                 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
7036                 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7037                     &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7038                     phys_disk_num))) {
7039                         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7040                             MPI2_IOCSTATUS_MASK;
7041                         if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7042                                 break;
7043                         phys_disk_num = pd_pg0.PhysDiskNum;
7044                         handle = le16_to_cpu(pd_pg0.DevHandle);
7045                         set_bit(handle, ioc->pd_handles);
7046                 }
7047         }
7048  out:
7049         pr_info(MPT3SAS_FMT "search for responding raid volumes: complete\n",
7050                 ioc->name);
7051 }
7052
7053 /**
7054  * _scsih_mark_responding_expander - mark a expander as responding
7055  * @ioc: per adapter object
7056  * @sas_address: sas address
7057  * @handle:
7058  *
7059  * After host reset, find out whether devices are still responding.
7060  * Used in _scsih_remove_unresponsive_expanders.
7061  *
7062  * Return nothing.
7063  */
7064 static void
7065 _scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
7066         u16 handle)
7067 {
7068         struct _sas_node *sas_expander;
7069         unsigned long flags;
7070         int i;
7071
7072         spin_lock_irqsave(&ioc->sas_node_lock, flags);
7073         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
7074                 if (sas_expander->sas_address != sas_address)
7075                         continue;
7076                 sas_expander->responding = 1;
7077                 if (sas_expander->handle == handle)
7078                         goto out;
7079                 pr_info("\texpander(0x%016llx): handle changed" \
7080                     " from(0x%04x) to (0x%04x)!!!\n",
7081                     (unsigned long long)sas_expander->sas_address,
7082                     sas_expander->handle, handle);
7083                 sas_expander->handle = handle;
7084                 for (i = 0 ; i < sas_expander->num_phys ; i++)
7085                         sas_expander->phy[i].handle = handle;
7086                 goto out;
7087         }
7088  out:
7089         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7090 }
7091
7092 /**
7093  * _scsih_search_responding_expanders -
7094  * @ioc: per adapter object
7095  *
7096  * After host reset, find out whether devices are still responding.
7097  * If not remove.
7098  *
7099  * Return nothing.
7100  */
7101 static void
7102 _scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
7103 {
7104         Mpi2ExpanderPage0_t expander_pg0;
7105         Mpi2ConfigReply_t mpi_reply;
7106         u16 ioc_status;
7107         u64 sas_address;
7108         u16 handle;
7109
7110         pr_info(MPT3SAS_FMT "search for expanders: start\n", ioc->name);
7111
7112         if (list_empty(&ioc->sas_expander_list))
7113                 goto out;
7114
7115         handle = 0xFFFF;
7116         while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7117             MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7118
7119                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7120                     MPI2_IOCSTATUS_MASK;
7121                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7122                         break;
7123
7124                 handle = le16_to_cpu(expander_pg0.DevHandle);
7125                 sas_address = le64_to_cpu(expander_pg0.SASAddress);
7126                 pr_info("\texpander present: handle(0x%04x), sas_addr(0x%016llx)\n",
7127                         handle,
7128                     (unsigned long long)sas_address);
7129                 _scsih_mark_responding_expander(ioc, sas_address, handle);
7130         }
7131
7132  out:
7133         pr_info(MPT3SAS_FMT "search for expanders: complete\n", ioc->name);
7134 }
7135
7136 /**
7137  * _scsih_remove_unresponding_sas_devices - removing unresponding devices
7138  * @ioc: per adapter object
7139  *
7140  * Return nothing.
7141  */
7142 static void
7143 _scsih_remove_unresponding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7144 {
7145         struct _sas_device *sas_device, *sas_device_next;
7146         struct _sas_node *sas_expander, *sas_expander_next;
7147         struct _raid_device *raid_device, *raid_device_next;
7148         struct list_head tmp_list;
7149         unsigned long flags;
7150         LIST_HEAD(head);
7151
7152         pr_info(MPT3SAS_FMT "removing unresponding devices: start\n",
7153             ioc->name);
7154
7155         /* removing unresponding end devices */
7156         pr_info(MPT3SAS_FMT "removing unresponding devices: end-devices\n",
7157             ioc->name);
7158         /*
7159          * Iterate, pulling off devices marked as non-responding. We become the
7160          * owner for the reference the list had on any object we prune.
7161          */
7162         spin_lock_irqsave(&ioc->sas_device_lock, flags);
7163         list_for_each_entry_safe(sas_device, sas_device_next,
7164             &ioc->sas_device_list, list) {
7165                 if (!sas_device->responding)
7166                         list_move_tail(&sas_device->list, &head);
7167                 else
7168                         sas_device->responding = 0;
7169         }
7170         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7171
7172         /*
7173          * Now, uninitialize and remove the unresponding devices we pruned.
7174          */
7175         list_for_each_entry_safe(sas_device, sas_device_next, &head, list) {
7176                 _scsih_remove_device(ioc, sas_device);
7177                 list_del_init(&sas_device->list);
7178                 sas_device_put(sas_device);
7179         }
7180
7181         /* removing unresponding volumes */
7182         if (ioc->ir_firmware) {
7183                 pr_info(MPT3SAS_FMT "removing unresponding devices: volumes\n",
7184                         ioc->name);
7185                 list_for_each_entry_safe(raid_device, raid_device_next,
7186                     &ioc->raid_device_list, list) {
7187                         if (!raid_device->responding)
7188                                 _scsih_sas_volume_delete(ioc,
7189                                     raid_device->handle);
7190                         else
7191                                 raid_device->responding = 0;
7192                 }
7193         }
7194
7195         /* removing unresponding expanders */
7196         pr_info(MPT3SAS_FMT "removing unresponding devices: expanders\n",
7197             ioc->name);
7198         spin_lock_irqsave(&ioc->sas_node_lock, flags);
7199         INIT_LIST_HEAD(&tmp_list);
7200         list_for_each_entry_safe(sas_expander, sas_expander_next,
7201             &ioc->sas_expander_list, list) {
7202                 if (!sas_expander->responding)
7203                         list_move_tail(&sas_expander->list, &tmp_list);
7204                 else
7205                         sas_expander->responding = 0;
7206         }
7207         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7208         list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
7209             list) {
7210                 list_del(&sas_expander->list);
7211                 _scsih_expander_node_remove(ioc, sas_expander);
7212         }
7213
7214         pr_info(MPT3SAS_FMT "removing unresponding devices: complete\n",
7215             ioc->name);
7216
7217         /* unblock devices */
7218         _scsih_ublock_io_all_device(ioc);
7219 }
7220
7221 static void
7222 _scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
7223         struct _sas_node *sas_expander, u16 handle)
7224 {
7225         Mpi2ExpanderPage1_t expander_pg1;
7226         Mpi2ConfigReply_t mpi_reply;
7227         int i;
7228
7229         for (i = 0 ; i < sas_expander->num_phys ; i++) {
7230                 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
7231                     &expander_pg1, i, handle))) {
7232                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7233                             ioc->name, __FILE__, __LINE__, __func__);
7234                         return;
7235                 }
7236
7237                 mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
7238                     le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7239                     expander_pg1.NegotiatedLinkRate >> 4);
7240         }
7241 }
7242
7243 /**
7244  * _scsih_scan_for_devices_after_reset - scan for devices after host reset
7245  * @ioc: per adapter object
7246  *
7247  * Return nothing.
7248  */
7249 static void
7250 _scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
7251 {
7252         Mpi2ExpanderPage0_t expander_pg0;
7253         Mpi2SasDevicePage0_t sas_device_pg0;
7254         Mpi2RaidVolPage1_t volume_pg1;
7255         Mpi2RaidVolPage0_t volume_pg0;
7256         Mpi2RaidPhysDiskPage0_t pd_pg0;
7257         Mpi2EventIrConfigElement_t element;
7258         Mpi2ConfigReply_t mpi_reply;
7259         u8 phys_disk_num;
7260         u16 ioc_status;
7261         u16 handle, parent_handle;
7262         u64 sas_address;
7263         struct _sas_device *sas_device;
7264         struct _sas_node *expander_device;
7265         static struct _raid_device *raid_device;
7266         u8 retry_count;
7267         unsigned long flags;
7268
7269         pr_info(MPT3SAS_FMT "scan devices: start\n", ioc->name);
7270
7271         _scsih_sas_host_refresh(ioc);
7272
7273         pr_info(MPT3SAS_FMT "\tscan devices: expanders start\n", ioc->name);
7274
7275         /* expanders */
7276         handle = 0xFFFF;
7277         while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7278             MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7279                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7280                     MPI2_IOCSTATUS_MASK;
7281                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7282                         pr_info(MPT3SAS_FMT "\tbreak from expander scan: " \
7283                             "ioc_status(0x%04x), loginfo(0x%08x)\n",
7284                             ioc->name, ioc_status,
7285                             le32_to_cpu(mpi_reply.IOCLogInfo));
7286                         break;
7287                 }
7288                 handle = le16_to_cpu(expander_pg0.DevHandle);
7289                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7290                 expander_device = mpt3sas_scsih_expander_find_by_sas_address(
7291                     ioc, le64_to_cpu(expander_pg0.SASAddress));
7292                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7293                 if (expander_device)
7294                         _scsih_refresh_expander_links(ioc, expander_device,
7295                             handle);
7296                 else {
7297                         pr_info(MPT3SAS_FMT "\tBEFORE adding expander: " \
7298                             "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7299                             handle, (unsigned long long)
7300                             le64_to_cpu(expander_pg0.SASAddress));
7301                         _scsih_expander_add(ioc, handle);
7302                         pr_info(MPT3SAS_FMT "\tAFTER adding expander: " \
7303                             "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7304                             handle, (unsigned long long)
7305                             le64_to_cpu(expander_pg0.SASAddress));
7306                 }
7307         }
7308
7309         pr_info(MPT3SAS_FMT "\tscan devices: expanders complete\n",
7310             ioc->name);
7311
7312         if (!ioc->ir_firmware)
7313                 goto skip_to_sas;
7314
7315         pr_info(MPT3SAS_FMT "\tscan devices: phys disk start\n", ioc->name);
7316
7317         /* phys disk */
7318         phys_disk_num = 0xFF;
7319         while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7320             &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7321             phys_disk_num))) {
7322                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7323                     MPI2_IOCSTATUS_MASK;
7324                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7325                         pr_info(MPT3SAS_FMT "\tbreak from phys disk scan: "\
7326                             "ioc_status(0x%04x), loginfo(0x%08x)\n",
7327                             ioc->name, ioc_status,
7328                             le32_to_cpu(mpi_reply.IOCLogInfo));
7329                         break;
7330                 }
7331                 phys_disk_num = pd_pg0.PhysDiskNum;
7332                 handle = le16_to_cpu(pd_pg0.DevHandle);
7333                 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
7334                 if (sas_device) {
7335                         sas_device_put(sas_device);
7336                         continue;
7337                 }
7338                 if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7339                     &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7340                     handle) != 0)
7341                         continue;
7342                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7343                     MPI2_IOCSTATUS_MASK;
7344                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7345                         pr_info(MPT3SAS_FMT "\tbreak from phys disk scan " \
7346                             "ioc_status(0x%04x), loginfo(0x%08x)\n",
7347                             ioc->name, ioc_status,
7348                             le32_to_cpu(mpi_reply.IOCLogInfo));
7349                         break;
7350                 }
7351                 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7352                 if (!_scsih_get_sas_address(ioc, parent_handle,
7353                     &sas_address)) {
7354                         pr_info(MPT3SAS_FMT "\tBEFORE adding phys disk: " \
7355                             " handle (0x%04x), sas_addr(0x%016llx)\n",
7356                             ioc->name, handle, (unsigned long long)
7357                             le64_to_cpu(sas_device_pg0.SASAddress));
7358                         mpt3sas_transport_update_links(ioc, sas_address,
7359                             handle, sas_device_pg0.PhyNum,
7360                             MPI2_SAS_NEG_LINK_RATE_1_5);
7361                         set_bit(handle, ioc->pd_handles);
7362                         retry_count = 0;
7363                         /* This will retry adding the end device.
7364                          * _scsih_add_device() will decide on retries and
7365                          * return "1" when it should be retried
7366                          */
7367                         while (_scsih_add_device(ioc, handle, retry_count++,
7368                             1)) {
7369                                 ssleep(1);
7370                         }
7371                         pr_info(MPT3SAS_FMT "\tAFTER adding phys disk: " \
7372                             " handle (0x%04x), sas_addr(0x%016llx)\n",
7373                             ioc->name, handle, (unsigned long long)
7374                             le64_to_cpu(sas_device_pg0.SASAddress));
7375                 }
7376         }
7377
7378         pr_info(MPT3SAS_FMT "\tscan devices: phys disk complete\n",
7379             ioc->name);
7380
7381         pr_info(MPT3SAS_FMT "\tscan devices: volumes start\n", ioc->name);
7382
7383         /* volumes */
7384         handle = 0xFFFF;
7385         while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7386             &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7387                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7388                     MPI2_IOCSTATUS_MASK;
7389                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7390                         pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7391                             "ioc_status(0x%04x), loginfo(0x%08x)\n",
7392                             ioc->name, ioc_status,
7393                             le32_to_cpu(mpi_reply.IOCLogInfo));
7394                         break;
7395                 }
7396                 handle = le16_to_cpu(volume_pg1.DevHandle);
7397                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7398                 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7399                     le64_to_cpu(volume_pg1.WWID));
7400                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7401                 if (raid_device)
7402                         continue;
7403                 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7404                     &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7405                      sizeof(Mpi2RaidVolPage0_t)))
7406                         continue;
7407                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7408                     MPI2_IOCSTATUS_MASK;
7409                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7410                         pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7411                             "ioc_status(0x%04x), loginfo(0x%08x)\n",
7412                             ioc->name, ioc_status,
7413                             le32_to_cpu(mpi_reply.IOCLogInfo));
7414                         break;
7415                 }
7416                 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7417                     volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7418                     volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7419                         memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7420                         element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7421                         element.VolDevHandle = volume_pg1.DevHandle;
7422                         pr_info(MPT3SAS_FMT
7423                                 "\tBEFORE adding volume: handle (0x%04x)\n",
7424                                 ioc->name, volume_pg1.DevHandle);
7425                         _scsih_sas_volume_add(ioc, &element);
7426                         pr_info(MPT3SAS_FMT
7427                                 "\tAFTER adding volume: handle (0x%04x)\n",
7428                                 ioc->name, volume_pg1.DevHandle);
7429                 }
7430         }
7431
7432         pr_info(MPT3SAS_FMT "\tscan devices: volumes complete\n",
7433             ioc->name);
7434
7435  skip_to_sas:
7436
7437         pr_info(MPT3SAS_FMT "\tscan devices: end devices start\n",
7438             ioc->name);
7439
7440         /* sas devices */
7441         handle = 0xFFFF;
7442         while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7443             &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7444             handle))) {
7445                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7446                     MPI2_IOCSTATUS_MASK;
7447                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7448                         pr_info(MPT3SAS_FMT "\tbreak from end device scan:"\
7449                             " ioc_status(0x%04x), loginfo(0x%08x)\n",
7450                             ioc->name, ioc_status,
7451                             le32_to_cpu(mpi_reply.IOCLogInfo));
7452                         break;
7453                 }
7454                 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7455                 if (!(_scsih_is_end_device(
7456                     le32_to_cpu(sas_device_pg0.DeviceInfo))))
7457                         continue;
7458                 sas_device = mpt3sas_get_sdev_by_addr(ioc,
7459                     le64_to_cpu(sas_device_pg0.SASAddress));
7460                 if (sas_device) {
7461                         sas_device_put(sas_device);
7462                         continue;
7463                 }
7464                 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7465                 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7466                         pr_info(MPT3SAS_FMT "\tBEFORE adding end device: " \
7467                             "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7468                             handle, (unsigned long long)
7469                             le64_to_cpu(sas_device_pg0.SASAddress));
7470                         mpt3sas_transport_update_links(ioc, sas_address, handle,
7471                             sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7472                         retry_count = 0;
7473                         /* This will retry adding the end device.
7474                          * _scsih_add_device() will decide on retries and
7475                          * return "1" when it should be retried
7476                          */
7477                         while (_scsih_add_device(ioc, handle, retry_count++,
7478                             0)) {
7479                                 ssleep(1);
7480                         }
7481                         pr_info(MPT3SAS_FMT "\tAFTER adding end device: " \
7482                             "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7483                             handle, (unsigned long long)
7484                             le64_to_cpu(sas_device_pg0.SASAddress));
7485                 }
7486         }
7487         pr_info(MPT3SAS_FMT "\tscan devices: end devices complete\n",
7488             ioc->name);
7489
7490         pr_info(MPT3SAS_FMT "scan devices: complete\n", ioc->name);
7491 }
7492 /**
7493  * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
7494  * @ioc: per adapter object
7495  * @reset_phase: phase
7496  *
7497  * The handler for doing any required cleanup or initialization.
7498  *
7499  * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
7500  * MPT3_IOC_DONE_RESET
7501  *
7502  * Return nothing.
7503  */
7504 void
7505 mpt3sas_scsih_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
7506 {
7507         switch (reset_phase) {
7508         case MPT3_IOC_PRE_RESET:
7509                 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7510                         "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
7511                 break;
7512         case MPT3_IOC_AFTER_RESET:
7513                 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7514                         "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
7515                 if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
7516                         ioc->scsih_cmds.status |= MPT3_CMD_RESET;
7517                         mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7518                         complete(&ioc->scsih_cmds.done);
7519                 }
7520                 if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
7521                         ioc->tm_cmds.status |= MPT3_CMD_RESET;
7522                         mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7523                         complete(&ioc->tm_cmds.done);
7524                 }
7525
7526                 _scsih_fw_event_cleanup_queue(ioc);
7527                 _scsih_flush_running_cmds(ioc);
7528                 break;
7529         case MPT3_IOC_DONE_RESET:
7530                 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7531                         "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
7532                 if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7533                     !ioc->sas_hba.num_phys)) {
7534                         _scsih_prep_device_scan(ioc);
7535                         _scsih_search_responding_sas_devices(ioc);
7536                         _scsih_search_responding_raid_devices(ioc);
7537                         _scsih_search_responding_expanders(ioc);
7538                         _scsih_error_recovery_delete_devices(ioc);
7539                 }
7540                 break;
7541         }
7542 }
7543
7544 /**
7545  * _mpt3sas_fw_work - delayed task for processing firmware events
7546  * @ioc: per adapter object
7547  * @fw_event: The fw_event_work object
7548  * Context: user.
7549  *
7550  * Return nothing.
7551  */
7552 static void
7553 _mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
7554 {
7555         _scsih_fw_event_del_from_list(ioc, fw_event);
7556
7557         /* the queue is being flushed so ignore this event */
7558         if (ioc->remove_host || ioc->pci_error_recovery) {
7559                 fw_event_work_put(fw_event);
7560                 return;
7561         }
7562
7563         switch (fw_event->event) {
7564         case MPT3SAS_PROCESS_TRIGGER_DIAG:
7565                 mpt3sas_process_trigger_data(ioc,
7566                         (struct SL_WH_TRIGGERS_EVENT_DATA_T *)
7567                         fw_event->event_data);
7568                 break;
7569         case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
7570                 while (scsi_host_in_recovery(ioc->shost) ||
7571                                          ioc->shost_recovery) {
7572                         /*
7573                          * If we're unloading, bail. Otherwise, this can become
7574                          * an infinite loop.
7575                          */
7576                         if (ioc->remove_host)
7577                                 goto out;
7578                         ssleep(1);
7579                 }
7580                 _scsih_remove_unresponding_sas_devices(ioc);
7581                 _scsih_scan_for_devices_after_reset(ioc);
7582                 break;
7583         case MPT3SAS_PORT_ENABLE_COMPLETE:
7584                 ioc->start_scan = 0;
7585         if (missing_delay[0] != -1 && missing_delay[1] != -1)
7586                         mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
7587                             missing_delay[1]);
7588                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
7589                         "port enable: complete from worker thread\n",
7590                         ioc->name));
7591                 break;
7592         case MPT3SAS_TURN_ON_PFA_LED:
7593                 _scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
7594                 break;
7595         case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7596                 _scsih_sas_topology_change_event(ioc, fw_event);
7597                 break;
7598         case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7599                 _scsih_sas_device_status_change_event(ioc, fw_event);
7600                 break;
7601         case MPI2_EVENT_SAS_DISCOVERY:
7602                 _scsih_sas_discovery_event(ioc, fw_event);
7603                 break;
7604         case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7605                 _scsih_sas_broadcast_primitive_event(ioc, fw_event);
7606                 break;
7607         case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7608                 _scsih_sas_enclosure_dev_status_change_event(ioc,
7609                     fw_event);
7610                 break;
7611         case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7612                 _scsih_sas_ir_config_change_event(ioc, fw_event);
7613                 break;
7614         case MPI2_EVENT_IR_VOLUME:
7615                 _scsih_sas_ir_volume_event(ioc, fw_event);
7616                 break;
7617         case MPI2_EVENT_IR_PHYSICAL_DISK:
7618                 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
7619                 break;
7620         case MPI2_EVENT_IR_OPERATION_STATUS:
7621                 _scsih_sas_ir_operation_status_event(ioc, fw_event);
7622                 break;
7623         }
7624 out:
7625         fw_event_work_put(fw_event);
7626 }
7627
7628 /**
7629  * _firmware_event_work
7630  * @ioc: per adapter object
7631  * @work: The fw_event_work object
7632  * Context: user.
7633  *
7634  * wrappers for the work thread handling firmware events
7635  *
7636  * Return nothing.
7637  */
7638
7639 static void
7640 _firmware_event_work(struct work_struct *work)
7641 {
7642         struct fw_event_work *fw_event = container_of(work,
7643             struct fw_event_work, work);
7644
7645         _mpt3sas_fw_work(fw_event->ioc, fw_event);
7646 }
7647
7648 /**
7649  * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
7650  * @ioc: per adapter object
7651  * @msix_index: MSIX table index supplied by the OS
7652  * @reply: reply message frame(lower 32bit addr)
7653  * Context: interrupt.
7654  *
7655  * This function merely adds a new work task into ioc->firmware_event_thread.
7656  * The tasks are worked from _firmware_event_work in user context.
7657  *
7658  * Return 1 meaning mf should be freed from _base_interrupt
7659  *        0 means the mf is freed from this function.
7660  */
7661 u8
7662 mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
7663         u32 reply)
7664 {
7665         struct fw_event_work *fw_event;
7666         Mpi2EventNotificationReply_t *mpi_reply;
7667         u16 event;
7668         u16 sz;
7669
7670         /* events turned off due to host reset or driver unloading */
7671         if (ioc->remove_host || ioc->pci_error_recovery)
7672                 return 1;
7673
7674         mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
7675
7676         if (unlikely(!mpi_reply)) {
7677                 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7678                     ioc->name, __FILE__, __LINE__, __func__);
7679                 return 1;
7680         }
7681
7682         event = le16_to_cpu(mpi_reply->Event);
7683
7684         if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
7685                 mpt3sas_trigger_event(ioc, event, 0);
7686
7687         switch (event) {
7688         /* handle these */
7689         case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7690         {
7691                 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7692                     (Mpi2EventDataSasBroadcastPrimitive_t *)
7693                     mpi_reply->EventData;
7694
7695                 if (baen_data->Primitive !=
7696                     MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
7697                         return 1;
7698
7699                 if (ioc->broadcast_aen_busy) {
7700                         ioc->broadcast_aen_pending++;
7701                         return 1;
7702                 } else
7703                         ioc->broadcast_aen_busy = 1;
7704                 break;
7705         }
7706
7707         case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7708                 _scsih_check_topo_delete_events(ioc,
7709                     (Mpi2EventDataSasTopologyChangeList_t *)
7710                     mpi_reply->EventData);
7711                 break;
7712         case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7713                 _scsih_check_ir_config_unhide_events(ioc,
7714                     (Mpi2EventDataIrConfigChangeList_t *)
7715                     mpi_reply->EventData);
7716                 break;
7717         case MPI2_EVENT_IR_VOLUME:
7718                 _scsih_check_volume_delete_events(ioc,
7719                     (Mpi2EventDataIrVolume_t *)
7720                     mpi_reply->EventData);
7721                 break;
7722         case MPI2_EVENT_LOG_ENTRY_ADDED:
7723         {
7724                 Mpi2EventDataLogEntryAdded_t *log_entry;
7725                 u32 *log_code;
7726
7727                 if (!ioc->is_warpdrive)
7728                         break;
7729
7730                 log_entry = (Mpi2EventDataLogEntryAdded_t *)
7731                     mpi_reply->EventData;
7732                 log_code = (u32 *)log_entry->LogData;
7733
7734                 if (le16_to_cpu(log_entry->LogEntryQualifier)
7735                     != MPT2_WARPDRIVE_LOGENTRY)
7736                         break;
7737
7738                 switch (le32_to_cpu(*log_code)) {
7739                 case MPT2_WARPDRIVE_LC_SSDT:
7740                         pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
7741                             "IO Throttling has occurred in the WarpDrive "
7742                             "subsystem. Check WarpDrive documentation for "
7743                             "additional details.\n", ioc->name);
7744                         break;
7745                 case MPT2_WARPDRIVE_LC_SSDLW:
7746                         pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
7747                             "Program/Erase Cycles for the WarpDrive subsystem "
7748                             "in degraded range. Check WarpDrive documentation "
7749                             "for additional details.\n", ioc->name);
7750                         break;
7751                 case MPT2_WARPDRIVE_LC_SSDLF:
7752                         pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
7753                             "There are no Program/Erase Cycles for the "
7754                             "WarpDrive subsystem. The storage device will be "
7755                             "in read-only mode. Check WarpDrive documentation "
7756                             "for additional details.\n", ioc->name);
7757                         break;
7758                 case MPT2_WARPDRIVE_LC_BRMF:
7759                         pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
7760                             "The Backup Rail Monitor has failed on the "
7761                             "WarpDrive subsystem. Check WarpDrive "
7762                             "documentation for additional details.\n",
7763                             ioc->name);
7764                         break;
7765                 }
7766
7767                 break;
7768         }
7769         case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7770         case MPI2_EVENT_IR_OPERATION_STATUS:
7771         case MPI2_EVENT_SAS_DISCOVERY:
7772         case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7773         case MPI2_EVENT_IR_PHYSICAL_DISK:
7774                 break;
7775
7776         case MPI2_EVENT_TEMP_THRESHOLD:
7777                 _scsih_temp_threshold_events(ioc,
7778                         (Mpi2EventDataTemperature_t *)
7779                         mpi_reply->EventData);
7780                 break;
7781
7782         default: /* ignore the rest */
7783                 return 1;
7784         }
7785
7786         sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7787         fw_event = alloc_fw_event_work(sz);
7788         if (!fw_event) {
7789                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7790                     ioc->name, __FILE__, __LINE__, __func__);
7791                 return 1;
7792         }
7793
7794         memcpy(fw_event->event_data, mpi_reply->EventData, sz);
7795         fw_event->ioc = ioc;
7796         fw_event->VF_ID = mpi_reply->VF_ID;
7797         fw_event->VP_ID = mpi_reply->VP_ID;
7798         fw_event->event = event;
7799         _scsih_fw_event_add(ioc, fw_event);
7800         fw_event_work_put(fw_event);
7801         return 1;
7802 }
7803
7804 /**
7805  * _scsih_expander_node_remove - removing expander device from list.
7806  * @ioc: per adapter object
7807  * @sas_expander: the sas_device object
7808  * Context: Calling function should acquire ioc->sas_node_lock.
7809  *
7810  * Removing object and freeing associated memory from the
7811  * ioc->sas_expander_list.
7812  *
7813  * Return nothing.
7814  */
7815 static void
7816 _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
7817         struct _sas_node *sas_expander)
7818 {
7819         struct _sas_port *mpt3sas_port, *next;
7820
7821         /* remove sibling ports attached to this expander */
7822         list_for_each_entry_safe(mpt3sas_port, next,
7823            &sas_expander->sas_port_list, port_list) {
7824                 if (ioc->shost_recovery)
7825                         return;
7826                 if (mpt3sas_port->remote_identify.device_type ==
7827                     SAS_END_DEVICE)
7828                         mpt3sas_device_remove_by_sas_address(ioc,
7829                             mpt3sas_port->remote_identify.sas_address);
7830                 else if (mpt3sas_port->remote_identify.device_type ==
7831                     SAS_EDGE_EXPANDER_DEVICE ||
7832                     mpt3sas_port->remote_identify.device_type ==
7833                     SAS_FANOUT_EXPANDER_DEVICE)
7834                         mpt3sas_expander_remove(ioc,
7835                             mpt3sas_port->remote_identify.sas_address);
7836         }
7837
7838         mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
7839             sas_expander->sas_address_parent);
7840
7841         pr_info(MPT3SAS_FMT
7842                 "expander_remove: handle(0x%04x), sas_addr(0x%016llx)\n",
7843                 ioc->name,
7844             sas_expander->handle, (unsigned long long)
7845             sas_expander->sas_address);
7846
7847         kfree(sas_expander->phy);
7848         kfree(sas_expander);
7849 }
7850
7851 /**
7852  * _scsih_ir_shutdown - IR shutdown notification
7853  * @ioc: per adapter object
7854  *
7855  * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7856  * the host system is shutting down.
7857  *
7858  * Return nothing.
7859  */
7860 static void
7861 _scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
7862 {
7863         Mpi2RaidActionRequest_t *mpi_request;
7864         Mpi2RaidActionReply_t *mpi_reply;
7865         u16 smid;
7866
7867         /* is IR firmware build loaded ? */
7868         if (!ioc->ir_firmware)
7869                 return;
7870
7871         /* are there any volumes ? */
7872         if (list_empty(&ioc->raid_device_list))
7873                 return;
7874
7875         mutex_lock(&ioc->scsih_cmds.mutex);
7876
7877         if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
7878                 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
7879                     ioc->name, __func__);
7880                 goto out;
7881         }
7882         ioc->scsih_cmds.status = MPT3_CMD_PENDING;
7883
7884         smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7885         if (!smid) {
7886                 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
7887                     ioc->name, __func__);
7888                 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
7889                 goto out;
7890         }
7891
7892         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
7893         ioc->scsih_cmds.smid = smid;
7894         memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7895
7896         mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7897         mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7898
7899         if (!ioc->hide_ir_msg)
7900                 pr_info(MPT3SAS_FMT "IR shutdown (sending)\n", ioc->name);
7901         init_completion(&ioc->scsih_cmds.done);
7902         mpt3sas_base_put_smid_default(ioc, smid);
7903         wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7904
7905         if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
7906                 pr_err(MPT3SAS_FMT "%s: timeout\n",
7907                     ioc->name, __func__);
7908                 goto out;
7909         }
7910
7911         if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
7912                 mpi_reply = ioc->scsih_cmds.reply;
7913                 if (!ioc->hide_ir_msg)
7914                         pr_info(MPT3SAS_FMT "IR shutdown "
7915                            "(complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
7916                             ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7917                             le32_to_cpu(mpi_reply->IOCLogInfo));
7918         }
7919
7920  out:
7921         ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
7922         mutex_unlock(&ioc->scsih_cmds.mutex);
7923 }
7924
7925 /**
7926  * scsih_remove - detach and remove add host
7927  * @pdev: PCI device struct
7928  *
7929  * Routine called when unloading the driver.
7930  * Return nothing.
7931  */
7932 void scsih_remove(struct pci_dev *pdev)
7933 {
7934         struct Scsi_Host *shost = pci_get_drvdata(pdev);
7935         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
7936         struct _sas_port *mpt3sas_port, *next_port;
7937         struct _raid_device *raid_device, *next;
7938         struct MPT3SAS_TARGET *sas_target_priv_data;
7939         struct workqueue_struct *wq;
7940         unsigned long flags;
7941
7942         ioc->remove_host = 1;
7943         _scsih_fw_event_cleanup_queue(ioc);
7944
7945         spin_lock_irqsave(&ioc->fw_event_lock, flags);
7946         wq = ioc->firmware_event_thread;
7947         ioc->firmware_event_thread = NULL;
7948         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7949         if (wq)
7950                 destroy_workqueue(wq);
7951
7952         /* release all the volumes */
7953         _scsih_ir_shutdown(ioc);
7954         list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7955             list) {
7956                 if (raid_device->starget) {
7957                         sas_target_priv_data =
7958                             raid_device->starget->hostdata;
7959                         sas_target_priv_data->deleted = 1;
7960                         scsi_remove_target(&raid_device->starget->dev);
7961                 }
7962                 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
7963                         ioc->name,  raid_device->handle,
7964                     (unsigned long long) raid_device->wwid);
7965                 _scsih_raid_device_remove(ioc, raid_device);
7966         }
7967
7968         /* free ports attached to the sas_host */
7969         list_for_each_entry_safe(mpt3sas_port, next_port,
7970            &ioc->sas_hba.sas_port_list, port_list) {
7971                 if (mpt3sas_port->remote_identify.device_type ==
7972                     SAS_END_DEVICE)
7973                         mpt3sas_device_remove_by_sas_address(ioc,
7974                             mpt3sas_port->remote_identify.sas_address);
7975                 else if (mpt3sas_port->remote_identify.device_type ==
7976                     SAS_EDGE_EXPANDER_DEVICE ||
7977                     mpt3sas_port->remote_identify.device_type ==
7978                     SAS_FANOUT_EXPANDER_DEVICE)
7979                         mpt3sas_expander_remove(ioc,
7980                             mpt3sas_port->remote_identify.sas_address);
7981         }
7982
7983         /* free phys attached to the sas_host */
7984         if (ioc->sas_hba.num_phys) {
7985                 kfree(ioc->sas_hba.phy);
7986                 ioc->sas_hba.phy = NULL;
7987                 ioc->sas_hba.num_phys = 0;
7988         }
7989
7990         sas_remove_host(shost);
7991         scsi_remove_host(shost);
7992         mpt3sas_base_detach(ioc);
7993         spin_lock(&gioc_lock);
7994         list_del(&ioc->list);
7995         spin_unlock(&gioc_lock);
7996         scsi_host_put(shost);
7997 }
7998
7999 /**
8000  * scsih_shutdown - routine call during system shutdown
8001  * @pdev: PCI device struct
8002  *
8003  * Return nothing.
8004  */
8005 void
8006 scsih_shutdown(struct pci_dev *pdev)
8007 {
8008         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8009         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8010         struct workqueue_struct *wq;
8011         unsigned long flags;
8012
8013         ioc->remove_host = 1;
8014         _scsih_fw_event_cleanup_queue(ioc);
8015
8016         spin_lock_irqsave(&ioc->fw_event_lock, flags);
8017         wq = ioc->firmware_event_thread;
8018         ioc->firmware_event_thread = NULL;
8019         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8020         if (wq)
8021                 destroy_workqueue(wq);
8022
8023         _scsih_ir_shutdown(ioc);
8024         mpt3sas_base_detach(ioc);
8025 }
8026
8027
8028 /**
8029  * _scsih_probe_boot_devices - reports 1st device
8030  * @ioc: per adapter object
8031  *
8032  * If specified in bios page 2, this routine reports the 1st
8033  * device scsi-ml or sas transport for persistent boot device
8034  * purposes.  Please refer to function _scsih_determine_boot_device()
8035  */
8036 static void
8037 _scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
8038 {
8039         u8 is_raid;
8040         void *device;
8041         struct _sas_device *sas_device;
8042         struct _raid_device *raid_device;
8043         u16 handle;
8044         u64 sas_address_parent;
8045         u64 sas_address;
8046         unsigned long flags;
8047         int rc;
8048
8049          /* no Bios, return immediately */
8050         if (!ioc->bios_pg3.BiosVersion)
8051                 return;
8052
8053         device = NULL;
8054         is_raid = 0;
8055         if (ioc->req_boot_device.device) {
8056                 device =  ioc->req_boot_device.device;
8057                 is_raid = ioc->req_boot_device.is_raid;
8058         } else if (ioc->req_alt_boot_device.device) {
8059                 device =  ioc->req_alt_boot_device.device;
8060                 is_raid = ioc->req_alt_boot_device.is_raid;
8061         } else if (ioc->current_boot_device.device) {
8062                 device =  ioc->current_boot_device.device;
8063                 is_raid = ioc->current_boot_device.is_raid;
8064         }
8065
8066         if (!device)
8067                 return;
8068
8069         if (is_raid) {
8070                 raid_device = device;
8071                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8072                     raid_device->id, 0);
8073                 if (rc)
8074                         _scsih_raid_device_remove(ioc, raid_device);
8075         } else {
8076                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8077                 sas_device = device;
8078                 handle = sas_device->handle;
8079                 sas_address_parent = sas_device->sas_address_parent;
8080                 sas_address = sas_device->sas_address;
8081                 list_move_tail(&sas_device->list, &ioc->sas_device_list);
8082                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8083
8084                 if (ioc->hide_drives)
8085                         return;
8086                 if (!mpt3sas_transport_port_add(ioc, handle,
8087                     sas_address_parent)) {
8088                         _scsih_sas_device_remove(ioc, sas_device);
8089                 } else if (!sas_device->starget) {
8090                         if (!ioc->is_driver_loading) {
8091                                 mpt3sas_transport_port_remove(ioc,
8092                                     sas_address,
8093                                     sas_address_parent);
8094                                 _scsih_sas_device_remove(ioc, sas_device);
8095                         }
8096                 }
8097         }
8098 }
8099
8100 /**
8101  * _scsih_probe_raid - reporting raid volumes to scsi-ml
8102  * @ioc: per adapter object
8103  *
8104  * Called during initial loading of the driver.
8105  */
8106 static void
8107 _scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
8108 {
8109         struct _raid_device *raid_device, *raid_next;
8110         int rc;
8111
8112         list_for_each_entry_safe(raid_device, raid_next,
8113             &ioc->raid_device_list, list) {
8114                 if (raid_device->starget)
8115                         continue;
8116                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8117                     raid_device->id, 0);
8118                 if (rc)
8119                         _scsih_raid_device_remove(ioc, raid_device);
8120         }
8121 }
8122
8123 static struct _sas_device *get_next_sas_device(struct MPT3SAS_ADAPTER *ioc)
8124 {
8125         struct _sas_device *sas_device = NULL;
8126         unsigned long flags;
8127
8128         spin_lock_irqsave(&ioc->sas_device_lock, flags);
8129         if (!list_empty(&ioc->sas_device_init_list)) {
8130                 sas_device = list_first_entry(&ioc->sas_device_init_list,
8131                                 struct _sas_device, list);
8132                 sas_device_get(sas_device);
8133         }
8134         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8135
8136         return sas_device;
8137 }
8138
8139 static void sas_device_make_active(struct MPT3SAS_ADAPTER *ioc,
8140                 struct _sas_device *sas_device)
8141 {
8142         unsigned long flags;
8143
8144         spin_lock_irqsave(&ioc->sas_device_lock, flags);
8145
8146         /*
8147          * Since we dropped the lock during the call to port_add(), we need to
8148          * be careful here that somebody else didn't move or delete this item
8149          * while we were busy with other things.
8150          *
8151          * If it was on the list, we need a put() for the reference the list
8152          * had. Either way, we need a get() for the destination list.
8153          */
8154         if (!list_empty(&sas_device->list)) {
8155                 list_del_init(&sas_device->list);
8156                 sas_device_put(sas_device);
8157         }
8158
8159         sas_device_get(sas_device);
8160         list_add_tail(&sas_device->list, &ioc->sas_device_list);
8161
8162         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8163 }
8164
8165 /**
8166  * _scsih_probe_sas - reporting sas devices to sas transport
8167  * @ioc: per adapter object
8168  *
8169  * Called during initial loading of the driver.
8170  */
8171 static void
8172 _scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
8173 {
8174         struct _sas_device *sas_device;
8175
8176         if (ioc->hide_drives)
8177                 return;
8178
8179         while ((sas_device = get_next_sas_device(ioc))) {
8180                 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
8181                     sas_device->sas_address_parent)) {
8182                         _scsih_sas_device_remove(ioc, sas_device);
8183                         sas_device_put(sas_device);
8184                         continue;
8185                 } else if (!sas_device->starget) {
8186                         /*
8187                          * When asyn scanning is enabled, its not possible to
8188                          * remove devices while scanning is turned on due to an
8189                          * oops in scsi_sysfs_add_sdev()->add_device()->
8190                          * sysfs_addrm_start()
8191                          */
8192                         if (!ioc->is_driver_loading) {
8193                                 mpt3sas_transport_port_remove(ioc,
8194                                     sas_device->sas_address,
8195                                     sas_device->sas_address_parent);
8196                                 _scsih_sas_device_remove(ioc, sas_device);
8197                                 sas_device_put(sas_device);
8198                                 continue;
8199                         }
8200                 }
8201                 sas_device_make_active(ioc, sas_device);
8202                 sas_device_put(sas_device);
8203         }
8204 }
8205
8206 /**
8207  * _scsih_probe_devices - probing for devices
8208  * @ioc: per adapter object
8209  *
8210  * Called during initial loading of the driver.
8211  */
8212 static void
8213 _scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
8214 {
8215         u16 volume_mapping_flags;
8216
8217         if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
8218                 return;  /* return when IOC doesn't support initiator mode */
8219
8220         _scsih_probe_boot_devices(ioc);
8221
8222         if (ioc->ir_firmware) {
8223                 volume_mapping_flags =
8224                     le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
8225                     MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
8226                 if (volume_mapping_flags ==
8227                     MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
8228                         _scsih_probe_raid(ioc);
8229                         _scsih_probe_sas(ioc);
8230                 } else {
8231                         _scsih_probe_sas(ioc);
8232                         _scsih_probe_raid(ioc);
8233                 }
8234         } else
8235                 _scsih_probe_sas(ioc);
8236 }
8237
8238 /**
8239  * scsih_scan_start - scsi lld callback for .scan_start
8240  * @shost: SCSI host pointer
8241  *
8242  * The shost has the ability to discover targets on its own instead
8243  * of scanning the entire bus.  In our implemention, we will kick off
8244  * firmware discovery.
8245  */
8246 void
8247 scsih_scan_start(struct Scsi_Host *shost)
8248 {
8249         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8250         int rc;
8251         if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
8252                 mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
8253
8254         if (disable_discovery > 0)
8255                 return;
8256
8257         ioc->start_scan = 1;
8258         rc = mpt3sas_port_enable(ioc);
8259
8260         if (rc != 0)
8261                 pr_info(MPT3SAS_FMT "port enable: FAILED\n", ioc->name);
8262 }
8263
8264 /**
8265  * scsih_scan_finished - scsi lld callback for .scan_finished
8266  * @shost: SCSI host pointer
8267  * @time: elapsed time of the scan in jiffies
8268  *
8269  * This function will be called periodicallyn until it returns 1 with the
8270  * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
8271  * we wait for firmware discovery to complete, then return 1.
8272  */
8273 int
8274 scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
8275 {
8276         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8277
8278         if (disable_discovery > 0) {
8279                 ioc->is_driver_loading = 0;
8280                 ioc->wait_for_discovery_to_complete = 0;
8281                 return 1;
8282         }
8283
8284         if (time >= (300 * HZ)) {
8285                 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8286                 pr_info(MPT3SAS_FMT
8287                         "port enable: FAILED with timeout (timeout=300s)\n",
8288                         ioc->name);
8289                 ioc->is_driver_loading = 0;
8290                 return 1;
8291         }
8292
8293         if (ioc->start_scan)
8294                 return 0;
8295
8296         if (ioc->start_scan_failed) {
8297                 pr_info(MPT3SAS_FMT
8298                         "port enable: FAILED with (ioc_status=0x%08x)\n",
8299                         ioc->name, ioc->start_scan_failed);
8300                 ioc->is_driver_loading = 0;
8301                 ioc->wait_for_discovery_to_complete = 0;
8302                 ioc->remove_host = 1;
8303                 return 1;
8304         }
8305
8306         pr_info(MPT3SAS_FMT "port enable: SUCCESS\n", ioc->name);
8307         ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8308
8309         if (ioc->wait_for_discovery_to_complete) {
8310                 ioc->wait_for_discovery_to_complete = 0;
8311                 _scsih_probe_devices(ioc);
8312         }
8313         mpt3sas_base_start_watchdog(ioc);
8314         ioc->is_driver_loading = 0;
8315         return 1;
8316 }
8317
8318 /* shost template for SAS 2.0 HBA devices */
8319 static struct scsi_host_template mpt2sas_driver_template = {
8320         .module                         = THIS_MODULE,
8321         .name                           = "Fusion MPT SAS Host",
8322         .proc_name                      = MPT2SAS_DRIVER_NAME,
8323         .queuecommand                   = scsih_qcmd,
8324         .target_alloc                   = scsih_target_alloc,
8325         .slave_alloc                    = scsih_slave_alloc,
8326         .slave_configure                = scsih_slave_configure,
8327         .target_destroy                 = scsih_target_destroy,
8328         .slave_destroy                  = scsih_slave_destroy,
8329         .scan_finished                  = scsih_scan_finished,
8330         .scan_start                     = scsih_scan_start,
8331         .change_queue_depth             = scsih_change_queue_depth,
8332         .eh_abort_handler               = scsih_abort,
8333         .eh_device_reset_handler        = scsih_dev_reset,
8334         .eh_target_reset_handler        = scsih_target_reset,
8335         .eh_host_reset_handler          = scsih_host_reset,
8336         .bios_param                     = scsih_bios_param,
8337         .can_queue                      = 1,
8338         .this_id                        = -1,
8339         .sg_tablesize                   = MPT2SAS_SG_DEPTH,
8340         .max_sectors                    = 32767,
8341         .cmd_per_lun                    = 7,
8342         .use_clustering                 = ENABLE_CLUSTERING,
8343         .shost_attrs                    = mpt3sas_host_attrs,
8344         .sdev_attrs                     = mpt3sas_dev_attrs,
8345         .track_queue_depth              = 1,
8346 };
8347
8348 /* raid transport support for SAS 2.0 HBA devices */
8349 static struct raid_function_template mpt2sas_raid_functions = {
8350         .cookie         = &mpt2sas_driver_template,
8351         .is_raid        = scsih_is_raid,
8352         .get_resync     = scsih_get_resync,
8353         .get_state      = scsih_get_state,
8354 };
8355
8356 /* shost template for SAS 3.0 HBA devices */
8357 static struct scsi_host_template mpt3sas_driver_template = {
8358         .module                         = THIS_MODULE,
8359         .name                           = "Fusion MPT SAS Host",
8360         .proc_name                      = MPT3SAS_DRIVER_NAME,
8361         .queuecommand                   = scsih_qcmd,
8362         .target_alloc                   = scsih_target_alloc,
8363         .slave_alloc                    = scsih_slave_alloc,
8364         .slave_configure                = scsih_slave_configure,
8365         .target_destroy                 = scsih_target_destroy,
8366         .slave_destroy                  = scsih_slave_destroy,
8367         .scan_finished                  = scsih_scan_finished,
8368         .scan_start                     = scsih_scan_start,
8369         .change_queue_depth             = scsih_change_queue_depth,
8370         .eh_abort_handler               = scsih_abort,
8371         .eh_device_reset_handler        = scsih_dev_reset,
8372         .eh_target_reset_handler        = scsih_target_reset,
8373         .eh_host_reset_handler          = scsih_host_reset,
8374         .bios_param                     = scsih_bios_param,
8375         .can_queue                      = 1,
8376         .this_id                        = -1,
8377         .sg_tablesize                   = MPT3SAS_SG_DEPTH,
8378         .max_sectors                    = 32767,
8379         .cmd_per_lun                    = 7,
8380         .use_clustering                 = ENABLE_CLUSTERING,
8381         .shost_attrs                    = mpt3sas_host_attrs,
8382         .sdev_attrs                     = mpt3sas_dev_attrs,
8383         .track_queue_depth              = 1,
8384 };
8385
8386 /* raid transport support for SAS 3.0 HBA devices */
8387 static struct raid_function_template mpt3sas_raid_functions = {
8388         .cookie         = &mpt3sas_driver_template,
8389         .is_raid        = scsih_is_raid,
8390         .get_resync     = scsih_get_resync,
8391         .get_state      = scsih_get_state,
8392 };
8393
8394 /**
8395  * _scsih_determine_hba_mpi_version - determine in which MPI version class
8396  *                                      this device belongs to.
8397  * @pdev: PCI device struct
8398  *
8399  * return MPI2_VERSION for SAS 2.0 HBA devices,
8400  *      MPI25_VERSION for SAS 3.0 HBA devices.
8401  */
8402 u16
8403 _scsih_determine_hba_mpi_version(struct pci_dev *pdev)
8404 {
8405
8406         switch (pdev->device) {
8407         case MPI2_MFGPAGE_DEVID_SSS6200:
8408         case MPI2_MFGPAGE_DEVID_SAS2004:
8409         case MPI2_MFGPAGE_DEVID_SAS2008:
8410         case MPI2_MFGPAGE_DEVID_SAS2108_1:
8411         case MPI2_MFGPAGE_DEVID_SAS2108_2:
8412         case MPI2_MFGPAGE_DEVID_SAS2108_3:
8413         case MPI2_MFGPAGE_DEVID_SAS2116_1:
8414         case MPI2_MFGPAGE_DEVID_SAS2116_2:
8415         case MPI2_MFGPAGE_DEVID_SAS2208_1:
8416         case MPI2_MFGPAGE_DEVID_SAS2208_2:
8417         case MPI2_MFGPAGE_DEVID_SAS2208_3:
8418         case MPI2_MFGPAGE_DEVID_SAS2208_4:
8419         case MPI2_MFGPAGE_DEVID_SAS2208_5:
8420         case MPI2_MFGPAGE_DEVID_SAS2208_6:
8421         case MPI2_MFGPAGE_DEVID_SAS2308_1:
8422         case MPI2_MFGPAGE_DEVID_SAS2308_2:
8423         case MPI2_MFGPAGE_DEVID_SAS2308_3:
8424                 return MPI2_VERSION;
8425         case MPI25_MFGPAGE_DEVID_SAS3004:
8426         case MPI25_MFGPAGE_DEVID_SAS3008:
8427         case MPI25_MFGPAGE_DEVID_SAS3108_1:
8428         case MPI25_MFGPAGE_DEVID_SAS3108_2:
8429         case MPI25_MFGPAGE_DEVID_SAS3108_5:
8430         case MPI25_MFGPAGE_DEVID_SAS3108_6:
8431                 return MPI25_VERSION;
8432         }
8433         return 0;
8434 }
8435
8436 /**
8437  * _scsih_probe - attach and add scsi host
8438  * @pdev: PCI device struct
8439  * @id: pci device id
8440  *
8441  * Returns 0 success, anything else error.
8442  */
8443 int
8444 _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
8445 {
8446         struct MPT3SAS_ADAPTER *ioc;
8447         struct Scsi_Host *shost = NULL;
8448         int rv;
8449         u16 hba_mpi_version;
8450
8451         /* Determine in which MPI version class this pci device belongs */
8452         hba_mpi_version = _scsih_determine_hba_mpi_version(pdev);
8453         if (hba_mpi_version == 0)
8454                 return -ENODEV;
8455
8456         /* Enumerate only SAS 2.0 HBA's if hbas_to_enumerate is one,
8457          * for other generation HBA's return with -ENODEV
8458          */
8459         if ((hbas_to_enumerate == 1) && (hba_mpi_version !=  MPI2_VERSION))
8460                 return -ENODEV;
8461
8462         /* Enumerate only SAS 3.0 HBA's if hbas_to_enumerate is two,
8463          * for other generation HBA's return with -ENODEV
8464          */
8465         if ((hbas_to_enumerate == 2) && (hba_mpi_version !=  MPI25_VERSION))
8466                 return -ENODEV;
8467
8468         switch (hba_mpi_version) {
8469         case MPI2_VERSION:
8470                 /* Use mpt2sas driver host template for SAS 2.0 HBA's */
8471                 shost = scsi_host_alloc(&mpt2sas_driver_template,
8472                   sizeof(struct MPT3SAS_ADAPTER));
8473                 if (!shost)
8474                         return -ENODEV;
8475                 ioc = shost_priv(shost);
8476                 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8477                 ioc->hba_mpi_version_belonged = hba_mpi_version;
8478                 ioc->id = mpt2_ids++;
8479                 sprintf(ioc->driver_name, "%s", MPT2SAS_DRIVER_NAME);
8480                 if (pdev->device == MPI2_MFGPAGE_DEVID_SSS6200) {
8481                         ioc->is_warpdrive = 1;
8482                         ioc->hide_ir_msg = 1;
8483                 } else
8484                         ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
8485                 break;
8486         case MPI25_VERSION:
8487                 /* Use mpt3sas driver host template for SAS 3.0 HBA's */
8488                 shost = scsi_host_alloc(&mpt3sas_driver_template,
8489                   sizeof(struct MPT3SAS_ADAPTER));
8490                 if (!shost)
8491                         return -ENODEV;
8492                 ioc = shost_priv(shost);
8493                 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8494                 ioc->hba_mpi_version_belonged = hba_mpi_version;
8495                 ioc->id = mpt3_ids++;
8496                 sprintf(ioc->driver_name, "%s", MPT3SAS_DRIVER_NAME);
8497                 if (pdev->revision >= SAS3_PCI_DEVICE_C0_REVISION)
8498                         ioc->msix96_vector = 1;
8499                 break;
8500         default:
8501                 return -ENODEV;
8502         }
8503
8504         INIT_LIST_HEAD(&ioc->list);
8505         spin_lock(&gioc_lock);
8506         list_add_tail(&ioc->list, &mpt3sas_ioc_list);
8507         spin_unlock(&gioc_lock);
8508         ioc->shost = shost;
8509         ioc->pdev = pdev;
8510         ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8511         ioc->tm_cb_idx = tm_cb_idx;
8512         ioc->ctl_cb_idx = ctl_cb_idx;
8513         ioc->base_cb_idx = base_cb_idx;
8514         ioc->port_enable_cb_idx = port_enable_cb_idx;
8515         ioc->transport_cb_idx = transport_cb_idx;
8516         ioc->scsih_cb_idx = scsih_cb_idx;
8517         ioc->config_cb_idx = config_cb_idx;
8518         ioc->tm_tr_cb_idx = tm_tr_cb_idx;
8519         ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
8520         ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
8521         ioc->logging_level = logging_level;
8522         ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
8523         /* misc semaphores and spin locks */
8524         mutex_init(&ioc->reset_in_progress_mutex);
8525         /* initializing pci_access_mutex lock */
8526         mutex_init(&ioc->pci_access_mutex);
8527         spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8528         spin_lock_init(&ioc->scsi_lookup_lock);
8529         spin_lock_init(&ioc->sas_device_lock);
8530         spin_lock_init(&ioc->sas_node_lock);
8531         spin_lock_init(&ioc->fw_event_lock);
8532         spin_lock_init(&ioc->raid_device_lock);
8533         spin_lock_init(&ioc->diag_trigger_lock);
8534
8535         INIT_LIST_HEAD(&ioc->sas_device_list);
8536         INIT_LIST_HEAD(&ioc->sas_device_init_list);
8537         INIT_LIST_HEAD(&ioc->sas_expander_list);
8538         INIT_LIST_HEAD(&ioc->fw_event_list);
8539         INIT_LIST_HEAD(&ioc->raid_device_list);
8540         INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
8541         INIT_LIST_HEAD(&ioc->delayed_tr_list);
8542         INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
8543         INIT_LIST_HEAD(&ioc->reply_queue_list);
8544
8545         sprintf(ioc->name, "%s_cm%d", ioc->driver_name, ioc->id);
8546
8547         /* init shost parameters */
8548         shost->max_cmd_len = 32;
8549         shost->max_lun = max_lun;
8550         shost->transportt = mpt3sas_transport_template;
8551         shost->unique_id = ioc->id;
8552
8553         if (max_sectors != 0xFFFF) {
8554                 if (max_sectors < 64) {
8555                         shost->max_sectors = 64;
8556                         pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8557                             "for max_sectors, range is 64 to 32767. Assigning "
8558                             "value of 64.\n", ioc->name, max_sectors);
8559                 } else if (max_sectors > 32767) {
8560                         shost->max_sectors = 32767;
8561                         pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8562                             "for max_sectors, range is 64 to 32767. Assigning "
8563                             "default value of 32767.\n", ioc->name,
8564                             max_sectors);
8565                 } else {
8566                         shost->max_sectors = max_sectors & 0xFFFE;
8567                         pr_info(MPT3SAS_FMT
8568                                 "The max_sectors value is set to %d\n",
8569                                 ioc->name, shost->max_sectors);
8570                 }
8571         }
8572
8573         /* register EEDP capabilities with SCSI layer */
8574         if (prot_mask > 0)
8575                 scsi_host_set_prot(shost, prot_mask);
8576         else
8577                 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8578                                    | SHOST_DIF_TYPE2_PROTECTION
8579                                    | SHOST_DIF_TYPE3_PROTECTION);
8580
8581         scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
8582
8583         /* event thread */
8584         snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
8585             "fw_event_%s%d", ioc->driver_name, ioc->id);
8586         ioc->firmware_event_thread = alloc_ordered_workqueue(
8587             ioc->firmware_event_name, WQ_MEM_RECLAIM);
8588         if (!ioc->firmware_event_thread) {
8589                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8590                     ioc->name, __FILE__, __LINE__, __func__);
8591                 rv = -ENODEV;
8592                 goto out_thread_fail;
8593         }
8594
8595         ioc->is_driver_loading = 1;
8596         if ((mpt3sas_base_attach(ioc))) {
8597                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8598                     ioc->name, __FILE__, __LINE__, __func__);
8599                 rv = -ENODEV;
8600                 goto out_attach_fail;
8601         }
8602
8603         if (ioc->is_warpdrive) {
8604                 if (ioc->mfg_pg10_hide_flag ==  MFG_PAGE10_EXPOSE_ALL_DISKS)
8605                         ioc->hide_drives = 0;
8606                 else if (ioc->mfg_pg10_hide_flag ==  MFG_PAGE10_HIDE_ALL_DISKS)
8607                         ioc->hide_drives = 1;
8608                 else {
8609                         if (mpt3sas_get_num_volumes(ioc))
8610                                 ioc->hide_drives = 1;
8611                         else
8612                                 ioc->hide_drives = 0;
8613                 }
8614         } else
8615                 ioc->hide_drives = 0;
8616
8617         rv = scsi_add_host(shost, &pdev->dev);
8618         if (rv) {
8619                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8620                     ioc->name, __FILE__, __LINE__, __func__);
8621                 goto out_add_shost_fail;
8622         }
8623
8624         scsi_scan_host(shost);
8625         return 0;
8626 out_add_shost_fail:
8627         mpt3sas_base_detach(ioc);
8628  out_attach_fail:
8629         destroy_workqueue(ioc->firmware_event_thread);
8630  out_thread_fail:
8631         spin_lock(&gioc_lock);
8632         list_del(&ioc->list);
8633         spin_unlock(&gioc_lock);
8634         scsi_host_put(shost);
8635         return rv;
8636 }
8637
8638 #ifdef CONFIG_PM
8639 /**
8640  * scsih_suspend - power management suspend main entry point
8641  * @pdev: PCI device struct
8642  * @state: PM state change to (usually PCI_D3)
8643  *
8644  * Returns 0 success, anything else error.
8645  */
8646 int
8647 scsih_suspend(struct pci_dev *pdev, pm_message_t state)
8648 {
8649         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8650         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8651         pci_power_t device_state;
8652
8653         mpt3sas_base_stop_watchdog(ioc);
8654         flush_scheduled_work();
8655         scsi_block_requests(shost);
8656         device_state = pci_choose_state(pdev, state);
8657         pr_info(MPT3SAS_FMT
8658                 "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
8659                 ioc->name, pdev, pci_name(pdev), device_state);
8660
8661         pci_save_state(pdev);
8662         mpt3sas_base_free_resources(ioc);
8663         pci_set_power_state(pdev, device_state);
8664         return 0;
8665 }
8666
8667 /**
8668  * scsih_resume - power management resume main entry point
8669  * @pdev: PCI device struct
8670  *
8671  * Returns 0 success, anything else error.
8672  */
8673 int
8674 scsih_resume(struct pci_dev *pdev)
8675 {
8676         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8677         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8678         pci_power_t device_state = pdev->current_state;
8679         int r;
8680
8681         pr_info(MPT3SAS_FMT
8682                 "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
8683                 ioc->name, pdev, pci_name(pdev), device_state);
8684
8685         pci_set_power_state(pdev, PCI_D0);
8686         pci_enable_wake(pdev, PCI_D0, 0);
8687         pci_restore_state(pdev);
8688         ioc->pdev = pdev;
8689         r = mpt3sas_base_map_resources(ioc);
8690         if (r)
8691                 return r;
8692
8693         mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8694         scsi_unblock_requests(shost);
8695         mpt3sas_base_start_watchdog(ioc);
8696         return 0;
8697 }
8698 #endif /* CONFIG_PM */
8699
8700 /**
8701  * scsih_pci_error_detected - Called when a PCI error is detected.
8702  * @pdev: PCI device struct
8703  * @state: PCI channel state
8704  *
8705  * Description: Called when a PCI error is detected.
8706  *
8707  * Return value:
8708  *      PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8709  */
8710 pci_ers_result_t
8711 scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8712 {
8713         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8714         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8715
8716         pr_info(MPT3SAS_FMT "PCI error: detected callback, state(%d)!!\n",
8717             ioc->name, state);
8718
8719         switch (state) {
8720         case pci_channel_io_normal:
8721                 return PCI_ERS_RESULT_CAN_RECOVER;
8722         case pci_channel_io_frozen:
8723                 /* Fatal error, prepare for slot reset */
8724                 ioc->pci_error_recovery = 1;
8725                 scsi_block_requests(ioc->shost);
8726                 mpt3sas_base_stop_watchdog(ioc);
8727                 mpt3sas_base_free_resources(ioc);
8728                 return PCI_ERS_RESULT_NEED_RESET;
8729         case pci_channel_io_perm_failure:
8730                 /* Permanent error, prepare for device removal */
8731                 ioc->pci_error_recovery = 1;
8732                 mpt3sas_base_stop_watchdog(ioc);
8733                 _scsih_flush_running_cmds(ioc);
8734                 return PCI_ERS_RESULT_DISCONNECT;
8735         }
8736         return PCI_ERS_RESULT_NEED_RESET;
8737 }
8738
8739 /**
8740  * scsih_pci_slot_reset - Called when PCI slot has been reset.
8741  * @pdev: PCI device struct
8742  *
8743  * Description: This routine is called by the pci error recovery
8744  * code after the PCI slot has been reset, just before we
8745  * should resume normal operations.
8746  */
8747 pci_ers_result_t
8748 scsih_pci_slot_reset(struct pci_dev *pdev)
8749 {
8750         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8751         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8752         int rc;
8753
8754         pr_info(MPT3SAS_FMT "PCI error: slot reset callback!!\n",
8755              ioc->name);
8756
8757         ioc->pci_error_recovery = 0;
8758         ioc->pdev = pdev;
8759         pci_restore_state(pdev);
8760         rc = mpt3sas_base_map_resources(ioc);
8761         if (rc)
8762                 return PCI_ERS_RESULT_DISCONNECT;
8763
8764         rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8765             FORCE_BIG_HAMMER);
8766
8767         pr_warn(MPT3SAS_FMT "hard reset: %s\n", ioc->name,
8768             (rc == 0) ? "success" : "failed");
8769
8770         if (!rc)
8771                 return PCI_ERS_RESULT_RECOVERED;
8772         else
8773                 return PCI_ERS_RESULT_DISCONNECT;
8774 }
8775
8776 /**
8777  * scsih_pci_resume() - resume normal ops after PCI reset
8778  * @pdev: pointer to PCI device
8779  *
8780  * Called when the error recovery driver tells us that its
8781  * OK to resume normal operation. Use completion to allow
8782  * halted scsi ops to resume.
8783  */
8784 void
8785 scsih_pci_resume(struct pci_dev *pdev)
8786 {
8787         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8788         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8789
8790         pr_info(MPT3SAS_FMT "PCI error: resume callback!!\n", ioc->name);
8791
8792         pci_cleanup_aer_uncorrect_error_status(pdev);
8793         mpt3sas_base_start_watchdog(ioc);
8794         scsi_unblock_requests(ioc->shost);
8795 }
8796
8797 /**
8798  * scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
8799  * @pdev: pointer to PCI device
8800  */
8801 pci_ers_result_t
8802 scsih_pci_mmio_enabled(struct pci_dev *pdev)
8803 {
8804         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8805         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8806
8807         pr_info(MPT3SAS_FMT "PCI error: mmio enabled callback!!\n",
8808             ioc->name);
8809
8810         /* TODO - dump whatever for debugging purposes */
8811
8812         /* Request a slot reset. */
8813         return PCI_ERS_RESULT_NEED_RESET;
8814 }
8815
8816 /*
8817  * The pci device ids are defined in mpi/mpi2_cnfg.h.
8818  */
8819 static const struct pci_device_id mpt3sas_pci_table[] = {
8820         /* Spitfire ~ 2004 */
8821         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
8822                 PCI_ANY_ID, PCI_ANY_ID },
8823         /* Falcon ~ 2008 */
8824         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
8825                 PCI_ANY_ID, PCI_ANY_ID },
8826         /* Liberator ~ 2108 */
8827         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
8828                 PCI_ANY_ID, PCI_ANY_ID },
8829         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
8830                 PCI_ANY_ID, PCI_ANY_ID },
8831         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
8832                 PCI_ANY_ID, PCI_ANY_ID },
8833         /* Meteor ~ 2116 */
8834         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
8835                 PCI_ANY_ID, PCI_ANY_ID },
8836         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
8837                 PCI_ANY_ID, PCI_ANY_ID },
8838         /* Thunderbolt ~ 2208 */
8839         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
8840                 PCI_ANY_ID, PCI_ANY_ID },
8841         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
8842                 PCI_ANY_ID, PCI_ANY_ID },
8843         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
8844                 PCI_ANY_ID, PCI_ANY_ID },
8845         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
8846                 PCI_ANY_ID, PCI_ANY_ID },
8847         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
8848                 PCI_ANY_ID, PCI_ANY_ID },
8849         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
8850                 PCI_ANY_ID, PCI_ANY_ID },
8851         /* Mustang ~ 2308 */
8852         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
8853                 PCI_ANY_ID, PCI_ANY_ID },
8854         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
8855                 PCI_ANY_ID, PCI_ANY_ID },
8856         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
8857                 PCI_ANY_ID, PCI_ANY_ID },
8858         /* SSS6200 */
8859         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
8860                 PCI_ANY_ID, PCI_ANY_ID },
8861         /* Fury ~ 3004 and 3008 */
8862         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
8863                 PCI_ANY_ID, PCI_ANY_ID },
8864         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
8865                 PCI_ANY_ID, PCI_ANY_ID },
8866         /* Invader ~ 3108 */
8867         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
8868                 PCI_ANY_ID, PCI_ANY_ID },
8869         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
8870                 PCI_ANY_ID, PCI_ANY_ID },
8871         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
8872                 PCI_ANY_ID, PCI_ANY_ID },
8873         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
8874                 PCI_ANY_ID, PCI_ANY_ID },
8875         {0}     /* Terminating entry */
8876 };
8877 MODULE_DEVICE_TABLE(pci, mpt3sas_pci_table);
8878
8879 static struct pci_error_handlers _mpt3sas_err_handler = {
8880         .error_detected = scsih_pci_error_detected,
8881         .mmio_enabled   = scsih_pci_mmio_enabled,
8882         .slot_reset     = scsih_pci_slot_reset,
8883         .resume         = scsih_pci_resume,
8884 };
8885
8886 static struct pci_driver mpt3sas_driver = {
8887         .name           = MPT3SAS_DRIVER_NAME,
8888         .id_table       = mpt3sas_pci_table,
8889         .probe          = _scsih_probe,
8890         .remove         = scsih_remove,
8891         .shutdown       = scsih_shutdown,
8892         .err_handler    = &_mpt3sas_err_handler,
8893 #ifdef CONFIG_PM
8894         .suspend        = scsih_suspend,
8895         .resume         = scsih_resume,
8896 #endif
8897 };
8898
8899 /**
8900  * scsih_init - main entry point for this driver.
8901  *
8902  * Returns 0 success, anything else error.
8903  */
8904 int
8905 scsih_init(void)
8906 {
8907         mpt2_ids = 0;
8908         mpt3_ids = 0;
8909
8910         mpt3sas_base_initialize_callback_handler();
8911
8912          /* queuecommand callback hander */
8913         scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);
8914
8915         /* task managment callback handler */
8916         tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);
8917
8918         /* base internal commands callback handler */
8919         base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
8920         port_enable_cb_idx = mpt3sas_base_register_callback_handler(
8921             mpt3sas_port_enable_done);
8922
8923         /* transport internal commands callback handler */
8924         transport_cb_idx = mpt3sas_base_register_callback_handler(
8925             mpt3sas_transport_done);
8926
8927         /* scsih internal commands callback handler */
8928         scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);
8929
8930         /* configuration page API internal commands callback handler */
8931         config_cb_idx = mpt3sas_base_register_callback_handler(
8932             mpt3sas_config_done);
8933
8934         /* ctl module callback handler */
8935         ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);
8936
8937         tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
8938             _scsih_tm_tr_complete);
8939
8940         tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
8941             _scsih_tm_volume_tr_complete);
8942
8943         tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
8944             _scsih_sas_control_complete);
8945
8946         return 0;
8947 }
8948
8949 /**
8950  * scsih_exit - exit point for this driver (when it is a module).
8951  *
8952  * Returns 0 success, anything else error.
8953  */
8954 void
8955 scsih_exit(void)
8956 {
8957
8958         mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
8959         mpt3sas_base_release_callback_handler(tm_cb_idx);
8960         mpt3sas_base_release_callback_handler(base_cb_idx);
8961         mpt3sas_base_release_callback_handler(port_enable_cb_idx);
8962         mpt3sas_base_release_callback_handler(transport_cb_idx);
8963         mpt3sas_base_release_callback_handler(scsih_cb_idx);
8964         mpt3sas_base_release_callback_handler(config_cb_idx);
8965         mpt3sas_base_release_callback_handler(ctl_cb_idx);
8966
8967         mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
8968         mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
8969         mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);
8970
8971 /* raid transport support */
8972         if (hbas_to_enumerate != 1)
8973                 raid_class_release(mpt3sas_raid_template);
8974         if (hbas_to_enumerate != 2)
8975                 raid_class_release(mpt2sas_raid_template);
8976         sas_release_transport(mpt3sas_transport_template);
8977 }
8978
8979 /**
8980  * _mpt3sas_init - main entry point for this driver.
8981  *
8982  * Returns 0 success, anything else error.
8983  */
8984 static int __init
8985 _mpt3sas_init(void)
8986 {
8987         int error;
8988
8989         pr_info("%s version %s loaded\n", MPT3SAS_DRIVER_NAME,
8990                                         MPT3SAS_DRIVER_VERSION);
8991
8992         mpt3sas_transport_template =
8993             sas_attach_transport(&mpt3sas_transport_functions);
8994         if (!mpt3sas_transport_template)
8995                 return -ENODEV;
8996
8997         /* No need attach mpt3sas raid functions template
8998          * if hbas_to_enumarate value is one.
8999          */
9000         if (hbas_to_enumerate != 1) {
9001                 mpt3sas_raid_template =
9002                                 raid_class_attach(&mpt3sas_raid_functions);
9003                 if (!mpt3sas_raid_template) {
9004                         sas_release_transport(mpt3sas_transport_template);
9005                         return -ENODEV;
9006                 }
9007         }
9008
9009         /* No need to attach mpt2sas raid functions template
9010          * if hbas_to_enumarate value is two
9011          */
9012         if (hbas_to_enumerate != 2) {
9013                 mpt2sas_raid_template =
9014                                 raid_class_attach(&mpt2sas_raid_functions);
9015                 if (!mpt2sas_raid_template) {
9016                         sas_release_transport(mpt3sas_transport_template);
9017                         return -ENODEV;
9018                 }
9019         }
9020
9021         error = scsih_init();
9022         if (error) {
9023                 scsih_exit();
9024                 return error;
9025         }
9026
9027         mpt3sas_ctl_init(hbas_to_enumerate);
9028
9029         error = pci_register_driver(&mpt3sas_driver);
9030         if (error)
9031                 scsih_exit();
9032
9033         return error;
9034 }
9035
9036 /**
9037  * _mpt3sas_exit - exit point for this driver (when it is a module).
9038  *
9039  */
9040 static void __exit
9041 _mpt3sas_exit(void)
9042 {
9043         pr_info("mpt3sas version %s unloading\n",
9044                                 MPT3SAS_DRIVER_VERSION);
9045
9046         pci_unregister_driver(&mpt3sas_driver);
9047
9048         mpt3sas_ctl_exit(hbas_to_enumerate);
9049
9050         scsih_exit();
9051 }
9052
9053 module_init(_mpt3sas_init);
9054 module_exit(_mpt3sas_exit);