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