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