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