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