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