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