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