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