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