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