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