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