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