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