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