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