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