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