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