OSDN Git Service

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