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