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