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