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