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