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