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