Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / 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
3258 /**
3259  * _scsih_sas_control_complete - completion routine
3260  * @ioc: per adapter object
3261  * @smid: system request message index
3262  * @msix_index: MSIX table index supplied by the OS
3263  * @reply: reply message frame(lower 32bit addr)
3264  * Context: interrupt time.
3265  *
3266  * This is the sas iounit control completion routine.
3267  * This code is part of the code to initiate the device removal
3268  * handshake protocol with controller firmware.
3269  *
3270  * Return 1 meaning mf should be freed from _base_interrupt
3271  *        0 means the mf is freed from this function.
3272  */
3273 static u8
3274 _scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3275         u8 msix_index, u32 reply)
3276 {
3277         Mpi2SasIoUnitControlReply_t *mpi_reply =
3278             mpt3sas_base_get_reply_virt_addr(ioc, reply);
3279
3280         if (likely(mpi_reply)) {
3281                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3282                 "sc_complete:handle(0x%04x), (open) "
3283                 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3284                 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3285                 le16_to_cpu(mpi_reply->IOCStatus),
3286                 le32_to_cpu(mpi_reply->IOCLogInfo)));
3287         } else {
3288                 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3289                     ioc->name, __FILE__, __LINE__, __func__);
3290         }
3291         return 1;
3292 }
3293
3294 /**
3295  * _scsih_tm_tr_volume_send - send target reset request for volumes
3296  * @ioc: per adapter object
3297  * @handle: device handle
3298  * Context: interrupt time.
3299  *
3300  * This is designed to send muliple task management request at the same
3301  * time to the fifo. If the fifo is full, we will append the request,
3302  * and process it in a future completion.
3303  */
3304 static void
3305 _scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3306 {
3307         Mpi2SCSITaskManagementRequest_t *mpi_request;
3308         u16 smid;
3309         struct _tr_list *delayed_tr;
3310
3311         if (ioc->shost_recovery || ioc->remove_host ||
3312             ioc->pci_error_recovery) {
3313                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3314                         "%s: host reset in progress!\n",
3315                         __func__, ioc->name));
3316                 return;
3317         }
3318
3319         smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3320         if (!smid) {
3321                 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3322                 if (!delayed_tr)
3323                         return;
3324                 INIT_LIST_HEAD(&delayed_tr->list);
3325                 delayed_tr->handle = handle;
3326                 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3327                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3328                     "DELAYED:tr:handle(0x%04x), (open)\n",
3329                     ioc->name, handle));
3330                 return;
3331         }
3332
3333         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3334                 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3335                 ioc->name, handle, smid,
3336             ioc->tm_tr_volume_cb_idx));
3337         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3338         memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3339         mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3340         mpi_request->DevHandle = cpu_to_le16(handle);
3341         mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3342         mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
3343 }
3344
3345 /**
3346  * _scsih_tm_volume_tr_complete - target reset completion
3347  * @ioc: per adapter object
3348  * @smid: system request message index
3349  * @msix_index: MSIX table index supplied by the OS
3350  * @reply: reply message frame(lower 32bit addr)
3351  * Context: interrupt time.
3352  *
3353  * Return 1 meaning mf should be freed from _base_interrupt
3354  *        0 means the mf is freed from this function.
3355  */
3356 static u8
3357 _scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3358         u8 msix_index, u32 reply)
3359 {
3360         u16 handle;
3361         Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3362         Mpi2SCSITaskManagementReply_t *mpi_reply =
3363             mpt3sas_base_get_reply_virt_addr(ioc, reply);
3364
3365         if (ioc->shost_recovery || ioc->remove_host ||
3366             ioc->pci_error_recovery) {
3367                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3368                         "%s: host reset in progress!\n",
3369                         __func__, ioc->name));
3370                 return 1;
3371         }
3372         if (unlikely(!mpi_reply)) {
3373                 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3374                     ioc->name, __FILE__, __LINE__, __func__);
3375                 return 1;
3376         }
3377
3378         mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3379         handle = le16_to_cpu(mpi_request_tm->DevHandle);
3380         if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3381                 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3382                         "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3383                         ioc->name, handle,
3384                     le16_to_cpu(mpi_reply->DevHandle), smid));
3385                 return 0;
3386         }
3387
3388         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3389             "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3390             "loginfo(0x%08x), completed(%d)\n", ioc->name,
3391             handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3392             le32_to_cpu(mpi_reply->IOCLogInfo),
3393             le32_to_cpu(mpi_reply->TerminationCount)));
3394
3395         return _scsih_check_for_pending_tm(ioc, smid);
3396 }
3397
3398
3399 /**
3400  * _scsih_check_for_pending_tm - check for pending task management
3401  * @ioc: per adapter object
3402  * @smid: system request message index
3403  *
3404  * This will check delayed target reset list, and feed the
3405  * next reqeust.
3406  *
3407  * Return 1 meaning mf should be freed from _base_interrupt
3408  *        0 means the mf is freed from this function.
3409  */
3410 static u8
3411 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3412 {
3413         struct _tr_list *delayed_tr;
3414
3415         if (!list_empty(&ioc->delayed_tr_volume_list)) {
3416                 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3417                     struct _tr_list, list);
3418                 mpt3sas_base_free_smid(ioc, smid);
3419                 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3420                 list_del(&delayed_tr->list);
3421                 kfree(delayed_tr);
3422                 return 0;
3423         }
3424
3425         if (!list_empty(&ioc->delayed_tr_list)) {
3426                 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3427                     struct _tr_list, list);
3428                 mpt3sas_base_free_smid(ioc, smid);
3429                 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3430                 list_del(&delayed_tr->list);
3431                 kfree(delayed_tr);
3432                 return 0;
3433         }
3434
3435         return 1;
3436 }
3437
3438 /**
3439  * _scsih_check_topo_delete_events - sanity check on topo events
3440  * @ioc: per adapter object
3441  * @event_data: the event data payload
3442  *
3443  * This routine added to better handle cable breaker.
3444  *
3445  * This handles the case where driver receives multiple expander
3446  * add and delete events in a single shot.  When there is a delete event
3447  * the routine will void any pending add events waiting in the event queue.
3448  *
3449  * Return nothing.
3450  */
3451 static void
3452 _scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
3453         Mpi2EventDataSasTopologyChangeList_t *event_data)
3454 {
3455         struct fw_event_work *fw_event;
3456         Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3457         u16 expander_handle;
3458         struct _sas_node *sas_expander;
3459         unsigned long flags;
3460         int i, reason_code;
3461         u16 handle;
3462
3463         for (i = 0 ; i < event_data->NumEntries; i++) {
3464                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3465                 if (!handle)
3466                         continue;
3467                 reason_code = event_data->PHY[i].PhyStatus &
3468                     MPI2_EVENT_SAS_TOPO_RC_MASK;
3469                 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3470                         _scsih_tm_tr_send(ioc, handle);
3471         }
3472
3473         expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3474         if (expander_handle < ioc->sas_hba.num_phys) {
3475                 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3476                 return;
3477         }
3478         if (event_data->ExpStatus ==
3479             MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3480                 /* put expander attached devices into blocking state */
3481                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3482                 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
3483                     expander_handle);
3484                 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3485                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3486                 do {
3487                         handle = find_first_bit(ioc->blocking_handles,
3488                             ioc->facts.MaxDevHandle);
3489                         if (handle < ioc->facts.MaxDevHandle)
3490                                 _scsih_block_io_device(ioc, handle);
3491                 } while (test_and_clear_bit(handle, ioc->blocking_handles));
3492         } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3493                 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3494
3495         if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3496                 return;
3497
3498         /* mark ignore flag for pending events */
3499         spin_lock_irqsave(&ioc->fw_event_lock, flags);
3500         list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3501                 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3502                     fw_event->ignore)
3503                         continue;
3504                 local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3505                                    fw_event->event_data;
3506                 if (local_event_data->ExpStatus ==
3507                     MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3508                     local_event_data->ExpStatus ==
3509                     MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3510                         if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3511                             expander_handle) {
3512                                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3513                                     "setting ignoring flag\n", ioc->name));
3514                                 fw_event->ignore = 1;
3515                         }
3516                 }
3517         }
3518         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3519 }
3520
3521 /**
3522  * _scsih_set_volume_delete_flag - setting volume delete flag
3523  * @ioc: per adapter object
3524  * @handle: device handle
3525  *
3526  * This returns nothing.
3527  */
3528 static void
3529 _scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3530 {
3531         struct _raid_device *raid_device;
3532         struct MPT3SAS_TARGET *sas_target_priv_data;
3533         unsigned long flags;
3534
3535         spin_lock_irqsave(&ioc->raid_device_lock, flags);
3536         raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
3537         if (raid_device && raid_device->starget &&
3538             raid_device->starget->hostdata) {
3539                 sas_target_priv_data =
3540                     raid_device->starget->hostdata;
3541                 sas_target_priv_data->deleted = 1;
3542                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3543                     "setting delete flag: handle(0x%04x), "
3544                     "wwid(0x%016llx)\n", ioc->name, handle,
3545                     (unsigned long long) raid_device->wwid));
3546         }
3547         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3548 }
3549
3550 /**
3551  * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3552  * @handle: input handle
3553  * @a: handle for volume a
3554  * @b: handle for volume b
3555  *
3556  * IR firmware only supports two raid volumes.  The purpose of this
3557  * routine is to set the volume handle in either a or b. When the given
3558  * input handle is non-zero, or when a and b have not been set before.
3559  */
3560 static void
3561 _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3562 {
3563         if (!handle || handle == *a || handle == *b)
3564                 return;
3565         if (!*a)
3566                 *a = handle;
3567         else if (!*b)
3568                 *b = handle;
3569 }
3570
3571 /**
3572  * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3573  * @ioc: per adapter object
3574  * @event_data: the event data payload
3575  * Context: interrupt time.
3576  *
3577  * This routine will send target reset to volume, followed by target
3578  * resets to the PDs. This is called when a PD has been removed, or
3579  * volume has been deleted or removed. When the target reset is sent
3580  * to volume, the PD target resets need to be queued to start upon
3581  * completion of the volume target reset.
3582  *
3583  * Return nothing.
3584  */
3585 static void
3586 _scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
3587         Mpi2EventDataIrConfigChangeList_t *event_data)
3588 {
3589         Mpi2EventIrConfigElement_t *element;
3590         int i;
3591         u16 handle, volume_handle, a, b;
3592         struct _tr_list *delayed_tr;
3593
3594         a = 0;
3595         b = 0;
3596
3597         if (ioc->is_warpdrive)
3598                 return;
3599
3600         /* Volume Resets for Deleted or Removed */
3601         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3602         for (i = 0; i < event_data->NumElements; i++, element++) {
3603                 if (le32_to_cpu(event_data->Flags) &
3604                     MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3605                         continue;
3606                 if (element->ReasonCode ==
3607                     MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3608                     element->ReasonCode ==
3609                     MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3610                         volume_handle = le16_to_cpu(element->VolDevHandle);
3611                         _scsih_set_volume_delete_flag(ioc, volume_handle);
3612                         _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3613                 }
3614         }
3615
3616         /* Volume Resets for UNHIDE events */
3617         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3618         for (i = 0; i < event_data->NumElements; i++, element++) {
3619                 if (le32_to_cpu(event_data->Flags) &
3620                     MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3621                         continue;
3622                 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3623                         volume_handle = le16_to_cpu(element->VolDevHandle);
3624                         _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3625                 }
3626         }
3627
3628         if (a)
3629                 _scsih_tm_tr_volume_send(ioc, a);
3630         if (b)
3631                 _scsih_tm_tr_volume_send(ioc, b);
3632
3633         /* PD target resets */
3634         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3635         for (i = 0; i < event_data->NumElements; i++, element++) {
3636                 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3637                         continue;
3638                 handle = le16_to_cpu(element->PhysDiskDevHandle);
3639                 volume_handle = le16_to_cpu(element->VolDevHandle);
3640                 clear_bit(handle, ioc->pd_handles);
3641                 if (!volume_handle)
3642                         _scsih_tm_tr_send(ioc, handle);
3643                 else if (volume_handle == a || volume_handle == b) {
3644                         delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3645                         BUG_ON(!delayed_tr);
3646                         INIT_LIST_HEAD(&delayed_tr->list);
3647                         delayed_tr->handle = handle;
3648                         list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3649                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3650                             "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3651                             handle));
3652                 } else
3653                         _scsih_tm_tr_send(ioc, handle);
3654         }
3655 }
3656
3657
3658 /**
3659  * _scsih_check_volume_delete_events - set delete flag for volumes
3660  * @ioc: per adapter object
3661  * @event_data: the event data payload
3662  * Context: interrupt time.
3663  *
3664  * This will handle the case when the cable connected to entire volume is
3665  * pulled. We will take care of setting the deleted flag so normal IO will
3666  * not be sent.
3667  *
3668  * Return nothing.
3669  */
3670 static void
3671 _scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
3672         Mpi2EventDataIrVolume_t *event_data)
3673 {
3674         u32 state;
3675
3676         if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3677                 return;
3678         state = le32_to_cpu(event_data->NewValue);
3679         if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3680             MPI2_RAID_VOL_STATE_FAILED)
3681                 _scsih_set_volume_delete_flag(ioc,
3682                     le16_to_cpu(event_data->VolDevHandle));
3683 }
3684
3685 /**
3686  * _scsih_temp_threshold_events - display temperature threshold exceeded events
3687  * @ioc: per adapter object
3688  * @event_data: the temp threshold event data
3689  * Context: interrupt time.
3690  *
3691  * Return nothing.
3692  */
3693 static void
3694 _scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
3695         Mpi2EventDataTemperature_t *event_data)
3696 {
3697         if (ioc->temp_sensors_count >= event_data->SensorNum) {
3698                 pr_err(MPT3SAS_FMT "Temperature Threshold flags %s%s%s%s"
3699                   " exceeded for Sensor: %d !!!\n", ioc->name,
3700                   ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
3701                   ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
3702                   ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
3703                   ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
3704                   event_data->SensorNum);
3705                 pr_err(MPT3SAS_FMT "Current Temp In Celsius: %d\n",
3706                         ioc->name, event_data->CurrentTemperature);
3707         }
3708 }
3709
3710 static inline bool ata_12_16_cmd(struct scsi_cmnd *scmd)
3711 {
3712         return (scmd->cmnd[0] == ATA_12 || scmd->cmnd[0] == ATA_16);
3713 }
3714
3715 /**
3716  * _scsih_flush_running_cmds - completing outstanding commands.
3717  * @ioc: per adapter object
3718  *
3719  * The flushing out of all pending scmd commands following host reset,
3720  * where all IO is dropped to the floor.
3721  *
3722  * Return nothing.
3723  */
3724 static void
3725 _scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
3726 {
3727         struct scsi_cmnd *scmd;
3728         u16 smid;
3729         u16 count = 0;
3730
3731         for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3732                 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3733                 if (!scmd)
3734                         continue;
3735                 count++;
3736                 if (ata_12_16_cmd(scmd))
3737                         scsi_internal_device_unblock(scmd->device,
3738                                                         SDEV_RUNNING);
3739                 mpt3sas_base_free_smid(ioc, smid);
3740                 scsi_dma_unmap(scmd);
3741                 if (ioc->pci_error_recovery)
3742                         scmd->result = DID_NO_CONNECT << 16;
3743                 else
3744                         scmd->result = DID_RESET << 16;
3745                 scmd->scsi_done(scmd);
3746         }
3747         dtmprintk(ioc, pr_info(MPT3SAS_FMT "completing %d cmds\n",
3748             ioc->name, count));
3749 }
3750
3751 /**
3752  * _scsih_setup_eedp - setup MPI request for EEDP transfer
3753  * @ioc: per adapter object
3754  * @scmd: pointer to scsi command object
3755  * @mpi_request: pointer to the SCSI_IO reqest message frame
3756  *
3757  * Supporting protection 1 and 3.
3758  *
3759  * Returns nothing
3760  */
3761 static void
3762 _scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3763         Mpi2SCSIIORequest_t *mpi_request)
3764 {
3765         u16 eedp_flags;
3766         unsigned char prot_op = scsi_get_prot_op(scmd);
3767         unsigned char prot_type = scsi_get_prot_type(scmd);
3768         Mpi25SCSIIORequest_t *mpi_request_3v =
3769            (Mpi25SCSIIORequest_t *)mpi_request;
3770
3771         if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3772                 return;
3773
3774         if (prot_op ==  SCSI_PROT_READ_STRIP)
3775                 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3776         else if (prot_op ==  SCSI_PROT_WRITE_INSERT)
3777                 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3778         else
3779                 return;
3780
3781         switch (prot_type) {
3782         case SCSI_PROT_DIF_TYPE1:
3783         case SCSI_PROT_DIF_TYPE2:
3784
3785                 /*
3786                 * enable ref/guard checking
3787                 * auto increment ref tag
3788                 */
3789                 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3790                     MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3791                     MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3792                 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3793                     cpu_to_be32(scsi_get_lba(scmd));
3794                 break;
3795
3796         case SCSI_PROT_DIF_TYPE3:
3797
3798                 /*
3799                 * enable guard checking
3800                 */
3801                 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3802
3803                 break;
3804         }
3805
3806         mpi_request_3v->EEDPBlockSize =
3807             cpu_to_le16(scmd->device->sector_size);
3808         mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3809 }
3810
3811 /**
3812  * _scsih_eedp_error_handling - return sense code for EEDP errors
3813  * @scmd: pointer to scsi command object
3814  * @ioc_status: ioc status
3815  *
3816  * Returns nothing
3817  */
3818 static void
3819 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3820 {
3821         u8 ascq;
3822
3823         switch (ioc_status) {
3824         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3825                 ascq = 0x01;
3826                 break;
3827         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3828                 ascq = 0x02;
3829                 break;
3830         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3831                 ascq = 0x03;
3832                 break;
3833         default:
3834                 ascq = 0x00;
3835                 break;
3836         }
3837         scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10,
3838             ascq);
3839         scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
3840             SAM_STAT_CHECK_CONDITION;
3841 }
3842
3843 /**
3844  * scsih_qcmd - main scsi request entry point
3845  * @scmd: pointer to scsi command object
3846  * @done: function pointer to be invoked on completion
3847  *
3848  * The callback index is set inside `ioc->scsi_io_cb_idx`.
3849  *
3850  * Returns 0 on success.  If there's a failure, return either:
3851  * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3852  * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3853  */
3854 int
3855 scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
3856 {
3857         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3858         struct MPT3SAS_DEVICE *sas_device_priv_data;
3859         struct MPT3SAS_TARGET *sas_target_priv_data;
3860         struct _raid_device *raid_device;
3861         Mpi2SCSIIORequest_t *mpi_request;
3862         u32 mpi_control;
3863         u16 smid;
3864         u16 handle;
3865
3866         if (ioc->logging_level & MPT_DEBUG_SCSI)
3867                 scsi_print_command(scmd);
3868
3869         /*
3870          * Lock the device for any subsequent command until command is
3871          * done.
3872          */
3873         if (ata_12_16_cmd(scmd))
3874                 scsi_internal_device_block(scmd->device);
3875
3876         sas_device_priv_data = scmd->device->hostdata;
3877         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
3878                 scmd->result = DID_NO_CONNECT << 16;
3879                 scmd->scsi_done(scmd);
3880                 return 0;
3881         }
3882
3883         if (ioc->pci_error_recovery || ioc->remove_host) {
3884                 scmd->result = DID_NO_CONNECT << 16;
3885                 scmd->scsi_done(scmd);
3886                 return 0;
3887         }
3888
3889         sas_target_priv_data = sas_device_priv_data->sas_target;
3890
3891         /* invalid device handle */
3892         handle = sas_target_priv_data->handle;
3893         if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
3894                 scmd->result = DID_NO_CONNECT << 16;
3895                 scmd->scsi_done(scmd);
3896                 return 0;
3897         }
3898
3899
3900         /* host recovery or link resets sent via IOCTLs */
3901         if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
3902                 return SCSI_MLQUEUE_HOST_BUSY;
3903
3904         /* device has been deleted */
3905         else if (sas_target_priv_data->deleted) {
3906                 scmd->result = DID_NO_CONNECT << 16;
3907                 scmd->scsi_done(scmd);
3908                 return 0;
3909         /* device busy with task managment */
3910         } else if (sas_target_priv_data->tm_busy ||
3911             sas_device_priv_data->block)
3912                 return SCSI_MLQUEUE_DEVICE_BUSY;
3913
3914         if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3915                 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3916         else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3917                 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3918         else
3919                 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3920
3921         /* set tags */
3922         mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3923
3924         /* Make sure Device is not raid volume.
3925          * We do not expose raid functionality to upper layer for warpdrive.
3926          */
3927         if (!ioc->is_warpdrive && !scsih_is_raid(&scmd->device->sdev_gendev)
3928             && sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
3929                 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3930
3931         smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
3932         if (!smid) {
3933                 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3934                     ioc->name, __func__);
3935                 goto out;
3936         }
3937         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3938         memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
3939         _scsih_setup_eedp(ioc, scmd, mpi_request);
3940
3941         if (scmd->cmd_len == 32)
3942                 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
3943         mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3944         if (sas_device_priv_data->sas_target->flags &
3945             MPT_TARGET_FLAGS_RAID_COMPONENT)
3946                 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3947         else
3948                 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3949         mpi_request->DevHandle = cpu_to_le16(handle);
3950         mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3951         mpi_request->Control = cpu_to_le32(mpi_control);
3952         mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3953         mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3954         mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3955         mpi_request->SenseBufferLowAddress =
3956             mpt3sas_base_get_sense_buffer_dma(ioc, smid);
3957         mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3958         int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3959             mpi_request->LUN);
3960         memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3961
3962         if (mpi_request->DataLength) {
3963                 if (ioc->build_sg_scmd(ioc, scmd, smid)) {
3964                         mpt3sas_base_free_smid(ioc, smid);
3965                         goto out;
3966                 }
3967         } else
3968                 ioc->build_zero_len_sge(ioc, &mpi_request->SGL);
3969
3970         raid_device = sas_target_priv_data->raid_device;
3971         if (raid_device && raid_device->direct_io_enabled)
3972                 mpt3sas_setup_direct_io(ioc, scmd, raid_device, mpi_request,
3973                     smid);
3974
3975         if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
3976                 if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
3977                         mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
3978                             MPI25_SCSIIO_IOFLAGS_FAST_PATH);
3979                         mpt3sas_base_put_smid_fast_path(ioc, smid, handle);
3980                 } else
3981                         mpt3sas_base_put_smid_scsi_io(ioc, smid,
3982                             le16_to_cpu(mpi_request->DevHandle));
3983         } else
3984                 mpt3sas_base_put_smid_default(ioc, smid);
3985         return 0;
3986
3987  out:
3988         return SCSI_MLQUEUE_HOST_BUSY;
3989 }
3990
3991 /**
3992  * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3993  * @sense_buffer: sense data returned by target
3994  * @data: normalized skey/asc/ascq
3995  *
3996  * Return nothing.
3997  */
3998 static void
3999 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4000 {
4001         if ((sense_buffer[0] & 0x7F) >= 0x72) {
4002                 /* descriptor format */
4003                 data->skey = sense_buffer[1] & 0x0F;
4004                 data->asc = sense_buffer[2];
4005                 data->ascq = sense_buffer[3];
4006         } else {
4007                 /* fixed format */
4008                 data->skey = sense_buffer[2] & 0x0F;
4009                 data->asc = sense_buffer[12];
4010                 data->ascq = sense_buffer[13];
4011         }
4012 }
4013
4014 /**
4015  * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
4016  * @ioc: per adapter object
4017  * @scmd: pointer to scsi command object
4018  * @mpi_reply: reply mf payload returned from firmware
4019  *
4020  * scsi_status - SCSI Status code returned from target device
4021  * scsi_state - state info associated with SCSI_IO determined by ioc
4022  * ioc_status - ioc supplied status info
4023  *
4024  * Return nothing.
4025  */
4026 static void
4027 _scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4028         Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4029 {
4030         u32 response_info;
4031         u8 *response_bytes;
4032         u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4033             MPI2_IOCSTATUS_MASK;
4034         u8 scsi_state = mpi_reply->SCSIState;
4035         u8 scsi_status = mpi_reply->SCSIStatus;
4036         char *desc_ioc_state = NULL;
4037         char *desc_scsi_status = NULL;
4038         char *desc_scsi_state = ioc->tmp_string;
4039         u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4040         struct _sas_device *sas_device = NULL;
4041         struct scsi_target *starget = scmd->device->sdev_target;
4042         struct MPT3SAS_TARGET *priv_target = starget->hostdata;
4043         char *device_str = NULL;
4044
4045         if (!priv_target)
4046                 return;
4047         if (ioc->hide_ir_msg)
4048                 device_str = "WarpDrive";
4049         else
4050                 device_str = "volume";
4051
4052         if (log_info == 0x31170000)
4053                 return;
4054
4055         switch (ioc_status) {
4056         case MPI2_IOCSTATUS_SUCCESS:
4057                 desc_ioc_state = "success";
4058                 break;
4059         case MPI2_IOCSTATUS_INVALID_FUNCTION:
4060                 desc_ioc_state = "invalid function";
4061                 break;
4062         case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4063                 desc_ioc_state = "scsi recovered error";
4064                 break;
4065         case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4066                 desc_ioc_state = "scsi invalid dev handle";
4067                 break;
4068         case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4069                 desc_ioc_state = "scsi device not there";
4070                 break;
4071         case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4072                 desc_ioc_state = "scsi data overrun";
4073                 break;
4074         case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4075                 desc_ioc_state = "scsi data underrun";
4076                 break;
4077         case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4078                 desc_ioc_state = "scsi io data error";
4079                 break;
4080         case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4081                 desc_ioc_state = "scsi protocol error";
4082                 break;
4083         case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4084                 desc_ioc_state = "scsi task terminated";
4085                 break;
4086         case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4087                 desc_ioc_state = "scsi residual mismatch";
4088                 break;
4089         case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4090                 desc_ioc_state = "scsi task mgmt failed";
4091                 break;
4092         case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4093                 desc_ioc_state = "scsi ioc terminated";
4094                 break;
4095         case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4096                 desc_ioc_state = "scsi ext terminated";
4097                 break;
4098         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4099                 desc_ioc_state = "eedp guard error";
4100                 break;
4101         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4102                 desc_ioc_state = "eedp ref tag error";
4103                 break;
4104         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4105                 desc_ioc_state = "eedp app tag error";
4106                 break;
4107         default:
4108                 desc_ioc_state = "unknown";
4109                 break;
4110         }
4111
4112         switch (scsi_status) {
4113         case MPI2_SCSI_STATUS_GOOD:
4114                 desc_scsi_status = "good";
4115                 break;
4116         case MPI2_SCSI_STATUS_CHECK_CONDITION:
4117                 desc_scsi_status = "check condition";
4118                 break;
4119         case MPI2_SCSI_STATUS_CONDITION_MET:
4120                 desc_scsi_status = "condition met";
4121                 break;
4122         case MPI2_SCSI_STATUS_BUSY:
4123                 desc_scsi_status = "busy";
4124                 break;
4125         case MPI2_SCSI_STATUS_INTERMEDIATE:
4126                 desc_scsi_status = "intermediate";
4127                 break;
4128         case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4129                 desc_scsi_status = "intermediate condmet";
4130                 break;
4131         case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4132                 desc_scsi_status = "reservation conflict";
4133                 break;
4134         case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4135                 desc_scsi_status = "command terminated";
4136                 break;
4137         case MPI2_SCSI_STATUS_TASK_SET_FULL:
4138                 desc_scsi_status = "task set full";
4139                 break;
4140         case MPI2_SCSI_STATUS_ACA_ACTIVE:
4141                 desc_scsi_status = "aca active";
4142                 break;
4143         case MPI2_SCSI_STATUS_TASK_ABORTED:
4144                 desc_scsi_status = "task aborted";
4145                 break;
4146         default:
4147                 desc_scsi_status = "unknown";
4148                 break;
4149         }
4150
4151         desc_scsi_state[0] = '\0';
4152         if (!scsi_state)
4153                 desc_scsi_state = " ";
4154         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4155                 strcat(desc_scsi_state, "response info ");
4156         if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4157                 strcat(desc_scsi_state, "state terminated ");
4158         if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4159                 strcat(desc_scsi_state, "no status ");
4160         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4161                 strcat(desc_scsi_state, "autosense failed ");
4162         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4163                 strcat(desc_scsi_state, "autosense valid ");
4164
4165         scsi_print_command(scmd);
4166
4167         if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
4168                 pr_warn(MPT3SAS_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4169                     device_str, (unsigned long long)priv_target->sas_address);
4170         } else {
4171                 sas_device = mpt3sas_get_sdev_from_target(ioc, priv_target);
4172                 if (sas_device) {
4173                         pr_warn(MPT3SAS_FMT
4174                                 "\tsas_address(0x%016llx), phy(%d)\n",
4175                                 ioc->name, (unsigned long long)
4176                             sas_device->sas_address, sas_device->phy);
4177                         if (sas_device->enclosure_handle != 0)
4178                                 pr_warn(MPT3SAS_FMT
4179                                   "\tenclosure_logical_id(0x%016llx),"
4180                                   "slot(%d)\n", ioc->name,
4181                                   (unsigned long long)
4182                                   sas_device->enclosure_logical_id,
4183                                   sas_device->slot);
4184                         if (sas_device->connector_name[0])
4185                                 pr_warn(MPT3SAS_FMT
4186                                   "\tenclosure level(0x%04x),"
4187                                   " connector name( %s)\n", ioc->name,
4188                                   sas_device->enclosure_level,
4189                                   sas_device->connector_name);
4190
4191                         sas_device_put(sas_device);
4192                 }
4193         }
4194
4195         pr_warn(MPT3SAS_FMT
4196                 "\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
4197                 ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4198             desc_ioc_state, ioc_status, smid);
4199         pr_warn(MPT3SAS_FMT
4200                 "\trequest_len(%d), underflow(%d), resid(%d)\n",
4201                 ioc->name, scsi_bufflen(scmd), scmd->underflow,
4202             scsi_get_resid(scmd));
4203         pr_warn(MPT3SAS_FMT
4204                 "\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
4205                 ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4206             le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4207         pr_warn(MPT3SAS_FMT
4208                 "\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
4209                 ioc->name, desc_scsi_status,
4210             scsi_status, desc_scsi_state, scsi_state);
4211
4212         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4213                 struct sense_info data;
4214                 _scsih_normalize_sense(scmd->sense_buffer, &data);
4215                 pr_warn(MPT3SAS_FMT
4216                         "\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
4217                         ioc->name, data.skey,
4218                     data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
4219         }
4220
4221         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4222                 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4223                 response_bytes = (u8 *)&response_info;
4224                 _scsih_response_code(ioc, response_bytes[0]);
4225         }
4226 }
4227
4228 /**
4229  * _scsih_turn_on_pfa_led - illuminate PFA LED
4230  * @ioc: per adapter object
4231  * @handle: device handle
4232  * Context: process
4233  *
4234  * Return nothing.
4235  */
4236 static void
4237 _scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4238 {
4239         Mpi2SepReply_t mpi_reply;
4240         Mpi2SepRequest_t mpi_request;
4241         struct _sas_device *sas_device;
4242
4243         sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
4244         if (!sas_device)
4245                 return;
4246
4247         memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4248         mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4249         mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4250         mpi_request.SlotStatus =
4251             cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4252         mpi_request.DevHandle = cpu_to_le16(handle);
4253         mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4254         if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4255             &mpi_request)) != 0) {
4256                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4257                 __FILE__, __LINE__, __func__);
4258                 goto out;
4259         }
4260         sas_device->pfa_led_on = 1;
4261
4262         if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4263                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
4264                         "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4265                         ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4266                     le32_to_cpu(mpi_reply.IOCLogInfo)));
4267                 goto out;
4268         }
4269 out:
4270         sas_device_put(sas_device);
4271 }
4272
4273 /**
4274  * _scsih_turn_off_pfa_led - turn off Fault LED
4275  * @ioc: per adapter object
4276  * @sas_device: sas device whose PFA LED has to turned off
4277  * Context: process
4278  *
4279  * Return nothing.
4280  */
4281 static void
4282 _scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
4283         struct _sas_device *sas_device)
4284 {
4285         Mpi2SepReply_t mpi_reply;
4286         Mpi2SepRequest_t mpi_request;
4287
4288         memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4289         mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4290         mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4291         mpi_request.SlotStatus = 0;
4292         mpi_request.Slot = cpu_to_le16(sas_device->slot);
4293         mpi_request.DevHandle = 0;
4294         mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
4295         mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
4296         if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4297                 &mpi_request)) != 0) {
4298                 printk(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4299                 __FILE__, __LINE__, __func__);
4300                 return;
4301         }
4302
4303         if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4304                 dewtprintk(ioc, printk(MPT3SAS_FMT
4305                  "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4306                  ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4307                  le32_to_cpu(mpi_reply.IOCLogInfo)));
4308                 return;
4309         }
4310 }
4311
4312 /**
4313  * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
4314  * @ioc: per adapter object
4315  * @handle: device handle
4316  * Context: interrupt.
4317  *
4318  * Return nothing.
4319  */
4320 static void
4321 _scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4322 {
4323         struct fw_event_work *fw_event;
4324
4325         fw_event = alloc_fw_event_work(0);
4326         if (!fw_event)
4327                 return;
4328         fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
4329         fw_event->device_handle = handle;
4330         fw_event->ioc = ioc;
4331         _scsih_fw_event_add(ioc, fw_event);
4332         fw_event_work_put(fw_event);
4333 }
4334
4335 /**
4336  * _scsih_smart_predicted_fault - process smart errors
4337  * @ioc: per adapter object
4338  * @handle: device handle
4339  * Context: interrupt.
4340  *
4341  * Return nothing.
4342  */
4343 static void
4344 _scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4345 {
4346         struct scsi_target *starget;
4347         struct MPT3SAS_TARGET *sas_target_priv_data;
4348         Mpi2EventNotificationReply_t *event_reply;
4349         Mpi2EventDataSasDeviceStatusChange_t *event_data;
4350         struct _sas_device *sas_device;
4351         ssize_t sz;
4352         unsigned long flags;
4353
4354         /* only handle non-raid devices */
4355         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4356         sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
4357         if (!sas_device)
4358                 goto out_unlock;
4359
4360         starget = sas_device->starget;
4361         sas_target_priv_data = starget->hostdata;
4362
4363         if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4364            ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)))
4365                 goto out_unlock;
4366
4367         if (sas_device->enclosure_handle != 0)
4368                 starget_printk(KERN_INFO, starget, "predicted fault, "
4369                         "enclosure logical id(0x%016llx), slot(%d)\n",
4370                         (unsigned long long)sas_device->enclosure_logical_id,
4371                         sas_device->slot);
4372         if (sas_device->connector_name[0] != '\0')
4373                 starget_printk(KERN_WARNING, starget, "predicted fault, "
4374                         "enclosure level(0x%04x), connector name( %s)\n",
4375                         sas_device->enclosure_level,
4376                         sas_device->connector_name);
4377         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4378
4379         if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4380                 _scsih_send_event_to_turn_on_pfa_led(ioc, handle);
4381
4382         /* insert into event log */
4383         sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4384              sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4385         event_reply = kzalloc(sz, GFP_KERNEL);
4386         if (!event_reply) {
4387                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4388                     ioc->name, __FILE__, __LINE__, __func__);
4389                 goto out;
4390         }
4391
4392         event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4393         event_reply->Event =
4394             cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4395         event_reply->MsgLength = sz/4;
4396         event_reply->EventDataLength =
4397             cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4398         event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4399             event_reply->EventData;
4400         event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4401         event_data->ASC = 0x5D;
4402         event_data->DevHandle = cpu_to_le16(handle);
4403         event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4404         mpt3sas_ctl_add_to_event_log(ioc, event_reply);
4405         kfree(event_reply);
4406 out:
4407         if (sas_device)
4408                 sas_device_put(sas_device);
4409         return;
4410
4411 out_unlock:
4412         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4413         goto out;
4414 }
4415
4416 /**
4417  * _scsih_io_done - scsi request callback
4418  * @ioc: per adapter object
4419  * @smid: system request message index
4420  * @msix_index: MSIX table index supplied by the OS
4421  * @reply: reply message frame(lower 32bit addr)
4422  *
4423  * Callback handler when using _scsih_qcmd.
4424  *
4425  * Return 1 meaning mf should be freed from _base_interrupt
4426  *        0 means the mf is freed from this function.
4427  */
4428 static u8
4429 _scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4430 {
4431         Mpi2SCSIIORequest_t *mpi_request;
4432         Mpi2SCSIIOReply_t *mpi_reply;
4433         struct scsi_cmnd *scmd;
4434         u16 ioc_status;
4435         u32 xfer_cnt;
4436         u8 scsi_state;
4437         u8 scsi_status;
4438         u32 log_info;
4439         struct MPT3SAS_DEVICE *sas_device_priv_data;
4440         u32 response_code = 0;
4441         unsigned long flags;
4442
4443         mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
4444         scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
4445         if (scmd == NULL)
4446                 return 1;
4447
4448         if (ata_12_16_cmd(scmd))
4449                 scsi_internal_device_unblock(scmd->device, SDEV_RUNNING);
4450
4451         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4452
4453         if (mpi_reply == NULL) {
4454                 scmd->result = DID_OK << 16;
4455                 goto out;
4456         }
4457
4458         sas_device_priv_data = scmd->device->hostdata;
4459         if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4460              sas_device_priv_data->sas_target->deleted) {
4461                 scmd->result = DID_NO_CONNECT << 16;
4462                 goto out;
4463         }
4464         ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4465
4466         /*
4467          * WARPDRIVE: If direct_io is set then it is directIO,
4468          * the failed direct I/O should be redirected to volume
4469          */
4470         if (mpt3sas_scsi_direct_io_get(ioc, smid) &&
4471              ((ioc_status & MPI2_IOCSTATUS_MASK)
4472               != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
4473                 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4474                 ioc->scsi_lookup[smid - 1].scmd = scmd;
4475                 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4476                 mpt3sas_scsi_direct_io_set(ioc, smid, 0);
4477                 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4478                 mpi_request->DevHandle =
4479                     cpu_to_le16(sas_device_priv_data->sas_target->handle);
4480                 mpt3sas_base_put_smid_scsi_io(ioc, smid,
4481                     sas_device_priv_data->sas_target->handle);
4482                 return 0;
4483         }
4484         /* turning off TLR */
4485         scsi_state = mpi_reply->SCSIState;
4486         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4487                 response_code =
4488                     le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
4489         if (!sas_device_priv_data->tlr_snoop_check) {
4490                 sas_device_priv_data->tlr_snoop_check++;
4491                 if (!ioc->is_warpdrive &&
4492                     !scsih_is_raid(&scmd->device->sdev_gendev) &&
4493                     sas_is_tlr_enabled(scmd->device) &&
4494                     response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4495                         sas_disable_tlr(scmd->device);
4496                         sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4497                 }
4498         }
4499
4500         xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4501         scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4502         if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4503                 log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
4504         else
4505                 log_info = 0;
4506         ioc_status &= MPI2_IOCSTATUS_MASK;
4507         scsi_status = mpi_reply->SCSIStatus;
4508
4509         if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4510             (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4511              scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4512              scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4513                 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4514         }
4515
4516         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4517                 struct sense_info data;
4518                 const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
4519                     smid);
4520                 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
4521                     le32_to_cpu(mpi_reply->SenseCount));
4522                 memcpy(scmd->sense_buffer, sense_data, sz);
4523                 _scsih_normalize_sense(scmd->sense_buffer, &data);
4524                 /* failure prediction threshold exceeded */
4525                 if (data.asc == 0x5D)
4526                         _scsih_smart_predicted_fault(ioc,
4527                             le16_to_cpu(mpi_reply->DevHandle));
4528                 mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);
4529
4530                 if ((ioc->logging_level & MPT_DEBUG_REPLY) &&
4531                      ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
4532                      (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
4533                      (scmd->sense_buffer[2] == HARDWARE_ERROR)))
4534                         _scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
4535         }
4536         switch (ioc_status) {
4537         case MPI2_IOCSTATUS_BUSY:
4538         case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4539                 scmd->result = SAM_STAT_BUSY;
4540                 break;
4541
4542         case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4543                 scmd->result = DID_NO_CONNECT << 16;
4544                 break;
4545
4546         case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4547                 if (sas_device_priv_data->block) {
4548                         scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4549                         goto out;
4550                 }
4551                 if (log_info == 0x31110630) {
4552                         if (scmd->retries > 2) {
4553                                 scmd->result = DID_NO_CONNECT << 16;
4554                                 scsi_device_set_state(scmd->device,
4555                                     SDEV_OFFLINE);
4556                         } else {
4557                                 scmd->result = DID_SOFT_ERROR << 16;
4558                                 scmd->device->expecting_cc_ua = 1;
4559                         }
4560                         break;
4561                 } else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
4562                         scmd->result = DID_RESET << 16;
4563                         break;
4564                 }
4565                 scmd->result = DID_SOFT_ERROR << 16;
4566                 break;
4567         case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4568         case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4569                 scmd->result = DID_RESET << 16;
4570                 break;
4571
4572         case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4573                 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4574                         scmd->result = DID_SOFT_ERROR << 16;
4575                 else
4576                         scmd->result = (DID_OK << 16) | scsi_status;
4577                 break;
4578
4579         case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4580                 scmd->result = (DID_OK << 16) | scsi_status;
4581
4582                 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4583                         break;
4584
4585                 if (xfer_cnt < scmd->underflow) {
4586                         if (scsi_status == SAM_STAT_BUSY)
4587                                 scmd->result = SAM_STAT_BUSY;
4588                         else
4589                                 scmd->result = DID_SOFT_ERROR << 16;
4590                 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4591                      MPI2_SCSI_STATE_NO_SCSI_STATUS))
4592                         scmd->result = DID_SOFT_ERROR << 16;
4593                 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4594                         scmd->result = DID_RESET << 16;
4595                 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4596                         mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4597                         mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4598                         scmd->result = (DRIVER_SENSE << 24) |
4599                             SAM_STAT_CHECK_CONDITION;
4600                         scmd->sense_buffer[0] = 0x70;
4601                         scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4602                         scmd->sense_buffer[12] = 0x20;
4603                         scmd->sense_buffer[13] = 0;
4604                 }
4605                 break;
4606
4607         case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4608                 scsi_set_resid(scmd, 0);
4609         case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4610         case MPI2_IOCSTATUS_SUCCESS:
4611                 scmd->result = (DID_OK << 16) | scsi_status;
4612                 if (response_code ==
4613                     MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4614                     (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4615                      MPI2_SCSI_STATE_NO_SCSI_STATUS)))
4616                         scmd->result = DID_SOFT_ERROR << 16;
4617                 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4618                         scmd->result = DID_RESET << 16;
4619                 break;
4620
4621         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4622         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4623         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4624                 _scsih_eedp_error_handling(scmd, ioc_status);
4625                 break;
4626
4627         case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4628         case MPI2_IOCSTATUS_INVALID_FUNCTION:
4629         case MPI2_IOCSTATUS_INVALID_SGL:
4630         case MPI2_IOCSTATUS_INTERNAL_ERROR:
4631         case MPI2_IOCSTATUS_INVALID_FIELD:
4632         case MPI2_IOCSTATUS_INVALID_STATE:
4633         case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4634         case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4635         default:
4636                 scmd->result = DID_SOFT_ERROR << 16;
4637                 break;
4638
4639         }
4640
4641         if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4642                 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4643
4644  out:
4645
4646         scsi_dma_unmap(scmd);
4647
4648         scmd->scsi_done(scmd);
4649         return 1;
4650 }
4651
4652 /**
4653  * _scsih_sas_host_refresh - refreshing sas host object contents
4654  * @ioc: per adapter object
4655  * Context: user
4656  *
4657  * During port enable, fw will send topology events for every device. Its
4658  * possible that the handles may change from the previous setting, so this
4659  * code keeping handles updating if changed.
4660  *
4661  * Return nothing.
4662  */
4663 static void
4664 _scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
4665 {
4666         u16 sz;
4667         u16 ioc_status;
4668         int i;
4669         Mpi2ConfigReply_t mpi_reply;
4670         Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4671         u16 attached_handle;
4672         u8 link_rate;
4673
4674         dtmprintk(ioc, pr_info(MPT3SAS_FMT
4675             "updating handles for sas_host(0x%016llx)\n",
4676             ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4677
4678         sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4679             * sizeof(Mpi2SasIOUnit0PhyData_t));
4680         sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4681         if (!sas_iounit_pg0) {
4682                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4683                     ioc->name, __FILE__, __LINE__, __func__);
4684                 return;
4685         }
4686
4687         if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4688             sas_iounit_pg0, sz)) != 0)
4689                 goto out;
4690         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4691         if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4692                 goto out;
4693         for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4694                 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4695                 if (i == 0)
4696                         ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4697                             PhyData[0].ControllerDevHandle);
4698                 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4699                 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4700                     AttachedDevHandle);
4701                 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4702                         link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4703                 mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4704                     attached_handle, i, link_rate);
4705         }
4706  out:
4707         kfree(sas_iounit_pg0);
4708 }
4709
4710 /**
4711  * _scsih_sas_host_add - create sas host object
4712  * @ioc: per adapter object
4713  *
4714  * Creating host side data object, stored in ioc->sas_hba
4715  *
4716  * Return nothing.
4717  */
4718 static void
4719 _scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
4720 {
4721         int i;
4722         Mpi2ConfigReply_t mpi_reply;
4723         Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4724         Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4725         Mpi2SasPhyPage0_t phy_pg0;
4726         Mpi2SasDevicePage0_t sas_device_pg0;
4727         Mpi2SasEnclosurePage0_t enclosure_pg0;
4728         u16 ioc_status;
4729         u16 sz;
4730         u8 device_missing_delay;
4731
4732         mpt3sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4733         if (!ioc->sas_hba.num_phys) {
4734                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4735                     ioc->name, __FILE__, __LINE__, __func__);
4736                 return;
4737         }
4738
4739         /* sas_iounit page 0 */
4740         sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4741             sizeof(Mpi2SasIOUnit0PhyData_t));
4742         sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4743         if (!sas_iounit_pg0) {
4744                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4745                     ioc->name, __FILE__, __LINE__, __func__);
4746                 return;
4747         }
4748         if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4749             sas_iounit_pg0, sz))) {
4750                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4751                     ioc->name, __FILE__, __LINE__, __func__);
4752                 goto out;
4753         }
4754         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4755             MPI2_IOCSTATUS_MASK;
4756         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4757                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4758                     ioc->name, __FILE__, __LINE__, __func__);
4759                 goto out;
4760         }
4761
4762         /* sas_iounit page 1 */
4763         sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4764             sizeof(Mpi2SasIOUnit1PhyData_t));
4765         sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4766         if (!sas_iounit_pg1) {
4767                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4768                     ioc->name, __FILE__, __LINE__, __func__);
4769                 goto out;
4770         }
4771         if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4772             sas_iounit_pg1, sz))) {
4773                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4774                     ioc->name, __FILE__, __LINE__, __func__);
4775                 goto out;
4776         }
4777         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4778             MPI2_IOCSTATUS_MASK;
4779         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4780                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4781                     ioc->name, __FILE__, __LINE__, __func__);
4782                 goto out;
4783         }
4784
4785         ioc->io_missing_delay =
4786             sas_iounit_pg1->IODeviceMissingDelay;
4787         device_missing_delay =
4788             sas_iounit_pg1->ReportDeviceMissingDelay;
4789         if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4790                 ioc->device_missing_delay = (device_missing_delay &
4791                     MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4792         else
4793                 ioc->device_missing_delay = device_missing_delay &
4794                     MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4795
4796         ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4797         ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4798             sizeof(struct _sas_phy), GFP_KERNEL);
4799         if (!ioc->sas_hba.phy) {
4800                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4801                     ioc->name, __FILE__, __LINE__, __func__);
4802                 goto out;
4803         }
4804         for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4805                 if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4806                     i))) {
4807                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4808                             ioc->name, __FILE__, __LINE__, __func__);
4809                         goto out;
4810                 }
4811                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4812                     MPI2_IOCSTATUS_MASK;
4813                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4814                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4815                             ioc->name, __FILE__, __LINE__, __func__);
4816                         goto out;
4817                 }
4818
4819                 if (i == 0)
4820                         ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4821                             PhyData[0].ControllerDevHandle);
4822                 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4823                 ioc->sas_hba.phy[i].phy_id = i;
4824                 mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4825                     phy_pg0, ioc->sas_hba.parent_dev);
4826         }
4827         if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4828             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
4829                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4830                     ioc->name, __FILE__, __LINE__, __func__);
4831                 goto out;
4832         }
4833         ioc->sas_hba.enclosure_handle =
4834             le16_to_cpu(sas_device_pg0.EnclosureHandle);
4835         ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4836         pr_info(MPT3SAS_FMT
4837                 "host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
4838                 ioc->name, ioc->sas_hba.handle,
4839             (unsigned long long) ioc->sas_hba.sas_address,
4840             ioc->sas_hba.num_phys) ;
4841
4842         if (ioc->sas_hba.enclosure_handle) {
4843                 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4844                     &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4845                    ioc->sas_hba.enclosure_handle)))
4846                         ioc->sas_hba.enclosure_logical_id =
4847                             le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4848         }
4849
4850  out:
4851         kfree(sas_iounit_pg1);
4852         kfree(sas_iounit_pg0);
4853 }
4854
4855 /**
4856  * _scsih_expander_add -  creating expander object
4857  * @ioc: per adapter object
4858  * @handle: expander handle
4859  *
4860  * Creating expander object, stored in ioc->sas_expander_list.
4861  *
4862  * Return 0 for success, else error.
4863  */
4864 static int
4865 _scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4866 {
4867         struct _sas_node *sas_expander;
4868         Mpi2ConfigReply_t mpi_reply;
4869         Mpi2ExpanderPage0_t expander_pg0;
4870         Mpi2ExpanderPage1_t expander_pg1;
4871         Mpi2SasEnclosurePage0_t enclosure_pg0;
4872         u32 ioc_status;
4873         u16 parent_handle;
4874         u64 sas_address, sas_address_parent = 0;
4875         int i;
4876         unsigned long flags;
4877         struct _sas_port *mpt3sas_port = NULL;
4878
4879         int rc = 0;
4880
4881         if (!handle)
4882                 return -1;
4883
4884         if (ioc->shost_recovery || ioc->pci_error_recovery)
4885                 return -1;
4886
4887         if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4888             MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4889                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4890                     ioc->name, __FILE__, __LINE__, __func__);
4891                 return -1;
4892         }
4893
4894         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4895             MPI2_IOCSTATUS_MASK;
4896         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4897                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4898                     ioc->name, __FILE__, __LINE__, __func__);
4899                 return -1;
4900         }
4901
4902         /* handle out of order topology events */
4903         parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
4904         if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4905             != 0) {
4906                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4907                     ioc->name, __FILE__, __LINE__, __func__);
4908                 return -1;
4909         }
4910         if (sas_address_parent != ioc->sas_hba.sas_address) {
4911                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4912                 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4913                     sas_address_parent);
4914                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4915                 if (!sas_expander) {
4916                         rc = _scsih_expander_add(ioc, parent_handle);
4917                         if (rc != 0)
4918                                 return rc;
4919                 }
4920         }
4921
4922         spin_lock_irqsave(&ioc->sas_node_lock, flags);
4923         sas_address = le64_to_cpu(expander_pg0.SASAddress);
4924         sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4925             sas_address);
4926         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4927
4928         if (sas_expander)
4929                 return 0;
4930
4931         sas_expander = kzalloc(sizeof(struct _sas_node),
4932             GFP_KERNEL);
4933         if (!sas_expander) {
4934                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4935                     ioc->name, __FILE__, __LINE__, __func__);
4936                 return -1;
4937         }
4938
4939         sas_expander->handle = handle;
4940         sas_expander->num_phys = expander_pg0.NumPhys;
4941         sas_expander->sas_address_parent = sas_address_parent;
4942         sas_expander->sas_address = sas_address;
4943
4944         pr_info(MPT3SAS_FMT "expander_add: handle(0x%04x)," \
4945             " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
4946             handle, parent_handle, (unsigned long long)
4947             sas_expander->sas_address, sas_expander->num_phys);
4948
4949         if (!sas_expander->num_phys)
4950                 goto out_fail;
4951         sas_expander->phy = kcalloc(sas_expander->num_phys,
4952             sizeof(struct _sas_phy), GFP_KERNEL);
4953         if (!sas_expander->phy) {
4954                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4955                     ioc->name, __FILE__, __LINE__, __func__);
4956                 rc = -1;
4957                 goto out_fail;
4958         }
4959
4960         INIT_LIST_HEAD(&sas_expander->sas_port_list);
4961         mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
4962             sas_address_parent);
4963         if (!mpt3sas_port) {
4964                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4965                     ioc->name, __FILE__, __LINE__, __func__);
4966                 rc = -1;
4967                 goto out_fail;
4968         }
4969         sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
4970
4971         for (i = 0 ; i < sas_expander->num_phys ; i++) {
4972                 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
4973                     &expander_pg1, i, handle))) {
4974                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4975                             ioc->name, __FILE__, __LINE__, __func__);
4976                         rc = -1;
4977                         goto out_fail;
4978                 }
4979                 sas_expander->phy[i].handle = handle;
4980                 sas_expander->phy[i].phy_id = i;
4981
4982                 if ((mpt3sas_transport_add_expander_phy(ioc,
4983                     &sas_expander->phy[i], expander_pg1,
4984                     sas_expander->parent_dev))) {
4985                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4986                             ioc->name, __FILE__, __LINE__, __func__);
4987                         rc = -1;
4988                         goto out_fail;
4989                 }
4990         }
4991
4992         if (sas_expander->enclosure_handle) {
4993                 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4994                     &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4995                    sas_expander->enclosure_handle)))
4996                         sas_expander->enclosure_logical_id =
4997                             le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4998         }
4999
5000         _scsih_expander_node_add(ioc, sas_expander);
5001          return 0;
5002
5003  out_fail:
5004
5005         if (mpt3sas_port)
5006                 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
5007                     sas_address_parent);
5008         kfree(sas_expander);
5009         return rc;
5010 }
5011
5012 /**
5013  * mpt3sas_expander_remove - removing expander object
5014  * @ioc: per adapter object
5015  * @sas_address: expander sas_address
5016  *
5017  * Return nothing.
5018  */
5019 void
5020 mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
5021 {
5022         struct _sas_node *sas_expander;
5023         unsigned long flags;
5024
5025         if (ioc->shost_recovery)
5026                 return;
5027
5028         spin_lock_irqsave(&ioc->sas_node_lock, flags);
5029         sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5030             sas_address);
5031         if (sas_expander)
5032                 list_del(&sas_expander->list);
5033         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5034         if (sas_expander)
5035                 _scsih_expander_node_remove(ioc, sas_expander);
5036 }
5037
5038 /**
5039  * _scsih_done -  internal SCSI_IO callback handler.
5040  * @ioc: per adapter object
5041  * @smid: system request message index
5042  * @msix_index: MSIX table index supplied by the OS
5043  * @reply: reply message frame(lower 32bit addr)
5044  *
5045  * Callback handler when sending internal generated SCSI_IO.
5046  * The callback index passed is `ioc->scsih_cb_idx`
5047  *
5048  * Return 1 meaning mf should be freed from _base_interrupt
5049  *        0 means the mf is freed from this function.
5050  */
5051 static u8
5052 _scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
5053 {
5054         MPI2DefaultReply_t *mpi_reply;
5055
5056         mpi_reply =  mpt3sas_base_get_reply_virt_addr(ioc, reply);
5057         if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
5058                 return 1;
5059         if (ioc->scsih_cmds.smid != smid)
5060                 return 1;
5061         ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
5062         if (mpi_reply) {
5063                 memcpy(ioc->scsih_cmds.reply, mpi_reply,
5064                     mpi_reply->MsgLength*4);
5065                 ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
5066         }
5067         ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
5068         complete(&ioc->scsih_cmds.done);
5069         return 1;
5070 }
5071
5072
5073
5074
5075 #define MPT3_MAX_LUNS (255)
5076
5077
5078 /**
5079  * _scsih_check_access_status - check access flags
5080  * @ioc: per adapter object
5081  * @sas_address: sas address
5082  * @handle: sas device handle
5083  * @access_flags: errors returned during discovery of the device
5084  *
5085  * Return 0 for success, else failure
5086  */
5087 static u8
5088 _scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
5089         u16 handle, u8 access_status)
5090 {
5091         u8 rc = 1;
5092         char *desc = NULL;
5093
5094         switch (access_status) {
5095         case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5096         case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5097                 rc = 0;
5098                 break;
5099         case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5100                 desc = "sata capability failed";
5101                 break;
5102         case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5103                 desc = "sata affiliation conflict";
5104                 break;
5105         case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5106                 desc = "route not addressable";
5107                 break;
5108         case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5109                 desc = "smp error not addressable";
5110                 break;
5111         case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5112                 desc = "device blocked";
5113                 break;
5114         case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5115         case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5116         case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5117         case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5118         case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5119         case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5120         case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5121         case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5122         case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5123         case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5124         case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5125         case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5126                 desc = "sata initialization failed";
5127                 break;
5128         default:
5129                 desc = "unknown";
5130                 break;
5131         }
5132
5133         if (!rc)
5134                 return 0;
5135
5136         pr_err(MPT3SAS_FMT
5137                 "discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
5138                 ioc->name, desc, (unsigned long long)sas_address, handle);
5139         return rc;
5140 }
5141
5142 /**
5143  * _scsih_check_device - checking device responsiveness
5144  * @ioc: per adapter object
5145  * @parent_sas_address: sas address of parent expander or sas host
5146  * @handle: attached device handle
5147  * @phy_numberv: phy number
5148  * @link_rate: new link rate
5149  *
5150  * Returns nothing.
5151  */
5152 static void
5153 _scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
5154         u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
5155 {
5156         Mpi2ConfigReply_t mpi_reply;
5157         Mpi2SasDevicePage0_t sas_device_pg0;
5158         struct _sas_device *sas_device;
5159         u32 ioc_status;
5160         unsigned long flags;
5161         u64 sas_address;
5162         struct scsi_target *starget;
5163         struct MPT3SAS_TARGET *sas_target_priv_data;
5164         u32 device_info;
5165
5166
5167         if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5168             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5169                 return;
5170
5171         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5172         if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5173                 return;
5174
5175         /* wide port handling ~ we need only handle device once for the phy that
5176          * is matched in sas device page zero
5177          */
5178         if (phy_number != sas_device_pg0.PhyNum)
5179                 return;
5180
5181         /* check if this is end device */
5182         device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5183         if (!(_scsih_is_end_device(device_info)))
5184                 return;
5185
5186         spin_lock_irqsave(&ioc->sas_device_lock, flags);
5187         sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5188         sas_device = __mpt3sas_get_sdev_by_addr(ioc,
5189             sas_address);
5190
5191         if (!sas_device)
5192                 goto out_unlock;
5193
5194         if (unlikely(sas_device->handle != handle)) {
5195                 starget = sas_device->starget;
5196                 sas_target_priv_data = starget->hostdata;
5197                 starget_printk(KERN_INFO, starget,
5198                         "handle changed from(0x%04x) to (0x%04x)!!!\n",
5199                         sas_device->handle, handle);
5200                 sas_target_priv_data->handle = handle;
5201                 sas_device->handle = handle;
5202                 if (sas_device_pg0.Flags &
5203                      MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5204                         sas_device->enclosure_level =
5205                                 le16_to_cpu(sas_device_pg0.EnclosureLevel);
5206                         memcpy(&sas_device->connector_name[0],
5207                                 &sas_device_pg0.ConnectorName[0], 4);
5208                 } else {
5209                         sas_device->enclosure_level = 0;
5210                         sas_device->connector_name[0] = '\0';
5211                 }
5212         }
5213
5214         /* check if device is present */
5215         if (!(le16_to_cpu(sas_device_pg0.Flags) &
5216             MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5217                 pr_err(MPT3SAS_FMT
5218                         "device is not present handle(0x%04x), flags!!!\n",
5219                         ioc->name, handle);
5220                 goto out_unlock;
5221         }
5222
5223         /* check if there were any issues with discovery */
5224         if (_scsih_check_access_status(ioc, sas_address, handle,
5225             sas_device_pg0.AccessStatus))
5226                 goto out_unlock;
5227
5228         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5229         _scsih_ublock_io_device(ioc, sas_address);
5230
5231         if (sas_device)
5232                 sas_device_put(sas_device);
5233         return;
5234
5235 out_unlock:
5236         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5237         if (sas_device)
5238                 sas_device_put(sas_device);
5239 }
5240
5241 /**
5242  * _scsih_add_device -  creating sas device object
5243  * @ioc: per adapter object
5244  * @handle: sas device handle
5245  * @phy_num: phy number end device attached to
5246  * @is_pd: is this hidden raid component
5247  *
5248  * Creating end device object, stored in ioc->sas_device_list.
5249  *
5250  * Returns 0 for success, non-zero for failure.
5251  */
5252 static int
5253 _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phy_num,
5254         u8 is_pd)
5255 {
5256         Mpi2ConfigReply_t mpi_reply;
5257         Mpi2SasDevicePage0_t sas_device_pg0;
5258         Mpi2SasEnclosurePage0_t enclosure_pg0;
5259         struct _sas_device *sas_device;
5260         u32 ioc_status;
5261         u64 sas_address;
5262         u32 device_info;
5263
5264         if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5265             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5266                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5267                     ioc->name, __FILE__, __LINE__, __func__);
5268                 return -1;
5269         }
5270
5271         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5272             MPI2_IOCSTATUS_MASK;
5273         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5274                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5275                     ioc->name, __FILE__, __LINE__, __func__);
5276                 return -1;
5277         }
5278
5279         /* check if this is end device */
5280         device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5281         if (!(_scsih_is_end_device(device_info)))
5282                 return -1;
5283         sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5284
5285         /* check if device is present */
5286         if (!(le16_to_cpu(sas_device_pg0.Flags) &
5287             MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5288                 pr_err(MPT3SAS_FMT "device is not present handle(0x04%x)!!!\n",
5289                         ioc->name, handle);
5290                 return -1;
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                 return -1;
5297
5298         sas_device = mpt3sas_get_sdev_by_addr(ioc,
5299                                         sas_address);
5300         if (sas_device) {
5301                 sas_device_put(sas_device);
5302                 return -1;
5303         }
5304
5305         sas_device = kzalloc(sizeof(struct _sas_device),
5306             GFP_KERNEL);
5307         if (!sas_device) {
5308                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5309                     ioc->name, __FILE__, __LINE__, __func__);
5310                 return 0;
5311         }
5312
5313         kref_init(&sas_device->refcount);
5314         sas_device->handle = handle;
5315         if (_scsih_get_sas_address(ioc,
5316             le16_to_cpu(sas_device_pg0.ParentDevHandle),
5317             &sas_device->sas_address_parent) != 0)
5318                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5319                     ioc->name, __FILE__, __LINE__, __func__);
5320         sas_device->enclosure_handle =
5321             le16_to_cpu(sas_device_pg0.EnclosureHandle);
5322         if (sas_device->enclosure_handle != 0)
5323                 sas_device->slot =
5324                     le16_to_cpu(sas_device_pg0.Slot);
5325         sas_device->device_info = device_info;
5326         sas_device->sas_address = sas_address;
5327         sas_device->phy = sas_device_pg0.PhyNum;
5328         sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
5329             MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
5330
5331         if (sas_device_pg0.Flags & MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5332                 sas_device->enclosure_level =
5333                         le16_to_cpu(sas_device_pg0.EnclosureLevel);
5334                 memcpy(&sas_device->connector_name[0],
5335                         &sas_device_pg0.ConnectorName[0], 4);
5336         } else {
5337                 sas_device->enclosure_level = 0;
5338                 sas_device->connector_name[0] = '\0';
5339         }
5340         /* get enclosure_logical_id */
5341         if (sas_device->enclosure_handle && !(mpt3sas_config_get_enclosure_pg0(
5342            ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5343            sas_device->enclosure_handle)))
5344                 sas_device->enclosure_logical_id =
5345                     le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5346
5347         /* get device name */
5348         sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5349
5350         if (ioc->wait_for_discovery_to_complete)
5351                 _scsih_sas_device_init_add(ioc, sas_device);
5352         else
5353                 _scsih_sas_device_add(ioc, sas_device);
5354
5355         sas_device_put(sas_device);
5356         return 0;
5357 }
5358
5359 /**
5360  * _scsih_remove_device -  removing sas device object
5361  * @ioc: per adapter object
5362  * @sas_device_delete: the sas_device object
5363  *
5364  * Return nothing.
5365  */
5366 static void
5367 _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
5368         struct _sas_device *sas_device)
5369 {
5370         struct MPT3SAS_TARGET *sas_target_priv_data;
5371
5372         if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
5373              (sas_device->pfa_led_on)) {
5374                 _scsih_turn_off_pfa_led(ioc, sas_device);
5375                 sas_device->pfa_led_on = 0;
5376         }
5377         dewtprintk(ioc, pr_info(MPT3SAS_FMT
5378                 "%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
5379                 ioc->name, __func__,
5380             sas_device->handle, (unsigned long long)
5381             sas_device->sas_address));
5382         if (sas_device->enclosure_handle != 0)
5383                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5384                     "%s: enter: enclosure logical id(0x%016llx), slot(%d)\n",
5385                     ioc->name, __func__,
5386                     (unsigned long long)sas_device->enclosure_logical_id,
5387                     sas_device->slot));
5388         if (sas_device->connector_name[0] != '\0')
5389                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5390                   "%s: enter: enclosure level(0x%04x), connector name( %s)\n",
5391                   ioc->name, __func__,
5392                   sas_device->enclosure_level,
5393                   sas_device->connector_name));
5394
5395         if (sas_device->starget && sas_device->starget->hostdata) {
5396                 sas_target_priv_data = sas_device->starget->hostdata;
5397                 sas_target_priv_data->deleted = 1;
5398                 _scsih_ublock_io_device(ioc, sas_device->sas_address);
5399                 sas_target_priv_data->handle =
5400                      MPT3SAS_INVALID_DEVICE_HANDLE;
5401         }
5402
5403         if (!ioc->hide_drives)
5404                 mpt3sas_transport_port_remove(ioc,
5405                     sas_device->sas_address,
5406                     sas_device->sas_address_parent);
5407
5408         pr_info(MPT3SAS_FMT
5409                 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
5410                 ioc->name, sas_device->handle,
5411             (unsigned long long) sas_device->sas_address);
5412         if (sas_device->enclosure_handle != 0)
5413                 pr_info(MPT3SAS_FMT
5414                   "removing : enclosure logical id(0x%016llx), slot(%d)\n",
5415                   ioc->name,
5416                   (unsigned long long)sas_device->enclosure_logical_id,
5417                   sas_device->slot);
5418         if (sas_device->connector_name[0] != '\0')
5419                 pr_info(MPT3SAS_FMT
5420                   "removing enclosure level(0x%04x), connector name( %s)\n",
5421                   ioc->name, sas_device->enclosure_level,
5422                   sas_device->connector_name);
5423
5424         dewtprintk(ioc, pr_info(MPT3SAS_FMT
5425                 "%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
5426                 ioc->name, __func__,
5427                 sas_device->handle, (unsigned long long)
5428                 sas_device->sas_address));
5429         if (sas_device->enclosure_handle != 0)
5430                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5431                     "%s: exit: enclosure logical id(0x%016llx), slot(%d)\n",
5432                     ioc->name, __func__,
5433                     (unsigned long long)sas_device->enclosure_logical_id,
5434                     sas_device->slot));
5435         if (sas_device->connector_name[0] != '\0')
5436                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5437                     "%s: exit: enclosure level(0x%04x), connector name(%s)\n",
5438                     ioc->name, __func__, sas_device->enclosure_level,
5439                     sas_device->connector_name));
5440 }
5441
5442 /**
5443  * _scsih_sas_topology_change_event_debug - debug for topology event
5444  * @ioc: per adapter object
5445  * @event_data: event data payload
5446  * Context: user.
5447  */
5448 static void
5449 _scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5450         Mpi2EventDataSasTopologyChangeList_t *event_data)
5451 {
5452         int i;
5453         u16 handle;
5454         u16 reason_code;
5455         u8 phy_number;
5456         char *status_str = NULL;
5457         u8 link_rate, prev_link_rate;
5458
5459         switch (event_data->ExpStatus) {
5460         case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5461                 status_str = "add";
5462                 break;
5463         case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5464                 status_str = "remove";
5465                 break;
5466         case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
5467         case 0:
5468                 status_str =  "responding";
5469                 break;
5470         case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5471                 status_str = "remove delay";
5472                 break;
5473         default:
5474                 status_str = "unknown status";
5475                 break;
5476         }
5477         pr_info(MPT3SAS_FMT "sas topology change: (%s)\n",
5478             ioc->name, status_str);
5479         pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
5480             "start_phy(%02d), count(%d)\n",
5481             le16_to_cpu(event_data->ExpanderDevHandle),
5482             le16_to_cpu(event_data->EnclosureHandle),
5483             event_data->StartPhyNum, event_data->NumEntries);
5484         for (i = 0; i < event_data->NumEntries; i++) {
5485                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5486                 if (!handle)
5487                         continue;
5488                 phy_number = event_data->StartPhyNum + i;
5489                 reason_code = event_data->PHY[i].PhyStatus &
5490                     MPI2_EVENT_SAS_TOPO_RC_MASK;
5491                 switch (reason_code) {
5492                 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5493                         status_str = "target add";
5494                         break;
5495                 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5496                         status_str = "target remove";
5497                         break;
5498                 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
5499                         status_str = "delay target remove";
5500                         break;
5501                 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5502                         status_str = "link rate change";
5503                         break;
5504                 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
5505                         status_str = "target responding";
5506                         break;
5507                 default:
5508                         status_str = "unknown";
5509                         break;
5510                 }
5511                 link_rate = event_data->PHY[i].LinkRate >> 4;
5512                 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5513                 pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
5514                     " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5515                     handle, status_str, link_rate, prev_link_rate);
5516
5517         }
5518 }
5519
5520 /**
5521  * _scsih_sas_topology_change_event - handle topology changes
5522  * @ioc: per adapter object
5523  * @fw_event: The fw_event_work object
5524  * Context: user.
5525  *
5526  */
5527 static int
5528 _scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
5529         struct fw_event_work *fw_event)
5530 {
5531         int i;
5532         u16 parent_handle, handle;
5533         u16 reason_code;
5534         u8 phy_number, max_phys;
5535         struct _sas_node *sas_expander;
5536         u64 sas_address;
5537         unsigned long flags;
5538         u8 link_rate, prev_link_rate;
5539         Mpi2EventDataSasTopologyChangeList_t *event_data =
5540                 (Mpi2EventDataSasTopologyChangeList_t *)
5541                 fw_event->event_data;
5542
5543         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5544                 _scsih_sas_topology_change_event_debug(ioc, event_data);
5545
5546         if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
5547                 return 0;
5548
5549         if (!ioc->sas_hba.num_phys)
5550                 _scsih_sas_host_add(ioc);
5551         else
5552                 _scsih_sas_host_refresh(ioc);
5553
5554         if (fw_event->ignore) {
5555                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5556                         "ignoring expander event\n", ioc->name));
5557                 return 0;
5558         }
5559
5560         parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5561
5562         /* handle expander add */
5563         if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5564                 if (_scsih_expander_add(ioc, parent_handle) != 0)
5565                         return 0;
5566
5567         spin_lock_irqsave(&ioc->sas_node_lock, flags);
5568         sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
5569             parent_handle);
5570         if (sas_expander) {
5571                 sas_address = sas_expander->sas_address;
5572                 max_phys = sas_expander->num_phys;
5573         } else if (parent_handle < ioc->sas_hba.num_phys) {
5574                 sas_address = ioc->sas_hba.sas_address;
5575                 max_phys = ioc->sas_hba.num_phys;
5576         } else {
5577                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5578                 return 0;
5579         }
5580         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5581
5582         /* handle siblings events */
5583         for (i = 0; i < event_data->NumEntries; i++) {
5584                 if (fw_event->ignore) {
5585                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
5586                                 "ignoring expander event\n", ioc->name));
5587                         return 0;
5588                 }
5589                 if (ioc->remove_host || ioc->pci_error_recovery)
5590                         return 0;
5591                 phy_number = event_data->StartPhyNum + i;
5592                 if (phy_number >= max_phys)
5593                         continue;
5594                 reason_code = event_data->PHY[i].PhyStatus &
5595                     MPI2_EVENT_SAS_TOPO_RC_MASK;
5596                 if ((event_data->PHY[i].PhyStatus &
5597                     MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5598                     MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
5599                                 continue;
5600                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5601                 if (!handle)
5602                         continue;
5603                 link_rate = event_data->PHY[i].LinkRate >> 4;
5604                 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5605                 switch (reason_code) {
5606                 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5607
5608                         if (ioc->shost_recovery)
5609                                 break;
5610
5611                         if (link_rate == prev_link_rate)
5612                                 break;
5613
5614                         mpt3sas_transport_update_links(ioc, sas_address,
5615                             handle, phy_number, link_rate);
5616
5617                         if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5618                                 break;
5619
5620                         _scsih_check_device(ioc, sas_address, handle,
5621                             phy_number, link_rate);
5622
5623
5624                 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5625
5626                         if (ioc->shost_recovery)
5627                                 break;
5628
5629                         mpt3sas_transport_update_links(ioc, sas_address,
5630                             handle, phy_number, link_rate);
5631
5632                         _scsih_add_device(ioc, handle, phy_number, 0);
5633
5634                         break;
5635                 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5636
5637                         _scsih_device_remove_by_handle(ioc, handle);
5638                         break;
5639                 }
5640         }
5641
5642         /* handle expander removal */
5643         if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5644             sas_expander)
5645                 mpt3sas_expander_remove(ioc, sas_address);
5646
5647         return 0;
5648 }
5649
5650 /**
5651  * _scsih_sas_device_status_change_event_debug - debug for device event
5652  * @event_data: event data payload
5653  * Context: user.
5654  *
5655  * Return nothing.
5656  */
5657 static void
5658 _scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5659         Mpi2EventDataSasDeviceStatusChange_t *event_data)
5660 {
5661         char *reason_str = NULL;
5662
5663         switch (event_data->ReasonCode) {
5664         case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5665                 reason_str = "smart data";
5666                 break;
5667         case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5668                 reason_str = "unsupported device discovered";
5669                 break;
5670         case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5671                 reason_str = "internal device reset";
5672                 break;
5673         case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5674                 reason_str = "internal task abort";
5675                 break;
5676         case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5677                 reason_str = "internal task abort set";
5678                 break;
5679         case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5680                 reason_str = "internal clear task set";
5681                 break;
5682         case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5683                 reason_str = "internal query task";
5684                 break;
5685         case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5686                 reason_str = "sata init failure";
5687                 break;
5688         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5689                 reason_str = "internal device reset complete";
5690                 break;
5691         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5692                 reason_str = "internal task abort complete";
5693                 break;
5694         case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5695                 reason_str = "internal async notification";
5696                 break;
5697         case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5698                 reason_str = "expander reduced functionality";
5699                 break;
5700         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5701                 reason_str = "expander reduced functionality complete";
5702                 break;
5703         default:
5704                 reason_str = "unknown reason";
5705                 break;
5706         }
5707         pr_info(MPT3SAS_FMT "device status change: (%s)\n"
5708             "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5709             ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5710             (unsigned long long)le64_to_cpu(event_data->SASAddress),
5711             le16_to_cpu(event_data->TaskTag));
5712         if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
5713                 pr_info(MPT3SAS_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
5714                     event_data->ASC, event_data->ASCQ);
5715         pr_info("\n");
5716 }
5717
5718 /**
5719  * _scsih_sas_device_status_change_event - handle device status change
5720  * @ioc: per adapter object
5721  * @fw_event: The fw_event_work object
5722  * Context: user.
5723  *
5724  * Return nothing.
5725  */
5726 static void
5727 _scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5728         struct fw_event_work *fw_event)
5729 {
5730         struct MPT3SAS_TARGET *target_priv_data;
5731         struct _sas_device *sas_device;
5732         u64 sas_address;
5733         unsigned long flags;
5734         Mpi2EventDataSasDeviceStatusChange_t *event_data =
5735                 (Mpi2EventDataSasDeviceStatusChange_t *)
5736                 fw_event->event_data;
5737
5738         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5739                 _scsih_sas_device_status_change_event_debug(ioc,
5740                      event_data);
5741
5742         /* In MPI Revision K (0xC), the internal device reset complete was
5743          * implemented, so avoid setting tm_busy flag for older firmware.
5744          */
5745         if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5746                 return;
5747
5748         if (event_data->ReasonCode !=
5749             MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
5750            event_data->ReasonCode !=
5751             MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
5752                 return;
5753
5754         spin_lock_irqsave(&ioc->sas_device_lock, flags);
5755         sas_address = le64_to_cpu(event_data->SASAddress);
5756         sas_device = __mpt3sas_get_sdev_by_addr(ioc,
5757             sas_address);
5758
5759         if (!sas_device || !sas_device->starget)
5760                 goto out;
5761
5762         target_priv_data = sas_device->starget->hostdata;
5763         if (!target_priv_data)
5764                 goto out;
5765
5766         if (event_data->ReasonCode ==
5767             MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5768                 target_priv_data->tm_busy = 1;
5769         else
5770                 target_priv_data->tm_busy = 0;
5771
5772 out:
5773         if (sas_device)
5774                 sas_device_put(sas_device);
5775
5776         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5777
5778 }
5779
5780 /**
5781  * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
5782  * event
5783  * @ioc: per adapter object
5784  * @event_data: event data payload
5785  * Context: user.
5786  *
5787  * Return nothing.
5788  */
5789 static void
5790 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5791         Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5792 {
5793         char *reason_str = NULL;
5794
5795         switch (event_data->ReasonCode) {
5796         case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5797                 reason_str = "enclosure add";
5798                 break;
5799         case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5800                 reason_str = "enclosure remove";
5801                 break;
5802         default:
5803                 reason_str = "unknown reason";
5804                 break;
5805         }
5806
5807         pr_info(MPT3SAS_FMT "enclosure status change: (%s)\n"
5808             "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5809             " number slots(%d)\n", ioc->name, reason_str,
5810             le16_to_cpu(event_data->EnclosureHandle),
5811             (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5812             le16_to_cpu(event_data->StartSlot));
5813 }
5814
5815 /**
5816  * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5817  * @ioc: per adapter object
5818  * @fw_event: The fw_event_work object
5819  * Context: user.
5820  *
5821  * Return nothing.
5822  */
5823 static void
5824 _scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5825         struct fw_event_work *fw_event)
5826 {
5827         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5828                 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
5829                      (Mpi2EventDataSasEnclDevStatusChange_t *)
5830                      fw_event->event_data);
5831 }
5832
5833 /**
5834  * _scsih_sas_broadcast_primitive_event - handle broadcast events
5835  * @ioc: per adapter object
5836  * @fw_event: The fw_event_work object
5837  * Context: user.
5838  *
5839  * Return nothing.
5840  */
5841 static void
5842 _scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
5843         struct fw_event_work *fw_event)
5844 {
5845         struct scsi_cmnd *scmd;
5846         struct scsi_device *sdev;
5847         u16 smid, handle;
5848         u32 lun;
5849         struct MPT3SAS_DEVICE *sas_device_priv_data;
5850         u32 termination_count;
5851         u32 query_count;
5852         Mpi2SCSITaskManagementReply_t *mpi_reply;
5853         Mpi2EventDataSasBroadcastPrimitive_t *event_data =
5854                 (Mpi2EventDataSasBroadcastPrimitive_t *)
5855                 fw_event->event_data;
5856         u16 ioc_status;
5857         unsigned long flags;
5858         int r;
5859         u8 max_retries = 0;
5860         u8 task_abort_retries;
5861
5862         mutex_lock(&ioc->tm_cmds.mutex);
5863         pr_info(MPT3SAS_FMT
5864                 "%s: enter: phy number(%d), width(%d)\n",
5865                 ioc->name, __func__, event_data->PhyNum,
5866              event_data->PortWidth);
5867
5868         _scsih_block_io_all_device(ioc);
5869
5870         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5871         mpi_reply = ioc->tm_cmds.reply;
5872  broadcast_aen_retry:
5873
5874         /* sanity checks for retrying this loop */
5875         if (max_retries++ == 5) {
5876                 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: giving up\n",
5877                     ioc->name, __func__));
5878                 goto out;
5879         } else if (max_retries > 1)
5880                 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: %d retry\n",
5881                     ioc->name, __func__, max_retries - 1));
5882
5883         termination_count = 0;
5884         query_count = 0;
5885         for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
5886                 if (ioc->shost_recovery)
5887                         goto out;
5888                 scmd = _scsih_scsi_lookup_get(ioc, smid);
5889                 if (!scmd)
5890                         continue;
5891                 sdev = scmd->device;
5892                 sas_device_priv_data = sdev->hostdata;
5893                 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5894                         continue;
5895                  /* skip hidden raid components */
5896                 if (sas_device_priv_data->sas_target->flags &
5897                     MPT_TARGET_FLAGS_RAID_COMPONENT)
5898                         continue;
5899                  /* skip volumes */
5900                 if (sas_device_priv_data->sas_target->flags &
5901                     MPT_TARGET_FLAGS_VOLUME)
5902                         continue;
5903
5904                 handle = sas_device_priv_data->sas_target->handle;
5905                 lun = sas_device_priv_data->lun;
5906                 query_count++;
5907
5908                 if (ioc->shost_recovery)
5909                         goto out;
5910
5911                 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5912                 r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5913                     MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30,
5914                     TM_MUTEX_OFF);
5915                 if (r == FAILED) {
5916                         sdev_printk(KERN_WARNING, sdev,
5917                             "mpt3sas_scsih_issue_tm: FAILED when sending "
5918                             "QUERY_TASK: scmd(%p)\n", scmd);
5919                         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5920                         goto broadcast_aen_retry;
5921                 }
5922                 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5923                     & MPI2_IOCSTATUS_MASK;
5924                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5925                         sdev_printk(KERN_WARNING, sdev,
5926                                 "query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
5927                                 ioc_status, scmd);
5928                         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5929                         goto broadcast_aen_retry;
5930                 }
5931
5932                 /* see if IO is still owned by IOC and target */
5933                 if (mpi_reply->ResponseCode ==
5934                      MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5935                      mpi_reply->ResponseCode ==
5936                      MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
5937                         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5938                         continue;
5939                 }
5940                 task_abort_retries = 0;
5941  tm_retry:
5942                 if (task_abort_retries++ == 60) {
5943                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
5944                             "%s: ABORT_TASK: giving up\n", ioc->name,
5945                             __func__));
5946                         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5947                         goto broadcast_aen_retry;
5948                 }
5949
5950                 if (ioc->shost_recovery)
5951                         goto out_no_lock;
5952
5953                 r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5954                     sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
5955                     TM_MUTEX_OFF);
5956                 if (r == FAILED) {
5957                         sdev_printk(KERN_WARNING, sdev,
5958                             "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
5959                             "scmd(%p)\n", scmd);
5960                         goto tm_retry;
5961                 }
5962
5963                 if (task_abort_retries > 1)
5964                         sdev_printk(KERN_WARNING, sdev,
5965                             "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5966                             " scmd(%p)\n",
5967                             task_abort_retries - 1, scmd);
5968
5969                 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
5970                 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5971         }
5972
5973         if (ioc->broadcast_aen_pending) {
5974                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5975                         "%s: loop back due to pending AEN\n",
5976                         ioc->name, __func__));
5977                  ioc->broadcast_aen_pending = 0;
5978                  goto broadcast_aen_retry;
5979         }
5980
5981  out:
5982         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5983  out_no_lock:
5984
5985         dewtprintk(ioc, pr_info(MPT3SAS_FMT
5986             "%s - exit, query_count = %d termination_count = %d\n",
5987             ioc->name, __func__, query_count, termination_count));
5988
5989         ioc->broadcast_aen_busy = 0;
5990         if (!ioc->shost_recovery)
5991                 _scsih_ublock_io_all_device(ioc);
5992         mutex_unlock(&ioc->tm_cmds.mutex);
5993 }
5994
5995 /**
5996  * _scsih_sas_discovery_event - handle discovery events
5997  * @ioc: per adapter object
5998  * @fw_event: The fw_event_work object
5999  * Context: user.
6000  *
6001  * Return nothing.
6002  */
6003 static void
6004 _scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
6005         struct fw_event_work *fw_event)
6006 {
6007         Mpi2EventDataSasDiscovery_t *event_data =
6008                 (Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
6009
6010         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
6011                 pr_info(MPT3SAS_FMT "discovery event: (%s)", ioc->name,
6012                     (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
6013                     "start" : "stop");
6014                 if (event_data->DiscoveryStatus)
6015                         pr_info("discovery_status(0x%08x)",
6016                             le32_to_cpu(event_data->DiscoveryStatus));
6017                 pr_info("\n");
6018         }
6019
6020         if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
6021             !ioc->sas_hba.num_phys) {
6022                 if (disable_discovery > 0 && ioc->shost_recovery) {
6023                         /* Wait for the reset to complete */
6024                         while (ioc->shost_recovery)
6025                                 ssleep(1);
6026                 }
6027                 _scsih_sas_host_add(ioc);
6028         }
6029 }
6030
6031 /**
6032  * _scsih_ir_fastpath - turn on fastpath for IR physdisk
6033  * @ioc: per adapter object
6034  * @handle: device handle for physical disk
6035  * @phys_disk_num: physical disk number
6036  *
6037  * Return 0 for success, else failure.
6038  */
6039 static int
6040 _scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
6041 {
6042         Mpi2RaidActionRequest_t *mpi_request;
6043         Mpi2RaidActionReply_t *mpi_reply;
6044         u16 smid;
6045         u8 issue_reset = 0;
6046         int rc = 0;
6047         u16 ioc_status;
6048         u32 log_info;
6049
6050         if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
6051                 return rc;
6052
6053         mutex_lock(&ioc->scsih_cmds.mutex);
6054
6055         if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
6056                 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
6057                     ioc->name, __func__);
6058                 rc = -EAGAIN;
6059                 goto out;
6060         }
6061         ioc->scsih_cmds.status = MPT3_CMD_PENDING;
6062
6063         smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6064         if (!smid) {
6065                 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
6066                     ioc->name, __func__);
6067                 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6068                 rc = -EAGAIN;
6069                 goto out;
6070         }
6071
6072         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
6073         ioc->scsih_cmds.smid = smid;
6074         memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6075
6076         mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6077         mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
6078         mpi_request->PhysDiskNum = phys_disk_num;
6079
6080         dewtprintk(ioc, pr_info(MPT3SAS_FMT "IR RAID_ACTION: turning fast "\
6081             "path on for handle(0x%04x), phys_disk_num (0x%02x)\n", ioc->name,
6082             handle, phys_disk_num));
6083
6084         init_completion(&ioc->scsih_cmds.done);
6085         mpt3sas_base_put_smid_default(ioc, smid);
6086         wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6087
6088         if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
6089                 pr_err(MPT3SAS_FMT "%s: timeout\n",
6090                     ioc->name, __func__);
6091                 if (!(ioc->scsih_cmds.status & MPT3_CMD_RESET))
6092                         issue_reset = 1;
6093                 rc = -EFAULT;
6094                 goto out;
6095         }
6096
6097         if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
6098
6099                 mpi_reply = ioc->scsih_cmds.reply;
6100                 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
6101                 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
6102                         log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
6103                 else
6104                         log_info = 0;
6105                 ioc_status &= MPI2_IOCSTATUS_MASK;
6106                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6107                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
6108                             "IR RAID_ACTION: failed: ioc_status(0x%04x), "
6109                             "loginfo(0x%08x)!!!\n", ioc->name, ioc_status,
6110                             log_info));
6111                         rc = -EFAULT;
6112                 } else
6113                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
6114                             "IR RAID_ACTION: completed successfully\n",
6115                             ioc->name));
6116         }
6117
6118  out:
6119         ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6120         mutex_unlock(&ioc->scsih_cmds.mutex);
6121
6122         if (issue_reset)
6123                 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
6124                     FORCE_BIG_HAMMER);
6125         return rc;
6126 }
6127
6128 /**
6129  * _scsih_reprobe_lun - reprobing lun
6130  * @sdev: scsi device struct
6131  * @no_uld_attach: sdev->no_uld_attach flag setting
6132  *
6133  **/
6134 static void
6135 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
6136 {
6137         int rc;
6138         sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6139         sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6140             sdev->no_uld_attach ? "hidding" : "exposing");
6141         rc = scsi_device_reprobe(sdev);
6142 }
6143
6144 /**
6145  * _scsih_sas_volume_add - add new volume
6146  * @ioc: per adapter object
6147  * @element: IR config element data
6148  * Context: user.
6149  *
6150  * Return nothing.
6151  */
6152 static void
6153 _scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
6154         Mpi2EventIrConfigElement_t *element)
6155 {
6156         struct _raid_device *raid_device;
6157         unsigned long flags;
6158         u64 wwid;
6159         u16 handle = le16_to_cpu(element->VolDevHandle);
6160         int rc;
6161
6162         mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6163         if (!wwid) {
6164                 pr_err(MPT3SAS_FMT
6165                     "failure at %s:%d/%s()!\n", ioc->name,
6166                     __FILE__, __LINE__, __func__);
6167                 return;
6168         }
6169
6170         spin_lock_irqsave(&ioc->raid_device_lock, flags);
6171         raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6172         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6173
6174         if (raid_device)
6175                 return;
6176
6177         raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6178         if (!raid_device) {
6179                 pr_err(MPT3SAS_FMT
6180                     "failure at %s:%d/%s()!\n", ioc->name,
6181                     __FILE__, __LINE__, __func__);
6182                 return;
6183         }
6184
6185         raid_device->id = ioc->sas_id++;
6186         raid_device->channel = RAID_CHANNEL;
6187         raid_device->handle = handle;
6188         raid_device->wwid = wwid;
6189         _scsih_raid_device_add(ioc, raid_device);
6190         if (!ioc->wait_for_discovery_to_complete) {
6191                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6192                     raid_device->id, 0);
6193                 if (rc)
6194                         _scsih_raid_device_remove(ioc, raid_device);
6195         } else {
6196                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6197                 _scsih_determine_boot_device(ioc, raid_device, 1);
6198                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6199         }
6200 }
6201
6202 /**
6203  * _scsih_sas_volume_delete - delete volume
6204  * @ioc: per adapter object
6205  * @handle: volume device handle
6206  * Context: user.
6207  *
6208  * Return nothing.
6209  */
6210 static void
6211 _scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
6212 {
6213         struct _raid_device *raid_device;
6214         unsigned long flags;
6215         struct MPT3SAS_TARGET *sas_target_priv_data;
6216         struct scsi_target *starget = NULL;
6217
6218         spin_lock_irqsave(&ioc->raid_device_lock, flags);
6219         raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
6220         if (raid_device) {
6221                 if (raid_device->starget) {
6222                         starget = raid_device->starget;
6223                         sas_target_priv_data = starget->hostdata;
6224                         sas_target_priv_data->deleted = 1;
6225                 }
6226                 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
6227                         ioc->name,  raid_device->handle,
6228                     (unsigned long long) raid_device->wwid);
6229                 list_del(&raid_device->list);
6230                 kfree(raid_device);
6231         }
6232         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6233         if (starget)
6234                 scsi_remove_target(&starget->dev);
6235 }
6236
6237 /**
6238  * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6239  * @ioc: per adapter object
6240  * @element: IR config element data
6241  * Context: user.
6242  *
6243  * Return nothing.
6244  */
6245 static void
6246 _scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
6247         Mpi2EventIrConfigElement_t *element)
6248 {
6249         struct _sas_device *sas_device;
6250         struct scsi_target *starget = NULL;
6251         struct MPT3SAS_TARGET *sas_target_priv_data;
6252         unsigned long flags;
6253         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6254
6255         spin_lock_irqsave(&ioc->sas_device_lock, flags);
6256         sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
6257         if (sas_device) {
6258                 sas_device->volume_handle = 0;
6259                 sas_device->volume_wwid = 0;
6260                 clear_bit(handle, ioc->pd_handles);
6261                 if (sas_device->starget && sas_device->starget->hostdata) {
6262                         starget = sas_device->starget;
6263                         sas_target_priv_data = starget->hostdata;
6264                         sas_target_priv_data->flags &=
6265                             ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6266                 }
6267         }
6268         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6269         if (!sas_device)
6270                 return;
6271
6272         /* exposing raid component */
6273         if (starget)
6274                 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
6275
6276         sas_device_put(sas_device);
6277 }
6278
6279 /**
6280  * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6281  * @ioc: per adapter object
6282  * @element: IR config element data
6283  * Context: user.
6284  *
6285  * Return nothing.
6286  */
6287 static void
6288 _scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
6289         Mpi2EventIrConfigElement_t *element)
6290 {
6291         struct _sas_device *sas_device;
6292         struct scsi_target *starget = NULL;
6293         struct MPT3SAS_TARGET *sas_target_priv_data;
6294         unsigned long flags;
6295         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6296         u16 volume_handle = 0;
6297         u64 volume_wwid = 0;
6298
6299         mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
6300         if (volume_handle)
6301                 mpt3sas_config_get_volume_wwid(ioc, volume_handle,
6302                     &volume_wwid);
6303
6304         spin_lock_irqsave(&ioc->sas_device_lock, flags);
6305         sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
6306         if (sas_device) {
6307                 set_bit(handle, ioc->pd_handles);
6308                 if (sas_device->starget && sas_device->starget->hostdata) {
6309                         starget = sas_device->starget;
6310                         sas_target_priv_data = starget->hostdata;
6311                         sas_target_priv_data->flags |=
6312                             MPT_TARGET_FLAGS_RAID_COMPONENT;
6313                         sas_device->volume_handle = volume_handle;
6314                         sas_device->volume_wwid = volume_wwid;
6315                 }
6316         }
6317         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6318         if (!sas_device)
6319                 return;
6320
6321         /* hiding raid component */
6322         _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6323
6324         if (starget)
6325                 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
6326
6327         sas_device_put(sas_device);
6328 }
6329
6330 /**
6331  * _scsih_sas_pd_delete - delete pd component
6332  * @ioc: per adapter object
6333  * @element: IR config element data
6334  * Context: user.
6335  *
6336  * Return nothing.
6337  */
6338 static void
6339 _scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
6340         Mpi2EventIrConfigElement_t *element)
6341 {
6342         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6343
6344         _scsih_device_remove_by_handle(ioc, handle);
6345 }
6346
6347 /**
6348  * _scsih_sas_pd_add - remove pd component
6349  * @ioc: per adapter object
6350  * @element: IR config element data
6351  * Context: user.
6352  *
6353  * Return nothing.
6354  */
6355 static void
6356 _scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
6357         Mpi2EventIrConfigElement_t *element)
6358 {
6359         struct _sas_device *sas_device;
6360         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6361         Mpi2ConfigReply_t mpi_reply;
6362         Mpi2SasDevicePage0_t sas_device_pg0;
6363         u32 ioc_status;
6364         u64 sas_address;
6365         u16 parent_handle;
6366
6367         set_bit(handle, ioc->pd_handles);
6368
6369         sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6370         if (sas_device) {
6371                 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6372                 sas_device_put(sas_device);
6373                 return;
6374         }
6375
6376         if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6377             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6378                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6379                     ioc->name, __FILE__, __LINE__, __func__);
6380                 return;
6381         }
6382
6383         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6384             MPI2_IOCSTATUS_MASK;
6385         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6386                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6387                     ioc->name, __FILE__, __LINE__, __func__);
6388                 return;
6389         }
6390
6391         parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6392         if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6393                 mpt3sas_transport_update_links(ioc, sas_address, handle,
6394                     sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6395
6396         _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6397         _scsih_add_device(ioc, handle, 0, 1);
6398 }
6399
6400 /**
6401  * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6402  * @ioc: per adapter object
6403  * @event_data: event data payload
6404  * Context: user.
6405  *
6406  * Return nothing.
6407  */
6408 static void
6409 _scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
6410         Mpi2EventDataIrConfigChangeList_t *event_data)
6411 {
6412         Mpi2EventIrConfigElement_t *element;
6413         u8 element_type;
6414         int i;
6415         char *reason_str = NULL, *element_str = NULL;
6416
6417         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6418
6419         pr_info(MPT3SAS_FMT "raid config change: (%s), elements(%d)\n",
6420             ioc->name, (le32_to_cpu(event_data->Flags) &
6421             MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6422             "foreign" : "native", event_data->NumElements);
6423         for (i = 0; i < event_data->NumElements; i++, element++) {
6424                 switch (element->ReasonCode) {
6425                 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6426                         reason_str = "add";
6427                         break;
6428                 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6429                         reason_str = "remove";
6430                         break;
6431                 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6432                         reason_str = "no change";
6433                         break;
6434                 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6435                         reason_str = "hide";
6436                         break;
6437                 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6438                         reason_str = "unhide";
6439                         break;
6440                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6441                         reason_str = "volume_created";
6442                         break;
6443                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6444                         reason_str = "volume_deleted";
6445                         break;
6446                 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6447                         reason_str = "pd_created";
6448                         break;
6449                 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6450                         reason_str = "pd_deleted";
6451                         break;
6452                 default:
6453                         reason_str = "unknown reason";
6454                         break;
6455                 }
6456                 element_type = le16_to_cpu(element->ElementFlags) &
6457                     MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6458                 switch (element_type) {
6459                 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6460                         element_str = "volume";
6461                         break;
6462                 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6463                         element_str = "phys disk";
6464                         break;
6465                 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6466                         element_str = "hot spare";
6467                         break;
6468                 default:
6469                         element_str = "unknown element";
6470                         break;
6471                 }
6472                 pr_info("\t(%s:%s), vol handle(0x%04x), " \
6473                     "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6474                     reason_str, le16_to_cpu(element->VolDevHandle),
6475                     le16_to_cpu(element->PhysDiskDevHandle),
6476                     element->PhysDiskNum);
6477         }
6478 }
6479
6480 /**
6481  * _scsih_sas_ir_config_change_event - handle ir configuration change events
6482  * @ioc: per adapter object
6483  * @fw_event: The fw_event_work object
6484  * Context: user.
6485  *
6486  * Return nothing.
6487  */
6488 static void
6489 _scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
6490         struct fw_event_work *fw_event)
6491 {
6492         Mpi2EventIrConfigElement_t *element;
6493         int i;
6494         u8 foreign_config;
6495         Mpi2EventDataIrConfigChangeList_t *event_data =
6496                 (Mpi2EventDataIrConfigChangeList_t *)
6497                 fw_event->event_data;
6498
6499         if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
6500              (!ioc->hide_ir_msg))
6501                 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6502
6503         foreign_config = (le32_to_cpu(event_data->Flags) &
6504             MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
6505
6506         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6507         if (ioc->shost_recovery &&
6508             ioc->hba_mpi_version_belonged != MPI2_VERSION) {
6509                 for (i = 0; i < event_data->NumElements; i++, element++) {
6510                         if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
6511                                 _scsih_ir_fastpath(ioc,
6512                                         le16_to_cpu(element->PhysDiskDevHandle),
6513                                         element->PhysDiskNum);
6514                 }
6515                 return;
6516         }
6517
6518         for (i = 0; i < event_data->NumElements; i++, element++) {
6519
6520                 switch (element->ReasonCode) {
6521                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6522                 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6523                         if (!foreign_config)
6524                                 _scsih_sas_volume_add(ioc, element);
6525                         break;
6526                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6527                 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6528                         if (!foreign_config)
6529                                 _scsih_sas_volume_delete(ioc,
6530                                     le16_to_cpu(element->VolDevHandle));
6531                         break;
6532                 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6533                         if (!ioc->is_warpdrive)
6534                                 _scsih_sas_pd_hide(ioc, element);
6535                         break;
6536                 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6537                         if (!ioc->is_warpdrive)
6538                                 _scsih_sas_pd_expose(ioc, element);
6539                         break;
6540                 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6541                         if (!ioc->is_warpdrive)
6542                                 _scsih_sas_pd_add(ioc, element);
6543                         break;
6544                 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6545                         if (!ioc->is_warpdrive)
6546                                 _scsih_sas_pd_delete(ioc, element);
6547                         break;
6548                 }
6549         }
6550 }
6551
6552 /**
6553  * _scsih_sas_ir_volume_event - IR volume event
6554  * @ioc: per adapter object
6555  * @fw_event: The fw_event_work object
6556  * Context: user.
6557  *
6558  * Return nothing.
6559  */
6560 static void
6561 _scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
6562         struct fw_event_work *fw_event)
6563 {
6564         u64 wwid;
6565         unsigned long flags;
6566         struct _raid_device *raid_device;
6567         u16 handle;
6568         u32 state;
6569         int rc;
6570         Mpi2EventDataIrVolume_t *event_data =
6571                 (Mpi2EventDataIrVolume_t *) fw_event->event_data;
6572
6573         if (ioc->shost_recovery)
6574                 return;
6575
6576         if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6577                 return;
6578
6579         handle = le16_to_cpu(event_data->VolDevHandle);
6580         state = le32_to_cpu(event_data->NewValue);
6581         if (!ioc->hide_ir_msg)
6582                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6583                     "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6584                     ioc->name, __func__,  handle,
6585                     le32_to_cpu(event_data->PreviousValue), state));
6586         switch (state) {
6587         case MPI2_RAID_VOL_STATE_MISSING:
6588         case MPI2_RAID_VOL_STATE_FAILED:
6589                 _scsih_sas_volume_delete(ioc, handle);
6590                 break;
6591
6592         case MPI2_RAID_VOL_STATE_ONLINE:
6593         case MPI2_RAID_VOL_STATE_DEGRADED:
6594         case MPI2_RAID_VOL_STATE_OPTIMAL:
6595
6596                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6597                 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
6598                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6599
6600                 if (raid_device)
6601                         break;
6602
6603                 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6604                 if (!wwid) {
6605                         pr_err(MPT3SAS_FMT
6606                             "failure at %s:%d/%s()!\n", ioc->name,
6607                             __FILE__, __LINE__, __func__);
6608                         break;
6609                 }
6610
6611                 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6612                 if (!raid_device) {
6613                         pr_err(MPT3SAS_FMT
6614                             "failure at %s:%d/%s()!\n", ioc->name,
6615                             __FILE__, __LINE__, __func__);
6616                         break;
6617                 }
6618
6619                 raid_device->id = ioc->sas_id++;
6620                 raid_device->channel = RAID_CHANNEL;
6621                 raid_device->handle = handle;
6622                 raid_device->wwid = wwid;
6623                 _scsih_raid_device_add(ioc, raid_device);
6624                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6625                     raid_device->id, 0);
6626                 if (rc)
6627                         _scsih_raid_device_remove(ioc, raid_device);
6628                 break;
6629
6630         case MPI2_RAID_VOL_STATE_INITIALIZING:
6631         default:
6632                 break;
6633         }
6634 }
6635
6636 /**
6637  * _scsih_sas_ir_physical_disk_event - PD event
6638  * @ioc: per adapter object
6639  * @fw_event: The fw_event_work object
6640  * Context: user.
6641  *
6642  * Return nothing.
6643  */
6644 static void
6645 _scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
6646         struct fw_event_work *fw_event)
6647 {
6648         u16 handle, parent_handle;
6649         u32 state;
6650         struct _sas_device *sas_device;
6651         Mpi2ConfigReply_t mpi_reply;
6652         Mpi2SasDevicePage0_t sas_device_pg0;
6653         u32 ioc_status;
6654         Mpi2EventDataIrPhysicalDisk_t *event_data =
6655                 (Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
6656         u64 sas_address;
6657
6658         if (ioc->shost_recovery)
6659                 return;
6660
6661         if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6662                 return;
6663
6664         handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6665         state = le32_to_cpu(event_data->NewValue);
6666
6667         if (!ioc->hide_ir_msg)
6668                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6669                     "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6670                     ioc->name, __func__,  handle,
6671                     le32_to_cpu(event_data->PreviousValue), state));
6672
6673         switch (state) {
6674         case MPI2_RAID_PD_STATE_ONLINE:
6675         case MPI2_RAID_PD_STATE_DEGRADED:
6676         case MPI2_RAID_PD_STATE_REBUILDING:
6677         case MPI2_RAID_PD_STATE_OPTIMAL:
6678         case MPI2_RAID_PD_STATE_HOT_SPARE:
6679
6680                 if (!ioc->is_warpdrive)
6681                         set_bit(handle, ioc->pd_handles);
6682
6683                 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6684                 if (sas_device) {
6685                         sas_device_put(sas_device);
6686                         return;
6687                 }
6688
6689                 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6690                     &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6691                     handle))) {
6692                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6693                             ioc->name, __FILE__, __LINE__, __func__);
6694                         return;
6695                 }
6696
6697                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6698                     MPI2_IOCSTATUS_MASK;
6699                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6700                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6701                             ioc->name, __FILE__, __LINE__, __func__);
6702                         return;
6703                 }
6704
6705                 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6706                 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6707                         mpt3sas_transport_update_links(ioc, sas_address, handle,
6708                             sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6709
6710                 _scsih_add_device(ioc, handle, 0, 1);
6711
6712                 break;
6713
6714         case MPI2_RAID_PD_STATE_OFFLINE:
6715         case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6716         case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
6717         default:
6718                 break;
6719         }
6720 }
6721
6722 /**
6723  * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6724  * @ioc: per adapter object
6725  * @event_data: event data payload
6726  * Context: user.
6727  *
6728  * Return nothing.
6729  */
6730 static void
6731 _scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
6732         Mpi2EventDataIrOperationStatus_t *event_data)
6733 {
6734         char *reason_str = NULL;
6735
6736         switch (event_data->RAIDOperation) {
6737         case MPI2_EVENT_IR_RAIDOP_RESYNC:
6738                 reason_str = "resync";
6739                 break;
6740         case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6741                 reason_str = "online capacity expansion";
6742                 break;
6743         case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6744                 reason_str = "consistency check";
6745                 break;
6746         case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6747                 reason_str = "background init";
6748                 break;
6749         case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6750                 reason_str = "make data consistent";
6751                 break;
6752         }
6753
6754         if (!reason_str)
6755                 return;
6756
6757         pr_info(MPT3SAS_FMT "raid operational status: (%s)" \
6758             "\thandle(0x%04x), percent complete(%d)\n",
6759             ioc->name, reason_str,
6760             le16_to_cpu(event_data->VolDevHandle),
6761             event_data->PercentComplete);
6762 }
6763
6764 /**
6765  * _scsih_sas_ir_operation_status_event - handle RAID operation events
6766  * @ioc: per adapter object
6767  * @fw_event: The fw_event_work object
6768  * Context: user.
6769  *
6770  * Return nothing.
6771  */
6772 static void
6773 _scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
6774         struct fw_event_work *fw_event)
6775 {
6776         Mpi2EventDataIrOperationStatus_t *event_data =
6777                 (Mpi2EventDataIrOperationStatus_t *)
6778                 fw_event->event_data;
6779         static struct _raid_device *raid_device;
6780         unsigned long flags;
6781         u16 handle;
6782
6783         if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
6784             (!ioc->hide_ir_msg))
6785                 _scsih_sas_ir_operation_status_event_debug(ioc,
6786                      event_data);
6787
6788         /* code added for raid transport support */
6789         if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6790
6791                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6792                 handle = le16_to_cpu(event_data->VolDevHandle);
6793                 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
6794                 if (raid_device)
6795                         raid_device->percent_complete =
6796                             event_data->PercentComplete;
6797                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6798         }
6799 }
6800
6801 /**
6802  * _scsih_prep_device_scan - initialize parameters prior to device scan
6803  * @ioc: per adapter object
6804  *
6805  * Set the deleted flag prior to device scan.  If the device is found during
6806  * the scan, then we clear the deleted flag.
6807  */
6808 static void
6809 _scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
6810 {
6811         struct MPT3SAS_DEVICE *sas_device_priv_data;
6812         struct scsi_device *sdev;
6813
6814         shost_for_each_device(sdev, ioc->shost) {
6815                 sas_device_priv_data = sdev->hostdata;
6816                 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6817                         sas_device_priv_data->sas_target->deleted = 1;
6818         }
6819 }
6820
6821 /**
6822  * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6823  * @ioc: per adapter object
6824  * @sas_device_pg0: SAS Device page 0
6825  *
6826  * After host reset, find out whether devices are still responding.
6827  * Used in _scsih_remove_unresponsive_sas_devices.
6828  *
6829  * Return nothing.
6830  */
6831 static void
6832 _scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
6833 Mpi2SasDevicePage0_t *sas_device_pg0)
6834 {
6835         struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
6836         struct scsi_target *starget;
6837         struct _sas_device *sas_device;
6838         unsigned long flags;
6839
6840         spin_lock_irqsave(&ioc->sas_device_lock, flags);
6841         list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6842                 if ((sas_device->sas_address == sas_device_pg0->SASAddress) &&
6843                         (sas_device->slot == sas_device_pg0->Slot)) {
6844                         sas_device->responding = 1;
6845                         starget = sas_device->starget;
6846                         if (starget && starget->hostdata) {
6847                                 sas_target_priv_data = starget->hostdata;
6848                                 sas_target_priv_data->tm_busy = 0;
6849                                 sas_target_priv_data->deleted = 0;
6850                         } else
6851                                 sas_target_priv_data = NULL;
6852                         if (starget) {
6853                                 starget_printk(KERN_INFO, starget,
6854                                     "handle(0x%04x), sas_addr(0x%016llx)\n",
6855                                     sas_device_pg0->DevHandle,
6856                                     (unsigned long long)
6857                                     sas_device->sas_address);
6858
6859                                 if (sas_device->enclosure_handle != 0)
6860                                         starget_printk(KERN_INFO, starget,
6861                                          "enclosure logical id(0x%016llx),"
6862                                          " slot(%d)\n",
6863                                          (unsigned long long)
6864                                          sas_device->enclosure_logical_id,
6865                                          sas_device->slot);
6866                         }
6867                         if (sas_device_pg0->Flags &
6868                               MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
6869                                 sas_device->enclosure_level =
6870                                    le16_to_cpu(sas_device_pg0->EnclosureLevel);
6871                                 memcpy(&sas_device->connector_name[0],
6872                                         &sas_device_pg0->ConnectorName[0], 4);
6873                         } else {
6874                                 sas_device->enclosure_level = 0;
6875                                 sas_device->connector_name[0] = '\0';
6876                         }
6877
6878                         if (sas_device->handle == sas_device_pg0->DevHandle)
6879                                 goto out;
6880                         pr_info("\thandle changed from(0x%04x)!!!\n",
6881                             sas_device->handle);
6882                         sas_device->handle = sas_device_pg0->DevHandle;
6883                         if (sas_target_priv_data)
6884                                 sas_target_priv_data->handle =
6885                                         sas_device_pg0->DevHandle;
6886                         goto out;
6887                 }
6888         }
6889  out:
6890         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6891 }
6892
6893 /**
6894  * _scsih_search_responding_sas_devices -
6895  * @ioc: per adapter object
6896  *
6897  * After host reset, find out whether devices are still responding.
6898  * If not remove.
6899  *
6900  * Return nothing.
6901  */
6902 static void
6903 _scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
6904 {
6905         Mpi2SasDevicePage0_t sas_device_pg0;
6906         Mpi2ConfigReply_t mpi_reply;
6907         u16 ioc_status;
6908         u16 handle;
6909         u32 device_info;
6910
6911         pr_info(MPT3SAS_FMT "search for end-devices: start\n", ioc->name);
6912
6913         if (list_empty(&ioc->sas_device_list))
6914                 goto out;
6915
6916         handle = 0xFFFF;
6917         while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6918             &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6919             handle))) {
6920                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6921                     MPI2_IOCSTATUS_MASK;
6922                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6923                         break;
6924                 handle = sas_device_pg0.DevHandle =
6925                                 le16_to_cpu(sas_device_pg0.DevHandle);
6926                 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6927                 if (!(_scsih_is_end_device(device_info)))
6928                         continue;
6929                 sas_device_pg0.SASAddress =
6930                                 le64_to_cpu(sas_device_pg0.SASAddress);
6931                 sas_device_pg0.Slot = le16_to_cpu(sas_device_pg0.Slot);
6932                 _scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
6933         }
6934
6935  out:
6936         pr_info(MPT3SAS_FMT "search for end-devices: complete\n",
6937             ioc->name);
6938 }
6939
6940 /**
6941  * _scsih_mark_responding_raid_device - mark a raid_device as responding
6942  * @ioc: per adapter object
6943  * @wwid: world wide identifier for raid volume
6944  * @handle: device handle
6945  *
6946  * After host reset, find out whether devices are still responding.
6947  * Used in _scsih_remove_unresponsive_raid_devices.
6948  *
6949  * Return nothing.
6950  */
6951 static void
6952 _scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
6953         u16 handle)
6954 {
6955         struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
6956         struct scsi_target *starget;
6957         struct _raid_device *raid_device;
6958         unsigned long flags;
6959
6960         spin_lock_irqsave(&ioc->raid_device_lock, flags);
6961         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6962                 if (raid_device->wwid == wwid && raid_device->starget) {
6963                         starget = raid_device->starget;
6964                         if (starget && starget->hostdata) {
6965                                 sas_target_priv_data = starget->hostdata;
6966                                 sas_target_priv_data->deleted = 0;
6967                         } else
6968                                 sas_target_priv_data = NULL;
6969                         raid_device->responding = 1;
6970                         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6971                         starget_printk(KERN_INFO, raid_device->starget,
6972                             "handle(0x%04x), wwid(0x%016llx)\n", handle,
6973                             (unsigned long long)raid_device->wwid);
6974
6975                         /*
6976                          * WARPDRIVE: The handles of the PDs might have changed
6977                          * across the host reset so re-initialize the
6978                          * required data for Direct IO
6979                          */
6980                         mpt3sas_init_warpdrive_properties(ioc, raid_device);
6981                         spin_lock_irqsave(&ioc->raid_device_lock, flags);
6982                         if (raid_device->handle == handle) {
6983                                 spin_unlock_irqrestore(&ioc->raid_device_lock,
6984                                     flags);
6985                                 return;
6986                         }
6987                         pr_info("\thandle changed from(0x%04x)!!!\n",
6988                             raid_device->handle);
6989                         raid_device->handle = handle;
6990                         if (sas_target_priv_data)
6991                                 sas_target_priv_data->handle = handle;
6992                         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6993                         return;
6994                 }
6995         }
6996         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6997 }
6998
6999 /**
7000  * _scsih_search_responding_raid_devices -
7001  * @ioc: per adapter object
7002  *
7003  * After host reset, find out whether devices are still responding.
7004  * If not remove.
7005  *
7006  * Return nothing.
7007  */
7008 static void
7009 _scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
7010 {
7011         Mpi2RaidVolPage1_t volume_pg1;
7012         Mpi2RaidVolPage0_t volume_pg0;
7013         Mpi2RaidPhysDiskPage0_t pd_pg0;
7014         Mpi2ConfigReply_t mpi_reply;
7015         u16 ioc_status;
7016         u16 handle;
7017         u8 phys_disk_num;
7018
7019         if (!ioc->ir_firmware)
7020                 return;
7021
7022         pr_info(MPT3SAS_FMT "search for raid volumes: start\n",
7023             ioc->name);
7024
7025         if (list_empty(&ioc->raid_device_list))
7026                 goto out;
7027
7028         handle = 0xFFFF;
7029         while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7030             &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7031                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7032                     MPI2_IOCSTATUS_MASK;
7033                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7034                         break;
7035                 handle = le16_to_cpu(volume_pg1.DevHandle);
7036
7037                 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7038                     &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7039                      sizeof(Mpi2RaidVolPage0_t)))
7040                         continue;
7041
7042                 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7043                     volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7044                     volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
7045                         _scsih_mark_responding_raid_device(ioc,
7046                             le64_to_cpu(volume_pg1.WWID), handle);
7047         }
7048
7049         /* refresh the pd_handles */
7050         if (!ioc->is_warpdrive) {
7051                 phys_disk_num = 0xFF;
7052                 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
7053                 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7054                     &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7055                     phys_disk_num))) {
7056                         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7057                             MPI2_IOCSTATUS_MASK;
7058                         if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7059                                 break;
7060                         phys_disk_num = pd_pg0.PhysDiskNum;
7061                         handle = le16_to_cpu(pd_pg0.DevHandle);
7062                         set_bit(handle, ioc->pd_handles);
7063                 }
7064         }
7065  out:
7066         pr_info(MPT3SAS_FMT "search for responding raid volumes: complete\n",
7067                 ioc->name);
7068 }
7069
7070 /**
7071  * _scsih_mark_responding_expander - mark a expander as responding
7072  * @ioc: per adapter object
7073  * @sas_address: sas address
7074  * @handle:
7075  *
7076  * After host reset, find out whether devices are still responding.
7077  * Used in _scsih_remove_unresponsive_expanders.
7078  *
7079  * Return nothing.
7080  */
7081 static void
7082 _scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
7083         u16 handle)
7084 {
7085         struct _sas_node *sas_expander;
7086         unsigned long flags;
7087         int i;
7088
7089         spin_lock_irqsave(&ioc->sas_node_lock, flags);
7090         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
7091                 if (sas_expander->sas_address != sas_address)
7092                         continue;
7093                 sas_expander->responding = 1;
7094                 if (sas_expander->handle == handle)
7095                         goto out;
7096                 pr_info("\texpander(0x%016llx): handle changed" \
7097                     " from(0x%04x) to (0x%04x)!!!\n",
7098                     (unsigned long long)sas_expander->sas_address,
7099                     sas_expander->handle, handle);
7100                 sas_expander->handle = handle;
7101                 for (i = 0 ; i < sas_expander->num_phys ; i++)
7102                         sas_expander->phy[i].handle = handle;
7103                 goto out;
7104         }
7105  out:
7106         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7107 }
7108
7109 /**
7110  * _scsih_search_responding_expanders -
7111  * @ioc: per adapter object
7112  *
7113  * After host reset, find out whether devices are still responding.
7114  * If not remove.
7115  *
7116  * Return nothing.
7117  */
7118 static void
7119 _scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
7120 {
7121         Mpi2ExpanderPage0_t expander_pg0;
7122         Mpi2ConfigReply_t mpi_reply;
7123         u16 ioc_status;
7124         u64 sas_address;
7125         u16 handle;
7126
7127         pr_info(MPT3SAS_FMT "search for expanders: start\n", ioc->name);
7128
7129         if (list_empty(&ioc->sas_expander_list))
7130                 goto out;
7131
7132         handle = 0xFFFF;
7133         while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7134             MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7135
7136                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7137                     MPI2_IOCSTATUS_MASK;
7138                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7139                         break;
7140
7141                 handle = le16_to_cpu(expander_pg0.DevHandle);
7142                 sas_address = le64_to_cpu(expander_pg0.SASAddress);
7143                 pr_info("\texpander present: handle(0x%04x), sas_addr(0x%016llx)\n",
7144                         handle,
7145                     (unsigned long long)sas_address);
7146                 _scsih_mark_responding_expander(ioc, sas_address, handle);
7147         }
7148
7149  out:
7150         pr_info(MPT3SAS_FMT "search for expanders: complete\n", ioc->name);
7151 }
7152
7153 /**
7154  * _scsih_remove_unresponding_sas_devices - removing unresponding devices
7155  * @ioc: per adapter object
7156  *
7157  * Return nothing.
7158  */
7159 static void
7160 _scsih_remove_unresponding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7161 {
7162         struct _sas_device *sas_device, *sas_device_next;
7163         struct _sas_node *sas_expander, *sas_expander_next;
7164         struct _raid_device *raid_device, *raid_device_next;
7165         struct list_head tmp_list;
7166         unsigned long flags;
7167         LIST_HEAD(head);
7168
7169         pr_info(MPT3SAS_FMT "removing unresponding devices: start\n",
7170             ioc->name);
7171
7172         /* removing unresponding end devices */
7173         pr_info(MPT3SAS_FMT "removing unresponding devices: end-devices\n",
7174             ioc->name);
7175         /*
7176          * Iterate, pulling off devices marked as non-responding. We become the
7177          * owner for the reference the list had on any object we prune.
7178          */
7179         spin_lock_irqsave(&ioc->sas_device_lock, flags);
7180         list_for_each_entry_safe(sas_device, sas_device_next,
7181             &ioc->sas_device_list, list) {
7182                 if (!sas_device->responding)
7183                         list_move_tail(&sas_device->list, &head);
7184                 else
7185                         sas_device->responding = 0;
7186         }
7187         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7188
7189         /*
7190          * Now, uninitialize and remove the unresponding devices we pruned.
7191          */
7192         list_for_each_entry_safe(sas_device, sas_device_next, &head, list) {
7193                 _scsih_remove_device(ioc, sas_device);
7194                 list_del_init(&sas_device->list);
7195                 sas_device_put(sas_device);
7196         }
7197
7198         /* removing unresponding volumes */
7199         if (ioc->ir_firmware) {
7200                 pr_info(MPT3SAS_FMT "removing unresponding devices: volumes\n",
7201                         ioc->name);
7202                 list_for_each_entry_safe(raid_device, raid_device_next,
7203                     &ioc->raid_device_list, list) {
7204                         if (!raid_device->responding)
7205                                 _scsih_sas_volume_delete(ioc,
7206                                     raid_device->handle);
7207                         else
7208                                 raid_device->responding = 0;
7209                 }
7210         }
7211
7212         /* removing unresponding expanders */
7213         pr_info(MPT3SAS_FMT "removing unresponding devices: expanders\n",
7214             ioc->name);
7215         spin_lock_irqsave(&ioc->sas_node_lock, flags);
7216         INIT_LIST_HEAD(&tmp_list);
7217         list_for_each_entry_safe(sas_expander, sas_expander_next,
7218             &ioc->sas_expander_list, list) {
7219                 if (!sas_expander->responding)
7220                         list_move_tail(&sas_expander->list, &tmp_list);
7221                 else
7222                         sas_expander->responding = 0;
7223         }
7224         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7225         list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
7226             list) {
7227                 list_del(&sas_expander->list);
7228                 _scsih_expander_node_remove(ioc, sas_expander);
7229         }
7230
7231         pr_info(MPT3SAS_FMT "removing unresponding devices: complete\n",
7232             ioc->name);
7233
7234         /* unblock devices */
7235         _scsih_ublock_io_all_device(ioc);
7236 }
7237
7238 static void
7239 _scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
7240         struct _sas_node *sas_expander, u16 handle)
7241 {
7242         Mpi2ExpanderPage1_t expander_pg1;
7243         Mpi2ConfigReply_t mpi_reply;
7244         int i;
7245
7246         for (i = 0 ; i < sas_expander->num_phys ; i++) {
7247                 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
7248                     &expander_pg1, i, handle))) {
7249                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7250                             ioc->name, __FILE__, __LINE__, __func__);
7251                         return;
7252                 }
7253
7254                 mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
7255                     le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7256                     expander_pg1.NegotiatedLinkRate >> 4);
7257         }
7258 }
7259
7260 /**
7261  * _scsih_scan_for_devices_after_reset - scan for devices after host reset
7262  * @ioc: per adapter object
7263  *
7264  * Return nothing.
7265  */
7266 static void
7267 _scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
7268 {
7269         Mpi2ExpanderPage0_t expander_pg0;
7270         Mpi2SasDevicePage0_t sas_device_pg0;
7271         Mpi2RaidVolPage1_t volume_pg1;
7272         Mpi2RaidVolPage0_t volume_pg0;
7273         Mpi2RaidPhysDiskPage0_t pd_pg0;
7274         Mpi2EventIrConfigElement_t element;
7275         Mpi2ConfigReply_t mpi_reply;
7276         u8 phys_disk_num;
7277         u16 ioc_status;
7278         u16 handle, parent_handle;
7279         u64 sas_address;
7280         struct _sas_device *sas_device;
7281         struct _sas_node *expander_device;
7282         static struct _raid_device *raid_device;
7283         u8 retry_count;
7284         unsigned long flags;
7285
7286         pr_info(MPT3SAS_FMT "scan devices: start\n", ioc->name);
7287
7288         _scsih_sas_host_refresh(ioc);
7289
7290         pr_info(MPT3SAS_FMT "\tscan devices: expanders start\n", ioc->name);
7291
7292         /* expanders */
7293         handle = 0xFFFF;
7294         while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7295             MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7296                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7297                     MPI2_IOCSTATUS_MASK;
7298                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7299                         pr_info(MPT3SAS_FMT "\tbreak from expander scan: " \
7300                             "ioc_status(0x%04x), loginfo(0x%08x)\n",
7301                             ioc->name, ioc_status,
7302                             le32_to_cpu(mpi_reply.IOCLogInfo));
7303                         break;
7304                 }
7305                 handle = le16_to_cpu(expander_pg0.DevHandle);
7306                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7307                 expander_device = mpt3sas_scsih_expander_find_by_sas_address(
7308                     ioc, le64_to_cpu(expander_pg0.SASAddress));
7309                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7310                 if (expander_device)
7311                         _scsih_refresh_expander_links(ioc, expander_device,
7312                             handle);
7313                 else {
7314                         pr_info(MPT3SAS_FMT "\tBEFORE adding expander: " \
7315                             "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7316                             handle, (unsigned long long)
7317                             le64_to_cpu(expander_pg0.SASAddress));
7318                         _scsih_expander_add(ioc, handle);
7319                         pr_info(MPT3SAS_FMT "\tAFTER adding expander: " \
7320                             "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7321                             handle, (unsigned long long)
7322                             le64_to_cpu(expander_pg0.SASAddress));
7323                 }
7324         }
7325
7326         pr_info(MPT3SAS_FMT "\tscan devices: expanders complete\n",
7327             ioc->name);
7328
7329         if (!ioc->ir_firmware)
7330                 goto skip_to_sas;
7331
7332         pr_info(MPT3SAS_FMT "\tscan devices: phys disk start\n", ioc->name);
7333
7334         /* phys disk */
7335         phys_disk_num = 0xFF;
7336         while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7337             &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7338             phys_disk_num))) {
7339                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7340                     MPI2_IOCSTATUS_MASK;
7341                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7342                         pr_info(MPT3SAS_FMT "\tbreak from phys disk scan: "\
7343                             "ioc_status(0x%04x), loginfo(0x%08x)\n",
7344                             ioc->name, ioc_status,
7345                             le32_to_cpu(mpi_reply.IOCLogInfo));
7346                         break;
7347                 }
7348                 phys_disk_num = pd_pg0.PhysDiskNum;
7349                 handle = le16_to_cpu(pd_pg0.DevHandle);
7350                 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
7351                 if (sas_device) {
7352                         sas_device_put(sas_device);
7353                         continue;
7354                 }
7355                 if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7356                     &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7357                     handle) != 0)
7358                         continue;
7359                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7360                     MPI2_IOCSTATUS_MASK;
7361                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7362                         pr_info(MPT3SAS_FMT "\tbreak from phys disk scan " \
7363                             "ioc_status(0x%04x), loginfo(0x%08x)\n",
7364                             ioc->name, ioc_status,
7365                             le32_to_cpu(mpi_reply.IOCLogInfo));
7366                         break;
7367                 }
7368                 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7369                 if (!_scsih_get_sas_address(ioc, parent_handle,
7370                     &sas_address)) {
7371                         pr_info(MPT3SAS_FMT "\tBEFORE adding phys disk: " \
7372                             " handle (0x%04x), sas_addr(0x%016llx)\n",
7373                             ioc->name, handle, (unsigned long long)
7374                             le64_to_cpu(sas_device_pg0.SASAddress));
7375                         mpt3sas_transport_update_links(ioc, sas_address,
7376                             handle, sas_device_pg0.PhyNum,
7377                             MPI2_SAS_NEG_LINK_RATE_1_5);
7378                         set_bit(handle, ioc->pd_handles);
7379                         retry_count = 0;
7380                         /* This will retry adding the end device.
7381                          * _scsih_add_device() will decide on retries and
7382                          * return "1" when it should be retried
7383                          */
7384                         while (_scsih_add_device(ioc, handle, retry_count++,
7385                             1)) {
7386                                 ssleep(1);
7387                         }
7388                         pr_info(MPT3SAS_FMT "\tAFTER adding phys disk: " \
7389                             " handle (0x%04x), sas_addr(0x%016llx)\n",
7390                             ioc->name, handle, (unsigned long long)
7391                             le64_to_cpu(sas_device_pg0.SASAddress));
7392                 }
7393         }
7394
7395         pr_info(MPT3SAS_FMT "\tscan devices: phys disk complete\n",
7396             ioc->name);
7397
7398         pr_info(MPT3SAS_FMT "\tscan devices: volumes start\n", ioc->name);
7399
7400         /* volumes */
7401         handle = 0xFFFF;
7402         while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7403             &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7404                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7405                     MPI2_IOCSTATUS_MASK;
7406                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7407                         pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7408                             "ioc_status(0x%04x), loginfo(0x%08x)\n",
7409                             ioc->name, ioc_status,
7410                             le32_to_cpu(mpi_reply.IOCLogInfo));
7411                         break;
7412                 }
7413                 handle = le16_to_cpu(volume_pg1.DevHandle);
7414                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7415                 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7416                     le64_to_cpu(volume_pg1.WWID));
7417                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7418                 if (raid_device)
7419                         continue;
7420                 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7421                     &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7422                      sizeof(Mpi2RaidVolPage0_t)))
7423                         continue;
7424                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7425                     MPI2_IOCSTATUS_MASK;
7426                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7427                         pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7428                             "ioc_status(0x%04x), loginfo(0x%08x)\n",
7429                             ioc->name, ioc_status,
7430                             le32_to_cpu(mpi_reply.IOCLogInfo));
7431                         break;
7432                 }
7433                 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7434                     volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7435                     volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7436                         memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7437                         element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7438                         element.VolDevHandle = volume_pg1.DevHandle;
7439                         pr_info(MPT3SAS_FMT
7440                                 "\tBEFORE adding volume: handle (0x%04x)\n",
7441                                 ioc->name, volume_pg1.DevHandle);
7442                         _scsih_sas_volume_add(ioc, &element);
7443                         pr_info(MPT3SAS_FMT
7444                                 "\tAFTER adding volume: handle (0x%04x)\n",
7445                                 ioc->name, volume_pg1.DevHandle);
7446                 }
7447         }
7448
7449         pr_info(MPT3SAS_FMT "\tscan devices: volumes complete\n",
7450             ioc->name);
7451
7452  skip_to_sas:
7453
7454         pr_info(MPT3SAS_FMT "\tscan devices: end devices start\n",
7455             ioc->name);
7456
7457         /* sas devices */
7458         handle = 0xFFFF;
7459         while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7460             &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7461             handle))) {
7462                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7463                     MPI2_IOCSTATUS_MASK;
7464                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7465                         pr_info(MPT3SAS_FMT "\tbreak from end device scan:"\
7466                             " ioc_status(0x%04x), loginfo(0x%08x)\n",
7467                             ioc->name, ioc_status,
7468                             le32_to_cpu(mpi_reply.IOCLogInfo));
7469                         break;
7470                 }
7471                 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7472                 if (!(_scsih_is_end_device(
7473                     le32_to_cpu(sas_device_pg0.DeviceInfo))))
7474                         continue;
7475                 sas_device = mpt3sas_get_sdev_by_addr(ioc,
7476                     le64_to_cpu(sas_device_pg0.SASAddress));
7477                 if (sas_device) {
7478                         sas_device_put(sas_device);
7479                         continue;
7480                 }
7481                 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7482                 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7483                         pr_info(MPT3SAS_FMT "\tBEFORE adding end device: " \
7484                             "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7485                             handle, (unsigned long long)
7486                             le64_to_cpu(sas_device_pg0.SASAddress));
7487                         mpt3sas_transport_update_links(ioc, sas_address, handle,
7488                             sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7489                         retry_count = 0;
7490                         /* This will retry adding the end device.
7491                          * _scsih_add_device() will decide on retries and
7492                          * return "1" when it should be retried
7493                          */
7494                         while (_scsih_add_device(ioc, handle, retry_count++,
7495                             0)) {
7496                                 ssleep(1);
7497                         }
7498                         pr_info(MPT3SAS_FMT "\tAFTER adding end device: " \
7499                             "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7500                             handle, (unsigned long long)
7501                             le64_to_cpu(sas_device_pg0.SASAddress));
7502                 }
7503         }
7504         pr_info(MPT3SAS_FMT "\tscan devices: end devices complete\n",
7505             ioc->name);
7506
7507         pr_info(MPT3SAS_FMT "scan devices: complete\n", ioc->name);
7508 }
7509 /**
7510  * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
7511  * @ioc: per adapter object
7512  * @reset_phase: phase
7513  *
7514  * The handler for doing any required cleanup or initialization.
7515  *
7516  * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
7517  * MPT3_IOC_DONE_RESET
7518  *
7519  * Return nothing.
7520  */
7521 void
7522 mpt3sas_scsih_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
7523 {
7524         switch (reset_phase) {
7525         case MPT3_IOC_PRE_RESET:
7526                 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7527                         "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
7528                 break;
7529         case MPT3_IOC_AFTER_RESET:
7530                 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7531                         "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
7532                 if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
7533                         ioc->scsih_cmds.status |= MPT3_CMD_RESET;
7534                         mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7535                         complete(&ioc->scsih_cmds.done);
7536                 }
7537                 if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
7538                         ioc->tm_cmds.status |= MPT3_CMD_RESET;
7539                         mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7540                         complete(&ioc->tm_cmds.done);
7541                 }
7542
7543                 _scsih_fw_event_cleanup_queue(ioc);
7544                 _scsih_flush_running_cmds(ioc);
7545                 break;
7546         case MPT3_IOC_DONE_RESET:
7547                 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7548                         "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
7549                 if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7550                     !ioc->sas_hba.num_phys)) {
7551                         _scsih_prep_device_scan(ioc);
7552                         _scsih_search_responding_sas_devices(ioc);
7553                         _scsih_search_responding_raid_devices(ioc);
7554                         _scsih_search_responding_expanders(ioc);
7555                         _scsih_error_recovery_delete_devices(ioc);
7556                 }
7557                 break;
7558         }
7559 }
7560
7561 /**
7562  * _mpt3sas_fw_work - delayed task for processing firmware events
7563  * @ioc: per adapter object
7564  * @fw_event: The fw_event_work object
7565  * Context: user.
7566  *
7567  * Return nothing.
7568  */
7569 static void
7570 _mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
7571 {
7572         _scsih_fw_event_del_from_list(ioc, fw_event);
7573
7574         /* the queue is being flushed so ignore this event */
7575         if (ioc->remove_host || ioc->pci_error_recovery) {
7576                 fw_event_work_put(fw_event);
7577                 return;
7578         }
7579
7580         switch (fw_event->event) {
7581         case MPT3SAS_PROCESS_TRIGGER_DIAG:
7582                 mpt3sas_process_trigger_data(ioc,
7583                         (struct SL_WH_TRIGGERS_EVENT_DATA_T *)
7584                         fw_event->event_data);
7585                 break;
7586         case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
7587                 while (scsi_host_in_recovery(ioc->shost) ||
7588                                          ioc->shost_recovery) {
7589                         /*
7590                          * If we're unloading, bail. Otherwise, this can become
7591                          * an infinite loop.
7592                          */
7593                         if (ioc->remove_host)
7594                                 goto out;
7595                         ssleep(1);
7596                 }
7597                 _scsih_remove_unresponding_sas_devices(ioc);
7598                 _scsih_scan_for_devices_after_reset(ioc);
7599                 break;
7600         case MPT3SAS_PORT_ENABLE_COMPLETE:
7601                 ioc->start_scan = 0;
7602         if (missing_delay[0] != -1 && missing_delay[1] != -1)
7603                         mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
7604                             missing_delay[1]);
7605                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
7606                         "port enable: complete from worker thread\n",
7607                         ioc->name));
7608                 break;
7609         case MPT3SAS_TURN_ON_PFA_LED:
7610                 _scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
7611                 break;
7612         case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7613                 _scsih_sas_topology_change_event(ioc, fw_event);
7614                 break;
7615         case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7616                 _scsih_sas_device_status_change_event(ioc, fw_event);
7617                 break;
7618         case MPI2_EVENT_SAS_DISCOVERY:
7619                 _scsih_sas_discovery_event(ioc, fw_event);
7620                 break;
7621         case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7622                 _scsih_sas_broadcast_primitive_event(ioc, fw_event);
7623                 break;
7624         case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7625                 _scsih_sas_enclosure_dev_status_change_event(ioc,
7626                     fw_event);
7627                 break;
7628         case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7629                 _scsih_sas_ir_config_change_event(ioc, fw_event);
7630                 break;
7631         case MPI2_EVENT_IR_VOLUME:
7632                 _scsih_sas_ir_volume_event(ioc, fw_event);
7633                 break;
7634         case MPI2_EVENT_IR_PHYSICAL_DISK:
7635                 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
7636                 break;
7637         case MPI2_EVENT_IR_OPERATION_STATUS:
7638                 _scsih_sas_ir_operation_status_event(ioc, fw_event);
7639                 break;
7640         }
7641 out:
7642         fw_event_work_put(fw_event);
7643 }
7644
7645 /**
7646  * _firmware_event_work
7647  * @ioc: per adapter object
7648  * @work: The fw_event_work object
7649  * Context: user.
7650  *
7651  * wrappers for the work thread handling firmware events
7652  *
7653  * Return nothing.
7654  */
7655
7656 static void
7657 _firmware_event_work(struct work_struct *work)
7658 {
7659         struct fw_event_work *fw_event = container_of(work,
7660             struct fw_event_work, work);
7661
7662         _mpt3sas_fw_work(fw_event->ioc, fw_event);
7663 }
7664
7665 /**
7666  * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
7667  * @ioc: per adapter object
7668  * @msix_index: MSIX table index supplied by the OS
7669  * @reply: reply message frame(lower 32bit addr)
7670  * Context: interrupt.
7671  *
7672  * This function merely adds a new work task into ioc->firmware_event_thread.
7673  * The tasks are worked from _firmware_event_work in user context.
7674  *
7675  * Return 1 meaning mf should be freed from _base_interrupt
7676  *        0 means the mf is freed from this function.
7677  */
7678 u8
7679 mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
7680         u32 reply)
7681 {
7682         struct fw_event_work *fw_event;
7683         Mpi2EventNotificationReply_t *mpi_reply;
7684         u16 event;
7685         u16 sz;
7686
7687         /* events turned off due to host reset or driver unloading */
7688         if (ioc->remove_host || ioc->pci_error_recovery)
7689                 return 1;
7690
7691         mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
7692
7693         if (unlikely(!mpi_reply)) {
7694                 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7695                     ioc->name, __FILE__, __LINE__, __func__);
7696                 return 1;
7697         }
7698
7699         event = le16_to_cpu(mpi_reply->Event);
7700
7701         if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
7702                 mpt3sas_trigger_event(ioc, event, 0);
7703
7704         switch (event) {
7705         /* handle these */
7706         case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7707         {
7708                 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7709                     (Mpi2EventDataSasBroadcastPrimitive_t *)
7710                     mpi_reply->EventData;
7711
7712                 if (baen_data->Primitive !=
7713                     MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
7714                         return 1;
7715
7716                 if (ioc->broadcast_aen_busy) {
7717                         ioc->broadcast_aen_pending++;
7718                         return 1;
7719                 } else
7720                         ioc->broadcast_aen_busy = 1;
7721                 break;
7722         }
7723
7724         case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7725                 _scsih_check_topo_delete_events(ioc,
7726                     (Mpi2EventDataSasTopologyChangeList_t *)
7727                     mpi_reply->EventData);
7728                 break;
7729         case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7730                 _scsih_check_ir_config_unhide_events(ioc,
7731                     (Mpi2EventDataIrConfigChangeList_t *)
7732                     mpi_reply->EventData);
7733                 break;
7734         case MPI2_EVENT_IR_VOLUME:
7735                 _scsih_check_volume_delete_events(ioc,
7736                     (Mpi2EventDataIrVolume_t *)
7737                     mpi_reply->EventData);
7738                 break;
7739         case MPI2_EVENT_LOG_ENTRY_ADDED:
7740         {
7741                 Mpi2EventDataLogEntryAdded_t *log_entry;
7742                 u32 *log_code;
7743
7744                 if (!ioc->is_warpdrive)
7745                         break;
7746
7747                 log_entry = (Mpi2EventDataLogEntryAdded_t *)
7748                     mpi_reply->EventData;
7749                 log_code = (u32 *)log_entry->LogData;
7750
7751                 if (le16_to_cpu(log_entry->LogEntryQualifier)
7752                     != MPT2_WARPDRIVE_LOGENTRY)
7753                         break;
7754
7755                 switch (le32_to_cpu(*log_code)) {
7756                 case MPT2_WARPDRIVE_LC_SSDT:
7757                         pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
7758                             "IO Throttling has occurred in the WarpDrive "
7759                             "subsystem. Check WarpDrive documentation for "
7760                             "additional details.\n", ioc->name);
7761                         break;
7762                 case MPT2_WARPDRIVE_LC_SSDLW:
7763                         pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
7764                             "Program/Erase Cycles for the WarpDrive subsystem "
7765                             "in degraded range. Check WarpDrive documentation "
7766                             "for additional details.\n", ioc->name);
7767                         break;
7768                 case MPT2_WARPDRIVE_LC_SSDLF:
7769                         pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
7770                             "There are no Program/Erase Cycles for the "
7771                             "WarpDrive subsystem. The storage device will be "
7772                             "in read-only mode. Check WarpDrive documentation "
7773                             "for additional details.\n", ioc->name);
7774                         break;
7775                 case MPT2_WARPDRIVE_LC_BRMF:
7776                         pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
7777                             "The Backup Rail Monitor has failed on the "
7778                             "WarpDrive subsystem. Check WarpDrive "
7779                             "documentation for additional details.\n",
7780                             ioc->name);
7781                         break;
7782                 }
7783
7784                 break;
7785         }
7786         case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7787         case MPI2_EVENT_IR_OPERATION_STATUS:
7788         case MPI2_EVENT_SAS_DISCOVERY:
7789         case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7790         case MPI2_EVENT_IR_PHYSICAL_DISK:
7791                 break;
7792
7793         case MPI2_EVENT_TEMP_THRESHOLD:
7794                 _scsih_temp_threshold_events(ioc,
7795                         (Mpi2EventDataTemperature_t *)
7796                         mpi_reply->EventData);
7797                 break;
7798
7799         default: /* ignore the rest */
7800                 return 1;
7801         }
7802
7803         sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7804         fw_event = alloc_fw_event_work(sz);
7805         if (!fw_event) {
7806                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7807                     ioc->name, __FILE__, __LINE__, __func__);
7808                 return 1;
7809         }
7810
7811         memcpy(fw_event->event_data, mpi_reply->EventData, sz);
7812         fw_event->ioc = ioc;
7813         fw_event->VF_ID = mpi_reply->VF_ID;
7814         fw_event->VP_ID = mpi_reply->VP_ID;
7815         fw_event->event = event;
7816         _scsih_fw_event_add(ioc, fw_event);
7817         fw_event_work_put(fw_event);
7818         return 1;
7819 }
7820
7821 /**
7822  * _scsih_expander_node_remove - removing expander device from list.
7823  * @ioc: per adapter object
7824  * @sas_expander: the sas_device object
7825  * Context: Calling function should acquire ioc->sas_node_lock.
7826  *
7827  * Removing object and freeing associated memory from the
7828  * ioc->sas_expander_list.
7829  *
7830  * Return nothing.
7831  */
7832 static void
7833 _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
7834         struct _sas_node *sas_expander)
7835 {
7836         struct _sas_port *mpt3sas_port, *next;
7837
7838         /* remove sibling ports attached to this expander */
7839         list_for_each_entry_safe(mpt3sas_port, next,
7840            &sas_expander->sas_port_list, port_list) {
7841                 if (ioc->shost_recovery)
7842                         return;
7843                 if (mpt3sas_port->remote_identify.device_type ==
7844                     SAS_END_DEVICE)
7845                         mpt3sas_device_remove_by_sas_address(ioc,
7846                             mpt3sas_port->remote_identify.sas_address);
7847                 else if (mpt3sas_port->remote_identify.device_type ==
7848                     SAS_EDGE_EXPANDER_DEVICE ||
7849                     mpt3sas_port->remote_identify.device_type ==
7850                     SAS_FANOUT_EXPANDER_DEVICE)
7851                         mpt3sas_expander_remove(ioc,
7852                             mpt3sas_port->remote_identify.sas_address);
7853         }
7854
7855         mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
7856             sas_expander->sas_address_parent);
7857
7858         pr_info(MPT3SAS_FMT
7859                 "expander_remove: handle(0x%04x), sas_addr(0x%016llx)\n",
7860                 ioc->name,
7861             sas_expander->handle, (unsigned long long)
7862             sas_expander->sas_address);
7863
7864         kfree(sas_expander->phy);
7865         kfree(sas_expander);
7866 }
7867
7868 /**
7869  * _scsih_ir_shutdown - IR shutdown notification
7870  * @ioc: per adapter object
7871  *
7872  * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7873  * the host system is shutting down.
7874  *
7875  * Return nothing.
7876  */
7877 static void
7878 _scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
7879 {
7880         Mpi2RaidActionRequest_t *mpi_request;
7881         Mpi2RaidActionReply_t *mpi_reply;
7882         u16 smid;
7883
7884         /* is IR firmware build loaded ? */
7885         if (!ioc->ir_firmware)
7886                 return;
7887
7888         /* are there any volumes ? */
7889         if (list_empty(&ioc->raid_device_list))
7890                 return;
7891
7892         mutex_lock(&ioc->scsih_cmds.mutex);
7893
7894         if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
7895                 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
7896                     ioc->name, __func__);
7897                 goto out;
7898         }
7899         ioc->scsih_cmds.status = MPT3_CMD_PENDING;
7900
7901         smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7902         if (!smid) {
7903                 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
7904                     ioc->name, __func__);
7905                 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
7906                 goto out;
7907         }
7908
7909         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
7910         ioc->scsih_cmds.smid = smid;
7911         memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7912
7913         mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7914         mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7915
7916         if (!ioc->hide_ir_msg)
7917                 pr_info(MPT3SAS_FMT "IR shutdown (sending)\n", ioc->name);
7918         init_completion(&ioc->scsih_cmds.done);
7919         mpt3sas_base_put_smid_default(ioc, smid);
7920         wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7921
7922         if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
7923                 pr_err(MPT3SAS_FMT "%s: timeout\n",
7924                     ioc->name, __func__);
7925                 goto out;
7926         }
7927
7928         if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
7929                 mpi_reply = ioc->scsih_cmds.reply;
7930                 if (!ioc->hide_ir_msg)
7931                         pr_info(MPT3SAS_FMT "IR shutdown "
7932                            "(complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
7933                             ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7934                             le32_to_cpu(mpi_reply->IOCLogInfo));
7935         }
7936
7937  out:
7938         ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
7939         mutex_unlock(&ioc->scsih_cmds.mutex);
7940 }
7941
7942 /**
7943  * scsih_remove - detach and remove add host
7944  * @pdev: PCI device struct
7945  *
7946  * Routine called when unloading the driver.
7947  * Return nothing.
7948  */
7949 void scsih_remove(struct pci_dev *pdev)
7950 {
7951         struct Scsi_Host *shost = pci_get_drvdata(pdev);
7952         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
7953         struct _sas_port *mpt3sas_port, *next_port;
7954         struct _raid_device *raid_device, *next;
7955         struct MPT3SAS_TARGET *sas_target_priv_data;
7956         struct workqueue_struct *wq;
7957         unsigned long flags;
7958
7959         ioc->remove_host = 1;
7960         _scsih_fw_event_cleanup_queue(ioc);
7961
7962         spin_lock_irqsave(&ioc->fw_event_lock, flags);
7963         wq = ioc->firmware_event_thread;
7964         ioc->firmware_event_thread = NULL;
7965         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7966         if (wq)
7967                 destroy_workqueue(wq);
7968
7969         /* release all the volumes */
7970         _scsih_ir_shutdown(ioc);
7971         list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7972             list) {
7973                 if (raid_device->starget) {
7974                         sas_target_priv_data =
7975                             raid_device->starget->hostdata;
7976                         sas_target_priv_data->deleted = 1;
7977                         scsi_remove_target(&raid_device->starget->dev);
7978                 }
7979                 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
7980                         ioc->name,  raid_device->handle,
7981                     (unsigned long long) raid_device->wwid);
7982                 _scsih_raid_device_remove(ioc, raid_device);
7983         }
7984
7985         /* free ports attached to the sas_host */
7986         list_for_each_entry_safe(mpt3sas_port, next_port,
7987            &ioc->sas_hba.sas_port_list, port_list) {
7988                 if (mpt3sas_port->remote_identify.device_type ==
7989                     SAS_END_DEVICE)
7990                         mpt3sas_device_remove_by_sas_address(ioc,
7991                             mpt3sas_port->remote_identify.sas_address);
7992                 else if (mpt3sas_port->remote_identify.device_type ==
7993                     SAS_EDGE_EXPANDER_DEVICE ||
7994                     mpt3sas_port->remote_identify.device_type ==
7995                     SAS_FANOUT_EXPANDER_DEVICE)
7996                         mpt3sas_expander_remove(ioc,
7997                             mpt3sas_port->remote_identify.sas_address);
7998         }
7999
8000         /* free phys attached to the sas_host */
8001         if (ioc->sas_hba.num_phys) {
8002                 kfree(ioc->sas_hba.phy);
8003                 ioc->sas_hba.phy = NULL;
8004                 ioc->sas_hba.num_phys = 0;
8005         }
8006
8007         sas_remove_host(shost);
8008         scsi_remove_host(shost);
8009         mpt3sas_base_detach(ioc);
8010         spin_lock(&gioc_lock);
8011         list_del(&ioc->list);
8012         spin_unlock(&gioc_lock);
8013         scsi_host_put(shost);
8014 }
8015
8016 /**
8017  * scsih_shutdown - routine call during system shutdown
8018  * @pdev: PCI device struct
8019  *
8020  * Return nothing.
8021  */
8022 void
8023 scsih_shutdown(struct pci_dev *pdev)
8024 {
8025         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8026         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8027         struct workqueue_struct *wq;
8028         unsigned long flags;
8029
8030         ioc->remove_host = 1;
8031         _scsih_fw_event_cleanup_queue(ioc);
8032
8033         spin_lock_irqsave(&ioc->fw_event_lock, flags);
8034         wq = ioc->firmware_event_thread;
8035         ioc->firmware_event_thread = NULL;
8036         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8037         if (wq)
8038                 destroy_workqueue(wq);
8039
8040         _scsih_ir_shutdown(ioc);
8041         mpt3sas_base_detach(ioc);
8042 }
8043
8044
8045 /**
8046  * _scsih_probe_boot_devices - reports 1st device
8047  * @ioc: per adapter object
8048  *
8049  * If specified in bios page 2, this routine reports the 1st
8050  * device scsi-ml or sas transport for persistent boot device
8051  * purposes.  Please refer to function _scsih_determine_boot_device()
8052  */
8053 static void
8054 _scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
8055 {
8056         u8 is_raid;
8057         void *device;
8058         struct _sas_device *sas_device;
8059         struct _raid_device *raid_device;
8060         u16 handle;
8061         u64 sas_address_parent;
8062         u64 sas_address;
8063         unsigned long flags;
8064         int rc;
8065
8066          /* no Bios, return immediately */
8067         if (!ioc->bios_pg3.BiosVersion)
8068                 return;
8069
8070         device = NULL;
8071         is_raid = 0;
8072         if (ioc->req_boot_device.device) {
8073                 device =  ioc->req_boot_device.device;
8074                 is_raid = ioc->req_boot_device.is_raid;
8075         } else if (ioc->req_alt_boot_device.device) {
8076                 device =  ioc->req_alt_boot_device.device;
8077                 is_raid = ioc->req_alt_boot_device.is_raid;
8078         } else if (ioc->current_boot_device.device) {
8079                 device =  ioc->current_boot_device.device;
8080                 is_raid = ioc->current_boot_device.is_raid;
8081         }
8082
8083         if (!device)
8084                 return;
8085
8086         if (is_raid) {
8087                 raid_device = device;
8088                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8089                     raid_device->id, 0);
8090                 if (rc)
8091                         _scsih_raid_device_remove(ioc, raid_device);
8092         } else {
8093                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8094                 sas_device = device;
8095                 handle = sas_device->handle;
8096                 sas_address_parent = sas_device->sas_address_parent;
8097                 sas_address = sas_device->sas_address;
8098                 list_move_tail(&sas_device->list, &ioc->sas_device_list);
8099                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8100
8101                 if (ioc->hide_drives)
8102                         return;
8103                 if (!mpt3sas_transport_port_add(ioc, handle,
8104                     sas_address_parent)) {
8105                         _scsih_sas_device_remove(ioc, sas_device);
8106                 } else if (!sas_device->starget) {
8107                         if (!ioc->is_driver_loading) {
8108                                 mpt3sas_transport_port_remove(ioc,
8109                                     sas_address,
8110                                     sas_address_parent);
8111                                 _scsih_sas_device_remove(ioc, sas_device);
8112                         }
8113                 }
8114         }
8115 }
8116
8117 /**
8118  * _scsih_probe_raid - reporting raid volumes to scsi-ml
8119  * @ioc: per adapter object
8120  *
8121  * Called during initial loading of the driver.
8122  */
8123 static void
8124 _scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
8125 {
8126         struct _raid_device *raid_device, *raid_next;
8127         int rc;
8128
8129         list_for_each_entry_safe(raid_device, raid_next,
8130             &ioc->raid_device_list, list) {
8131                 if (raid_device->starget)
8132                         continue;
8133                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8134                     raid_device->id, 0);
8135                 if (rc)
8136                         _scsih_raid_device_remove(ioc, raid_device);
8137         }
8138 }
8139
8140 static struct _sas_device *get_next_sas_device(struct MPT3SAS_ADAPTER *ioc)
8141 {
8142         struct _sas_device *sas_device = NULL;
8143         unsigned long flags;
8144
8145         spin_lock_irqsave(&ioc->sas_device_lock, flags);
8146         if (!list_empty(&ioc->sas_device_init_list)) {
8147                 sas_device = list_first_entry(&ioc->sas_device_init_list,
8148                                 struct _sas_device, list);
8149                 sas_device_get(sas_device);
8150         }
8151         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8152
8153         return sas_device;
8154 }
8155
8156 static void sas_device_make_active(struct MPT3SAS_ADAPTER *ioc,
8157                 struct _sas_device *sas_device)
8158 {
8159         unsigned long flags;
8160
8161         spin_lock_irqsave(&ioc->sas_device_lock, flags);
8162
8163         /*
8164          * Since we dropped the lock during the call to port_add(), we need to
8165          * be careful here that somebody else didn't move or delete this item
8166          * while we were busy with other things.
8167          *
8168          * If it was on the list, we need a put() for the reference the list
8169          * had. Either way, we need a get() for the destination list.
8170          */
8171         if (!list_empty(&sas_device->list)) {
8172                 list_del_init(&sas_device->list);
8173                 sas_device_put(sas_device);
8174         }
8175
8176         sas_device_get(sas_device);
8177         list_add_tail(&sas_device->list, &ioc->sas_device_list);
8178
8179         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8180 }
8181
8182 /**
8183  * _scsih_probe_sas - reporting sas devices to sas transport
8184  * @ioc: per adapter object
8185  *
8186  * Called during initial loading of the driver.
8187  */
8188 static void
8189 _scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
8190 {
8191         struct _sas_device *sas_device;
8192
8193         if (ioc->hide_drives)
8194                 return;
8195
8196         while ((sas_device = get_next_sas_device(ioc))) {
8197                 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
8198                     sas_device->sas_address_parent)) {
8199                         _scsih_sas_device_remove(ioc, sas_device);
8200                         sas_device_put(sas_device);
8201                         continue;
8202                 } else if (!sas_device->starget) {
8203                         /*
8204                          * When asyn scanning is enabled, its not possible to
8205                          * remove devices while scanning is turned on due to an
8206                          * oops in scsi_sysfs_add_sdev()->add_device()->
8207                          * sysfs_addrm_start()
8208                          */
8209                         if (!ioc->is_driver_loading) {
8210                                 mpt3sas_transport_port_remove(ioc,
8211                                     sas_device->sas_address,
8212                                     sas_device->sas_address_parent);
8213                                 _scsih_sas_device_remove(ioc, sas_device);
8214                                 sas_device_put(sas_device);
8215                                 continue;
8216                         }
8217                 }
8218                 sas_device_make_active(ioc, sas_device);
8219                 sas_device_put(sas_device);
8220         }
8221 }
8222
8223 /**
8224  * _scsih_probe_devices - probing for devices
8225  * @ioc: per adapter object
8226  *
8227  * Called during initial loading of the driver.
8228  */
8229 static void
8230 _scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
8231 {
8232         u16 volume_mapping_flags;
8233
8234         if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
8235                 return;  /* return when IOC doesn't support initiator mode */
8236
8237         _scsih_probe_boot_devices(ioc);
8238
8239         if (ioc->ir_firmware) {
8240                 volume_mapping_flags =
8241                     le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
8242                     MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
8243                 if (volume_mapping_flags ==
8244                     MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
8245                         _scsih_probe_raid(ioc);
8246                         _scsih_probe_sas(ioc);
8247                 } else {
8248                         _scsih_probe_sas(ioc);
8249                         _scsih_probe_raid(ioc);
8250                 }
8251         } else
8252                 _scsih_probe_sas(ioc);
8253 }
8254
8255 /**
8256  * scsih_scan_start - scsi lld callback for .scan_start
8257  * @shost: SCSI host pointer
8258  *
8259  * The shost has the ability to discover targets on its own instead
8260  * of scanning the entire bus.  In our implemention, we will kick off
8261  * firmware discovery.
8262  */
8263 void
8264 scsih_scan_start(struct Scsi_Host *shost)
8265 {
8266         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8267         int rc;
8268         if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
8269                 mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
8270
8271         if (disable_discovery > 0)
8272                 return;
8273
8274         ioc->start_scan = 1;
8275         rc = mpt3sas_port_enable(ioc);
8276
8277         if (rc != 0)
8278                 pr_info(MPT3SAS_FMT "port enable: FAILED\n", ioc->name);
8279 }
8280
8281 /**
8282  * scsih_scan_finished - scsi lld callback for .scan_finished
8283  * @shost: SCSI host pointer
8284  * @time: elapsed time of the scan in jiffies
8285  *
8286  * This function will be called periodicallyn until it returns 1 with the
8287  * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
8288  * we wait for firmware discovery to complete, then return 1.
8289  */
8290 int
8291 scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
8292 {
8293         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8294
8295         if (disable_discovery > 0) {
8296                 ioc->is_driver_loading = 0;
8297                 ioc->wait_for_discovery_to_complete = 0;
8298                 return 1;
8299         }
8300
8301         if (time >= (300 * HZ)) {
8302                 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8303                 pr_info(MPT3SAS_FMT
8304                         "port enable: FAILED with timeout (timeout=300s)\n",
8305                         ioc->name);
8306                 ioc->is_driver_loading = 0;
8307                 return 1;
8308         }
8309
8310         if (ioc->start_scan)
8311                 return 0;
8312
8313         if (ioc->start_scan_failed) {
8314                 pr_info(MPT3SAS_FMT
8315                         "port enable: FAILED with (ioc_status=0x%08x)\n",
8316                         ioc->name, ioc->start_scan_failed);
8317                 ioc->is_driver_loading = 0;
8318                 ioc->wait_for_discovery_to_complete = 0;
8319                 ioc->remove_host = 1;
8320                 return 1;
8321         }
8322
8323         pr_info(MPT3SAS_FMT "port enable: SUCCESS\n", ioc->name);
8324         ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8325
8326         if (ioc->wait_for_discovery_to_complete) {
8327                 ioc->wait_for_discovery_to_complete = 0;
8328                 _scsih_probe_devices(ioc);
8329         }
8330         mpt3sas_base_start_watchdog(ioc);
8331         ioc->is_driver_loading = 0;
8332         return 1;
8333 }
8334
8335 /* shost template for SAS 2.0 HBA devices */
8336 static struct scsi_host_template mpt2sas_driver_template = {
8337         .module                         = THIS_MODULE,
8338         .name                           = "Fusion MPT SAS Host",
8339         .proc_name                      = MPT2SAS_DRIVER_NAME,
8340         .queuecommand                   = scsih_qcmd,
8341         .target_alloc                   = scsih_target_alloc,
8342         .slave_alloc                    = scsih_slave_alloc,
8343         .slave_configure                = scsih_slave_configure,
8344         .target_destroy                 = scsih_target_destroy,
8345         .slave_destroy                  = scsih_slave_destroy,
8346         .scan_finished                  = scsih_scan_finished,
8347         .scan_start                     = scsih_scan_start,
8348         .change_queue_depth             = scsih_change_queue_depth,
8349         .eh_abort_handler               = scsih_abort,
8350         .eh_device_reset_handler        = scsih_dev_reset,
8351         .eh_target_reset_handler        = scsih_target_reset,
8352         .eh_host_reset_handler          = scsih_host_reset,
8353         .bios_param                     = scsih_bios_param,
8354         .can_queue                      = 1,
8355         .this_id                        = -1,
8356         .sg_tablesize                   = MPT2SAS_SG_DEPTH,
8357         .max_sectors                    = 32767,
8358         .cmd_per_lun                    = 7,
8359         .use_clustering                 = ENABLE_CLUSTERING,
8360         .shost_attrs                    = mpt3sas_host_attrs,
8361         .sdev_attrs                     = mpt3sas_dev_attrs,
8362         .track_queue_depth              = 1,
8363 };
8364
8365 /* raid transport support for SAS 2.0 HBA devices */
8366 static struct raid_function_template mpt2sas_raid_functions = {
8367         .cookie         = &mpt2sas_driver_template,
8368         .is_raid        = scsih_is_raid,
8369         .get_resync     = scsih_get_resync,
8370         .get_state      = scsih_get_state,
8371 };
8372
8373 /* shost template for SAS 3.0 HBA devices */
8374 static struct scsi_host_template mpt3sas_driver_template = {
8375         .module                         = THIS_MODULE,
8376         .name                           = "Fusion MPT SAS Host",
8377         .proc_name                      = MPT3SAS_DRIVER_NAME,
8378         .queuecommand                   = scsih_qcmd,
8379         .target_alloc                   = scsih_target_alloc,
8380         .slave_alloc                    = scsih_slave_alloc,
8381         .slave_configure                = scsih_slave_configure,
8382         .target_destroy                 = scsih_target_destroy,
8383         .slave_destroy                  = scsih_slave_destroy,
8384         .scan_finished                  = scsih_scan_finished,
8385         .scan_start                     = scsih_scan_start,
8386         .change_queue_depth             = scsih_change_queue_depth,
8387         .eh_abort_handler               = scsih_abort,
8388         .eh_device_reset_handler        = scsih_dev_reset,
8389         .eh_target_reset_handler        = scsih_target_reset,
8390         .eh_host_reset_handler          = scsih_host_reset,
8391         .bios_param                     = scsih_bios_param,
8392         .can_queue                      = 1,
8393         .this_id                        = -1,
8394         .sg_tablesize                   = MPT3SAS_SG_DEPTH,
8395         .max_sectors                    = 32767,
8396         .cmd_per_lun                    = 7,
8397         .use_clustering                 = ENABLE_CLUSTERING,
8398         .shost_attrs                    = mpt3sas_host_attrs,
8399         .sdev_attrs                     = mpt3sas_dev_attrs,
8400         .track_queue_depth              = 1,
8401 };
8402
8403 /* raid transport support for SAS 3.0 HBA devices */
8404 static struct raid_function_template mpt3sas_raid_functions = {
8405         .cookie         = &mpt3sas_driver_template,
8406         .is_raid        = scsih_is_raid,
8407         .get_resync     = scsih_get_resync,
8408         .get_state      = scsih_get_state,
8409 };
8410
8411 /**
8412  * _scsih_determine_hba_mpi_version - determine in which MPI version class
8413  *                                      this device belongs to.
8414  * @pdev: PCI device struct
8415  *
8416  * return MPI2_VERSION for SAS 2.0 HBA devices,
8417  *      MPI25_VERSION for SAS 3.0 HBA devices.
8418  */
8419 u16
8420 _scsih_determine_hba_mpi_version(struct pci_dev *pdev)
8421 {
8422
8423         switch (pdev->device) {
8424         case MPI2_MFGPAGE_DEVID_SSS6200:
8425         case MPI2_MFGPAGE_DEVID_SAS2004:
8426         case MPI2_MFGPAGE_DEVID_SAS2008:
8427         case MPI2_MFGPAGE_DEVID_SAS2108_1:
8428         case MPI2_MFGPAGE_DEVID_SAS2108_2:
8429         case MPI2_MFGPAGE_DEVID_SAS2108_3:
8430         case MPI2_MFGPAGE_DEVID_SAS2116_1:
8431         case MPI2_MFGPAGE_DEVID_SAS2116_2:
8432         case MPI2_MFGPAGE_DEVID_SAS2208_1:
8433         case MPI2_MFGPAGE_DEVID_SAS2208_2:
8434         case MPI2_MFGPAGE_DEVID_SAS2208_3:
8435         case MPI2_MFGPAGE_DEVID_SAS2208_4:
8436         case MPI2_MFGPAGE_DEVID_SAS2208_5:
8437         case MPI2_MFGPAGE_DEVID_SAS2208_6:
8438         case MPI2_MFGPAGE_DEVID_SAS2308_1:
8439         case MPI2_MFGPAGE_DEVID_SAS2308_2:
8440         case MPI2_MFGPAGE_DEVID_SAS2308_3:
8441                 return MPI2_VERSION;
8442         case MPI25_MFGPAGE_DEVID_SAS3004:
8443         case MPI25_MFGPAGE_DEVID_SAS3008:
8444         case MPI25_MFGPAGE_DEVID_SAS3108_1:
8445         case MPI25_MFGPAGE_DEVID_SAS3108_2:
8446         case MPI25_MFGPAGE_DEVID_SAS3108_5:
8447         case MPI25_MFGPAGE_DEVID_SAS3108_6:
8448                 return MPI25_VERSION;
8449         }
8450         return 0;
8451 }
8452
8453 /**
8454  * _scsih_probe - attach and add scsi host
8455  * @pdev: PCI device struct
8456  * @id: pci device id
8457  *
8458  * Returns 0 success, anything else error.
8459  */
8460 int
8461 _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
8462 {
8463         struct MPT3SAS_ADAPTER *ioc;
8464         struct Scsi_Host *shost = NULL;
8465         int rv;
8466         u16 hba_mpi_version;
8467
8468         /* Determine in which MPI version class this pci device belongs */
8469         hba_mpi_version = _scsih_determine_hba_mpi_version(pdev);
8470         if (hba_mpi_version == 0)
8471                 return -ENODEV;
8472
8473         /* Enumerate only SAS 2.0 HBA's if hbas_to_enumerate is one,
8474          * for other generation HBA's return with -ENODEV
8475          */
8476         if ((hbas_to_enumerate == 1) && (hba_mpi_version !=  MPI2_VERSION))
8477                 return -ENODEV;
8478
8479         /* Enumerate only SAS 3.0 HBA's if hbas_to_enumerate is two,
8480          * for other generation HBA's return with -ENODEV
8481          */
8482         if ((hbas_to_enumerate == 2) && (hba_mpi_version !=  MPI25_VERSION))
8483                 return -ENODEV;
8484
8485         switch (hba_mpi_version) {
8486         case MPI2_VERSION:
8487                 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
8488                         PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
8489                 /* Use mpt2sas driver host template for SAS 2.0 HBA's */
8490                 shost = scsi_host_alloc(&mpt2sas_driver_template,
8491                   sizeof(struct MPT3SAS_ADAPTER));
8492                 if (!shost)
8493                         return -ENODEV;
8494                 ioc = shost_priv(shost);
8495                 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8496                 ioc->hba_mpi_version_belonged = hba_mpi_version;
8497                 ioc->id = mpt2_ids++;
8498                 sprintf(ioc->driver_name, "%s", MPT2SAS_DRIVER_NAME);
8499                 if (pdev->device == MPI2_MFGPAGE_DEVID_SSS6200) {
8500                         ioc->is_warpdrive = 1;
8501                         ioc->hide_ir_msg = 1;
8502                 } else
8503                         ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
8504                 break;
8505         case MPI25_VERSION:
8506                 /* Use mpt3sas driver host template for SAS 3.0 HBA's */
8507                 shost = scsi_host_alloc(&mpt3sas_driver_template,
8508                   sizeof(struct MPT3SAS_ADAPTER));
8509                 if (!shost)
8510                         return -ENODEV;
8511                 ioc = shost_priv(shost);
8512                 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8513                 ioc->hba_mpi_version_belonged = hba_mpi_version;
8514                 ioc->id = mpt3_ids++;
8515                 sprintf(ioc->driver_name, "%s", MPT3SAS_DRIVER_NAME);
8516                 if (pdev->revision >= SAS3_PCI_DEVICE_C0_REVISION)
8517                         ioc->msix96_vector = 1;
8518                 break;
8519         default:
8520                 return -ENODEV;
8521         }
8522
8523         INIT_LIST_HEAD(&ioc->list);
8524         spin_lock(&gioc_lock);
8525         list_add_tail(&ioc->list, &mpt3sas_ioc_list);
8526         spin_unlock(&gioc_lock);
8527         ioc->shost = shost;
8528         ioc->pdev = pdev;
8529         ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8530         ioc->tm_cb_idx = tm_cb_idx;
8531         ioc->ctl_cb_idx = ctl_cb_idx;
8532         ioc->base_cb_idx = base_cb_idx;
8533         ioc->port_enable_cb_idx = port_enable_cb_idx;
8534         ioc->transport_cb_idx = transport_cb_idx;
8535         ioc->scsih_cb_idx = scsih_cb_idx;
8536         ioc->config_cb_idx = config_cb_idx;
8537         ioc->tm_tr_cb_idx = tm_tr_cb_idx;
8538         ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
8539         ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
8540         ioc->logging_level = logging_level;
8541         ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
8542         /* misc semaphores and spin locks */
8543         mutex_init(&ioc->reset_in_progress_mutex);
8544         /* initializing pci_access_mutex lock */
8545         mutex_init(&ioc->pci_access_mutex);
8546         spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8547         spin_lock_init(&ioc->scsi_lookup_lock);
8548         spin_lock_init(&ioc->sas_device_lock);
8549         spin_lock_init(&ioc->sas_node_lock);
8550         spin_lock_init(&ioc->fw_event_lock);
8551         spin_lock_init(&ioc->raid_device_lock);
8552         spin_lock_init(&ioc->diag_trigger_lock);
8553
8554         INIT_LIST_HEAD(&ioc->sas_device_list);
8555         INIT_LIST_HEAD(&ioc->sas_device_init_list);
8556         INIT_LIST_HEAD(&ioc->sas_expander_list);
8557         INIT_LIST_HEAD(&ioc->fw_event_list);
8558         INIT_LIST_HEAD(&ioc->raid_device_list);
8559         INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
8560         INIT_LIST_HEAD(&ioc->delayed_tr_list);
8561         INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
8562         INIT_LIST_HEAD(&ioc->reply_queue_list);
8563
8564         sprintf(ioc->name, "%s_cm%d", ioc->driver_name, ioc->id);
8565
8566         /* init shost parameters */
8567         shost->max_cmd_len = 32;
8568         shost->max_lun = max_lun;
8569         shost->transportt = mpt3sas_transport_template;
8570         shost->unique_id = ioc->id;
8571
8572         if (max_sectors != 0xFFFF) {
8573                 if (max_sectors < 64) {
8574                         shost->max_sectors = 64;
8575                         pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8576                             "for max_sectors, range is 64 to 32767. Assigning "
8577                             "value of 64.\n", ioc->name, max_sectors);
8578                 } else if (max_sectors > 32767) {
8579                         shost->max_sectors = 32767;
8580                         pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8581                             "for max_sectors, range is 64 to 32767. Assigning "
8582                             "default value of 32767.\n", ioc->name,
8583                             max_sectors);
8584                 } else {
8585                         shost->max_sectors = max_sectors & 0xFFFE;
8586                         pr_info(MPT3SAS_FMT
8587                                 "The max_sectors value is set to %d\n",
8588                                 ioc->name, shost->max_sectors);
8589                 }
8590         }
8591
8592         /* register EEDP capabilities with SCSI layer */
8593         if (prot_mask > 0)
8594                 scsi_host_set_prot(shost, prot_mask);
8595         else
8596                 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8597                                    | SHOST_DIF_TYPE2_PROTECTION
8598                                    | SHOST_DIF_TYPE3_PROTECTION);
8599
8600         scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
8601
8602         /* event thread */
8603         snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
8604             "fw_event_%s%d", ioc->driver_name, ioc->id);
8605         ioc->firmware_event_thread = alloc_ordered_workqueue(
8606             ioc->firmware_event_name, WQ_MEM_RECLAIM);
8607         if (!ioc->firmware_event_thread) {
8608                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8609                     ioc->name, __FILE__, __LINE__, __func__);
8610                 rv = -ENODEV;
8611                 goto out_thread_fail;
8612         }
8613
8614         ioc->is_driver_loading = 1;
8615         if ((mpt3sas_base_attach(ioc))) {
8616                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8617                     ioc->name, __FILE__, __LINE__, __func__);
8618                 rv = -ENODEV;
8619                 goto out_attach_fail;
8620         }
8621
8622         if (ioc->is_warpdrive) {
8623                 if (ioc->mfg_pg10_hide_flag ==  MFG_PAGE10_EXPOSE_ALL_DISKS)
8624                         ioc->hide_drives = 0;
8625                 else if (ioc->mfg_pg10_hide_flag ==  MFG_PAGE10_HIDE_ALL_DISKS)
8626                         ioc->hide_drives = 1;
8627                 else {
8628                         if (mpt3sas_get_num_volumes(ioc))
8629                                 ioc->hide_drives = 1;
8630                         else
8631                                 ioc->hide_drives = 0;
8632                 }
8633         } else
8634                 ioc->hide_drives = 0;
8635
8636         rv = scsi_add_host(shost, &pdev->dev);
8637         if (rv) {
8638                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8639                     ioc->name, __FILE__, __LINE__, __func__);
8640                 goto out_add_shost_fail;
8641         }
8642
8643         scsi_scan_host(shost);
8644         return 0;
8645 out_add_shost_fail:
8646         mpt3sas_base_detach(ioc);
8647  out_attach_fail:
8648         destroy_workqueue(ioc->firmware_event_thread);
8649  out_thread_fail:
8650         spin_lock(&gioc_lock);
8651         list_del(&ioc->list);
8652         spin_unlock(&gioc_lock);
8653         scsi_host_put(shost);
8654         return rv;
8655 }
8656
8657 #ifdef CONFIG_PM
8658 /**
8659  * scsih_suspend - power management suspend main entry point
8660  * @pdev: PCI device struct
8661  * @state: PM state change to (usually PCI_D3)
8662  *
8663  * Returns 0 success, anything else error.
8664  */
8665 int
8666 scsih_suspend(struct pci_dev *pdev, pm_message_t state)
8667 {
8668         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8669         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8670         pci_power_t device_state;
8671
8672         mpt3sas_base_stop_watchdog(ioc);
8673         flush_scheduled_work();
8674         scsi_block_requests(shost);
8675         device_state = pci_choose_state(pdev, state);
8676         pr_info(MPT3SAS_FMT
8677                 "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
8678                 ioc->name, pdev, pci_name(pdev), device_state);
8679
8680         pci_save_state(pdev);
8681         mpt3sas_base_free_resources(ioc);
8682         pci_set_power_state(pdev, device_state);
8683         return 0;
8684 }
8685
8686 /**
8687  * scsih_resume - power management resume main entry point
8688  * @pdev: PCI device struct
8689  *
8690  * Returns 0 success, anything else error.
8691  */
8692 int
8693 scsih_resume(struct pci_dev *pdev)
8694 {
8695         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8696         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8697         pci_power_t device_state = pdev->current_state;
8698         int r;
8699
8700         pr_info(MPT3SAS_FMT
8701                 "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
8702                 ioc->name, pdev, pci_name(pdev), device_state);
8703
8704         pci_set_power_state(pdev, PCI_D0);
8705         pci_enable_wake(pdev, PCI_D0, 0);
8706         pci_restore_state(pdev);
8707         ioc->pdev = pdev;
8708         r = mpt3sas_base_map_resources(ioc);
8709         if (r)
8710                 return r;
8711
8712         mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8713         scsi_unblock_requests(shost);
8714         mpt3sas_base_start_watchdog(ioc);
8715         return 0;
8716 }
8717 #endif /* CONFIG_PM */
8718
8719 /**
8720  * scsih_pci_error_detected - Called when a PCI error is detected.
8721  * @pdev: PCI device struct
8722  * @state: PCI channel state
8723  *
8724  * Description: Called when a PCI error is detected.
8725  *
8726  * Return value:
8727  *      PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8728  */
8729 pci_ers_result_t
8730 scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8731 {
8732         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8733         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8734
8735         pr_info(MPT3SAS_FMT "PCI error: detected callback, state(%d)!!\n",
8736             ioc->name, state);
8737
8738         switch (state) {
8739         case pci_channel_io_normal:
8740                 return PCI_ERS_RESULT_CAN_RECOVER;
8741         case pci_channel_io_frozen:
8742                 /* Fatal error, prepare for slot reset */
8743                 ioc->pci_error_recovery = 1;
8744                 scsi_block_requests(ioc->shost);
8745                 mpt3sas_base_stop_watchdog(ioc);
8746                 mpt3sas_base_free_resources(ioc);
8747                 return PCI_ERS_RESULT_NEED_RESET;
8748         case pci_channel_io_perm_failure:
8749                 /* Permanent error, prepare for device removal */
8750                 ioc->pci_error_recovery = 1;
8751                 mpt3sas_base_stop_watchdog(ioc);
8752                 _scsih_flush_running_cmds(ioc);
8753                 return PCI_ERS_RESULT_DISCONNECT;
8754         }
8755         return PCI_ERS_RESULT_NEED_RESET;
8756 }
8757
8758 /**
8759  * scsih_pci_slot_reset - Called when PCI slot has been reset.
8760  * @pdev: PCI device struct
8761  *
8762  * Description: This routine is called by the pci error recovery
8763  * code after the PCI slot has been reset, just before we
8764  * should resume normal operations.
8765  */
8766 pci_ers_result_t
8767 scsih_pci_slot_reset(struct pci_dev *pdev)
8768 {
8769         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8770         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8771         int rc;
8772
8773         pr_info(MPT3SAS_FMT "PCI error: slot reset callback!!\n",
8774              ioc->name);
8775
8776         ioc->pci_error_recovery = 0;
8777         ioc->pdev = pdev;
8778         pci_restore_state(pdev);
8779         rc = mpt3sas_base_map_resources(ioc);
8780         if (rc)
8781                 return PCI_ERS_RESULT_DISCONNECT;
8782
8783         rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8784             FORCE_BIG_HAMMER);
8785
8786         pr_warn(MPT3SAS_FMT "hard reset: %s\n", ioc->name,
8787             (rc == 0) ? "success" : "failed");
8788
8789         if (!rc)
8790                 return PCI_ERS_RESULT_RECOVERED;
8791         else
8792                 return PCI_ERS_RESULT_DISCONNECT;
8793 }
8794
8795 /**
8796  * scsih_pci_resume() - resume normal ops after PCI reset
8797  * @pdev: pointer to PCI device
8798  *
8799  * Called when the error recovery driver tells us that its
8800  * OK to resume normal operation. Use completion to allow
8801  * halted scsi ops to resume.
8802  */
8803 void
8804 scsih_pci_resume(struct pci_dev *pdev)
8805 {
8806         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8807         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8808
8809         pr_info(MPT3SAS_FMT "PCI error: resume callback!!\n", ioc->name);
8810
8811         pci_cleanup_aer_uncorrect_error_status(pdev);
8812         mpt3sas_base_start_watchdog(ioc);
8813         scsi_unblock_requests(ioc->shost);
8814 }
8815
8816 /**
8817  * scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
8818  * @pdev: pointer to PCI device
8819  */
8820 pci_ers_result_t
8821 scsih_pci_mmio_enabled(struct pci_dev *pdev)
8822 {
8823         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8824         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8825
8826         pr_info(MPT3SAS_FMT "PCI error: mmio enabled callback!!\n",
8827             ioc->name);
8828
8829         /* TODO - dump whatever for debugging purposes */
8830
8831         /* Request a slot reset. */
8832         return PCI_ERS_RESULT_NEED_RESET;
8833 }
8834
8835 /*
8836  * The pci device ids are defined in mpi/mpi2_cnfg.h.
8837  */
8838 static const struct pci_device_id mpt3sas_pci_table[] = {
8839         /* Spitfire ~ 2004 */
8840         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
8841                 PCI_ANY_ID, PCI_ANY_ID },
8842         /* Falcon ~ 2008 */
8843         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
8844                 PCI_ANY_ID, PCI_ANY_ID },
8845         /* Liberator ~ 2108 */
8846         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
8847                 PCI_ANY_ID, PCI_ANY_ID },
8848         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
8849                 PCI_ANY_ID, PCI_ANY_ID },
8850         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
8851                 PCI_ANY_ID, PCI_ANY_ID },
8852         /* Meteor ~ 2116 */
8853         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
8854                 PCI_ANY_ID, PCI_ANY_ID },
8855         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
8856                 PCI_ANY_ID, PCI_ANY_ID },
8857         /* Thunderbolt ~ 2208 */
8858         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
8859                 PCI_ANY_ID, PCI_ANY_ID },
8860         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
8861                 PCI_ANY_ID, PCI_ANY_ID },
8862         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
8863                 PCI_ANY_ID, PCI_ANY_ID },
8864         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
8865                 PCI_ANY_ID, PCI_ANY_ID },
8866         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
8867                 PCI_ANY_ID, PCI_ANY_ID },
8868         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
8869                 PCI_ANY_ID, PCI_ANY_ID },
8870         /* Mustang ~ 2308 */
8871         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
8872                 PCI_ANY_ID, PCI_ANY_ID },
8873         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
8874                 PCI_ANY_ID, PCI_ANY_ID },
8875         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
8876                 PCI_ANY_ID, PCI_ANY_ID },
8877         /* SSS6200 */
8878         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
8879                 PCI_ANY_ID, PCI_ANY_ID },
8880         /* Fury ~ 3004 and 3008 */
8881         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
8882                 PCI_ANY_ID, PCI_ANY_ID },
8883         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
8884                 PCI_ANY_ID, PCI_ANY_ID },
8885         /* Invader ~ 3108 */
8886         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
8887                 PCI_ANY_ID, PCI_ANY_ID },
8888         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
8889                 PCI_ANY_ID, PCI_ANY_ID },
8890         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
8891                 PCI_ANY_ID, PCI_ANY_ID },
8892         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
8893                 PCI_ANY_ID, PCI_ANY_ID },
8894         {0}     /* Terminating entry */
8895 };
8896 MODULE_DEVICE_TABLE(pci, mpt3sas_pci_table);
8897
8898 static struct pci_error_handlers _mpt3sas_err_handler = {
8899         .error_detected = scsih_pci_error_detected,
8900         .mmio_enabled   = scsih_pci_mmio_enabled,
8901         .slot_reset     = scsih_pci_slot_reset,
8902         .resume         = scsih_pci_resume,
8903 };
8904
8905 static struct pci_driver mpt3sas_driver = {
8906         .name           = MPT3SAS_DRIVER_NAME,
8907         .id_table       = mpt3sas_pci_table,
8908         .probe          = _scsih_probe,
8909         .remove         = scsih_remove,
8910         .shutdown       = scsih_shutdown,
8911         .err_handler    = &_mpt3sas_err_handler,
8912 #ifdef CONFIG_PM
8913         .suspend        = scsih_suspend,
8914         .resume         = scsih_resume,
8915 #endif
8916 };
8917
8918 /**
8919  * scsih_init - main entry point for this driver.
8920  *
8921  * Returns 0 success, anything else error.
8922  */
8923 int
8924 scsih_init(void)
8925 {
8926         mpt2_ids = 0;
8927         mpt3_ids = 0;
8928
8929         mpt3sas_base_initialize_callback_handler();
8930
8931          /* queuecommand callback hander */
8932         scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);
8933
8934         /* task managment callback handler */
8935         tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);
8936
8937         /* base internal commands callback handler */
8938         base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
8939         port_enable_cb_idx = mpt3sas_base_register_callback_handler(
8940             mpt3sas_port_enable_done);
8941
8942         /* transport internal commands callback handler */
8943         transport_cb_idx = mpt3sas_base_register_callback_handler(
8944             mpt3sas_transport_done);
8945
8946         /* scsih internal commands callback handler */
8947         scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);
8948
8949         /* configuration page API internal commands callback handler */
8950         config_cb_idx = mpt3sas_base_register_callback_handler(
8951             mpt3sas_config_done);
8952
8953         /* ctl module callback handler */
8954         ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);
8955
8956         tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
8957             _scsih_tm_tr_complete);
8958
8959         tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
8960             _scsih_tm_volume_tr_complete);
8961
8962         tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
8963             _scsih_sas_control_complete);
8964
8965         return 0;
8966 }
8967
8968 /**
8969  * scsih_exit - exit point for this driver (when it is a module).
8970  *
8971  * Returns 0 success, anything else error.
8972  */
8973 void
8974 scsih_exit(void)
8975 {
8976
8977         mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
8978         mpt3sas_base_release_callback_handler(tm_cb_idx);
8979         mpt3sas_base_release_callback_handler(base_cb_idx);
8980         mpt3sas_base_release_callback_handler(port_enable_cb_idx);
8981         mpt3sas_base_release_callback_handler(transport_cb_idx);
8982         mpt3sas_base_release_callback_handler(scsih_cb_idx);
8983         mpt3sas_base_release_callback_handler(config_cb_idx);
8984         mpt3sas_base_release_callback_handler(ctl_cb_idx);
8985
8986         mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
8987         mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
8988         mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);
8989
8990 /* raid transport support */
8991         if (hbas_to_enumerate != 1)
8992                 raid_class_release(mpt3sas_raid_template);
8993         if (hbas_to_enumerate != 2)
8994                 raid_class_release(mpt2sas_raid_template);
8995         sas_release_transport(mpt3sas_transport_template);
8996 }
8997
8998 /**
8999  * _mpt3sas_init - main entry point for this driver.
9000  *
9001  * Returns 0 success, anything else error.
9002  */
9003 static int __init
9004 _mpt3sas_init(void)
9005 {
9006         int error;
9007
9008         pr_info("%s version %s loaded\n", MPT3SAS_DRIVER_NAME,
9009                                         MPT3SAS_DRIVER_VERSION);
9010
9011         mpt3sas_transport_template =
9012             sas_attach_transport(&mpt3sas_transport_functions);
9013         if (!mpt3sas_transport_template)
9014                 return -ENODEV;
9015
9016         /* No need attach mpt3sas raid functions template
9017          * if hbas_to_enumarate value is one.
9018          */
9019         if (hbas_to_enumerate != 1) {
9020                 mpt3sas_raid_template =
9021                                 raid_class_attach(&mpt3sas_raid_functions);
9022                 if (!mpt3sas_raid_template) {
9023                         sas_release_transport(mpt3sas_transport_template);
9024                         return -ENODEV;
9025                 }
9026         }
9027
9028         /* No need to attach mpt2sas raid functions template
9029          * if hbas_to_enumarate value is two
9030          */
9031         if (hbas_to_enumerate != 2) {
9032                 mpt2sas_raid_template =
9033                                 raid_class_attach(&mpt2sas_raid_functions);
9034                 if (!mpt2sas_raid_template) {
9035                         sas_release_transport(mpt3sas_transport_template);
9036                         return -ENODEV;
9037                 }
9038         }
9039
9040         error = scsih_init();
9041         if (error) {
9042                 scsih_exit();
9043                 return error;
9044         }
9045
9046         mpt3sas_ctl_init(hbas_to_enumerate);
9047
9048         error = pci_register_driver(&mpt3sas_driver);
9049         if (error)
9050                 scsih_exit();
9051
9052         return error;
9053 }
9054
9055 /**
9056  * _mpt3sas_exit - exit point for this driver (when it is a module).
9057  *
9058  */
9059 static void __exit
9060 _mpt3sas_exit(void)
9061 {
9062         pr_info("mpt3sas version %s unloading\n",
9063                                 MPT3SAS_DRIVER_VERSION);
9064
9065         pci_unregister_driver(&mpt3sas_driver);
9066
9067         mpt3sas_ctl_exit(hbas_to_enumerate);
9068
9069         scsih_exit();
9070 }
9071
9072 module_init(_mpt3sas_init);
9073 module_exit(_mpt3sas_exit);