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