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