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