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