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