[SCSI] mpt2sas: T10 DIF Support
[linux-2.6] / 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-2008  LSI Corporation
6  *  (mailto:DL-MPTFusionLinux@lsi.com)
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * NO WARRANTY
19  * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20  * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21  * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23  * solely responsible for determining the appropriateness of using and
24  * distributing the Program and assumes all risks associated with its
25  * exercise of rights under this Agreement, including but not limited to
26  * the risks and costs of program errors, damage to or loss of data,
27  * programs or equipment, and unavailability or interruption of operations.
28
29  * DISCLAIMER OF LIABILITY
30  * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35  * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36  * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37
38  * You should have received a copy of the GNU General Public License
39  * along with this program; if not, write to the Free Software
40  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
41  * USA.
42  */
43
44 #include <linux/version.h>
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/init.h>
48 #include <linux/errno.h>
49 #include <linux/blkdev.h>
50 #include <linux/sched.h>
51 #include <linux/workqueue.h>
52 #include <linux/delay.h>
53 #include <linux/pci.h>
54 #include <linux/interrupt.h>
55
56 #include "mpt2sas_base.h"
57
58 MODULE_AUTHOR(MPT2SAS_AUTHOR);
59 MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
60 MODULE_LICENSE("GPL");
61 MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
62
63 #define RAID_CHANNEL 1
64
65 /* forward proto's */
66 static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
67     struct _sas_node *sas_expander);
68 static void _firmware_event_work(struct work_struct *work);
69
70 /* global parameters */
71 LIST_HEAD(mpt2sas_ioc_list);
72
73 /* local parameters */
74 static u8 scsi_io_cb_idx = -1;
75 static u8 tm_cb_idx = -1;
76 static u8 ctl_cb_idx = -1;
77 static u8 base_cb_idx = -1;
78 static u8 transport_cb_idx = -1;
79 static u8 config_cb_idx = -1;
80 static int mpt_ids;
81
82 /* command line options */
83 static u32 logging_level;
84 MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
85     "(default=0)");
86
87 /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
88 #define MPT2SAS_MAX_LUN (16895)
89 static int max_lun = MPT2SAS_MAX_LUN;
90 module_param(max_lun, int, 0);
91 MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
92
93 /**
94  * struct sense_info - common structure for obtaining sense keys
95  * @skey: sense key
96  * @asc: additional sense code
97  * @ascq: additional sense code qualifier
98  */
99 struct sense_info {
100         u8 skey;
101         u8 asc;
102         u8 ascq;
103 };
104
105
106 #define MPT2SAS_RESCAN_AFTER_HOST_RESET (0xFFFF)
107 /**
108  * struct fw_event_work - firmware event struct
109  * @list: link list framework
110  * @work: work object (ioc->fault_reset_work_q)
111  * @ioc: per adapter object
112  * @VF_ID: virtual function id
113  * @host_reset_handling: handling events during host reset
114  * @ignore: flag meaning this event has been marked to ignore
115  * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
116  * @event_data: reply event data payload follows
117  *
118  * This object stored on ioc->fw_event_list.
119  */
120 struct fw_event_work {
121         struct list_head        list;
122         struct work_struct      work;
123         struct MPT2SAS_ADAPTER *ioc;
124         u8                      VF_ID;
125         u8                      host_reset_handling;
126         u8                      ignore;
127         u16                     event;
128         void                    *event_data;
129 };
130
131 /**
132  * struct _scsi_io_transfer - scsi io transfer
133  * @handle: sas device handle (assigned by firmware)
134  * @is_raid: flag set for hidden raid components
135  * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
136  * @data_length: data transfer length
137  * @data_dma: dma pointer to data
138  * @sense: sense data
139  * @lun: lun number
140  * @cdb_length: cdb length
141  * @cdb: cdb contents
142  * @valid_reply: flag set for reply message
143  * @timeout: timeout for this command
144  * @sense_length: sense length
145  * @ioc_status: ioc status
146  * @scsi_state: scsi state
147  * @scsi_status: scsi staus
148  * @log_info: log information
149  * @transfer_length: data length transfer when there is a reply message
150  *
151  * Used for sending internal scsi commands to devices within this module.
152  * Refer to _scsi_send_scsi_io().
153  */
154 struct _scsi_io_transfer {
155         u16     handle;
156         u8      is_raid;
157         enum dma_data_direction dir;
158         u32     data_length;
159         dma_addr_t data_dma;
160         u8      sense[SCSI_SENSE_BUFFERSIZE];
161         u32     lun;
162         u8      cdb_length;
163         u8      cdb[32];
164         u8      timeout;
165         u8      valid_reply;
166   /* the following bits are only valid when 'valid_reply = 1' */
167         u32     sense_length;
168         u16     ioc_status;
169         u8      scsi_state;
170         u8      scsi_status;
171         u32     log_info;
172         u32     transfer_length;
173 };
174
175 /*
176  * The pci device ids are defined in mpi/mpi2_cnfg.h.
177  */
178 static struct pci_device_id scsih_pci_table[] = {
179         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
180                 PCI_ANY_ID, PCI_ANY_ID },
181         /* Falcon ~ 2008*/
182         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
183                 PCI_ANY_ID, PCI_ANY_ID },
184         /* Liberator ~ 2108 */
185         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
186                 PCI_ANY_ID, PCI_ANY_ID },
187         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
188                 PCI_ANY_ID, PCI_ANY_ID },
189         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
190                 PCI_ANY_ID, PCI_ANY_ID },
191         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
192                 PCI_ANY_ID, PCI_ANY_ID },
193         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
194                 PCI_ANY_ID, PCI_ANY_ID },
195         {0}     /* Terminating entry */
196 };
197 MODULE_DEVICE_TABLE(pci, scsih_pci_table);
198
199 /**
200  * scsih_set_debug_level - global setting of ioc->logging_level.
201  *
202  * Note: The logging levels are defined in mpt2sas_debug.h.
203  */
204 static int
205 scsih_set_debug_level(const char *val, struct kernel_param *kp)
206 {
207         int ret = param_set_int(val, kp);
208         struct MPT2SAS_ADAPTER *ioc;
209
210         if (ret)
211                 return ret;
212
213         printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
214         list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
215                 ioc->logging_level = logging_level;
216         return 0;
217 }
218 module_param_call(logging_level, scsih_set_debug_level, param_get_int,
219     &logging_level, 0644);
220
221 /**
222  * _scsih_srch_boot_sas_address - search based on sas_address
223  * @sas_address: sas address
224  * @boot_device: boot device object from bios page 2
225  *
226  * Returns 1 when there's a match, 0 means no match.
227  */
228 static inline int
229 _scsih_srch_boot_sas_address(u64 sas_address,
230     Mpi2BootDeviceSasWwid_t *boot_device)
231 {
232         return (sas_address == le64_to_cpu(boot_device->SASAddress)) ?  1 : 0;
233 }
234
235 /**
236  * _scsih_srch_boot_device_name - search based on device name
237  * @device_name: device name specified in INDENTIFY fram
238  * @boot_device: boot device object from bios page 2
239  *
240  * Returns 1 when there's a match, 0 means no match.
241  */
242 static inline int
243 _scsih_srch_boot_device_name(u64 device_name,
244     Mpi2BootDeviceDeviceName_t *boot_device)
245 {
246         return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
247 }
248
249 /**
250  * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
251  * @enclosure_logical_id: enclosure logical id
252  * @slot_number: slot number
253  * @boot_device: boot device object from bios page 2
254  *
255  * Returns 1 when there's a match, 0 means no match.
256  */
257 static inline int
258 _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
259     Mpi2BootDeviceEnclosureSlot_t *boot_device)
260 {
261         return (enclosure_logical_id == le64_to_cpu(boot_device->
262             EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
263             SlotNumber)) ? 1 : 0;
264 }
265
266 /**
267  * _scsih_is_boot_device - search for matching boot device.
268  * @sas_address: sas address
269  * @device_name: device name specified in INDENTIFY fram
270  * @enclosure_logical_id: enclosure logical id
271  * @slot_number: slot number
272  * @form: specifies boot device form
273  * @boot_device: boot device object from bios page 2
274  *
275  * Returns 1 when there's a match, 0 means no match.
276  */
277 static int
278 _scsih_is_boot_device(u64 sas_address, u64 device_name,
279     u64 enclosure_logical_id, u16 slot, u8 form,
280     Mpi2BiosPage2BootDevice_t *boot_device)
281 {
282         int rc = 0;
283
284         switch (form) {
285         case MPI2_BIOSPAGE2_FORM_SAS_WWID:
286                 if (!sas_address)
287                         break;
288                 rc = _scsih_srch_boot_sas_address(
289                     sas_address, &boot_device->SasWwid);
290                 break;
291         case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
292                 if (!enclosure_logical_id)
293                         break;
294                 rc = _scsih_srch_boot_encl_slot(
295                     enclosure_logical_id,
296                     slot, &boot_device->EnclosureSlot);
297                 break;
298         case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
299                 if (!device_name)
300                         break;
301                 rc = _scsih_srch_boot_device_name(
302                     device_name, &boot_device->DeviceName);
303                 break;
304         case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
305                 break;
306         }
307
308         return rc;
309 }
310
311 /**
312  * _scsih_determine_boot_device - determine boot device.
313  * @ioc: per adapter object
314  * @device: either sas_device or raid_device object
315  * @is_raid: [flag] 1 = raid object, 0 = sas object
316  *
317  * Determines whether this device should be first reported device to
318  * to scsi-ml or sas transport, this purpose is for persistant boot device.
319  * There are primary, alternate, and current entries in bios page 2. The order
320  * priority is primary, alternate, then current.  This routine saves
321  * the corresponding device object and is_raid flag in the ioc object.
322  * The saved data to be used later in _scsih_probe_boot_devices().
323  */
324 static void
325 _scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
326     void *device, u8 is_raid)
327 {
328         struct _sas_device *sas_device;
329         struct _raid_device *raid_device;
330         u64 sas_address;
331         u64 device_name;
332         u64 enclosure_logical_id;
333         u16 slot;
334
335          /* only process this function when driver loads */
336         if (!ioc->wait_for_port_enable_to_complete)
337                 return;
338
339         if (!is_raid) {
340                 sas_device = device;
341                 sas_address = sas_device->sas_address;
342                 device_name = sas_device->device_name;
343                 enclosure_logical_id = sas_device->enclosure_logical_id;
344                 slot = sas_device->slot;
345         } else {
346                 raid_device = device;
347                 sas_address = raid_device->wwid;
348                 device_name = 0;
349                 enclosure_logical_id = 0;
350                 slot = 0;
351         }
352
353         if (!ioc->req_boot_device.device) {
354                 if (_scsih_is_boot_device(sas_address, device_name,
355                     enclosure_logical_id, slot,
356                     (ioc->bios_pg2.ReqBootDeviceForm &
357                     MPI2_BIOSPAGE2_FORM_MASK),
358                     &ioc->bios_pg2.RequestedBootDevice)) {
359                         dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
360                            "%s: req_boot_device(0x%016llx)\n",
361                             ioc->name, __func__,
362                             (unsigned long long)sas_address));
363                         ioc->req_boot_device.device = device;
364                         ioc->req_boot_device.is_raid = is_raid;
365                 }
366         }
367
368         if (!ioc->req_alt_boot_device.device) {
369                 if (_scsih_is_boot_device(sas_address, device_name,
370                     enclosure_logical_id, slot,
371                     (ioc->bios_pg2.ReqAltBootDeviceForm &
372                     MPI2_BIOSPAGE2_FORM_MASK),
373                     &ioc->bios_pg2.RequestedAltBootDevice)) {
374                         dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
375                            "%s: req_alt_boot_device(0x%016llx)\n",
376                             ioc->name, __func__,
377                             (unsigned long long)sas_address));
378                         ioc->req_alt_boot_device.device = device;
379                         ioc->req_alt_boot_device.is_raid = is_raid;
380                 }
381         }
382
383         if (!ioc->current_boot_device.device) {
384                 if (_scsih_is_boot_device(sas_address, device_name,
385                     enclosure_logical_id, slot,
386                     (ioc->bios_pg2.CurrentBootDeviceForm &
387                     MPI2_BIOSPAGE2_FORM_MASK),
388                     &ioc->bios_pg2.CurrentBootDevice)) {
389                         dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
390                            "%s: current_boot_device(0x%016llx)\n",
391                             ioc->name, __func__,
392                             (unsigned long long)sas_address));
393                         ioc->current_boot_device.device = device;
394                         ioc->current_boot_device.is_raid = is_raid;
395                 }
396         }
397 }
398
399 /**
400  * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
401  * @ioc: per adapter object
402  * @sas_address: sas address
403  * Context: Calling function should acquire ioc->sas_device_lock
404  *
405  * This searches for sas_device based on sas_address, then return sas_device
406  * object.
407  */
408 struct _sas_device *
409 mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
410     u64 sas_address)
411 {
412         struct _sas_device *sas_device, *r;
413
414         r = NULL;
415         /* check the sas_device_init_list */
416         list_for_each_entry(sas_device, &ioc->sas_device_init_list,
417             list) {
418                 if (sas_device->sas_address != sas_address)
419                         continue;
420                 r = sas_device;
421                 goto out;
422         }
423
424         /* then check the sas_device_list */
425         list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
426                 if (sas_device->sas_address != sas_address)
427                         continue;
428                 r = sas_device;
429                 goto out;
430         }
431  out:
432         return r;
433 }
434
435 /**
436  * _scsih_sas_device_find_by_handle - sas device search
437  * @ioc: per adapter object
438  * @handle: sas device handle (assigned by firmware)
439  * Context: Calling function should acquire ioc->sas_device_lock
440  *
441  * This searches for sas_device based on sas_address, then return sas_device
442  * object.
443  */
444 static struct _sas_device *
445 _scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
446 {
447         struct _sas_device *sas_device, *r;
448
449         r = NULL;
450         if (ioc->wait_for_port_enable_to_complete) {
451                 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
452                     list) {
453                         if (sas_device->handle != handle)
454                                 continue;
455                         r = sas_device;
456                         goto out;
457                 }
458         } else {
459                 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
460                         if (sas_device->handle != handle)
461                                 continue;
462                         r = sas_device;
463                         goto out;
464                 }
465         }
466
467  out:
468         return r;
469 }
470
471 /**
472  * _scsih_sas_device_remove - remove sas_device from list.
473  * @ioc: per adapter object
474  * @sas_device: the sas_device object
475  * Context: This function will acquire ioc->sas_device_lock.
476  *
477  * Removing object and freeing associated memory from the ioc->sas_device_list.
478  */
479 static void
480 _scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
481     struct _sas_device *sas_device)
482 {
483         unsigned long flags;
484
485         spin_lock_irqsave(&ioc->sas_device_lock, flags);
486         list_del(&sas_device->list);
487         memset(sas_device, 0, sizeof(struct _sas_device));
488         kfree(sas_device);
489         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
490 }
491
492 /**
493  * _scsih_sas_device_add - insert sas_device to the list.
494  * @ioc: per adapter object
495  * @sas_device: the sas_device object
496  * Context: This function will acquire ioc->sas_device_lock.
497  *
498  * Adding new object to the ioc->sas_device_list.
499  */
500 static void
501 _scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
502     struct _sas_device *sas_device)
503 {
504         unsigned long flags;
505         u16 handle, parent_handle;
506         u64 sas_address;
507
508         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
509             "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
510             sas_device->handle, (unsigned long long)sas_device->sas_address));
511
512         spin_lock_irqsave(&ioc->sas_device_lock, flags);
513         list_add_tail(&sas_device->list, &ioc->sas_device_list);
514         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
515
516         handle = sas_device->handle;
517         parent_handle = sas_device->parent_handle;
518         sas_address = sas_device->sas_address;
519         if (!mpt2sas_transport_port_add(ioc, handle, parent_handle))
520                 _scsih_sas_device_remove(ioc, sas_device);
521 }
522
523 /**
524  * _scsih_sas_device_init_add - insert sas_device to the list.
525  * @ioc: per adapter object
526  * @sas_device: the sas_device object
527  * Context: This function will acquire ioc->sas_device_lock.
528  *
529  * Adding new object at driver load time to the ioc->sas_device_init_list.
530  */
531 static void
532 _scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
533     struct _sas_device *sas_device)
534 {
535         unsigned long flags;
536
537         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
538             "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
539             sas_device->handle, (unsigned long long)sas_device->sas_address));
540
541         spin_lock_irqsave(&ioc->sas_device_lock, flags);
542         list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
543         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
544         _scsih_determine_boot_device(ioc, sas_device, 0);
545 }
546
547 /**
548  * mpt2sas_scsih_expander_find_by_handle - expander device search
549  * @ioc: per adapter object
550  * @handle: expander handle (assigned by firmware)
551  * Context: Calling function should acquire ioc->sas_device_lock
552  *
553  * This searches for expander device based on handle, then returns the
554  * sas_node object.
555  */
556 struct _sas_node *
557 mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
558 {
559         struct _sas_node *sas_expander, *r;
560
561         r = NULL;
562         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
563                 if (sas_expander->handle != handle)
564                         continue;
565                 r = sas_expander;
566                 goto out;
567         }
568  out:
569         return r;
570 }
571
572 /**
573  * _scsih_raid_device_find_by_id - raid device search
574  * @ioc: per adapter object
575  * @id: sas device target id
576  * @channel: sas device channel
577  * Context: Calling function should acquire ioc->raid_device_lock
578  *
579  * This searches for raid_device based on target id, then return raid_device
580  * object.
581  */
582 static struct _raid_device *
583 _scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
584 {
585         struct _raid_device *raid_device, *r;
586
587         r = NULL;
588         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
589                 if (raid_device->id == id && raid_device->channel == channel) {
590                         r = raid_device;
591                         goto out;
592                 }
593         }
594
595  out:
596         return r;
597 }
598
599 /**
600  * _scsih_raid_device_find_by_handle - raid device search
601  * @ioc: per adapter object
602  * @handle: sas device handle (assigned by firmware)
603  * Context: Calling function should acquire ioc->raid_device_lock
604  *
605  * This searches for raid_device based on handle, then return raid_device
606  * object.
607  */
608 static struct _raid_device *
609 _scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
610 {
611         struct _raid_device *raid_device, *r;
612
613         r = NULL;
614         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
615                 if (raid_device->handle != handle)
616                         continue;
617                 r = raid_device;
618                 goto out;
619         }
620
621  out:
622         return r;
623 }
624
625 /**
626  * _scsih_raid_device_find_by_wwid - raid device search
627  * @ioc: per adapter object
628  * @handle: sas device handle (assigned by firmware)
629  * Context: Calling function should acquire ioc->raid_device_lock
630  *
631  * This searches for raid_device based on wwid, then return raid_device
632  * object.
633  */
634 static struct _raid_device *
635 _scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
636 {
637         struct _raid_device *raid_device, *r;
638
639         r = NULL;
640         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
641                 if (raid_device->wwid != wwid)
642                         continue;
643                 r = raid_device;
644                 goto out;
645         }
646
647  out:
648         return r;
649 }
650
651 /**
652  * _scsih_raid_device_add - add raid_device object
653  * @ioc: per adapter object
654  * @raid_device: raid_device object
655  *
656  * This is added to the raid_device_list link list.
657  */
658 static void
659 _scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
660     struct _raid_device *raid_device)
661 {
662         unsigned long flags;
663
664         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
665             "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
666             raid_device->handle, (unsigned long long)raid_device->wwid));
667
668         spin_lock_irqsave(&ioc->raid_device_lock, flags);
669         list_add_tail(&raid_device->list, &ioc->raid_device_list);
670         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
671 }
672
673 /**
674  * _scsih_raid_device_remove - delete raid_device object
675  * @ioc: per adapter object
676  * @raid_device: raid_device object
677  *
678  * This is removed from the raid_device_list link list.
679  */
680 static void
681 _scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
682     struct _raid_device *raid_device)
683 {
684         unsigned long flags;
685
686         spin_lock_irqsave(&ioc->raid_device_lock, flags);
687         list_del(&raid_device->list);
688         memset(raid_device, 0, sizeof(struct _raid_device));
689         kfree(raid_device);
690         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
691 }
692
693 /**
694  * mpt2sas_scsih_expander_find_by_sas_address - expander device search
695  * @ioc: per adapter object
696  * @sas_address: sas address
697  * Context: Calling function should acquire ioc->sas_node_lock.
698  *
699  * This searches for expander device based on sas_address, then returns the
700  * sas_node object.
701  */
702 struct _sas_node *
703 mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
704     u64 sas_address)
705 {
706         struct _sas_node *sas_expander, *r;
707
708         r = NULL;
709         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
710                 if (sas_expander->sas_address != sas_address)
711                         continue;
712                 r = sas_expander;
713                 goto out;
714         }
715  out:
716         return r;
717 }
718
719 /**
720  * _scsih_expander_node_add - insert expander device to the list.
721  * @ioc: per adapter object
722  * @sas_expander: the sas_device object
723  * Context: This function will acquire ioc->sas_node_lock.
724  *
725  * Adding new object to the ioc->sas_expander_list.
726  *
727  * Return nothing.
728  */
729 static void
730 _scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
731     struct _sas_node *sas_expander)
732 {
733         unsigned long flags;
734
735         spin_lock_irqsave(&ioc->sas_node_lock, flags);
736         list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
737         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
738 }
739
740 /**
741  * _scsih_is_end_device - determines if device is an end device
742  * @device_info: bitfield providing information about the device.
743  * Context: none
744  *
745  * Returns 1 if end device.
746  */
747 static int
748 _scsih_is_end_device(u32 device_info)
749 {
750         if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
751                 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
752                 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
753                 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
754                 return 1;
755         else
756                 return 0;
757 }
758
759 /**
760  * _scsih_scsi_lookup_get - returns scmd entry
761  * @ioc: per adapter object
762  * @smid: system request message index
763  * Context: This function will acquire ioc->scsi_lookup_lock.
764  *
765  * Returns the smid stored scmd pointer.
766  */
767 static struct scsi_cmnd *
768 _scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
769 {
770         unsigned long   flags;
771         struct scsi_cmnd *scmd;
772
773         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
774         scmd = ioc->scsi_lookup[smid - 1].scmd;
775         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
776         return scmd;
777 }
778
779 /**
780  * mptscsih_getclear_scsi_lookup - returns scmd entry
781  * @ioc: per adapter object
782  * @smid: system request message index
783  * Context: This function will acquire ioc->scsi_lookup_lock.
784  *
785  * Returns the smid stored scmd pointer, as well as clearing the scmd pointer.
786  */
787 static struct scsi_cmnd *
788 _scsih_scsi_lookup_getclear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
789 {
790         unsigned long   flags;
791         struct scsi_cmnd *scmd;
792
793         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
794         scmd = ioc->scsi_lookup[smid - 1].scmd;
795         ioc->scsi_lookup[smid - 1].scmd = NULL;
796         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
797         return scmd;
798 }
799
800 /**
801  * _scsih_scsi_lookup_set - updates scmd entry in lookup
802  * @ioc: per adapter object
803  * @smid: system request message index
804  * @scmd: pointer to scsi command object
805  * Context: This function will acquire ioc->scsi_lookup_lock.
806  *
807  * This will save scmd pointer in the scsi_lookup array.
808  *
809  * Return nothing.
810  */
811 static void
812 _scsih_scsi_lookup_set(struct MPT2SAS_ADAPTER *ioc, u16 smid,
813     struct scsi_cmnd *scmd)
814 {
815         unsigned long   flags;
816
817         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
818         ioc->scsi_lookup[smid - 1].scmd = scmd;
819         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
820 }
821
822 /**
823  * _scsih_scsi_lookup_find_by_scmd - scmd lookup
824  * @ioc: per adapter object
825  * @smid: system request message index
826  * @scmd: pointer to scsi command object
827  * Context: This function will acquire ioc->scsi_lookup_lock.
828  *
829  * This will search for a scmd pointer in the scsi_lookup array,
830  * returning the revelent smid.  A returned value of zero means invalid.
831  */
832 static u16
833 _scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
834     *scmd)
835 {
836         u16 smid;
837         unsigned long   flags;
838         int i;
839
840         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
841         smid = 0;
842         for (i = 0; i < ioc->request_depth; i++) {
843                 if (ioc->scsi_lookup[i].scmd == scmd) {
844                         smid = i + 1;
845                         goto out;
846                 }
847         }
848  out:
849         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
850         return smid;
851 }
852
853 /**
854  * _scsih_scsi_lookup_find_by_target - search for matching channel:id
855  * @ioc: per adapter object
856  * @id: target id
857  * @channel: channel
858  * Context: This function will acquire ioc->scsi_lookup_lock.
859  *
860  * This will search for a matching channel:id in the scsi_lookup array,
861  * returning 1 if found.
862  */
863 static u8
864 _scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
865     int channel)
866 {
867         u8 found;
868         unsigned long   flags;
869         int i;
870
871         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
872         found = 0;
873         for (i = 0 ; i < ioc->request_depth; i++) {
874                 if (ioc->scsi_lookup[i].scmd &&
875                     (ioc->scsi_lookup[i].scmd->device->id == id &&
876                     ioc->scsi_lookup[i].scmd->device->channel == channel)) {
877                         found = 1;
878                         goto out;
879                 }
880         }
881  out:
882         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
883         return found;
884 }
885
886 /**
887  * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
888  * @ioc: per adapter object
889  * @smid: system request message index
890  *
891  * Returns phys pointer to chain buffer.
892  */
893 static dma_addr_t
894 _scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
895 {
896         return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
897             ioc->chains_needed_per_io));
898 }
899
900 /**
901  * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
902  * @ioc: per adapter object
903  * @smid: system request message index
904  *
905  * Returns virt pointer to chain buffer.
906  */
907 static void *
908 _scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
909 {
910         return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
911             ioc->chains_needed_per_io)));
912 }
913
914 /**
915  * _scsih_build_scatter_gather - main sg creation routine
916  * @ioc: per adapter object
917  * @scmd: scsi command
918  * @smid: system request message index
919  * Context: none.
920  *
921  * The main routine that builds scatter gather table from a given
922  * scsi request sent via the .queuecommand main handler.
923  *
924  * Returns 0 success, anything else error
925  */
926 static int
927 _scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
928     struct scsi_cmnd *scmd, u16 smid)
929 {
930         Mpi2SCSIIORequest_t *mpi_request;
931         dma_addr_t chain_dma;
932         struct scatterlist *sg_scmd;
933         void *sg_local, *chain;
934         u32 chain_offset;
935         u32 chain_length;
936         u32 chain_flags;
937         u32 sges_left;
938         u32 sges_in_segment;
939         u32 sgl_flags;
940         u32 sgl_flags_last_element;
941         u32 sgl_flags_end_buffer;
942
943         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
944
945         /* init scatter gather flags */
946         sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
947         if (scmd->sc_data_direction == DMA_TO_DEVICE)
948                 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
949         sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
950             << MPI2_SGE_FLAGS_SHIFT;
951         sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
952             MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
953             << MPI2_SGE_FLAGS_SHIFT;
954         sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
955
956         sg_scmd = scsi_sglist(scmd);
957         sges_left = scsi_dma_map(scmd);
958         if (!sges_left) {
959                 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
960                 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
961                 return -ENOMEM;
962         }
963
964         sg_local = &mpi_request->SGL;
965         sges_in_segment = ioc->max_sges_in_main_message;
966         if (sges_left <= sges_in_segment)
967                 goto fill_in_last_segment;
968
969         mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
970             (sges_in_segment * ioc->sge_size))/4;
971
972         /* fill in main message segment when there is a chain following */
973         while (sges_in_segment) {
974                 if (sges_in_segment == 1)
975                         ioc->base_add_sg_single(sg_local,
976                             sgl_flags_last_element | sg_dma_len(sg_scmd),
977                             sg_dma_address(sg_scmd));
978                 else
979                         ioc->base_add_sg_single(sg_local, sgl_flags |
980                             sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
981                 sg_scmd = sg_next(sg_scmd);
982                 sg_local += ioc->sge_size;
983                 sges_left--;
984                 sges_in_segment--;
985         }
986
987         /* initializing the chain flags and pointers */
988         chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
989         chain = _scsih_get_chain_buffer(ioc, smid);
990         chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
991         do {
992                 sges_in_segment = (sges_left <=
993                     ioc->max_sges_in_chain_message) ? sges_left :
994                     ioc->max_sges_in_chain_message;
995                 chain_offset = (sges_left == sges_in_segment) ?
996                     0 : (sges_in_segment * ioc->sge_size)/4;
997                 chain_length = sges_in_segment * ioc->sge_size;
998                 if (chain_offset) {
999                         chain_offset = chain_offset <<
1000                             MPI2_SGE_CHAIN_OFFSET_SHIFT;
1001                         chain_length += ioc->sge_size;
1002                 }
1003                 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1004                     chain_length, chain_dma);
1005                 sg_local = chain;
1006                 if (!chain_offset)
1007                         goto fill_in_last_segment;
1008
1009                 /* fill in chain segments */
1010                 while (sges_in_segment) {
1011                         if (sges_in_segment == 1)
1012                                 ioc->base_add_sg_single(sg_local,
1013                                     sgl_flags_last_element |
1014                                     sg_dma_len(sg_scmd),
1015                                     sg_dma_address(sg_scmd));
1016                         else
1017                                 ioc->base_add_sg_single(sg_local, sgl_flags |
1018                                     sg_dma_len(sg_scmd),
1019                                     sg_dma_address(sg_scmd));
1020                         sg_scmd = sg_next(sg_scmd);
1021                         sg_local += ioc->sge_size;
1022                         sges_left--;
1023                         sges_in_segment--;
1024                 }
1025
1026                 chain_dma += ioc->request_sz;
1027                 chain += ioc->request_sz;
1028         } while (1);
1029
1030
1031  fill_in_last_segment:
1032
1033         /* fill the last segment */
1034         while (sges_left) {
1035                 if (sges_left == 1)
1036                         ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1037                             sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1038                 else
1039                         ioc->base_add_sg_single(sg_local, sgl_flags |
1040                             sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1041                 sg_scmd = sg_next(sg_scmd);
1042                 sg_local += ioc->sge_size;
1043                 sges_left--;
1044         }
1045
1046         return 0;
1047 }
1048
1049 /**
1050  * scsih_change_queue_depth - setting device queue depth
1051  * @sdev: scsi device struct
1052  * @qdepth: requested queue depth
1053  *
1054  * Returns queue depth.
1055  */
1056 static int
1057 scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1058 {
1059         struct Scsi_Host *shost = sdev->host;
1060         int max_depth;
1061         int tag_type;
1062
1063         max_depth = shost->can_queue;
1064         if (!sdev->tagged_supported)
1065                 max_depth = 1;
1066         if (qdepth > max_depth)
1067                 qdepth = max_depth;
1068         tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
1069         scsi_adjust_queue_depth(sdev, tag_type, qdepth);
1070
1071         if (sdev->inquiry_len > 7)
1072                 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1073                 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1074                 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1075                 sdev->ordered_tags, sdev->scsi_level,
1076                 (sdev->inquiry[7] & 2) >> 1);
1077
1078         return sdev->queue_depth;
1079 }
1080
1081 /**
1082  * scsih_change_queue_depth - changing device queue tag type
1083  * @sdev: scsi device struct
1084  * @tag_type: requested tag type
1085  *
1086  * Returns queue tag type.
1087  */
1088 static int
1089 scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
1090 {
1091         if (sdev->tagged_supported) {
1092                 scsi_set_tag_type(sdev, tag_type);
1093                 if (tag_type)
1094                         scsi_activate_tcq(sdev, sdev->queue_depth);
1095                 else
1096                         scsi_deactivate_tcq(sdev, sdev->queue_depth);
1097         } else
1098                 tag_type = 0;
1099
1100         return tag_type;
1101 }
1102
1103 /**
1104  * scsih_target_alloc - target add routine
1105  * @starget: scsi target struct
1106  *
1107  * Returns 0 if ok. Any other return is assumed to be an error and
1108  * the device is ignored.
1109  */
1110 static int
1111 scsih_target_alloc(struct scsi_target *starget)
1112 {
1113         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1114         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1115         struct MPT2SAS_TARGET *sas_target_priv_data;
1116         struct _sas_device *sas_device;
1117         struct _raid_device *raid_device;
1118         unsigned long flags;
1119         struct sas_rphy *rphy;
1120
1121         sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1122         if (!sas_target_priv_data)
1123                 return -ENOMEM;
1124
1125         starget->hostdata = sas_target_priv_data;
1126         sas_target_priv_data->starget = starget;
1127         sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1128
1129         /* RAID volumes */
1130         if (starget->channel == RAID_CHANNEL) {
1131                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1132                 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1133                     starget->channel);
1134                 if (raid_device) {
1135                         sas_target_priv_data->handle = raid_device->handle;
1136                         sas_target_priv_data->sas_address = raid_device->wwid;
1137                         sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1138                         raid_device->starget = starget;
1139                 }
1140                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1141                 return 0;
1142         }
1143
1144         /* sas/sata devices */
1145         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1146         rphy = dev_to_rphy(starget->dev.parent);
1147         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1148            rphy->identify.sas_address);
1149
1150         if (sas_device) {
1151                 sas_target_priv_data->handle = sas_device->handle;
1152                 sas_target_priv_data->sas_address = sas_device->sas_address;
1153                 sas_device->starget = starget;
1154                 sas_device->id = starget->id;
1155                 sas_device->channel = starget->channel;
1156                 if (sas_device->hidden_raid_component)
1157                         sas_target_priv_data->flags |=
1158                             MPT_TARGET_FLAGS_RAID_COMPONENT;
1159         }
1160         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1161
1162         return 0;
1163 }
1164
1165 /**
1166  * scsih_target_destroy - target destroy routine
1167  * @starget: scsi target struct
1168  *
1169  * Returns nothing.
1170  */
1171 static void
1172 scsih_target_destroy(struct scsi_target *starget)
1173 {
1174         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1175         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1176         struct MPT2SAS_TARGET *sas_target_priv_data;
1177         struct _sas_device *sas_device;
1178         struct _raid_device *raid_device;
1179         unsigned long flags;
1180         struct sas_rphy *rphy;
1181
1182         sas_target_priv_data = starget->hostdata;
1183         if (!sas_target_priv_data)
1184                 return;
1185
1186         if (starget->channel == RAID_CHANNEL) {
1187                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1188                 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1189                     starget->channel);
1190                 if (raid_device) {
1191                         raid_device->starget = NULL;
1192                         raid_device->sdev = NULL;
1193                 }
1194                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1195                 goto out;
1196         }
1197
1198         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1199         rphy = dev_to_rphy(starget->dev.parent);
1200         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1201            rphy->identify.sas_address);
1202         if (sas_device && (sas_device->starget == starget) &&
1203             (sas_device->id == starget->id) &&
1204             (sas_device->channel == starget->channel))
1205                 sas_device->starget = NULL;
1206
1207         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1208
1209  out:
1210         kfree(sas_target_priv_data);
1211         starget->hostdata = NULL;
1212 }
1213
1214 /**
1215  * scsih_slave_alloc - device add routine
1216  * @sdev: scsi device struct
1217  *
1218  * Returns 0 if ok. Any other return is assumed to be an error and
1219  * the device is ignored.
1220  */
1221 static int
1222 scsih_slave_alloc(struct scsi_device *sdev)
1223 {
1224         struct Scsi_Host *shost;
1225         struct MPT2SAS_ADAPTER *ioc;
1226         struct MPT2SAS_TARGET *sas_target_priv_data;
1227         struct MPT2SAS_DEVICE *sas_device_priv_data;
1228         struct scsi_target *starget;
1229         struct _raid_device *raid_device;
1230         struct _sas_device *sas_device;
1231         unsigned long flags;
1232
1233         sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1234         if (!sas_device_priv_data)
1235                 return -ENOMEM;
1236
1237         sas_device_priv_data->lun = sdev->lun;
1238         sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1239
1240         starget = scsi_target(sdev);
1241         sas_target_priv_data = starget->hostdata;
1242         sas_target_priv_data->num_luns++;
1243         sas_device_priv_data->sas_target = sas_target_priv_data;
1244         sdev->hostdata = sas_device_priv_data;
1245         if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1246                 sdev->no_uld_attach = 1;
1247
1248         shost = dev_to_shost(&starget->dev);
1249         ioc = shost_priv(shost);
1250         if (starget->channel == RAID_CHANNEL) {
1251                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1252                 raid_device = _scsih_raid_device_find_by_id(ioc,
1253                     starget->id, starget->channel);
1254                 if (raid_device)
1255                         raid_device->sdev = sdev; /* raid is single lun */
1256                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1257         } else {
1258                 /* set TLR bit for SSP devices */
1259                 if (!(ioc->facts.IOCCapabilities &
1260                      MPI2_IOCFACTS_CAPABILITY_TLR))
1261                         goto out;
1262                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1263                 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1264                    sas_device_priv_data->sas_target->sas_address);
1265                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1266                 if (sas_device && sas_device->device_info &
1267                     MPI2_SAS_DEVICE_INFO_SSP_TARGET)
1268                         sas_device_priv_data->flags |= MPT_DEVICE_TLR_ON;
1269         }
1270
1271  out:
1272         return 0;
1273 }
1274
1275 /**
1276  * scsih_slave_destroy - device destroy routine
1277  * @sdev: scsi device struct
1278  *
1279  * Returns nothing.
1280  */
1281 static void
1282 scsih_slave_destroy(struct scsi_device *sdev)
1283 {
1284         struct MPT2SAS_TARGET *sas_target_priv_data;
1285         struct scsi_target *starget;
1286
1287         if (!sdev->hostdata)
1288                 return;
1289
1290         starget = scsi_target(sdev);
1291         sas_target_priv_data = starget->hostdata;
1292         sas_target_priv_data->num_luns--;
1293         kfree(sdev->hostdata);
1294         sdev->hostdata = NULL;
1295 }
1296
1297 /**
1298  * scsih_display_sata_capabilities - sata capabilities
1299  * @ioc: per adapter object
1300  * @sas_device: the sas_device object
1301  * @sdev: scsi device struct
1302  */
1303 static void
1304 scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
1305     struct _sas_device *sas_device, struct scsi_device *sdev)
1306 {
1307         Mpi2ConfigReply_t mpi_reply;
1308         Mpi2SasDevicePage0_t sas_device_pg0;
1309         u32 ioc_status;
1310         u16 flags;
1311         u32 device_info;
1312
1313         if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1314             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1315                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1316                     ioc->name, __FILE__, __LINE__, __func__);
1317                 return;
1318         }
1319
1320         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1321             MPI2_IOCSTATUS_MASK;
1322         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1323                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1324                     ioc->name, __FILE__, __LINE__, __func__);
1325                 return;
1326         }
1327
1328         flags = le16_to_cpu(sas_device_pg0.Flags);
1329         device_info = le16_to_cpu(sas_device_pg0.DeviceInfo);
1330
1331         sdev_printk(KERN_INFO, sdev,
1332             "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1333             "sw_preserve(%s)\n",
1334             (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1335             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1336             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1337             "n",
1338             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1339             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1340             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1341 }
1342
1343 /**
1344  * _scsih_get_volume_capabilities - volume capabilities
1345  * @ioc: per adapter object
1346  * @sas_device: the raid_device object
1347  */
1348 static void
1349 _scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1350     struct _raid_device *raid_device)
1351 {
1352         Mpi2RaidVolPage0_t *vol_pg0;
1353         Mpi2RaidPhysDiskPage0_t pd_pg0;
1354         Mpi2SasDevicePage0_t sas_device_pg0;
1355         Mpi2ConfigReply_t mpi_reply;
1356         u16 sz;
1357         u8 num_pds;
1358
1359         if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1360             &num_pds)) || !num_pds) {
1361                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1362                     ioc->name, __FILE__, __LINE__, __func__);
1363                 return;
1364         }
1365
1366         raid_device->num_pds = num_pds;
1367         sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1368             sizeof(Mpi2RaidVol0PhysDisk_t));
1369         vol_pg0 = kzalloc(sz, GFP_KERNEL);
1370         if (!vol_pg0) {
1371                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1372                     ioc->name, __FILE__, __LINE__, __func__);
1373                 return;
1374         }
1375
1376         if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1377              MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1378                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1379                     ioc->name, __FILE__, __LINE__, __func__);
1380                 kfree(vol_pg0);
1381                 return;
1382         }
1383
1384         raid_device->volume_type = vol_pg0->VolumeType;
1385
1386         /* figure out what the underlying devices are by
1387          * obtaining the device_info bits for the 1st device
1388          */
1389         if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1390             &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1391             vol_pg0->PhysDisk[0].PhysDiskNum))) {
1392                 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1393                     &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1394                     le16_to_cpu(pd_pg0.DevHandle)))) {
1395                         raid_device->device_info =
1396                             le32_to_cpu(sas_device_pg0.DeviceInfo);
1397                 }
1398         }
1399
1400         kfree(vol_pg0);
1401 }
1402
1403 /**
1404  * scsih_slave_configure - device configure routine.
1405  * @sdev: scsi device struct
1406  *
1407  * Returns 0 if ok. Any other return is assumed to be an error and
1408  * the device is ignored.
1409  */
1410 static int
1411 scsih_slave_configure(struct scsi_device *sdev)
1412 {
1413         struct Scsi_Host *shost = sdev->host;
1414         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1415         struct MPT2SAS_DEVICE *sas_device_priv_data;
1416         struct MPT2SAS_TARGET *sas_target_priv_data;
1417         struct _sas_device *sas_device;
1418         struct _raid_device *raid_device;
1419         unsigned long flags;
1420         int qdepth;
1421         u8 ssp_target = 0;
1422         char *ds = "";
1423         char *r_level = "";
1424
1425         qdepth = 1;
1426         sas_device_priv_data = sdev->hostdata;
1427         sas_device_priv_data->configured_lun = 1;
1428         sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1429         sas_target_priv_data = sas_device_priv_data->sas_target;
1430
1431         /* raid volume handling */
1432         if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1433
1434                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1435                 raid_device = _scsih_raid_device_find_by_handle(ioc,
1436                      sas_target_priv_data->handle);
1437                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1438                 if (!raid_device) {
1439                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1440                             ioc->name, __FILE__, __LINE__, __func__);
1441                         return 0;
1442                 }
1443
1444                 _scsih_get_volume_capabilities(ioc, raid_device);
1445
1446                 /* RAID Queue Depth Support
1447                  * IS volume = underlying qdepth of drive type, either
1448                  *    MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1449                  * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1450                  */
1451                 if (raid_device->device_info &
1452                     MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1453                         qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1454                         ds = "SSP";
1455                 } else {
1456                         qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1457                          if (raid_device->device_info &
1458                             MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1459                                 ds = "SATA";
1460                         else
1461                                 ds = "STP";
1462                 }
1463
1464                 switch (raid_device->volume_type) {
1465                 case MPI2_RAID_VOL_TYPE_RAID0:
1466                         r_level = "RAID0";
1467                         break;
1468                 case MPI2_RAID_VOL_TYPE_RAID1E:
1469                         qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1470                         r_level = "RAID1E";
1471                         break;
1472                 case MPI2_RAID_VOL_TYPE_RAID1:
1473                         qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1474                         r_level = "RAID1";
1475                         break;
1476                 case MPI2_RAID_VOL_TYPE_RAID10:
1477                         qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1478                         r_level = "RAID10";
1479                         break;
1480                 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1481                 default:
1482                         qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1483                         r_level = "RAIDX";
1484                         break;
1485                 }
1486
1487                 sdev_printk(KERN_INFO, sdev, "%s: "
1488                     "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1489                     r_level, raid_device->handle,
1490                     (unsigned long long)raid_device->wwid,
1491                     raid_device->num_pds, ds);
1492                 scsih_change_queue_depth(sdev, qdepth);
1493                 return 0;
1494         }
1495
1496         /* non-raid handling */
1497         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1498         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1499            sas_device_priv_data->sas_target->sas_address);
1500         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1501         if (sas_device) {
1502                 if (sas_target_priv_data->flags &
1503                     MPT_TARGET_FLAGS_RAID_COMPONENT) {
1504                         mpt2sas_config_get_volume_handle(ioc,
1505                             sas_device->handle, &sas_device->volume_handle);
1506                         mpt2sas_config_get_volume_wwid(ioc,
1507                             sas_device->volume_handle,
1508                             &sas_device->volume_wwid);
1509                 }
1510                 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1511                         qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1512                         ssp_target = 1;
1513                         ds = "SSP";
1514                 } else {
1515                         qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1516                         if (sas_device->device_info &
1517                             MPI2_SAS_DEVICE_INFO_STP_TARGET)
1518                                 ds = "STP";
1519                         else if (sas_device->device_info &
1520                             MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1521                                 ds = "SATA";
1522                 }
1523
1524                 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1525                     "sas_addr(0x%016llx), device_name(0x%016llx)\n",
1526                     ds, sas_device->handle,
1527                     (unsigned long long)sas_device->sas_address,
1528                     (unsigned long long)sas_device->device_name);
1529                 sdev_printk(KERN_INFO, sdev, "%s: "
1530                     "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1531                     (unsigned long long) sas_device->enclosure_logical_id,
1532                     sas_device->slot);
1533
1534                 if (!ssp_target)
1535                         scsih_display_sata_capabilities(ioc, sas_device, sdev);
1536         }
1537
1538         scsih_change_queue_depth(sdev, qdepth);
1539
1540         if (ssp_target)
1541                 sas_read_port_mode_page(sdev);
1542         return 0;
1543 }
1544
1545 /**
1546  * scsih_bios_param - fetch head, sector, cylinder info for a disk
1547  * @sdev: scsi device struct
1548  * @bdev: pointer to block device context
1549  * @capacity: device size (in 512 byte sectors)
1550  * @params: three element array to place output:
1551  *              params[0] number of heads (max 255)
1552  *              params[1] number of sectors (max 63)
1553  *              params[2] number of cylinders
1554  *
1555  * Return nothing.
1556  */
1557 static int
1558 scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1559     sector_t capacity, int params[])
1560 {
1561         int             heads;
1562         int             sectors;
1563         sector_t        cylinders;
1564         ulong           dummy;
1565
1566         heads = 64;
1567         sectors = 32;
1568
1569         dummy = heads * sectors;
1570         cylinders = capacity;
1571         sector_div(cylinders, dummy);
1572
1573         /*
1574          * Handle extended translation size for logical drives
1575          * > 1Gb
1576          */
1577         if ((ulong)capacity >= 0x200000) {
1578                 heads = 255;
1579                 sectors = 63;
1580                 dummy = heads * sectors;
1581                 cylinders = capacity;
1582                 sector_div(cylinders, dummy);
1583         }
1584
1585         /* return result */
1586         params[0] = heads;
1587         params[1] = sectors;
1588         params[2] = cylinders;
1589
1590         return 0;
1591 }
1592
1593 /**
1594  * _scsih_response_code - translation of device response code
1595  * @ioc: per adapter object
1596  * @response_code: response code returned by the device
1597  *
1598  * Return nothing.
1599  */
1600 static void
1601 _scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1602 {
1603         char *desc;
1604
1605         switch (response_code) {
1606         case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1607                 desc = "task management request completed";
1608                 break;
1609         case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1610                 desc = "invalid frame";
1611                 break;
1612         case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1613                 desc = "task management request not supported";
1614                 break;
1615         case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1616                 desc = "task management request failed";
1617                 break;
1618         case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1619                 desc = "task management request succeeded";
1620                 break;
1621         case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1622                 desc = "invalid lun";
1623                 break;
1624         case 0xA:
1625                 desc = "overlapped tag attempted";
1626                 break;
1627         case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1628                 desc = "task queued, however not sent to target";
1629                 break;
1630         default:
1631                 desc = "unknown";
1632                 break;
1633         }
1634         printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1635                 ioc->name, response_code, desc);
1636 }
1637
1638 /**
1639  * scsih_tm_done - tm completion routine
1640  * @ioc: per adapter object
1641  * @smid: system request message index
1642  * @VF_ID: virtual function id
1643  * @reply: reply message frame(lower 32bit addr)
1644  * Context: none.
1645  *
1646  * The callback handler when using scsih_issue_tm.
1647  *
1648  * Return nothing.
1649  */
1650 static void
1651 scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
1652 {
1653         MPI2DefaultReply_t *mpi_reply;
1654
1655         if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
1656                 return;
1657         if (ioc->tm_cmds.smid != smid)
1658                 return;
1659         ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1660         mpi_reply =  mpt2sas_base_get_reply_virt_addr(ioc, reply);
1661         if (mpi_reply) {
1662                 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1663                 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1664         }
1665         ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1666         complete(&ioc->tm_cmds.done);
1667 }
1668
1669 /**
1670  * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1671  * @ioc: per adapter object
1672  * @handle: device handle
1673  *
1674  * During taskmangement request, we need to freeze the device queue.
1675  */
1676 void
1677 mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1678 {
1679         struct MPT2SAS_DEVICE *sas_device_priv_data;
1680         struct scsi_device *sdev;
1681         u8 skip = 0;
1682
1683         shost_for_each_device(sdev, ioc->shost) {
1684                 if (skip)
1685                         continue;
1686                 sas_device_priv_data = sdev->hostdata;
1687                 if (!sas_device_priv_data)
1688                         continue;
1689                 if (sas_device_priv_data->sas_target->handle == handle) {
1690                         sas_device_priv_data->sas_target->tm_busy = 1;
1691                         skip = 1;
1692                         ioc->ignore_loginfos = 1;
1693                 }
1694         }
1695 }
1696
1697 /**
1698  * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1699  * @ioc: per adapter object
1700  * @handle: device handle
1701  *
1702  * During taskmangement request, we need to freeze the device queue.
1703  */
1704 void
1705 mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1706 {
1707         struct MPT2SAS_DEVICE *sas_device_priv_data;
1708         struct scsi_device *sdev;
1709         u8 skip = 0;
1710
1711         shost_for_each_device(sdev, ioc->shost) {
1712                 if (skip)
1713                         continue;
1714                 sas_device_priv_data = sdev->hostdata;
1715                 if (!sas_device_priv_data)
1716                         continue;
1717                 if (sas_device_priv_data->sas_target->handle == handle) {
1718                         sas_device_priv_data->sas_target->tm_busy = 0;
1719                         skip = 1;
1720                         ioc->ignore_loginfos = 0;
1721                 }
1722         }
1723 }
1724
1725 /**
1726  * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1727  * @ioc: per adapter struct
1728  * @device_handle: device handle
1729  * @lun: lun number
1730  * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1731  * @smid_task: smid assigned to the task
1732  * @timeout: timeout in seconds
1733  * Context: The calling function needs to acquire the tm_cmds.mutex
1734  *
1735  * A generic API for sending task management requests to firmware.
1736  *
1737  * The ioc->tm_cmds.status flag should be MPT2_CMD_NOT_USED before calling
1738  * this API.
1739  *
1740  * The callback index is set inside `ioc->tm_cb_idx`.
1741  *
1742  * Return nothing.
1743  */
1744 void
1745 mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
1746     u8 type, u16 smid_task, ulong timeout)
1747 {
1748         Mpi2SCSITaskManagementRequest_t *mpi_request;
1749         Mpi2SCSITaskManagementReply_t *mpi_reply;
1750         u16 smid = 0;
1751         u32 ioc_state;
1752         unsigned long timeleft;
1753         u8 VF_ID = 0;
1754         unsigned long flags;
1755
1756         spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
1757         if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED ||
1758             ioc->shost_recovery) {
1759                 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
1760                 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1761                     __func__, ioc->name);
1762                 return;
1763         }
1764         spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
1765
1766         ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
1767         if (ioc_state & MPI2_DOORBELL_USED) {
1768                 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
1769                     "active!\n", ioc->name));
1770                 goto issue_host_reset;
1771         }
1772
1773         if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
1774                 mpt2sas_base_fault_info(ioc, ioc_state &
1775                     MPI2_DOORBELL_DATA_MASK);
1776                 goto issue_host_reset;
1777         }
1778
1779         smid = mpt2sas_base_get_smid(ioc, ioc->tm_cb_idx);
1780         if (!smid) {
1781                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1782                     ioc->name, __func__);
1783                 return;
1784         }
1785
1786         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
1787             " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type, smid));
1788         ioc->tm_cmds.status = MPT2_CMD_PENDING;
1789         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1790         ioc->tm_cmds.smid = smid;
1791         memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
1792         mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1793         mpi_request->DevHandle = cpu_to_le16(handle);
1794         mpi_request->TaskType = type;
1795         mpi_request->TaskMID = cpu_to_le16(smid_task);
1796         int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
1797         mpt2sas_scsih_set_tm_flag(ioc, handle);
1798         mpt2sas_base_put_smid_hi_priority(ioc, smid, VF_ID);
1799         timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
1800         mpt2sas_scsih_clear_tm_flag(ioc, handle);
1801         if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
1802                 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1803                     ioc->name, __func__);
1804                 _debug_dump_mf(mpi_request,
1805                     sizeof(Mpi2SCSITaskManagementRequest_t)/4);
1806                 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET))
1807                         goto issue_host_reset;
1808         }
1809
1810         if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
1811                 mpi_reply = ioc->tm_cmds.reply;
1812                 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
1813                     "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
1814                     ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
1815                     le32_to_cpu(mpi_reply->IOCLogInfo),
1816                     le32_to_cpu(mpi_reply->TerminationCount)));
1817                 if (ioc->logging_level & MPT_DEBUG_TM)
1818                         _scsih_response_code(ioc, mpi_reply->ResponseCode);
1819         }
1820         return;
1821  issue_host_reset:
1822         mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, FORCE_BIG_HAMMER);
1823 }
1824
1825 /**
1826  * scsih_abort - eh threads main abort routine
1827  * @sdev: scsi device struct
1828  *
1829  * Returns SUCCESS if command aborted else FAILED
1830  */
1831 static int
1832 scsih_abort(struct scsi_cmnd *scmd)
1833 {
1834         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1835         struct MPT2SAS_DEVICE *sas_device_priv_data;
1836         u16 smid;
1837         u16 handle;
1838         int r;
1839         struct scsi_cmnd *scmd_lookup;
1840
1841         printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
1842             ioc->name, scmd);
1843         scsi_print_command(scmd);
1844
1845         sas_device_priv_data = scmd->device->hostdata;
1846         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1847                 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1848                     ioc->name, scmd);
1849                 scmd->result = DID_NO_CONNECT << 16;
1850                 scmd->scsi_done(scmd);
1851                 r = SUCCESS;
1852                 goto out;
1853         }
1854
1855         /* search for the command */
1856         smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
1857         if (!smid) {
1858                 scmd->result = DID_RESET << 16;
1859                 r = SUCCESS;
1860                 goto out;
1861         }
1862
1863         /* for hidden raid components and volumes this is not supported */
1864         if (sas_device_priv_data->sas_target->flags &
1865             MPT_TARGET_FLAGS_RAID_COMPONENT ||
1866             sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
1867                 scmd->result = DID_RESET << 16;
1868                 r = FAILED;
1869                 goto out;
1870         }
1871
1872         mutex_lock(&ioc->tm_cmds.mutex);
1873         handle = sas_device_priv_data->sas_target->handle;
1874         mpt2sas_scsih_issue_tm(ioc, handle, sas_device_priv_data->lun,
1875             MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
1876
1877         /* sanity check - see whether command actually completed */
1878         scmd_lookup = _scsih_scsi_lookup_get(ioc, smid);
1879         if (scmd_lookup && (scmd_lookup->serial_number == scmd->serial_number))
1880                 r = FAILED;
1881         else
1882                 r = SUCCESS;
1883         ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1884         mutex_unlock(&ioc->tm_cmds.mutex);
1885
1886  out:
1887         printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
1888             ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1889         return r;
1890 }
1891
1892
1893 /**
1894  * scsih_dev_reset - eh threads main device reset routine
1895  * @sdev: scsi device struct
1896  *
1897  * Returns SUCCESS if command aborted else FAILED
1898  */
1899 static int
1900 scsih_dev_reset(struct scsi_cmnd *scmd)
1901 {
1902         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1903         struct MPT2SAS_DEVICE *sas_device_priv_data;
1904         struct _sas_device *sas_device;
1905         unsigned long flags;
1906         u16     handle;
1907         int r;
1908
1909         printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
1910             ioc->name, scmd);
1911         scsi_print_command(scmd);
1912
1913         sas_device_priv_data = scmd->device->hostdata;
1914         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1915                 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1916                     ioc->name, scmd);
1917                 scmd->result = DID_NO_CONNECT << 16;
1918                 scmd->scsi_done(scmd);
1919                 r = SUCCESS;
1920                 goto out;
1921         }
1922
1923         /* for hidden raid components obtain the volume_handle */
1924         handle = 0;
1925         if (sas_device_priv_data->sas_target->flags &
1926             MPT_TARGET_FLAGS_RAID_COMPONENT) {
1927                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1928                 sas_device = _scsih_sas_device_find_by_handle(ioc,
1929                    sas_device_priv_data->sas_target->handle);
1930                 if (sas_device)
1931                         handle = sas_device->volume_handle;
1932                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1933         } else
1934                 handle = sas_device_priv_data->sas_target->handle;
1935
1936         if (!handle) {
1937                 scmd->result = DID_RESET << 16;
1938                 r = FAILED;
1939                 goto out;
1940         }
1941
1942         mutex_lock(&ioc->tm_cmds.mutex);
1943         mpt2sas_scsih_issue_tm(ioc, handle, 0,
1944             MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
1945
1946         /*
1947          *  sanity check see whether all commands to this target been
1948          *  completed
1949          */
1950         if (_scsih_scsi_lookup_find_by_target(ioc, scmd->device->id,
1951             scmd->device->channel))
1952                 r = FAILED;
1953         else
1954                 r = SUCCESS;
1955         ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1956         mutex_unlock(&ioc->tm_cmds.mutex);
1957
1958  out:
1959         printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
1960             ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1961         return r;
1962 }
1963
1964 /**
1965  * scsih_abort - eh threads main host reset routine
1966  * @sdev: scsi device struct
1967  *
1968  * Returns SUCCESS if command aborted else FAILED
1969  */
1970 static int
1971 scsih_host_reset(struct scsi_cmnd *scmd)
1972 {
1973         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1974         int r, retval;
1975
1976         printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
1977             ioc->name, scmd);
1978         scsi_print_command(scmd);
1979
1980         retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1981             FORCE_BIG_HAMMER);
1982         r = (retval < 0) ? FAILED : SUCCESS;
1983         printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
1984             ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1985
1986         return r;
1987 }
1988
1989 /**
1990  * _scsih_fw_event_add - insert and queue up fw_event
1991  * @ioc: per adapter object
1992  * @fw_event: object describing the event
1993  * Context: This function will acquire ioc->fw_event_lock.
1994  *
1995  * This adds the firmware event object into link list, then queues it up to
1996  * be processed from user context.
1997  *
1998  * Return nothing.
1999  */
2000 static void
2001 _scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2002 {
2003         unsigned long flags;
2004
2005         if (ioc->firmware_event_thread == NULL)
2006                 return;
2007
2008         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2009         list_add_tail(&fw_event->list, &ioc->fw_event_list);
2010         INIT_WORK(&fw_event->work, _firmware_event_work);
2011         queue_work(ioc->firmware_event_thread, &fw_event->work);
2012         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2013 }
2014
2015 /**
2016  * _scsih_fw_event_free - delete fw_event
2017  * @ioc: per adapter object
2018  * @fw_event: object describing the event
2019  * Context: This function will acquire ioc->fw_event_lock.
2020  *
2021  * This removes firmware event object from link list, frees associated memory.
2022  *
2023  * Return nothing.
2024  */
2025 static void
2026 _scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2027     *fw_event)
2028 {
2029         unsigned long flags;
2030
2031         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2032         list_del(&fw_event->list);
2033         kfree(fw_event->event_data);
2034         kfree(fw_event);
2035         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2036 }
2037
2038 /**
2039  * _scsih_fw_event_add - requeue an event
2040  * @ioc: per adapter object
2041  * @fw_event: object describing the event
2042  * Context: This function will acquire ioc->fw_event_lock.
2043  *
2044  * Return nothing.
2045  */
2046 static void
2047 _scsih_fw_event_requeue(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2048     *fw_event, unsigned long delay)
2049 {
2050         unsigned long flags;
2051         if (ioc->firmware_event_thread == NULL)
2052                 return;
2053
2054         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2055         queue_work(ioc->firmware_event_thread, &fw_event->work);
2056         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2057 }
2058
2059 /**
2060  * _scsih_fw_event_off - turn flag off preventing event handling
2061  * @ioc: per adapter object
2062  *
2063  * Used to prevent handling of firmware events during adapter reset
2064  * driver unload.
2065  *
2066  * Return nothing.
2067  */
2068 static void
2069 _scsih_fw_event_off(struct MPT2SAS_ADAPTER *ioc)
2070 {
2071         unsigned long flags;
2072
2073         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2074         ioc->fw_events_off = 1;
2075         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2076
2077 }
2078
2079 /**
2080  * _scsih_fw_event_on - turn flag on allowing firmware event handling
2081  * @ioc: per adapter object
2082  *
2083  * Returns nothing.
2084  */
2085 static void
2086 _scsih_fw_event_on(struct MPT2SAS_ADAPTER *ioc)
2087 {
2088         unsigned long flags;
2089
2090         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2091         ioc->fw_events_off = 0;
2092         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2093 }
2094
2095 /**
2096  * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2097  * @ioc: per adapter object
2098  * @handle: device handle
2099  *
2100  * During device pull we need to appropiately set the sdev state.
2101  */
2102 static void
2103 _scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2104 {
2105         struct MPT2SAS_DEVICE *sas_device_priv_data;
2106         struct scsi_device *sdev;
2107
2108         shost_for_each_device(sdev, ioc->shost) {
2109                 sas_device_priv_data = sdev->hostdata;
2110                 if (!sas_device_priv_data)
2111                         continue;
2112                 if (!sas_device_priv_data->block)
2113                         continue;
2114                 if (sas_device_priv_data->sas_target->handle == handle) {
2115                         dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2116                             MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2117                             "handle(0x%04x)\n", ioc->name, handle));
2118                         sas_device_priv_data->block = 0;
2119                         scsi_device_set_state(sdev, SDEV_RUNNING);
2120                 }
2121         }
2122 }
2123
2124 /**
2125  * _scsih_block_io_device - set the device state to SDEV_BLOCK
2126  * @ioc: per adapter object
2127  * @handle: device handle
2128  *
2129  * During device pull we need to appropiately set the sdev state.
2130  */
2131 static void
2132 _scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2133 {
2134         struct MPT2SAS_DEVICE *sas_device_priv_data;
2135         struct scsi_device *sdev;
2136
2137         shost_for_each_device(sdev, ioc->shost) {
2138                 sas_device_priv_data = sdev->hostdata;
2139                 if (!sas_device_priv_data)
2140                         continue;
2141                 if (sas_device_priv_data->block)
2142                         continue;
2143                 if (sas_device_priv_data->sas_target->handle == handle) {
2144                         dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2145                             MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2146                             "handle(0x%04x)\n", ioc->name, handle));
2147                         sas_device_priv_data->block = 1;
2148                         scsi_device_set_state(sdev, SDEV_BLOCK);
2149                 }
2150         }
2151 }
2152
2153 /**
2154  * _scsih_block_io_to_children_attached_to_ex
2155  * @ioc: per adapter object
2156  * @sas_expander: the sas_device object
2157  *
2158  * This routine set sdev state to SDEV_BLOCK for all devices
2159  * attached to this expander. This function called when expander is
2160  * pulled.
2161  */
2162 static void
2163 _scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2164     struct _sas_node *sas_expander)
2165 {
2166         struct _sas_port *mpt2sas_port;
2167         struct _sas_device *sas_device;
2168         struct _sas_node *expander_sibling;
2169         unsigned long flags;
2170
2171         if (!sas_expander)
2172                 return;
2173
2174         list_for_each_entry(mpt2sas_port,
2175            &sas_expander->sas_port_list, port_list) {
2176                 if (mpt2sas_port->remote_identify.device_type ==
2177                     SAS_END_DEVICE) {
2178                         spin_lock_irqsave(&ioc->sas_device_lock, flags);
2179                         sas_device =
2180                             mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2181                            mpt2sas_port->remote_identify.sas_address);
2182                         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2183                         if (!sas_device)
2184                                 continue;
2185                         _scsih_block_io_device(ioc, sas_device->handle);
2186                 }
2187         }
2188
2189         list_for_each_entry(mpt2sas_port,
2190            &sas_expander->sas_port_list, port_list) {
2191
2192                 if (mpt2sas_port->remote_identify.device_type ==
2193                     MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2194                     mpt2sas_port->remote_identify.device_type ==
2195                     MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2196
2197                         spin_lock_irqsave(&ioc->sas_node_lock, flags);
2198                         expander_sibling =
2199                             mpt2sas_scsih_expander_find_by_sas_address(
2200                             ioc, mpt2sas_port->remote_identify.sas_address);
2201                         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2202                         _scsih_block_io_to_children_attached_to_ex(ioc,
2203                             expander_sibling);
2204                 }
2205         }
2206 }
2207
2208 /**
2209  * _scsih_block_io_to_children_attached_directly
2210  * @ioc: per adapter object
2211  * @event_data: topology change event data
2212  *
2213  * This routine set sdev state to SDEV_BLOCK for all devices
2214  * direct attached during device pull.
2215  */
2216 static void
2217 _scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2218     Mpi2EventDataSasTopologyChangeList_t *event_data)
2219 {
2220         int i;
2221         u16 handle;
2222         u16 reason_code;
2223         u8 phy_number;
2224
2225         for (i = 0; i < event_data->NumEntries; i++) {
2226                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2227                 if (!handle)
2228                         continue;
2229                 phy_number = event_data->StartPhyNum + i;
2230                 reason_code = event_data->PHY[i].PhyStatus &
2231                     MPI2_EVENT_SAS_TOPO_RC_MASK;
2232                 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2233                         _scsih_block_io_device(ioc, handle);
2234         }
2235 }
2236
2237 /**
2238  * _scsih_check_topo_delete_events - sanity check on topo events
2239  * @ioc: per adapter object
2240  * @event_data: the event data payload
2241  *
2242  * This routine added to better handle cable breaker.
2243  *
2244  * This handles the case where driver recieves multiple expander
2245  * add and delete events in a single shot.  When there is a delete event
2246  * the routine will void any pending add events waiting in the event queue.
2247  *
2248  * Return nothing.
2249  */
2250 static void
2251 _scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2252     Mpi2EventDataSasTopologyChangeList_t *event_data)
2253 {
2254         struct fw_event_work *fw_event;
2255         Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2256         u16 expander_handle;
2257         struct _sas_node *sas_expander;
2258         unsigned long flags;
2259
2260         expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2261         if (expander_handle < ioc->sas_hba.num_phys) {
2262                 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2263                 return;
2264         }
2265
2266         if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2267          || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2268                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2269                 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
2270                     expander_handle);
2271                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2272                 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
2273         } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
2274                 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2275
2276         if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
2277                 return;
2278
2279         /* mark ignore flag for pending events */
2280         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2281         list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
2282                 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
2283                     fw_event->ignore)
2284                         continue;
2285                 local_event_data = fw_event->event_data;
2286                 if (local_event_data->ExpStatus ==
2287                     MPI2_EVENT_SAS_TOPO_ES_ADDED ||
2288                     local_event_data->ExpStatus ==
2289                     MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
2290                         if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
2291                             expander_handle) {
2292                                 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2293                                     "setting ignoring flag\n", ioc->name));
2294                                 fw_event->ignore = 1;
2295                         }
2296                 }
2297         }
2298         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2299 }
2300
2301 /**
2302  * _scsih_queue_rescan - queue a topology rescan from user context
2303  * @ioc: per adapter object
2304  *
2305  * Return nothing.
2306  */
2307 static void
2308 _scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
2309 {
2310         struct fw_event_work *fw_event;
2311
2312         if (ioc->wait_for_port_enable_to_complete)
2313                 return;
2314         fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2315         if (!fw_event)
2316                 return;
2317         fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
2318         fw_event->ioc = ioc;
2319         _scsih_fw_event_add(ioc, fw_event);
2320 }
2321
2322 /**
2323  * _scsih_flush_running_cmds - completing outstanding commands.
2324  * @ioc: per adapter object
2325  *
2326  * The flushing out of all pending scmd commands following host reset,
2327  * where all IO is dropped to the floor.
2328  *
2329  * Return nothing.
2330  */
2331 static void
2332 _scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
2333 {
2334         struct scsi_cmnd *scmd;
2335         u16 smid;
2336         u16 count = 0;
2337
2338         for (smid = 1; smid <= ioc->request_depth; smid++) {
2339                 scmd = _scsih_scsi_lookup_getclear(ioc, smid);
2340                 if (!scmd)
2341                         continue;
2342                 count++;
2343                 mpt2sas_base_free_smid(ioc, smid);
2344                 scsi_dma_unmap(scmd);
2345                 scmd->result = DID_RESET << 16;
2346                 scmd->scsi_done(scmd);
2347         }
2348         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
2349             ioc->name, count));
2350 }
2351
2352 /**
2353  * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
2354  * @ioc: per adapter object
2355  * @reset_phase: phase
2356  *
2357  * The handler for doing any required cleanup or initialization.
2358  *
2359  * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
2360  * MPT2_IOC_DONE_RESET
2361  *
2362  * Return nothing.
2363  */
2364 void
2365 mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
2366 {
2367         switch (reset_phase) {
2368         case MPT2_IOC_PRE_RESET:
2369                 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
2370                     "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
2371                 _scsih_fw_event_off(ioc);
2372                 break;
2373         case MPT2_IOC_AFTER_RESET:
2374                 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
2375                     "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
2376                 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
2377                         ioc->tm_cmds.status |= MPT2_CMD_RESET;
2378                         mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
2379                         complete(&ioc->tm_cmds.done);
2380                 }
2381                 _scsih_fw_event_on(ioc);
2382                 _scsih_flush_running_cmds(ioc);
2383                 break;
2384         case MPT2_IOC_DONE_RESET:
2385                 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
2386                     "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
2387                 _scsih_queue_rescan(ioc);
2388                 break;
2389         }
2390 }
2391
2392 /**
2393  * _scsih_setup_eedp - setup MPI request for EEDP transfer
2394  * @scmd: pointer to scsi command object
2395  * @mpi_request: pointer to the SCSI_IO reqest message frame
2396  *
2397  * Supporting protection 1 and 3.
2398  *
2399  * Returns nothing
2400  */
2401 static void
2402 _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
2403 {
2404         u16 eedp_flags;
2405         unsigned char prot_op = scsi_get_prot_op(scmd);
2406         unsigned char prot_type = scsi_get_prot_type(scmd);
2407
2408         if (prot_type == SCSI_PROT_DIF_TYPE0 ||
2409            prot_type == SCSI_PROT_DIF_TYPE2 ||
2410            prot_op == SCSI_PROT_NORMAL)
2411                 return;
2412
2413         if (prot_op ==  SCSI_PROT_READ_STRIP)
2414                 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
2415         else if (prot_op ==  SCSI_PROT_WRITE_INSERT)
2416                 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
2417         else
2418                 return;
2419
2420         mpi_request->EEDPBlockSize = scmd->device->sector_size;
2421
2422         switch (prot_type) {
2423         case SCSI_PROT_DIF_TYPE1:
2424
2425                 /*
2426                 * enable ref/guard checking
2427                 * auto increment ref tag
2428                 */
2429                 mpi_request->EEDPFlags = eedp_flags |
2430                     MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2431                     MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2432                     MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2433                 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
2434                     cpu_to_be32(scsi_get_lba(scmd));
2435
2436                 break;
2437
2438         case SCSI_PROT_DIF_TYPE3:
2439
2440                 /*
2441                 * enable guard checking
2442                 */
2443                 mpi_request->EEDPFlags = eedp_flags |
2444                     MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2445
2446                 break;
2447         }
2448 }
2449
2450 /**
2451  * _scsih_eedp_error_handling - return sense code for EEDP errors
2452  * @scmd: pointer to scsi command object
2453  * @ioc_status: ioc status
2454  *
2455  * Returns nothing
2456  */
2457 static void
2458 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
2459 {
2460         u8 ascq;
2461         u8 sk;
2462         u8 host_byte;
2463
2464         switch (ioc_status) {
2465         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2466                 ascq = 0x01;
2467                 break;
2468         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2469                 ascq = 0x02;
2470                 break;
2471         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2472                 ascq = 0x03;
2473                 break;
2474         default:
2475                 ascq = 0x00;
2476                 break;
2477         }
2478
2479         if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2480                 sk = ILLEGAL_REQUEST;
2481                 host_byte = DID_ABORT;
2482         } else {
2483                 sk = ABORTED_COMMAND;
2484                 host_byte = DID_OK;
2485         }
2486
2487         scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
2488         scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
2489             SAM_STAT_CHECK_CONDITION;
2490 }
2491
2492 /**
2493  * scsih_qcmd - main scsi request entry point
2494  * @scmd: pointer to scsi command object
2495  * @done: function pointer to be invoked on completion
2496  *
2497  * The callback index is set inside `ioc->scsi_io_cb_idx`.
2498  *
2499  * Returns 0 on success.  If there's a failure, return either:
2500  * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
2501  * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
2502  */
2503 static int
2504 scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
2505 {
2506         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2507         struct MPT2SAS_DEVICE *sas_device_priv_data;
2508         struct MPT2SAS_TARGET *sas_target_priv_data;
2509         Mpi2SCSIIORequest_t *mpi_request;
2510         u32 mpi_control;
2511         u16 smid;
2512         unsigned long flags;
2513
2514         scmd->scsi_done = done;
2515         sas_device_priv_data = scmd->device->hostdata;
2516         if (!sas_device_priv_data) {
2517                 scmd->result = DID_NO_CONNECT << 16;
2518                 scmd->scsi_done(scmd);
2519                 return 0;
2520         }
2521
2522         sas_target_priv_data = sas_device_priv_data->sas_target;
2523         if (!sas_target_priv_data || sas_target_priv_data->handle ==
2524             MPT2SAS_INVALID_DEVICE_HANDLE || sas_target_priv_data->deleted) {
2525                 scmd->result = DID_NO_CONNECT << 16;
2526                 scmd->scsi_done(scmd);
2527                 return 0;
2528         }
2529
2530         /* see if we are busy with task managment stuff */
2531         spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
2532         if (sas_target_priv_data->tm_busy ||
2533             ioc->shost_recovery || ioc->ioc_link_reset_in_progress) {
2534                 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
2535                 return SCSI_MLQUEUE_HOST_BUSY;
2536         }
2537         spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
2538
2539         if (scmd->sc_data_direction == DMA_FROM_DEVICE)
2540                 mpi_control = MPI2_SCSIIO_CONTROL_READ;
2541         else if (scmd->sc_data_direction == DMA_TO_DEVICE)
2542                 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
2543         else
2544                 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
2545
2546         /* set tags */
2547         if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
2548                 if (scmd->device->tagged_supported) {
2549                         if (scmd->device->ordered_tags)
2550                                 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
2551                         else
2552                                 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2553                 } else
2554 /* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
2555 /*                      mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
2556  */
2557                         mpi_control |= (0x500);
2558
2559         } else
2560                 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2561
2562         if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON))
2563                 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
2564
2565         smid = mpt2sas_base_get_smid(ioc, ioc->scsi_io_cb_idx);
2566         if (!smid) {
2567                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2568                     ioc->name, __func__);
2569                 goto out;
2570         }
2571         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2572         memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
2573         _scsih_setup_eedp(scmd, mpi_request);
2574         mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2575         if (sas_device_priv_data->sas_target->flags &
2576             MPT_TARGET_FLAGS_RAID_COMPONENT)
2577                 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
2578         else
2579                 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2580         mpi_request->DevHandle =
2581             cpu_to_le16(sas_device_priv_data->sas_target->handle);
2582         mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2583         mpi_request->Control = cpu_to_le32(mpi_control);
2584         mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
2585         mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
2586         mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
2587         mpi_request->SenseBufferLowAddress =
2588             (u32)mpt2sas_base_get_sense_buffer_dma(ioc, smid);
2589         mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
2590         mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
2591             MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
2592
2593         int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
2594             mpi_request->LUN);
2595         memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
2596
2597         if (!mpi_request->DataLength) {
2598                 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
2599         } else {
2600                 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
2601                         mpt2sas_base_free_smid(ioc, smid);
2602                         goto out;
2603                 }
2604         }
2605
2606         _scsih_scsi_lookup_set(ioc, smid, scmd);
2607         mpt2sas_base_put_smid_scsi_io(ioc, smid, 0,
2608             sas_device_priv_data->sas_target->handle);
2609         return 0;
2610
2611  out:
2612         return SCSI_MLQUEUE_HOST_BUSY;
2613 }
2614
2615 /**
2616  * _scsih_normalize_sense - normalize descriptor and fixed format sense data
2617  * @sense_buffer: sense data returned by target
2618  * @data: normalized skey/asc/ascq
2619  *
2620  * Return nothing.
2621  */
2622 static void
2623 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
2624 {
2625         if ((sense_buffer[0] & 0x7F) >= 0x72) {
2626                 /* descriptor format */
2627                 data->skey = sense_buffer[1] & 0x0F;
2628                 data->asc = sense_buffer[2];
2629                 data->ascq = sense_buffer[3];
2630         } else {
2631                 /* fixed format */
2632                 data->skey = sense_buffer[2] & 0x0F;
2633                 data->asc = sense_buffer[12];
2634                 data->ascq = sense_buffer[13];
2635         }
2636 }
2637
2638 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
2639 /**
2640  * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
2641  * @ioc: per adapter object
2642  * @scmd: pointer to scsi command object
2643  * @mpi_reply: reply mf payload returned from firmware
2644  *
2645  * scsi_status - SCSI Status code returned from target device
2646  * scsi_state - state info associated with SCSI_IO determined by ioc
2647  * ioc_status - ioc supplied status info
2648  *
2649  * Return nothing.
2650  */
2651 static void
2652 _scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
2653     Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
2654 {
2655         u32 response_info;
2656         u8 *response_bytes;
2657         u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
2658             MPI2_IOCSTATUS_MASK;
2659         u8 scsi_state = mpi_reply->SCSIState;
2660         u8 scsi_status = mpi_reply->SCSIStatus;
2661         char *desc_ioc_state = NULL;
2662         char *desc_scsi_status = NULL;
2663         char *desc_scsi_state = ioc->tmp_string;
2664
2665         switch (ioc_status) {
2666         case MPI2_IOCSTATUS_SUCCESS:
2667                 desc_ioc_state = "success";
2668                 break;
2669         case MPI2_IOCSTATUS_INVALID_FUNCTION:
2670                 desc_ioc_state = "invalid function";
2671                 break;
2672         case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
2673                 desc_ioc_state = "scsi recovered error";
2674                 break;
2675         case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
2676                 desc_ioc_state = "scsi invalid dev handle";
2677                 break;
2678         case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2679                 desc_ioc_state = "scsi device not there";
2680                 break;
2681         case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
2682                 desc_ioc_state = "scsi data overrun";
2683                 break;
2684         case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
2685                 desc_ioc_state = "scsi data underrun";
2686                 break;
2687         case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
2688                 desc_ioc_state = "scsi io data error";
2689                 break;
2690         case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2691                 desc_ioc_state = "scsi protocol error";
2692                 break;
2693         case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
2694                 desc_ioc_state = "scsi task terminated";
2695                 break;
2696         case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2697                 desc_ioc_state = "scsi residual mismatch";
2698                 break;
2699         case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2700                 desc_ioc_state = "scsi task mgmt failed";
2701                 break;
2702         case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
2703                 desc_ioc_state = "scsi ioc terminated";
2704                 break;
2705         case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
2706                 desc_ioc_state = "scsi ext terminated";
2707                 break;
2708         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2709                 desc_ioc_state = "eedp guard error";
2710                 break;
2711         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2712                 desc_ioc_state = "eedp ref tag error";
2713                 break;
2714         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2715                 desc_ioc_state = "eedp app tag error";
2716                 break;
2717         default:
2718                 desc_ioc_state = "unknown";
2719                 break;
2720         }
2721
2722         switch (scsi_status) {
2723         case MPI2_SCSI_STATUS_GOOD:
2724                 desc_scsi_status = "good";
2725                 break;
2726         case MPI2_SCSI_STATUS_CHECK_CONDITION:
2727                 desc_scsi_status = "check condition";
2728                 break;
2729         case MPI2_SCSI_STATUS_CONDITION_MET:
2730                 desc_scsi_status = "condition met";
2731                 break;
2732         case MPI2_SCSI_STATUS_BUSY:
2733                 desc_scsi_status = "busy";
2734                 break;
2735         case MPI2_SCSI_STATUS_INTERMEDIATE:
2736                 desc_scsi_status = "intermediate";
2737                 break;
2738         case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
2739                 desc_scsi_status = "intermediate condmet";
2740                 break;
2741         case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
2742                 desc_scsi_status = "reservation conflict";
2743                 break;
2744         case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
2745                 desc_scsi_status = "command terminated";
2746                 break;
2747         case MPI2_SCSI_STATUS_TASK_SET_FULL:
2748                 desc_scsi_status = "task set full";
2749                 break;
2750         case MPI2_SCSI_STATUS_ACA_ACTIVE:
2751                 desc_scsi_status = "aca active";
2752                 break;
2753         case MPI2_SCSI_STATUS_TASK_ABORTED:
2754                 desc_scsi_status = "task aborted";
2755                 break;
2756         default:
2757                 desc_scsi_status = "unknown";
2758                 break;
2759         }
2760
2761         desc_scsi_state[0] = '\0';
2762         if (!scsi_state)
2763                 desc_scsi_state = " ";
2764         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
2765                 strcat(desc_scsi_state, "response info ");
2766         if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
2767                 strcat(desc_scsi_state, "state terminated ");
2768         if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
2769                 strcat(desc_scsi_state, "no status ");
2770         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
2771                 strcat(desc_scsi_state, "autosense failed ");
2772         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
2773                 strcat(desc_scsi_state, "autosense valid ");
2774
2775         scsi_print_command(scmd);
2776         printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
2777             "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
2778             le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
2779                 ioc_status, smid);
2780         printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
2781             "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
2782             scsi_get_resid(scmd));
2783         printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
2784             "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
2785             le32_to_cpu(mpi_reply->TransferCount), scmd->result);
2786         printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
2787             "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
2788             scsi_status, desc_scsi_state, scsi_state);
2789
2790         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2791                 struct sense_info data;
2792                 _scsih_normalize_sense(scmd->sense_buffer, &data);
2793                 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
2794                     "[0x%02x,0x%02x,0x%02x]\n", ioc->name, data.skey,
2795                     data.asc, data.ascq);
2796         }
2797
2798         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
2799                 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
2800                 response_bytes = (u8 *)&response_info;
2801                 _scsih_response_code(ioc, response_bytes[3]);
2802         }
2803 }
2804 #endif
2805
2806 /**
2807  * _scsih_smart_predicted_fault - illuminate Fault LED
2808  * @ioc: per adapter object
2809  * @handle: device handle
2810  *
2811  * Return nothing.
2812  */
2813 static void
2814 _scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2815 {
2816         Mpi2SepReply_t mpi_reply;
2817         Mpi2SepRequest_t mpi_request;
2818         struct scsi_target *starget;
2819         struct MPT2SAS_TARGET *sas_target_priv_data;
2820         Mpi2EventNotificationReply_t *event_reply;
2821         Mpi2EventDataSasDeviceStatusChange_t *event_data;
2822         struct _sas_device *sas_device;
2823         ssize_t sz;
2824         unsigned long flags;
2825
2826         /* only handle non-raid devices */
2827         spin_lock_irqsave(&ioc->sas_device_lock, flags);
2828         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2829         if (!sas_device) {
2830                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2831                 return;
2832         }
2833         starget = sas_device->starget;
2834         sas_target_priv_data = starget->hostdata;
2835
2836         if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
2837            ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
2838                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2839                 return;
2840         }
2841         starget_printk(KERN_WARNING, starget, "predicted fault\n");
2842         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2843
2844         if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
2845                 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
2846                 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
2847                 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
2848                 mpi_request.SlotStatus =
2849                     MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
2850                 mpi_request.DevHandle = cpu_to_le16(handle);
2851                 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
2852                 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
2853                     &mpi_request)) != 0) {
2854                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2855                             ioc->name, __FILE__, __LINE__, __func__);
2856                         return;
2857                 }
2858
2859                 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
2860                         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2861                             "enclosure_processor: ioc_status (0x%04x), "
2862                             "loginfo(0x%08x)\n", ioc->name,
2863                             le16_to_cpu(mpi_reply.IOCStatus),
2864                             le32_to_cpu(mpi_reply.IOCLogInfo)));
2865                         return;
2866                 }
2867         }
2868
2869         /* insert into event log */
2870         sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
2871              sizeof(Mpi2EventDataSasDeviceStatusChange_t);
2872         event_reply = kzalloc(sz, GFP_KERNEL);
2873         if (!event_reply) {
2874                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2875                     ioc->name, __FILE__, __LINE__, __func__);
2876                 return;
2877         }
2878
2879         event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
2880         event_reply->Event =
2881             cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
2882         event_reply->MsgLength = sz/4;
2883         event_reply->EventDataLength =
2884             cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
2885         event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
2886             event_reply->EventData;
2887         event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
2888         event_data->ASC = 0x5D;
2889         event_data->DevHandle = cpu_to_le16(handle);
2890         event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
2891         mpt2sas_ctl_add_to_event_log(ioc, event_reply);
2892         kfree(event_reply);
2893 }
2894
2895 /**
2896  * scsih_io_done - scsi request callback
2897  * @ioc: per adapter object
2898  * @smid: system request message index
2899  * @VF_ID: virtual function id
2900  * @reply: reply message frame(lower 32bit addr)
2901  *
2902  * Callback handler when using scsih_qcmd.
2903  *
2904  * Return nothing.
2905  */
2906 static void
2907 scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
2908 {
2909         Mpi2SCSIIORequest_t *mpi_request;
2910         Mpi2SCSIIOReply_t *mpi_reply;
2911         struct scsi_cmnd *scmd;
2912         u16 ioc_status;
2913         u32 xfer_cnt;
2914         u8 scsi_state;
2915         u8 scsi_status;
2916         u32 log_info;
2917         struct MPT2SAS_DEVICE *sas_device_priv_data;
2918         u32 response_code;
2919
2920         mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2921         scmd = _scsih_scsi_lookup_getclear(ioc, smid);
2922         if (scmd == NULL)
2923                 return;
2924
2925         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2926
2927         if (mpi_reply == NULL) {
2928                 scmd->result = DID_OK << 16;
2929                 goto out;
2930         }
2931
2932         sas_device_priv_data = scmd->device->hostdata;
2933         if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
2934              sas_device_priv_data->sas_target->deleted) {
2935                 scmd->result = DID_NO_CONNECT << 16;
2936                 goto out;
2937         }
2938
2939         /* turning off TLR */
2940         if (!sas_device_priv_data->tlr_snoop_check) {
2941                 sas_device_priv_data->tlr_snoop_check++;
2942                 if (sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) {
2943                         response_code = (le32_to_cpu(mpi_reply->ResponseInfo)
2944                             >> 24);
2945                         if (response_code ==
2946                             MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
2947                                 sas_device_priv_data->flags &=
2948                                     ~MPT_DEVICE_TLR_ON;
2949                 }
2950         }
2951
2952         xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
2953         scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
2954         ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
2955         if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
2956                 log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
2957         else
2958                 log_info = 0;
2959         ioc_status &= MPI2_IOCSTATUS_MASK;
2960         scsi_state = mpi_reply->SCSIState;
2961         scsi_status = mpi_reply->SCSIStatus;
2962
2963         if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
2964             (scsi_status == MPI2_SCSI_STATUS_BUSY ||
2965              scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
2966              scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
2967                 ioc_status = MPI2_IOCSTATUS_SUCCESS;
2968         }
2969
2970         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2971                 struct sense_info data;
2972                 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
2973                     smid);
2974                 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
2975                     le32_to_cpu(mpi_reply->SenseCount));
2976                 memcpy(scmd->sense_buffer, sense_data, sz);
2977                 _scsih_normalize_sense(scmd->sense_buffer, &data);
2978                 /* failure prediction threshold exceeded */
2979                 if (data.asc == 0x5D)
2980                         _scsih_smart_predicted_fault(ioc,
2981                             le16_to_cpu(mpi_reply->DevHandle));
2982         }
2983
2984         switch (ioc_status) {
2985         case MPI2_IOCSTATUS_BUSY:
2986         case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
2987                 scmd->result = SAM_STAT_BUSY;
2988                 break;
2989
2990         case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2991                 scmd->result = DID_NO_CONNECT << 16;
2992                 break;
2993
2994         case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
2995                 if (sas_device_priv_data->block) {
2996                         scmd->result = (DID_BUS_BUSY << 16);
2997                         break;
2998                 }
2999
3000         case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3001         case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3002                 scmd->result = DID_RESET << 16;
3003                 break;
3004
3005         case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3006                 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3007                         scmd->result = DID_SOFT_ERROR << 16;
3008                 else
3009                         scmd->result = (DID_OK << 16) | scsi_status;
3010                 break;
3011
3012         case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3013                 scmd->result = (DID_OK << 16) | scsi_status;
3014
3015                 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3016                         break;
3017
3018                 if (xfer_cnt < scmd->underflow) {
3019                         if (scsi_status == SAM_STAT_BUSY)
3020                                 scmd->result = SAM_STAT_BUSY;
3021                         else
3022                                 scmd->result = DID_SOFT_ERROR << 16;
3023                 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3024                      MPI2_SCSI_STATE_NO_SCSI_STATUS))
3025                         scmd->result = DID_SOFT_ERROR << 16;
3026                 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3027                         scmd->result = DID_RESET << 16;
3028                 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3029                         mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3030                         mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3031                         scmd->result = (DRIVER_SENSE << 24) |
3032                             SAM_STAT_CHECK_CONDITION;
3033                         scmd->sense_buffer[0] = 0x70;
3034                         scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3035                         scmd->sense_buffer[12] = 0x20;
3036                         scmd->sense_buffer[13] = 0;
3037                 }
3038                 break;
3039
3040         case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3041                 scsi_set_resid(scmd, 0);
3042         case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3043         case MPI2_IOCSTATUS_SUCCESS:
3044                 scmd->result = (DID_OK << 16) | scsi_status;
3045                 if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3046                      MPI2_SCSI_STATE_NO_SCSI_STATUS))
3047                         scmd->result = DID_SOFT_ERROR << 16;
3048                 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3049                         scmd->result = DID_RESET << 16;
3050                 break;
3051
3052         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3053         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3054         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3055                 _scsih_eedp_error_handling(scmd, ioc_status);
3056                 break;
3057         case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3058         case MPI2_IOCSTATUS_INVALID_FUNCTION:
3059         case MPI2_IOCSTATUS_INVALID_SGL:
3060         case MPI2_IOCSTATUS_INTERNAL_ERROR:
3061         case MPI2_IOCSTATUS_INVALID_FIELD:
3062         case MPI2_IOCSTATUS_INVALID_STATE:
3063         case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3064         case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3065         default:
3066                 scmd->result = DID_SOFT_ERROR << 16;
3067                 break;
3068
3069         }
3070
3071 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3072         if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3073                 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3074 #endif
3075
3076  out:
3077         scsi_dma_unmap(scmd);
3078         scmd->scsi_done(scmd);
3079 }
3080
3081 /**
3082  * _scsih_link_change - process phy link changes
3083  * @ioc: per adapter object
3084  * @handle: phy handle
3085  * @attached_handle: valid for devices attached to link
3086  * @phy_number: phy number
3087  * @link_rate: new link rate
3088  * Context: user.
3089  *
3090  * Return nothing.
3091  */
3092 static void
3093 _scsih_link_change(struct MPT2SAS_ADAPTER *ioc, u16 handle, u16 attached_handle,
3094    u8 phy_number, u8 link_rate)
3095 {
3096         mpt2sas_transport_update_phy_link_change(ioc, handle, attached_handle,
3097             phy_number, link_rate);
3098 }
3099
3100 /**
3101  * _scsih_sas_host_refresh - refreshing sas host object contents
3102  * @ioc: per adapter object
3103  * @update: update link information
3104  * Context: user
3105  *
3106  * During port enable, fw will send topology events for every device. Its
3107  * possible that the handles may change from the previous setting, so this
3108  * code keeping handles updating if changed.
3109  *
3110  * Return nothing.
3111  */
3112 static void
3113 _scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc, u8 update)
3114 {
3115         u16 sz;
3116         u16 ioc_status;
3117         int i;
3118         Mpi2ConfigReply_t mpi_reply;
3119         Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3120
3121         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3122             "updating handles for sas_host(0x%016llx)\n",
3123             ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3124
3125         sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3126             * sizeof(Mpi2SasIOUnit0PhyData_t));
3127         sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3128         if (!sas_iounit_pg0) {
3129                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3130                     ioc->name, __FILE__, __LINE__, __func__);
3131                 return;
3132         }
3133         if (!(mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3134             sas_iounit_pg0, sz))) {
3135                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3136                     MPI2_IOCSTATUS_MASK;
3137                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3138                         goto out;
3139                 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3140                         ioc->sas_hba.phy[i].handle =
3141                             le16_to_cpu(sas_iounit_pg0->PhyData[i].
3142                                 ControllerDevHandle);
3143                         if (update)
3144                                 _scsih_link_change(ioc,
3145                                     ioc->sas_hba.phy[i].handle,
3146                                     le16_to_cpu(sas_iounit_pg0->PhyData[i].
3147                                     AttachedDevHandle), i,
3148                                     sas_iounit_pg0->PhyData[i].
3149                                     NegotiatedLinkRate >> 4);
3150                 }
3151         }
3152
3153  out:
3154         kfree(sas_iounit_pg0);
3155 }
3156
3157 /**
3158  * _scsih_sas_host_add - create sas host object
3159  * @ioc: per adapter object
3160  *
3161  * Creating host side data object, stored in ioc->sas_hba
3162  *
3163  * Return nothing.
3164  */
3165 static void
3166 _scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
3167 {
3168         int i;
3169         Mpi2ConfigReply_t mpi_reply;
3170         Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3171         Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3172         Mpi2SasPhyPage0_t phy_pg0;
3173         Mpi2SasDevicePage0_t sas_device_pg0;
3174         Mpi2SasEnclosurePage0_t enclosure_pg0;
3175         u16 ioc_status;
3176         u16 sz;
3177         u16 device_missing_delay;
3178
3179         mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
3180         if (!ioc->sas_hba.num_phys) {
3181                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3182                     ioc->name, __FILE__, __LINE__, __func__);
3183                 return;
3184         }
3185
3186         /* sas_iounit page 0 */
3187         sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
3188             sizeof(Mpi2SasIOUnit0PhyData_t));
3189         sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3190         if (!sas_iounit_pg0) {
3191                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3192                     ioc->name, __FILE__, __LINE__, __func__);
3193                 return;
3194         }
3195         if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3196             sas_iounit_pg0, sz))) {
3197                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3198                     ioc->name, __FILE__, __LINE__, __func__);
3199                 goto out;
3200         }
3201         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3202             MPI2_IOCSTATUS_MASK;
3203         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3204                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3205                     ioc->name, __FILE__, __LINE__, __func__);
3206                 goto out;
3207         }
3208
3209         /* sas_iounit page 1 */
3210         sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
3211             sizeof(Mpi2SasIOUnit1PhyData_t));
3212         sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3213         if (!sas_iounit_pg1) {
3214                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3215                     ioc->name, __FILE__, __LINE__, __func__);
3216                 goto out;
3217         }
3218         if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3219             sas_iounit_pg1, sz))) {
3220                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3221                     ioc->name, __FILE__, __LINE__, __func__);
3222                 goto out;
3223         }
3224         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3225             MPI2_IOCSTATUS_MASK;
3226         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3227                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3228                     ioc->name, __FILE__, __LINE__, __func__);
3229                 goto out;
3230         }
3231
3232         ioc->io_missing_delay =
3233             le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
3234         device_missing_delay =
3235             le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
3236         if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3237                 ioc->device_missing_delay = (device_missing_delay &
3238                     MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3239         else
3240                 ioc->device_missing_delay = device_missing_delay &
3241                     MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3242
3243         ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
3244         ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
3245             sizeof(struct _sas_phy), GFP_KERNEL);
3246         if (!ioc->sas_hba.phy) {
3247                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3248                     ioc->name, __FILE__, __LINE__, __func__);
3249                 goto out;
3250         }
3251         for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3252                 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
3253                     i))) {
3254                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3255                             ioc->name, __FILE__, __LINE__, __func__);
3256                         goto out;
3257                 }
3258                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3259                     MPI2_IOCSTATUS_MASK;
3260                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3261                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3262                             ioc->name, __FILE__, __LINE__, __func__);
3263                         goto out;
3264                 }
3265                 ioc->sas_hba.phy[i].handle =
3266                     le16_to_cpu(sas_iounit_pg0->PhyData[i].ControllerDevHandle);
3267                 ioc->sas_hba.phy[i].phy_id = i;
3268                 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
3269                     phy_pg0, ioc->sas_hba.parent_dev);
3270         }
3271         if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3272             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.phy[0].handle))) {
3273                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3274                     ioc->name, __FILE__, __LINE__, __func__);
3275                 goto out;
3276         }
3277         ioc->sas_hba.handle = le16_to_cpu(sas_device_pg0.DevHandle);
3278         ioc->sas_hba.enclosure_handle =
3279             le16_to_cpu(sas_device_pg0.EnclosureHandle);
3280         ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3281         printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
3282             "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
3283             (unsigned long long) ioc->sas_hba.sas_address,
3284             ioc->sas_hba.num_phys) ;
3285
3286         if (ioc->sas_hba.enclosure_handle) {
3287                 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3288                     &enclosure_pg0,
3289                    MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3290                    ioc->sas_hba.enclosure_handle))) {
3291                         ioc->sas_hba.enclosure_logical_id =
3292                             le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3293                 }
3294         }
3295
3296  out:
3297         kfree(sas_iounit_pg1);
3298         kfree(sas_iounit_pg0);
3299 }
3300
3301 /**
3302  * _scsih_expander_add -  creating expander object
3303  * @ioc: per adapter object
3304  * @handle: expander handle
3305  *
3306  * Creating expander object, stored in ioc->sas_expander_list.
3307  *
3308  * Return 0 for success, else error.
3309  */
3310 static int
3311 _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3312 {
3313         struct _sas_node *sas_expander;
3314         Mpi2ConfigReply_t mpi_reply;
3315         Mpi2ExpanderPage0_t expander_pg0;
3316         Mpi2ExpanderPage1_t expander_pg1;
3317         Mpi2SasEnclosurePage0_t enclosure_pg0;
3318         u32 ioc_status;
3319         u16 parent_handle;
3320         __le64 sas_address;
3321         int i;
3322         unsigned long flags;
3323         struct _sas_port *mpt2sas_port;
3324         int rc = 0;
3325
3326         if (!handle)
3327                 return -1;
3328
3329         if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
3330             MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
3331                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3332                     ioc->name, __FILE__, __LINE__, __func__);
3333                 return -1;
3334         }
3335
3336         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3337             MPI2_IOCSTATUS_MASK;
3338         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3339                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3340                     ioc->name, __FILE__, __LINE__, __func__);
3341                 return -1;
3342         }
3343
3344         /* handle out of order topology events */
3345         parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
3346         if (parent_handle >= ioc->sas_hba.num_phys) {
3347                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3348                 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3349                     parent_handle);
3350                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3351                 if (!sas_expander) {
3352                         rc = _scsih_expander_add(ioc, parent_handle);
3353                         if (rc != 0)
3354                                 return rc;
3355                 }
3356         }
3357
3358         sas_address = le64_to_cpu(expander_pg0.SASAddress);
3359
3360         spin_lock_irqsave(&ioc->sas_node_lock, flags);
3361         sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3362             sas_address);
3363         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3364
3365         if (sas_expander)
3366                 return 0;
3367
3368         sas_expander = kzalloc(sizeof(struct _sas_node),
3369             GFP_KERNEL);
3370         if (!sas_expander) {
3371                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3372                     ioc->name, __FILE__, __LINE__, __func__);
3373                 return -1;
3374         }
3375
3376         sas_expander->handle = handle;
3377         sas_expander->num_phys = expander_pg0.NumPhys;
3378         sas_expander->parent_handle = parent_handle;
3379         sas_expander->enclosure_handle =
3380             le16_to_cpu(expander_pg0.EnclosureHandle);
3381         sas_expander->sas_address = sas_address;
3382
3383         printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
3384             " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
3385             handle, sas_expander->parent_handle, (unsigned long long)
3386             sas_expander->sas_address, sas_expander->num_phys);
3387
3388         if (!sas_expander->num_phys)
3389                 goto out_fail;
3390         sas_expander->phy = kcalloc(sas_expander->num_phys,
3391             sizeof(struct _sas_phy), GFP_KERNEL);
3392         if (!sas_expander->phy) {
3393                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3394                     ioc->name, __FILE__, __LINE__, __func__);
3395                 rc = -1;
3396                 goto out_fail;
3397         }
3398
3399         INIT_LIST_HEAD(&sas_expander->sas_port_list);
3400         mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
3401             sas_expander->parent_handle);
3402         if (!mpt2sas_port) {
3403                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3404                     ioc->name, __FILE__, __LINE__, __func__);
3405                 rc = -1;
3406                 goto out_fail;
3407         }
3408         sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
3409
3410         for (i = 0 ; i < sas_expander->num_phys ; i++) {
3411                 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
3412                     &expander_pg1, i, handle))) {
3413                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3414                             ioc->name, __FILE__, __LINE__, __func__);
3415                         continue;
3416                 }
3417                 sas_expander->phy[i].handle = handle;
3418                 sas_expander->phy[i].phy_id = i;
3419                 mpt2sas_transport_add_expander_phy(ioc, &sas_expander->phy[i],
3420                     expander_pg1, sas_expander->parent_dev);
3421         }
3422
3423         if (sas_expander->enclosure_handle) {
3424                 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3425                     &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3426                    sas_expander->enclosure_handle))) {
3427                         sas_expander->enclosure_logical_id =
3428                             le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3429                 }
3430         }
3431
3432         _scsih_expander_node_add(ioc, sas_expander);
3433          return 0;
3434
3435  out_fail:
3436
3437         if (sas_expander)
3438                 kfree(sas_expander->phy);
3439         kfree(sas_expander);
3440         return rc;
3441 }
3442
3443 /**
3444  * _scsih_expander_remove - removing expander object
3445  * @ioc: per adapter object
3446  * @handle: expander handle
3447  *
3448  * Return nothing.
3449  */
3450 static void
3451 _scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3452 {
3453         struct _sas_node *sas_expander;
3454         unsigned long flags;
3455
3456         spin_lock_irqsave(&ioc->sas_node_lock, flags);
3457         sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc, handle);
3458         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3459         _scsih_expander_node_remove(ioc, sas_expander);
3460 }
3461
3462 /**
3463  * _scsih_add_device -  creating sas device object
3464  * @ioc: per adapter object
3465  * @handle: sas device handle
3466  * @phy_num: phy number end device attached to
3467  * @is_pd: is this hidden raid component
3468  *
3469  * Creating end device object, stored in ioc->sas_device_list.
3470  *
3471  * Returns 0 for success, non-zero for failure.
3472  */
3473 static int
3474 _scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
3475 {
3476         Mpi2ConfigReply_t mpi_reply;
3477         Mpi2SasDevicePage0_t sas_device_pg0;
3478         Mpi2SasEnclosurePage0_t enclosure_pg0;
3479         struct _sas_device *sas_device;
3480         u32 ioc_status;
3481         __le64 sas_address;
3482         u32 device_info;
3483         unsigned long flags;
3484
3485         if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3486             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
3487                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3488                     ioc->name, __FILE__, __LINE__, __func__);
3489                 return -1;
3490         }
3491
3492         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3493             MPI2_IOCSTATUS_MASK;
3494         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3495                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3496                     ioc->name, __FILE__, __LINE__, __func__);
3497                 return -1;
3498         }
3499
3500         /* check if device is present */
3501         if (!(le16_to_cpu(sas_device_pg0.Flags) &
3502             MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
3503                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3504                     ioc->name, __FILE__, __LINE__, __func__);
3505                 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
3506                     ioc->name, le16_to_cpu(sas_device_pg0.Flags));
3507                 return -1;
3508         }
3509
3510         /* check if there were any issus with discovery */
3511         if (sas_device_pg0.AccessStatus ==
3512             MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED) {
3513                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3514                     ioc->name, __FILE__, __LINE__, __func__);
3515                 printk(MPT2SAS_ERR_FMT "AccessStatus = 0x%02x\n",
3516                     ioc->name, sas_device_pg0.AccessStatus);
3517                 return -1;
3518         }
3519
3520         /* check if this is end device */
3521         device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
3522         if (!(_scsih_is_end_device(device_info))) {
3523                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3524                     ioc->name, __FILE__, __LINE__, __func__);
3525                 return -1;
3526         }
3527
3528         sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3529
3530         spin_lock_irqsave(&ioc->sas_device_lock, flags);
3531         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3532             sas_address);
3533         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3534
3535         if (sas_device) {
3536                 _scsih_ublock_io_device(ioc, handle);
3537                 return 0;
3538         }
3539
3540         sas_device = kzalloc(sizeof(struct _sas_device),
3541             GFP_KERNEL);
3542         if (!sas_device) {
3543                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3544                     ioc->name, __FILE__, __LINE__, __func__);
3545                 return -1;
3546         }
3547
3548         sas_device->handle = handle;
3549         sas_device->parent_handle =
3550             le16_to_cpu(sas_device_pg0.ParentDevHandle);
3551         sas_device->enclosure_handle =
3552             le16_to_cpu(sas_device_pg0.EnclosureHandle);
3553         sas_device->slot =
3554             le16_to_cpu(sas_device_pg0.Slot);
3555         sas_device->device_info = device_info;
3556         sas_device->sas_address = sas_address;
3557         sas_device->hidden_raid_component = is_pd;
3558
3559         /* get enclosure_logical_id */
3560         if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply, &enclosure_pg0,
3561            MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3562            sas_device->enclosure_handle))) {
3563                 sas_device->enclosure_logical_id =
3564                     le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3565         }
3566
3567         /* get device name */
3568         sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
3569
3570         if (ioc->wait_for_port_enable_to_complete)
3571                 _scsih_sas_device_init_add(ioc, sas_device);
3572         else
3573                 _scsih_sas_device_add(ioc, sas_device);
3574
3575         return 0;
3576 }
3577
3578 /**
3579  * _scsih_remove_device -  removing sas device object
3580  * @ioc: per adapter object
3581  * @handle: sas device handle
3582  *
3583  * Return nothing.
3584  */
3585 static void
3586 _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3587 {
3588         struct MPT2SAS_TARGET *sas_target_priv_data;
3589         struct _sas_device *sas_device;
3590         unsigned long flags;
3591         Mpi2SasIoUnitControlReply_t mpi_reply;
3592         Mpi2SasIoUnitControlRequest_t mpi_request;
3593         u16 device_handle;
3594
3595         /* lookup sas_device */
3596         spin_lock_irqsave(&ioc->sas_device_lock, flags);
3597         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3598         if (!sas_device) {
3599                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3600                 return;
3601         }
3602
3603         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle"
3604             "(0x%04x)\n", ioc->name, __func__, handle));
3605
3606         if (sas_device->starget && sas_device->starget->hostdata) {
3607                 sas_target_priv_data = sas_device->starget->hostdata;
3608                 sas_target_priv_data->deleted = 1;
3609         }
3610         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3611
3612         if (ioc->remove_host)
3613                 goto out;
3614
3615         /* Target Reset to flush out all the outstanding IO */
3616         device_handle = (sas_device->hidden_raid_component) ?
3617             sas_device->volume_handle : handle;
3618         if (device_handle) {
3619                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
3620                     "handle(0x%04x)\n", ioc->name, device_handle));
3621                 mutex_lock(&ioc->tm_cmds.mutex);
3622                 mpt2sas_scsih_issue_tm(ioc, device_handle, 0,
3623                     MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
3624                 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
3625                 mutex_unlock(&ioc->tm_cmds.mutex);
3626                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
3627                     "done: handle(0x%04x)\n", ioc->name, device_handle));
3628         }
3629
3630         /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
3631         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: handle"
3632             "(0x%04x)\n", ioc->name, handle));
3633         memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3634         mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3635         mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3636         mpi_request.DevHandle = handle;
3637         mpi_request.VF_ID = 0;
3638         if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
3639             &mpi_request)) != 0) {
3640                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3641                     ioc->name, __FILE__, __LINE__, __func__);
3642         }
3643
3644         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: ioc_status"
3645             "(0x%04x), loginfo(0x%08x)\n", ioc->name,
3646             le16_to_cpu(mpi_reply.IOCStatus),
3647             le32_to_cpu(mpi_reply.IOCLogInfo)));
3648
3649  out:
3650         mpt2sas_transport_port_remove(ioc, sas_device->sas_address,
3651             sas_device->parent_handle);
3652
3653         printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
3654             "(0x%016llx)\n", ioc->name, sas_device->handle,
3655             (unsigned long long) sas_device->sas_address);
3656         _scsih_sas_device_remove(ioc, sas_device);
3657
3658         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle"
3659             "(0x%04x)\n", ioc->name, __func__, handle));
3660 }
3661
3662 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3663 /**
3664  * _scsih_sas_topology_change_event_debug - debug for topology event
3665  * @ioc: per adapter object
3666  * @event_data: event data payload
3667  * Context: user.
3668  */
3669 static void
3670 _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3671     Mpi2EventDataSasTopologyChangeList_t *event_data)
3672 {
3673         int i;
3674         u16 handle;
3675         u16 reason_code;
3676         u8 phy_number;
3677         char *status_str = NULL;
3678         char link_rate[25];
3679
3680         switch (event_data->ExpStatus) {
3681         case MPI2_EVENT_SAS_TOPO_ES_ADDED:
3682                 status_str = "add";
3683                 break;
3684         case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
3685                 status_str = "remove";
3686                 break;
3687         case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
3688                 status_str =  "responding";
3689                 break;
3690         case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
3691                 status_str = "remove delay";
3692                 break;
3693         default:
3694                 status_str = "unknown status";
3695                 break;
3696         }
3697         printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
3698             ioc->name, status_str);
3699         printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
3700             "start_phy(%02d), count(%d)\n",
3701             le16_to_cpu(event_data->ExpanderDevHandle),
3702             le16_to_cpu(event_data->EnclosureHandle),
3703             event_data->StartPhyNum, event_data->NumEntries);
3704         for (i = 0; i < event_data->NumEntries; i++) {
3705                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3706                 if (!handle)
3707                         continue;
3708                 phy_number = event_data->StartPhyNum + i;
3709                 reason_code = event_data->PHY[i].PhyStatus &
3710                     MPI2_EVENT_SAS_TOPO_RC_MASK;
3711                 switch (reason_code) {
3712                 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
3713                         snprintf(link_rate, 25, ": add, link(0x%02x)",
3714                             (event_data->PHY[i].LinkRate >> 4));
3715                         status_str = link_rate;
3716                         break;
3717                 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
3718                         status_str = ": remove";
3719                         break;
3720                 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
3721                         status_str = ": remove_delay";
3722                         break;
3723                 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
3724                         snprintf(link_rate, 25, ": link(0x%02x)",
3725                             (event_data->PHY[i].LinkRate >> 4));
3726                         status_str = link_rate;
3727                         break;
3728                 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
3729                         status_str = ": responding";
3730                         break;
3731                 default:
3732                         status_str = ": unknown";
3733                         break;
3734                 }
3735                 printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x)%s\n",
3736                     phy_number, handle, status_str);
3737         }
3738 }
3739 #endif
3740
3741 /**
3742  * _scsih_sas_topology_change_event - handle topology changes
3743  * @ioc: per adapter object
3744  * @VF_ID:
3745  * @event_data: event data payload
3746  * fw_event:
3747  * Context: user.
3748  *
3749  */
3750 static void
3751 _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
3752     Mpi2EventDataSasTopologyChangeList_t *event_data,
3753     struct fw_event_work *fw_event)
3754 {
3755         int i;
3756         u16 parent_handle, handle;
3757         u16 reason_code;
3758         u8 phy_number;
3759         struct _sas_node *sas_expander;
3760         unsigned long flags;
3761         u8 link_rate_;
3762
3763 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3764         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
3765                 _scsih_sas_topology_change_event_debug(ioc, event_data);
3766 #endif
3767
3768         if (!ioc->sas_hba.num_phys)
3769                 _scsih_sas_host_add(ioc);
3770         else
3771                 _scsih_sas_host_refresh(ioc, 0);
3772
3773         if (fw_event->ignore) {
3774                 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
3775                     "event\n", ioc->name));
3776                 return;
3777         }
3778
3779         parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3780
3781         /* handle expander add */
3782         if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
3783                 if (_scsih_expander_add(ioc, parent_handle) != 0)
3784                         return;
3785
3786         /* handle siblings events */
3787         for (i = 0; i < event_data->NumEntries; i++) {
3788                 if (fw_event->ignore) {
3789                         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
3790                             "expander event\n", ioc->name));
3791                         return;
3792                 }
3793                 if (event_data->PHY[i].PhyStatus &
3794                     MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
3795                         continue;
3796                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3797                 if (!handle)
3798                         continue;
3799                 phy_number = event_data->StartPhyNum + i;
3800                 reason_code = event_data->PHY[i].PhyStatus &
3801                     MPI2_EVENT_SAS_TOPO_RC_MASK;
3802                 link_rate_ = event_data->PHY[i].LinkRate >> 4;
3803                 switch (reason_code) {
3804                 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
3805                 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
3806                         if (!parent_handle) {
3807                                 if (phy_number < ioc->sas_hba.num_phys)
3808                                         _scsih_link_change(ioc,
3809                                            ioc->sas_hba.phy[phy_number].handle,
3810                                            handle, phy_number, link_rate_);
3811                         } else {
3812                                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3813                                 sas_expander =
3814                                     mpt2sas_scsih_expander_find_by_handle(ioc,
3815                                         parent_handle);
3816                                 spin_unlock_irqrestore(&ioc->sas_node_lock,
3817                                     flags);
3818                                 if (sas_expander) {
3819                                         if (phy_number < sas_expander->num_phys)
3820                                                 _scsih_link_change(ioc,
3821                                                    sas_expander->
3822                                                    phy[phy_number].handle,
3823                                                    handle, phy_number,
3824                                                    link_rate_);
3825                                 }
3826                         }
3827                         if (reason_code == MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED) {
3828                                 if (link_rate_ >= MPI2_SAS_NEG_LINK_RATE_1_5)
3829                                         _scsih_ublock_io_device(ioc, handle);
3830                         }
3831                         if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED) {
3832                                 if (link_rate_ < MPI2_SAS_NEG_LINK_RATE_1_5)
3833                                         break;
3834                                 _scsih_add_device(ioc, handle, phy_number, 0);
3835                         }
3836                         break;
3837                 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
3838                         _scsih_remove_device(ioc, handle);
3839                         break;
3840                 }
3841         }
3842
3843         /* handle expander removal */
3844         if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3845                 _scsih_expander_remove(ioc, parent_handle);
3846
3847 }
3848
3849 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3850 /**
3851  * _scsih_sas_device_status_change_event_debug - debug for device event
3852  * @event_data: event data payload
3853  * Context: user.
3854  *
3855  * Return nothing.
3856  */
3857 static void
3858 _scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3859     Mpi2EventDataSasDeviceStatusChange_t *event_data)
3860 {
3861         char *reason_str = NULL;
3862
3863         switch (event_data->ReasonCode) {
3864         case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
3865                 reason_str = "smart data";
3866                 break;
3867         case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
3868                 reason_str = "unsupported device discovered";
3869                 break;
3870         case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
3871                 reason_str = "internal device reset";
3872                 break;
3873         case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
3874                 reason_str = "internal task abort";
3875                 break;
3876         case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
3877                 reason_str = "internal task abort set";
3878                 break;
3879         case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
3880                 reason_str = "internal clear task set";
3881                 break;
3882         case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
3883                 reason_str = "internal query task";
3884                 break;
3885         case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
3886                 reason_str = "sata init failure";
3887                 break;
3888         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
3889                 reason_str = "internal device reset complete";
3890                 break;
3891         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
3892                 reason_str = "internal task abort complete";
3893                 break;
3894         case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
3895                 reason_str = "internal async notification";
3896                 break;
3897         default:
3898                 reason_str = "unknown reason";
3899                 break;
3900         }
3901         printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
3902             "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
3903             reason_str, le16_to_cpu(event_data->DevHandle),
3904             (unsigned long long)le64_to_cpu(event_data->SASAddress));
3905         if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
3906                 printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
3907                     event_data->ASC, event_data->ASCQ);
3908         printk(KERN_INFO "\n");
3909 }
3910 #endif
3911
3912 /**
3913  * _scsih_sas_device_status_change_event - handle device status change
3914  * @ioc: per adapter object
3915  * @VF_ID:
3916  * @event_data: event data payload
3917  * Context: user.
3918  *
3919  * Return nothing.
3920  */
3921 static void
3922 _scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
3923     Mpi2EventDataSasDeviceStatusChange_t *event_data)
3924 {
3925 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3926         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
3927                 _scsih_sas_device_status_change_event_debug(ioc, event_data);
3928 #endif
3929 }
3930
3931 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3932 /**
3933  * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
3934  * @ioc: per adapter object
3935  * @event_data: event data payload
3936  * Context: user.
3937  *
3938  * Return nothing.
3939  */
3940 static void
3941 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3942     Mpi2EventDataSasEnclDevStatusChange_t *event_data)
3943 {
3944         char *reason_str = NULL;
3945
3946         switch (event_data->ReasonCode) {
3947         case MPI2_EVENT_SAS_ENCL_RC_ADDED:
3948                 reason_str = "enclosure add";
3949                 break;
3950         case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
3951                 reason_str = "enclosure remove";
3952                 break;
3953         default:
3954                 reason_str = "unknown reason";
3955                 break;
3956         }
3957
3958         printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
3959             "\thandle(0x%04x), enclosure logical id(0x%016llx)"
3960             " number slots(%d)\n", ioc->name, reason_str,
3961             le16_to_cpu(event_data->EnclosureHandle),
3962             (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
3963             le16_to_cpu(event_data->StartSlot));
3964 }
3965 #endif
3966
3967 /**
3968  * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
3969  * @ioc: per adapter object
3970  * @VF_ID:
3971  * @event_data: event data payload
3972  * Context: user.
3973  *
3974  * Return nothing.
3975  */
3976 static void
3977 _scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
3978     u8 VF_ID, Mpi2EventDataSasEnclDevStatusChange_t *event_data)
3979 {
3980 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3981         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
3982                 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
3983                      event_data);
3984 #endif
3985 }
3986
3987 /**
3988  * _scsih_sas_broadcast_primative_event - handle broadcast events
3989  * @ioc: per adapter object
3990  * @event_data: event data payload
3991  * Context: user.
3992  *
3993  * Return nothing.
3994  */
3995 static void
3996 _scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
3997     Mpi2EventDataSasBroadcastPrimitive_t *event_data)
3998 {
3999         struct scsi_cmnd *scmd;
4000         u16 smid, handle;
4001         u32 lun;
4002         struct MPT2SAS_DEVICE *sas_device_priv_data;
4003         u32 termination_count;
4004         u32 query_count;
4005         Mpi2SCSITaskManagementReply_t *mpi_reply;
4006
4007         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
4008             "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
4009             event_data->PortWidth));
4010
4011         dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
4012             __func__));
4013
4014         mutex_lock(&ioc->tm_cmds.mutex);
4015         termination_count = 0;
4016         query_count = 0;
4017         mpi_reply = ioc->tm_cmds.reply;
4018         for (smid = 1; smid <= ioc->request_depth; smid++) {
4019                 scmd = _scsih_scsi_lookup_get(ioc, smid);
4020                 if (!scmd)
4021                         continue;
4022                 sas_device_priv_data = scmd->device->hostdata;
4023                 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
4024                         continue;
4025                  /* skip hidden raid components */
4026                 if (sas_device_priv_data->sas_target->flags &
4027                     MPT_TARGET_FLAGS_RAID_COMPONENT)
4028                         continue;
4029                  /* skip volumes */
4030                 if (sas_device_priv_data->sas_target->flags &
4031                     MPT_TARGET_FLAGS_VOLUME)
4032                         continue;
4033
4034                 handle = sas_device_priv_data->sas_target->handle;
4035                 lun = sas_device_priv_data->lun;
4036                 query_count++;
4037
4038                 mpt2sas_scsih_issue_tm(ioc, handle, lun,
4039                     MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
4040                 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4041
4042                 if ((mpi_reply->IOCStatus == MPI2_IOCSTATUS_SUCCESS) &&
4043                     (mpi_reply->ResponseCode ==
4044                      MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
4045                      mpi_reply->ResponseCode ==
4046                      MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
4047                         continue;
4048
4049                 mpt2sas_scsih_issue_tm(ioc, handle, lun,
4050                     MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30);
4051                 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4052                 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
4053         }
4054         ioc->broadcast_aen_busy = 0;
4055         mutex_unlock(&ioc->tm_cmds.mutex);
4056
4057         dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
4058             "%s - exit, query_count = %d termination_count = %d\n",
4059             ioc->name, __func__, query_count, termination_count));
4060 }
4061
4062 /**
4063  * _scsih_sas_discovery_event - handle discovery events
4064  * @ioc: per adapter object
4065  * @event_data: event data payload
4066  * Context: user.
4067  *
4068  * Return nothing.
4069  */
4070 static void
4071 _scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4072     Mpi2EventDataSasDiscovery_t *event_data)
4073 {
4074 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4075         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
4076                 printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
4077                     (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
4078                     "start" : "stop");
4079         if (event_data->DiscoveryStatus)
4080                 printk(MPT2SAS_DEBUG_FMT ", discovery_status(0x%08x)",
4081                     ioc->name, le32_to_cpu(event_data->DiscoveryStatus));
4082         printk("\n");
4083         }
4084 #endif
4085
4086         if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
4087             !ioc->sas_hba.num_phys)
4088                 _scsih_sas_host_add(ioc);
4089 }
4090
4091 /**
4092  * _scsih_reprobe_lun - reprobing lun
4093  * @sdev: scsi device struct
4094  * @no_uld_attach: sdev->no_uld_attach flag setting
4095  *
4096  **/
4097 static void
4098 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
4099 {
4100         int rc;
4101
4102         sdev->no_uld_attach = no_uld_attach ? 1 : 0;
4103         sdev_printk(KERN_INFO, sdev, "%s raid component\n",
4104             sdev->no_uld_attach ? "hidding" : "exposing");
4105         rc = scsi_device_reprobe(sdev);
4106 }
4107
4108 /**
4109  * _scsih_reprobe_target - reprobing target
4110  * @starget: scsi target struct
4111  * @no_uld_attach: sdev->no_uld_attach flag setting
4112  *
4113  * Note: no_uld_attach flag determines whether the disk device is attached
4114  * to block layer. A value of `1` means to not attach.
4115  **/
4116 static void
4117 _scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
4118 {
4119         struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
4120
4121         if (no_uld_attach)
4122                 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
4123         else
4124                 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
4125
4126         starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
4127             _scsih_reprobe_lun);
4128 }
4129 /**
4130  * _scsih_sas_volume_add - add new volume
4131  * @ioc: per adapter object
4132  * @element: IR config element data
4133  * Context: user.
4134  *
4135  * Return nothing.
4136  */
4137 static void
4138 _scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
4139     Mpi2EventIrConfigElement_t *element)
4140 {
4141         struct _raid_device *raid_device;
4142         unsigned long flags;
4143         u64 wwid;
4144         u16 handle = le16_to_cpu(element->VolDevHandle);
4145         int rc;
4146
4147 #if 0 /* RAID_HACKS */
4148         if (le32_to_cpu(event_data->Flags) &
4149             MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
4150                 return;
4151 #endif
4152
4153         mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4154         if (!wwid) {
4155                 printk(MPT2SAS_ERR_FMT
4156                     "failure at %s:%d/%s()!\n", ioc->name,
4157                     __FILE__, __LINE__, __func__);
4158                 return;
4159         }
4160
4161         spin_lock_irqsave(&ioc->raid_device_lock, flags);
4162         raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
4163         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4164
4165         if (raid_device)
4166                 return;
4167
4168         raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4169         if (!raid_device) {
4170                 printk(MPT2SAS_ERR_FMT
4171                     "failure at %s:%d/%s()!\n", ioc->name,
4172                     __FILE__, __LINE__, __func__);
4173                 return;
4174         }
4175
4176         raid_device->id = ioc->sas_id++;
4177         raid_device->channel = RAID_CHANNEL;
4178         raid_device->handle = handle;
4179         raid_device->wwid = wwid;
4180         _scsih_raid_device_add(ioc, raid_device);
4181         if (!ioc->wait_for_port_enable_to_complete) {
4182                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4183                     raid_device->id, 0);
4184                 if (rc)
4185                         _scsih_raid_device_remove(ioc, raid_device);
4186         } else
4187                 _scsih_determine_boot_device(ioc, raid_device, 1);
4188 }
4189
4190 /**
4191  * _scsih_sas_volume_delete - delete volume
4192  * @ioc: per adapter object
4193  * @element: IR config element data
4194  * Context: user.
4195  *
4196  * Return nothing.
4197  */
4198 static void
4199 _scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
4200     Mpi2EventIrConfigElement_t *element)
4201 {
4202         struct _raid_device *raid_device;
4203         u16 handle = le16_to_cpu(element->VolDevHandle);
4204         unsigned long flags;
4205         struct MPT2SAS_TARGET *sas_target_priv_data;
4206
4207 #if 0 /* RAID_HACKS */
4208         if (le32_to_cpu(event_data->Flags) &
4209             MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
4210                 return;
4211 #endif
4212
4213         spin_lock_irqsave(&ioc->raid_device_lock, flags);
4214         raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4215         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4216         if (!raid_device)
4217                 return;
4218         if (raid_device->starget) {
4219                 sas_target_priv_data = raid_device->starget->hostdata;
4220                 sas_target_priv_data->deleted = 1;
4221                 scsi_remove_target(&raid_device->starget->dev);
4222         }
4223         _scsih_raid_device_remove(ioc, raid_device);
4224 }
4225
4226 /**
4227  * _scsih_sas_pd_expose - expose pd component to /dev/sdX
4228  * @ioc: per adapter object
4229  * @element: IR config element data
4230  * Context: user.
4231  *
4232  * Return nothing.
4233  */
4234 static void
4235 _scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
4236     Mpi2EventIrConfigElement_t *element)
4237 {
4238         struct _sas_device *sas_device;
4239         unsigned long flags;
4240         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4241
4242         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4243         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4244         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4245         if (!sas_device)
4246                 return;
4247
4248         /* exposing raid component */
4249         sas_device->volume_handle = 0;
4250         sas_device->volume_wwid = 0;
4251         sas_device->hidden_raid_component = 0;
4252         _scsih_reprobe_target(sas_device->starget, 0);
4253 }
4254
4255 /**
4256  * _scsih_sas_pd_hide - hide pd component from /dev/sdX
4257  * @ioc: per adapter object
4258  * @element: IR config element data
4259  * Context: user.
4260  *
4261  * Return nothing.
4262  */
4263 static void
4264 _scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
4265     Mpi2EventIrConfigElement_t *element)
4266 {
4267         struct _sas_device *sas_device;
4268         unsigned long flags;
4269         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4270
4271         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4272         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4273         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4274         if (!sas_device)
4275                 return;
4276
4277         /* hiding raid component */
4278         mpt2sas_config_get_volume_handle(ioc, handle,
4279             &sas_device->volume_handle);
4280         mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
4281             &sas_device->volume_wwid);
4282         sas_device->hidden_raid_component = 1;
4283         _scsih_reprobe_target(sas_device->starget, 1);
4284 }
4285
4286 /**
4287  * _scsih_sas_pd_delete - delete pd component
4288  * @ioc: per adapter object
4289  * @element: IR config element data
4290  * Context: user.
4291  *
4292  * Return nothing.
4293  */
4294 static void
4295 _scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
4296     Mpi2EventIrConfigElement_t *element)
4297 {
4298         struct _sas_device *sas_device;
4299         unsigned long flags;
4300         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4301
4302         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4303         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4304         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4305         if (!sas_device)
4306                 return;
4307         _scsih_remove_device(ioc, handle);
4308 }
4309
4310 /**
4311  * _scsih_sas_pd_add - remove pd component
4312  * @ioc: per adapter object
4313  * @element: IR config element data
4314  * Context: user.
4315  *
4316  * Return nothing.
4317  */
4318 static void
4319 _scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
4320     Mpi2EventIrConfigElement_t *element)
4321 {
4322         struct _sas_device *sas_device;
4323         unsigned long flags;
4324         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4325
4326         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4327         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4328         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4329         if (sas_device)
4330                 sas_device->hidden_raid_component = 1;
4331         else
4332                 _scsih_add_device(ioc, handle, 0, 1);
4333 }
4334
4335 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4336 /**
4337  * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
4338  * @ioc: per adapter object
4339  * @event_data: event data payload
4340  * Context: user.
4341  *
4342  * Return nothing.
4343  */
4344 static void
4345 _scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4346     Mpi2EventDataIrConfigChangeList_t *event_data)
4347 {
4348         Mpi2EventIrConfigElement_t *element;
4349         u8 element_type;
4350         int i;
4351         char *reason_str = NULL, *element_str = NULL;
4352
4353         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4354
4355         printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
4356             ioc->name, (le32_to_cpu(event_data->Flags) &
4357             MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
4358             "foreign" : "native", event_data->NumElements);
4359         for (i = 0; i < event_data->NumElements; i++, element++) {
4360                 switch (element->ReasonCode) {
4361                 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4362                         reason_str = "add";
4363                         break;
4364                 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4365                         reason_str = "remove";
4366                         break;
4367                 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
4368                         reason_str = "no change";
4369                         break;
4370                 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4371                         reason_str = "hide";
4372                         break;
4373                 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4374                         reason_str = "unhide";
4375                         break;
4376                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4377                         reason_str = "volume_created";
4378                         break;
4379                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4380                         reason_str = "volume_deleted";
4381                         break;
4382                 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4383                         reason_str = "pd_created";
4384                         break;
4385                 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4386                         reason_str = "pd_deleted";
4387                         break;
4388                 default:
4389                         reason_str = "unknown reason";
4390                         break;
4391                 }
4392                 element_type = le16_to_cpu(element->ElementFlags) &
4393                     MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
4394                 switch (element_type) {
4395                 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
4396                         element_str = "volume";
4397                         break;
4398                 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
4399                         element_str = "phys disk";
4400                         break;
4401                 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
4402                         element_str = "hot spare";
4403                         break;
4404                 default:
4405                         element_str = "unknown element";
4406                         break;
4407                 }
4408                 printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
4409                     "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
4410                     reason_str, le16_to_cpu(element->VolDevHandle),
4411                     le16_to_cpu(element->PhysDiskDevHandle),
4412                     element->PhysDiskNum);
4413         }
4414 }
4415 #endif
4416
4417 /**
4418  * _scsih_sas_ir_config_change_event - handle ir configuration change events
4419  * @ioc: per adapter object
4420  * @VF_ID:
4421  * @event_data: event data payload
4422  * Context: user.
4423  *
4424  * Return nothing.
4425  */
4426 static void
4427 _scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4428     Mpi2EventDataIrConfigChangeList_t *event_data)
4429 {
4430         Mpi2EventIrConfigElement_t *element;
4431         int i;
4432
4433 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4434         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4435                 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
4436
4437 #endif
4438
4439         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4440         for (i = 0; i < event_data->NumElements; i++, element++) {
4441
4442                 switch (element->ReasonCode) {
4443                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4444                 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4445                         _scsih_sas_volume_add(ioc, element);
4446                         break;
4447                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4448                 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4449                         _scsih_sas_volume_delete(ioc, element);
4450                         break;
4451                 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4452                         _scsih_sas_pd_hide(ioc, element);
4453                         break;
4454                 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4455                         _scsih_sas_pd_expose(ioc, element);
4456                         break;
4457                 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4458                         _scsih_sas_pd_add(ioc, element);
4459                         break;
4460                 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4461                         _scsih_sas_pd_delete(ioc, element);
4462                         break;
4463                 }
4464         }
4465 }
4466
4467 /**
4468  * _scsih_sas_ir_volume_event - IR volume event
4469  * @ioc: per adapter object
4470  * @event_data: event data payload
4471  * Context: user.
4472  *
4473  * Return nothing.
4474  */
4475 static void
4476 _scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4477     Mpi2EventDataIrVolume_t *event_data)
4478 {
4479         u64 wwid;
4480         unsigned long flags;
4481         struct _raid_device *raid_device;
4482         u16 handle;
4483         u32 state;
4484         int rc;
4485         struct MPT2SAS_TARGET *sas_target_priv_data;
4486
4487         if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
4488                 return;
4489
4490         handle = le16_to_cpu(event_data->VolDevHandle);
4491         state = le32_to_cpu(event_data->NewValue);
4492         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4493             "old(0x%08x), new(0x%08x)\n", ioc->name, __func__,  handle,
4494             le32_to_cpu(event_data->PreviousValue), state));
4495
4496         spin_lock_irqsave(&ioc->raid_device_lock, flags);
4497         raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4498         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4499
4500         switch (state) {
4501         case MPI2_RAID_VOL_STATE_MISSING:
4502         case MPI2_RAID_VOL_STATE_FAILED:
4503                 if (!raid_device)
4504                         break;
4505                 if (raid_device->starget) {
4506                         sas_target_priv_data = raid_device->starget->hostdata;
4507                         sas_target_priv_data->deleted = 1;
4508                         scsi_remove_target(&raid_device->starget->dev);
4509                 }
4510                 _scsih_raid_device_remove(ioc, raid_device);
4511                 break;
4512
4513         case MPI2_RAID_VOL_STATE_ONLINE:
4514         case MPI2_RAID_VOL_STATE_DEGRADED:
4515         case MPI2_RAID_VOL_STATE_OPTIMAL:
4516                 if (raid_device)
4517                         break;
4518
4519                 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4520                 if (!wwid) {
4521                         printk(MPT2SAS_ERR_FMT
4522                             "failure at %s:%d/%s()!\n", ioc->name,
4523                             __FILE__, __LINE__, __func__);
4524                         break;
4525                 }
4526
4527                 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4528                 if (!raid_device) {
4529                         printk(MPT2SAS_ERR_FMT
4530                             "failure at %s:%d/%s()!\n", ioc->name,
4531                             __FILE__, __LINE__, __func__);
4532                         break;
4533                 }
4534
4535                 raid_device->id = ioc->sas_id++;
4536                 raid_device->channel = RAID_CHANNEL;
4537                 raid_device->handle = handle;
4538                 raid_device->wwid = wwid;
4539                 _scsih_raid_device_add(ioc, raid_device);
4540                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4541                     raid_device->id, 0);
4542                 if (rc)
4543                         _scsih_raid_device_remove(ioc, raid_device);
4544                 break;
4545
4546         case MPI2_RAID_VOL_STATE_INITIALIZING:
4547         default:
4548                 break;
4549         }
4550 }
4551
4552 /**
4553  * _scsih_sas_ir_physical_disk_event - PD event
4554  * @ioc: per adapter object
4555  * @event_data: event data payload
4556  * Context: user.
4557  *
4558  * Return nothing.
4559  */
4560 static void
4561 _scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4562    Mpi2EventDataIrPhysicalDisk_t *event_data)
4563 {
4564         u16 handle;
4565         u32 state;
4566         struct _sas_device *sas_device;
4567         unsigned long flags;
4568
4569         if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
4570                 return;
4571
4572         handle = le16_to_cpu(event_data->PhysDiskDevHandle);
4573         state = le32_to_cpu(event_data->NewValue);
4574
4575         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4576             "old(0x%08x), new(0x%08x)\n", ioc->name, __func__,  handle,
4577             le32_to_cpu(event_data->PreviousValue), state));
4578
4579         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4580         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4581         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4582
4583         switch (state) {
4584 #if 0
4585         case MPI2_RAID_PD_STATE_OFFLINE:
4586                 if (sas_device)
4587                         _scsih_remove_device(ioc, handle);
4588                 break;
4589 #endif
4590         case MPI2_RAID_PD_STATE_ONLINE:
4591         case MPI2_RAID_PD_STATE_DEGRADED:
4592         case MPI2_RAID_PD_STATE_REBUILDING:
4593         case MPI2_RAID_PD_STATE_OPTIMAL:
4594                 if (sas_device)
4595                         sas_device->hidden_raid_component = 1;
4596                 else
4597                         _scsih_add_device(ioc, handle, 0, 1);
4598                 break;
4599
4600         case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
4601         case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
4602         case MPI2_RAID_PD_STATE_HOT_SPARE:
4603         default:
4604                 break;
4605         }
4606 }
4607
4608 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4609 /**
4610  * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
4611  * @ioc: per adapter object
4612  * @event_data: event data payload
4613  * Context: user.
4614  *
4615  * Return nothing.
4616  */
4617 static void
4618 _scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
4619     Mpi2EventDataIrOperationStatus_t *event_data)
4620 {
4621         char *reason_str = NULL;
4622
4623         switch (event_data->RAIDOperation) {
4624         case MPI2_EVENT_IR_RAIDOP_RESYNC:
4625                 reason_str = "resync";
4626                 break;
4627         case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
4628                 reason_str = "online capacity expansion";
4629                 break;
4630         case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
4631                 reason_str = "consistency check";
4632                 break;
4633         default:
4634                 reason_str = "unknown reason";
4635                 break;
4636         }
4637
4638         printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
4639             "\thandle(0x%04x), percent complete(%d)\n",
4640             ioc->name, reason_str,
4641             le16_to_cpu(event_data->VolDevHandle),
4642             event_data->PercentComplete);
4643 }
4644 #endif
4645
4646 /**
4647  * _scsih_sas_ir_operation_status_event - handle RAID operation events
4648  * @ioc: per adapter object
4649  * @VF_ID:
4650  * @event_data: event data payload
4651  * Context: user.
4652  *
4653  * Return nothing.
4654  */
4655 static void
4656 _scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4657     Mpi2EventDataIrOperationStatus_t *event_data)
4658 {
4659 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4660         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4661                 _scsih_sas_ir_operation_status_event_debug(ioc, event_data);
4662 #endif
4663 }
4664
4665 /**
4666  * _scsih_task_set_full - handle task set full
4667  * @ioc: per adapter object
4668  * @event_data: event data payload
4669  * Context: user.
4670  *
4671  * Throttle back qdepth.
4672  */
4673 static void
4674 _scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4675     Mpi2EventDataTaskSetFull_t *event_data)
4676 {
4677         unsigned long flags;
4678         struct _sas_device *sas_device;
4679         static struct _raid_device *raid_device;
4680         struct scsi_device *sdev;
4681         int depth;
4682         u16 current_depth;
4683         u16 handle;
4684         int id, channel;
4685         u64 sas_address;
4686
4687         current_depth = le16_to_cpu(event_data->CurrentDepth);
4688         handle = le16_to_cpu(event_data->DevHandle);
4689         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4690         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4691         if (!sas_device) {
4692                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4693                 return;
4694         }
4695         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4696         id = sas_device->id;
4697         channel = sas_device->channel;
4698         sas_address = sas_device->sas_address;
4699
4700         /* if hidden raid component, then change to volume characteristics */
4701         if (sas_device->hidden_raid_component && sas_device->volume_handle) {
4702                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4703                 raid_device = _scsih_raid_device_find_by_handle(
4704                     ioc, sas_device->volume_handle);
4705                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4706                 if (raid_device) {
4707                         id = raid_device->id;
4708                         channel = raid_device->channel;
4709                         handle = raid_device->handle;
4710                         sas_address = raid_device->wwid;
4711                 }
4712         }
4713
4714         if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
4715                 starget_printk(KERN_DEBUG, sas_device->starget, "task set "
4716                     "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
4717                     handle, (unsigned long long)sas_address, current_depth);
4718
4719         shost_for_each_device(sdev, ioc->shost) {
4720                 if (sdev->id == id && sdev->channel == channel) {
4721                         if (current_depth > sdev->queue_depth) {
4722                                 if (ioc->logging_level &
4723                                     MPT_DEBUG_TASK_SET_FULL)
4724                                         sdev_printk(KERN_INFO, sdev, "strange "
4725                                             "observation, the queue depth is"
4726                                             " (%d) meanwhile fw queue depth "
4727                                             "is (%d)\n", sdev->queue_depth,
4728                                             current_depth);
4729                                 continue;
4730                         }
4731                         depth = scsi_track_queue_full(sdev,
4732                             current_depth - 1);
4733                         if (depth > 0)
4734                                 sdev_printk(KERN_INFO, sdev, "Queue depth "
4735                                     "reduced to (%d)\n", depth);
4736                         else if (depth < 0)
4737                                 sdev_printk(KERN_INFO, sdev, "Tagged Command "
4738                                     "Queueing is being disabled\n");
4739                         else if (depth == 0)
4740                                 if (ioc->logging_level &
4741                                      MPT_DEBUG_TASK_SET_FULL)
4742                                         sdev_printk(KERN_INFO, sdev,
4743                                              "Queue depth not changed yet\n");
4744                 }
4745         }
4746 }
4747
4748 /**
4749  * _scsih_mark_responding_sas_device - mark a sas_devices as responding
4750  * @ioc: per adapter object
4751  * @sas_address: sas address
4752  * @slot: enclosure slot id
4753  * @handle: device handle
4754  *
4755  * After host reset, find out whether devices are still responding.
4756  * Used in _scsi_remove_unresponsive_sas_devices.
4757  *
4758  * Return nothing.
4759  */
4760 static void
4761 _scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4762     u16 slot, u16 handle)
4763 {
4764         struct MPT2SAS_TARGET *sas_target_priv_data;
4765         struct scsi_target *starget;
4766         struct _sas_device *sas_device;
4767         unsigned long flags;
4768
4769         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4770         list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
4771                 if (sas_device->sas_address == sas_address &&
4772                     sas_device->slot == slot && sas_device->starget) {
4773                         sas_device->responding = 1;
4774                         starget_printk(KERN_INFO, sas_device->starget,
4775                             "handle(0x%04x), sas_addr(0x%016llx), enclosure "
4776                             "logical id(0x%016llx), slot(%d)\n", handle,
4777                             (unsigned long long)sas_device->sas_address,
4778                             (unsigned long long)
4779                             sas_device->enclosure_logical_id,
4780                             sas_device->slot);
4781                         if (sas_device->handle == handle)
4782                                 goto out;
4783                         printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
4784                             sas_device->handle);
4785                         sas_device->handle = handle;
4786                         starget = sas_device->starget;
4787                         sas_target_priv_data = starget->hostdata;
4788                         sas_target_priv_data->handle = handle;
4789                         goto out;
4790                 }
4791         }
4792  out:
4793         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4794 }
4795
4796 /**
4797  * _scsih_search_responding_sas_devices -
4798  * @ioc: per adapter object
4799  *
4800  * After host reset, find out whether devices are still responding.
4801  * If not remove.
4802  *
4803  * Return nothing.
4804  */
4805 static void
4806 _scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
4807 {
4808         Mpi2SasDevicePage0_t sas_device_pg0;
4809         Mpi2ConfigReply_t mpi_reply;
4810         u16 ioc_status;
4811         __le64 sas_address;
4812         u16 handle;
4813         u32 device_info;
4814         u16 slot;
4815
4816         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
4817
4818         if (list_empty(&ioc->sas_device_list))
4819                 return;
4820
4821         handle = 0xFFFF;
4822         while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
4823             &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
4824             handle))) {
4825                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4826                     MPI2_IOCSTATUS_MASK;
4827                 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
4828                         break;
4829                 handle = le16_to_cpu(sas_device_pg0.DevHandle);
4830                 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4831                 if (!(_scsih_is_end_device(device_info)))
4832                         continue;
4833                 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4834                 slot = le16_to_cpu(sas_device_pg0.Slot);
4835                 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
4836                     handle);
4837         }
4838 }
4839
4840 /**
4841  * _scsih_mark_responding_raid_device - mark a raid_device as responding
4842  * @ioc: per adapter object
4843  * @wwid: world wide identifier for raid volume
4844  * @handle: device handle
4845  *
4846  * After host reset, find out whether devices are still responding.
4847  * Used in _scsi_remove_unresponsive_raid_devices.
4848  *
4849  * Return nothing.
4850  */
4851 static void
4852 _scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
4853     u16 handle)
4854 {
4855         struct MPT2SAS_TARGET *sas_target_priv_data;
4856         struct scsi_target *starget;
4857         struct _raid_device *raid_device;
4858         unsigned long flags;
4859
4860         spin_lock_irqsave(&ioc->raid_device_lock, flags);
4861         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
4862                 if (raid_device->wwid == wwid && raid_device->starget) {
4863                         raid_device->responding = 1;
4864                         starget_printk(KERN_INFO, raid_device->starget,
4865                             "handle(0x%04x), wwid(0x%016llx)\n", handle,
4866                             (unsigned long long)raid_device->wwid);
4867                         if (raid_device->handle == handle)
4868                                 goto out;
4869                         printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
4870                             raid_device->handle);
4871                         raid_device->handle = handle;
4872                         starget = raid_device->starget;
4873                         sas_target_priv_data = starget->hostdata;
4874                         sas_target_priv_data->handle = handle;
4875                         goto out;
4876                 }
4877         }
4878  out:
4879         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4880 }
4881
4882 /**
4883  * _scsih_search_responding_raid_devices -
4884  * @ioc: per adapter object
4885  *
4886  * After host reset, find out whether devices are still responding.
4887  * If not remove.
4888  *
4889  * Return nothing.
4890  */
4891 static void
4892 _scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
4893 {
4894         Mpi2RaidVolPage1_t volume_pg1;
4895         Mpi2ConfigReply_t mpi_reply;
4896         u16 ioc_status;
4897         u16 handle;
4898
4899         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
4900
4901         if (list_empty(&ioc->raid_device_list))
4902                 return;
4903
4904         handle = 0xFFFF;
4905         while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
4906             &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
4907                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4908                     MPI2_IOCSTATUS_MASK;
4909                 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
4910                         break;
4911                 handle = le16_to_cpu(volume_pg1.DevHandle);
4912                 _scsih_mark_responding_raid_device(ioc,
4913                     le64_to_cpu(volume_pg1.WWID), handle);
4914         }
4915 }
4916
4917 /**
4918  * _scsih_mark_responding_expander - mark a expander as responding
4919  * @ioc: per adapter object
4920  * @sas_address: sas address
4921  * @handle:
4922  *
4923  * After host reset, find out whether devices are still responding.
4924  * Used in _scsi_remove_unresponsive_expanders.
4925  *
4926  * Return nothing.
4927  */
4928 static void
4929 _scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4930      u16 handle)
4931 {
4932         struct _sas_node *sas_expander;
4933         unsigned long flags;
4934
4935         spin_lock_irqsave(&ioc->sas_node_lock, flags);
4936         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
4937                 if (sas_expander->sas_address == sas_address) {
4938                         sas_expander->responding = 1;
4939                         if (sas_expander->handle != handle) {
4940                                 printk(KERN_INFO "old handle(0x%04x)\n",
4941                                     sas_expander->handle);
4942                                 sas_expander->handle = handle;
4943                         }
4944                         goto out;
4945                 }
4946         }
4947  out:
4948         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4949 }
4950
4951 /**
4952  * _scsih_search_responding_expanders -
4953  * @ioc: per adapter object
4954  *
4955  * After host reset, find out whether devices are still responding.
4956  * If not remove.
4957  *
4958  * Return nothing.
4959  */
4960 static void
4961 _scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
4962 {
4963         Mpi2ExpanderPage0_t expander_pg0;
4964         Mpi2ConfigReply_t mpi_reply;
4965         u16 ioc_status;
4966         __le64 sas_address;
4967         u16 handle;
4968
4969         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
4970
4971         if (list_empty(&ioc->sas_expander_list))
4972                 return;
4973
4974         handle = 0xFFFF;
4975         while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4976             MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
4977
4978                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4979                     MPI2_IOCSTATUS_MASK;
4980                 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
4981                         break;
4982
4983                 handle = le16_to_cpu(expander_pg0.DevHandle);
4984                 sas_address = le64_to_cpu(expander_pg0.SASAddress);
4985                 printk(KERN_INFO "\texpander present: handle(0x%04x), "
4986                     "sas_addr(0x%016llx)\n", handle,
4987                     (unsigned long long)sas_address);
4988                 _scsih_mark_responding_expander(ioc, sas_address, handle);
4989         }
4990
4991 }
4992
4993 /**
4994  * _scsih_remove_unresponding_devices - removing unresponding devices
4995  * @ioc: per adapter object
4996  *
4997  * Return nothing.
4998  */
4999 static void
5000 _scsih_remove_unresponding_devices(struct MPT2SAS_ADAPTER *ioc)
5001 {
5002         struct _sas_device *sas_device, *sas_device_next;
5003         struct _sas_node *sas_expander, *sas_expander_next;
5004         struct _raid_device *raid_device, *raid_device_next;
5005         unsigned long flags;
5006
5007         _scsih_search_responding_sas_devices(ioc);
5008         _scsih_search_responding_raid_devices(ioc);
5009         _scsih_search_responding_expanders(ioc);
5010
5011         spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
5012         ioc->shost_recovery = 0;
5013         if (ioc->shost->shost_state == SHOST_RECOVERY) {
5014                 printk(MPT2SAS_INFO_FMT "putting controller into "
5015                     "SHOST_RUNNING\n", ioc->name);
5016                 scsi_host_set_state(ioc->shost, SHOST_RUNNING);
5017         }
5018         spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
5019
5020         list_for_each_entry_safe(sas_device, sas_device_next,
5021             &ioc->sas_device_list, list) {
5022                 if (sas_device->responding) {
5023                         sas_device->responding = 0;
5024                         continue;
5025                 }
5026                 if (sas_device->starget)
5027                         starget_printk(KERN_INFO, sas_device->starget,
5028                             "removing: handle(0x%04x), sas_addr(0x%016llx), "
5029                             "enclosure logical id(0x%016llx), slot(%d)\n",
5030                             sas_device->handle,
5031                             (unsigned long long)sas_device->sas_address,
5032                             (unsigned long long)
5033                             sas_device->enclosure_logical_id,
5034                             sas_device->slot);
5035                 _scsih_remove_device(ioc, sas_device->handle);
5036         }
5037
5038         list_for_each_entry_safe(raid_device, raid_device_next,
5039             &ioc->raid_device_list, list) {
5040                 if (raid_device->responding) {
5041                         raid_device->responding = 0;
5042                         continue;
5043                 }
5044                 if (raid_device->starget) {
5045                         starget_printk(KERN_INFO, raid_device->starget,
5046                             "removing: handle(0x%04x), wwid(0x%016llx)\n",
5047                               raid_device->handle,
5048                             (unsigned long long)raid_device->wwid);
5049                         scsi_remove_target(&raid_device->starget->dev);
5050                 }
5051                 _scsih_raid_device_remove(ioc, raid_device);
5052         }
5053
5054         list_for_each_entry_safe(sas_expander, sas_expander_next,
5055             &ioc->sas_expander_list, list) {
5056                 if (sas_expander->responding) {
5057                         sas_expander->responding = 0;
5058                         continue;
5059                 }
5060                 printk("\tremoving expander: handle(0x%04x), "
5061                     " sas_addr(0x%016llx)\n", sas_expander->handle,
5062                     (unsigned long long)sas_expander->sas_address);
5063                 _scsih_expander_remove(ioc, sas_expander->handle);
5064         }
5065 }
5066
5067 /**
5068  * _firmware_event_work - delayed task for processing firmware events
5069  * @ioc: per adapter object
5070  * @work: equal to the fw_event_work object
5071  * Context: user.
5072  *
5073  * Return nothing.
5074  */
5075 static void
5076 _firmware_event_work(struct work_struct *work)
5077 {
5078         struct fw_event_work *fw_event = container_of(work,
5079             struct fw_event_work, work);
5080         unsigned long flags;
5081         struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
5082
5083         /* This is invoked by calling _scsih_queue_rescan(). */
5084         if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
5085                 _scsih_fw_event_free(ioc, fw_event);
5086                 _scsih_sas_host_refresh(ioc, 1);
5087                 _scsih_remove_unresponding_devices(ioc);
5088                 return;
5089         }
5090
5091         /* the queue is being flushed so ignore this event */
5092         spin_lock_irqsave(&ioc->fw_event_lock, flags);
5093         if (ioc->fw_events_off || ioc->remove_host) {
5094                 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5095                 _scsih_fw_event_free(ioc, fw_event);
5096                 return;
5097         }
5098         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5099
5100         spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
5101         if (ioc->shost_recovery) {
5102                 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
5103                 _scsih_fw_event_requeue(ioc, fw_event, 1000);
5104                 return;
5105         }
5106         spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
5107
5108         switch (fw_event->event) {
5109         case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
5110                 _scsih_sas_topology_change_event(ioc, fw_event->VF_ID,
5111                     fw_event->event_data, fw_event);
5112                 break;
5113         case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5114                 _scsih_sas_device_status_change_event(ioc, fw_event->VF_ID,
5115                     fw_event->event_data);
5116                 break;
5117         case MPI2_EVENT_SAS_DISCOVERY:
5118                 _scsih_sas_discovery_event(ioc, fw_event->VF_ID,
5119                     fw_event->event_data);
5120                 break;
5121         case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5122                 _scsih_sas_broadcast_primative_event(ioc, fw_event->VF_ID,
5123                     fw_event->event_data);
5124                 break;
5125         case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5126                 _scsih_sas_enclosure_dev_status_change_event(ioc,
5127                     fw_event->VF_ID, fw_event->event_data);
5128                 break;
5129         case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5130                 _scsih_sas_ir_config_change_event(ioc, fw_event->VF_ID,
5131                     fw_event->event_data);
5132                 break;
5133         case MPI2_EVENT_IR_VOLUME:
5134                 _scsih_sas_ir_volume_event(ioc, fw_event->VF_ID,
5135                     fw_event->event_data);
5136                 break;
5137         case MPI2_EVENT_IR_PHYSICAL_DISK:
5138                 _scsih_sas_ir_physical_disk_event(ioc, fw_event->VF_ID,
5139                     fw_event->event_data);
5140                 break;
5141         case MPI2_EVENT_IR_OPERATION_STATUS:
5142                 _scsih_sas_ir_operation_status_event(ioc, fw_event->VF_ID,
5143                     fw_event->event_data);
5144                 break;
5145         case MPI2_EVENT_TASK_SET_FULL:
5146                 _scsih_task_set_full(ioc, fw_event->VF_ID,
5147                     fw_event->event_data);
5148                 break;
5149         }
5150         _scsih_fw_event_free(ioc, fw_event);
5151 }
5152
5153 /**
5154  * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
5155  * @ioc: per adapter object
5156  * @VF_ID: virtual function id
5157  * @reply: reply message frame(lower 32bit addr)
5158  * Context: interrupt.
5159  *
5160  * This function merely adds a new work task into ioc->firmware_event_thread.
5161  * The tasks are worked from _firmware_event_work in user context.
5162  *
5163  * Return nothing.
5164  */
5165 void
5166 mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, u32 reply)
5167 {
5168         struct fw_event_work *fw_event;
5169         Mpi2EventNotificationReply_t *mpi_reply;
5170         unsigned long flags;
5171         u16 event;
5172
5173         /* events turned off due to host reset or driver unloading */
5174         spin_lock_irqsave(&ioc->fw_event_lock, flags);
5175         if (ioc->fw_events_off || ioc->remove_host) {
5176                 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5177                 return;
5178         }
5179         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5180
5181         mpi_reply =  mpt2sas_base_get_reply_virt_addr(ioc, reply);
5182         event = le16_to_cpu(mpi_reply->Event);
5183
5184         switch (event) {
5185         /* handle these */
5186         case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5187         {
5188                 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
5189                     (Mpi2EventDataSasBroadcastPrimitive_t *)
5190                     mpi_reply->EventData;
5191
5192                 if (baen_data->Primitive !=
5193                     MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
5194                     ioc->broadcast_aen_busy)
5195                         return;
5196                 ioc->broadcast_aen_busy = 1;
5197                 break;
5198         }
5199
5200         case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
5201                 _scsih_check_topo_delete_events(ioc,
5202                     (Mpi2EventDataSasTopologyChangeList_t *)
5203                     mpi_reply->EventData);
5204                 break;
5205
5206         case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5207         case MPI2_EVENT_IR_OPERATION_STATUS:
5208         case MPI2_EVENT_SAS_DISCOVERY:
5209         case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5210         case MPI2_EVENT_IR_VOLUME:
5211         case MPI2_EVENT_IR_PHYSICAL_DISK:
5212         case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5213         case MPI2_EVENT_TASK_SET_FULL:
5214                 break;
5215
5216         default: /* ignore the rest */
5217                 return;
5218         }
5219
5220         fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
5221         if (!fw_event) {
5222                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5223                     ioc->name, __FILE__, __LINE__, __func__);
5224                 return;
5225         }
5226         fw_event->event_data =
5227             kzalloc(mpi_reply->EventDataLength*4, GFP_ATOMIC);
5228         if (!fw_event->event_data) {
5229                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5230                     ioc->name, __FILE__, __LINE__, __func__);
5231                 kfree(fw_event);
5232                 return;
5233         }
5234
5235         memcpy(fw_event->event_data, mpi_reply->EventData,
5236             mpi_reply->EventDataLength*4);
5237         fw_event->ioc = ioc;
5238         fw_event->VF_ID = VF_ID;
5239         fw_event->event = event;
5240         _scsih_fw_event_add(ioc, fw_event);
5241 }
5242
5243 /* shost template */
5244 static struct scsi_host_template scsih_driver_template = {
5245         .module                         = THIS_MODULE,
5246         .name                           = "Fusion MPT SAS Host",
5247         .proc_name                      = MPT2SAS_DRIVER_NAME,
5248         .queuecommand                   = scsih_qcmd,
5249         .target_alloc                   = scsih_target_alloc,
5250         .slave_alloc                    = scsih_slave_alloc,
5251         .slave_configure                = scsih_slave_configure,
5252         .target_destroy                 = scsih_target_destroy,
5253         .slave_destroy                  = scsih_slave_destroy,
5254         .change_queue_depth             = scsih_change_queue_depth,
5255         .change_queue_type              = scsih_change_queue_type,
5256         .eh_abort_handler               = scsih_abort,
5257         .eh_device_reset_handler        = scsih_dev_reset,
5258         .eh_host_reset_handler          = scsih_host_reset,
5259         .bios_param                     = scsih_bios_param,
5260         .can_queue                      = 1,
5261         .this_id                        = -1,
5262         .sg_tablesize                   = MPT2SAS_SG_DEPTH,
5263         .max_sectors                    = 8192,
5264         .cmd_per_lun                    = 7,
5265         .use_clustering                 = ENABLE_CLUSTERING,
5266         .shost_attrs                    = mpt2sas_host_attrs,
5267         .sdev_attrs                     = mpt2sas_dev_attrs,
5268 };
5269
5270 /**
5271  * _scsih_expander_node_remove - removing expander device from list.
5272  * @ioc: per adapter object
5273  * @sas_expander: the sas_device object
5274  * Context: Calling function should acquire ioc->sas_node_lock.
5275  *
5276  * Removing object and freeing associated memory from the
5277  * ioc->sas_expander_list.
5278  *
5279  * Return nothing.
5280  */
5281 static void
5282 _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
5283     struct _sas_node *sas_expander)
5284 {
5285         struct _sas_port *mpt2sas_port;
5286         struct _sas_device *sas_device;
5287         struct _sas_node *expander_sibling;
5288         unsigned long flags;
5289
5290         if (!sas_expander)
5291                 return;
5292
5293         /* remove sibling ports attached to this expander */
5294  retry_device_search:
5295         list_for_each_entry(mpt2sas_port,
5296            &sas_expander->sas_port_list, port_list) {
5297                 if (mpt2sas_port->remote_identify.device_type ==
5298                     SAS_END_DEVICE) {
5299                         spin_lock_irqsave(&ioc->sas_device_lock, flags);
5300                         sas_device =
5301                             mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5302                            mpt2sas_port->remote_identify.sas_address);
5303                         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5304                         if (!sas_device)
5305                                 continue;
5306                         _scsih_remove_device(ioc, sas_device->handle);
5307                         goto retry_device_search;
5308                 }
5309         }
5310
5311  retry_expander_search:
5312         list_for_each_entry(mpt2sas_port,
5313            &sas_expander->sas_port_list, port_list) {
5314
5315                 if (mpt2sas_port->remote_identify.device_type ==
5316                     MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
5317                     mpt2sas_port->remote_identify.device_type ==
5318                     MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
5319
5320                         spin_lock_irqsave(&ioc->sas_node_lock, flags);
5321                         expander_sibling =
5322                             mpt2sas_scsih_expander_find_by_sas_address(
5323                             ioc, mpt2sas_port->remote_identify.sas_address);
5324                         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5325                         if (!expander_sibling)
5326                                 continue;
5327                         _scsih_expander_remove(ioc, expander_sibling->handle);
5328                         goto retry_expander_search;
5329                 }
5330         }
5331
5332         mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
5333             sas_expander->parent_handle);
5334
5335         printk(MPT2SAS_INFO_FMT "expander_remove: handle"
5336            "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
5337             sas_expander->handle, (unsigned long long)
5338             sas_expander->sas_address);
5339
5340         list_del(&sas_expander->list);
5341         kfree(sas_expander->phy);
5342         kfree(sas_expander);
5343 }
5344
5345 /**
5346  * scsih_remove - detach and remove add host
5347  * @pdev: PCI device struct
5348  *
5349  * Return nothing.
5350  */
5351 static void __devexit
5352 scsih_remove(struct pci_dev *pdev)
5353 {
5354         struct Scsi_Host *shost = pci_get_drvdata(pdev);
5355         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5356         struct _sas_port *mpt2sas_port;
5357         struct _sas_device *sas_device;
5358         struct _sas_node *expander_sibling;
5359         struct workqueue_struct *wq;
5360         unsigned long flags;
5361
5362         ioc->remove_host = 1;
5363         _scsih_fw_event_off(ioc);
5364
5365         spin_lock_irqsave(&ioc->fw_event_lock, flags);
5366         wq = ioc->firmware_event_thread;
5367         ioc->firmware_event_thread = NULL;
5368         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5369         if (wq)
5370                 destroy_workqueue(wq);
5371
5372         /* free ports attached to the sas_host */
5373  retry_again:
5374         list_for_each_entry(mpt2sas_port,
5375            &ioc->sas_hba.sas_port_list, port_list) {
5376                 if (mpt2sas_port->remote_identify.device_type ==
5377                     SAS_END_DEVICE) {
5378                         sas_device =
5379                             mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5380                            mpt2sas_port->remote_identify.sas_address);
5381                         if (sas_device) {
5382                                 _scsih_remove_device(ioc, sas_device->handle);
5383                                 goto retry_again;
5384                         }
5385                 } else {
5386                         expander_sibling =
5387                             mpt2sas_scsih_expander_find_by_sas_address(ioc,
5388                             mpt2sas_port->remote_identify.sas_address);
5389                         if (expander_sibling) {
5390                                 _scsih_expander_remove(ioc,
5391                                     expander_sibling->handle);
5392                                 goto retry_again;
5393                         }
5394                 }
5395         }
5396
5397         /* free phys attached to the sas_host */
5398         if (ioc->sas_hba.num_phys) {
5399                 kfree(ioc->sas_hba.phy);
5400                 ioc->sas_hba.phy = NULL;
5401                 ioc->sas_hba.num_phys = 0;
5402         }
5403
5404         sas_remove_host(shost);
5405         mpt2sas_base_detach(ioc);
5406         list_del(&ioc->list);
5407         scsi_remove_host(shost);
5408         scsi_host_put(shost);
5409 }
5410
5411 /**
5412  * _scsih_probe_boot_devices - reports 1st device
5413  * @ioc: per adapter object
5414  *
5415  * If specified in bios page 2, this routine reports the 1st
5416  * device scsi-ml or sas transport for persistent boot device
5417  * purposes.  Please refer to function _scsih_determine_boot_device()
5418  */
5419 static void
5420 _scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
5421 {
5422         u8 is_raid;
5423         void *device;
5424         struct _sas_device *sas_device;
5425         struct _raid_device *raid_device;
5426         u16 handle, parent_handle;
5427         u64 sas_address;
5428         unsigned long flags;
5429         int rc;
5430
5431         device = NULL;
5432         if (ioc->req_boot_device.device) {
5433                 device =  ioc->req_boot_device.device;
5434                 is_raid = ioc->req_boot_device.is_raid;
5435         } else if (ioc->req_alt_boot_device.device) {
5436                 device =  ioc->req_alt_boot_device.device;
5437                 is_raid = ioc->req_alt_boot_device.is_raid;
5438         } else if (ioc->current_boot_device.device) {
5439                 device =  ioc->current_boot_device.device;
5440                 is_raid = ioc->current_boot_device.is_raid;
5441         }
5442
5443         if (!device)
5444                 return;
5445
5446         if (is_raid) {
5447                 raid_device = device;
5448                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5449                     raid_device->id, 0);
5450                 if (rc)
5451                         _scsih_raid_device_remove(ioc, raid_device);
5452         } else {
5453                 sas_device = device;
5454                 handle = sas_device->handle;
5455                 parent_handle = sas_device->parent_handle;
5456                 sas_address = sas_device->sas_address;
5457                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5458                 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5459                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5460                 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
5461                     sas_device->parent_handle)) {
5462                         _scsih_sas_device_remove(ioc, sas_device);
5463                 } else if (!sas_device->starget) {
5464                         mpt2sas_transport_port_remove(ioc, sas_address,
5465                             parent_handle);
5466                         _scsih_sas_device_remove(ioc, sas_device);
5467                 }
5468         }
5469 }
5470
5471 /**
5472  * _scsih_probe_raid - reporting raid volumes to scsi-ml
5473  * @ioc: per adapter object
5474  *
5475  * Called during initial loading of the driver.
5476  */
5477 static void
5478 _scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
5479 {
5480         struct _raid_device *raid_device, *raid_next;
5481         int rc;
5482
5483         list_for_each_entry_safe(raid_device, raid_next,
5484             &ioc->raid_device_list, list) {
5485                 if (raid_device->starget)
5486                         continue;
5487                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5488                     raid_device->id, 0);
5489                 if (rc)
5490                         _scsih_raid_device_remove(ioc, raid_device);
5491         }
5492 }
5493
5494 /**
5495  * _scsih_probe_sas - reporting raid volumes to sas transport
5496  * @ioc: per adapter object
5497  *
5498  * Called during initial loading of the driver.
5499  */
5500 static void
5501 _scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
5502 {
5503         struct _sas_device *sas_device, *next;
5504         unsigned long flags;
5505         u16 handle, parent_handle;
5506         u64 sas_address;
5507
5508         /* SAS Device List */
5509         list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
5510             list) {
5511                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5512                 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5513                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5514
5515                 handle = sas_device->handle;
5516                 parent_handle = sas_device->parent_handle;
5517                 sas_address = sas_device->sas_address;
5518                 if (!mpt2sas_transport_port_add(ioc, handle, parent_handle)) {
5519                         _scsih_sas_device_remove(ioc, sas_device);
5520                 } else if (!sas_device->starget) {
5521                         mpt2sas_transport_port_remove(ioc, sas_address,
5522                             parent_handle);
5523                         _scsih_sas_device_remove(ioc, sas_device);
5524                 }
5525         }
5526 }
5527
5528 /**
5529  * _scsih_probe_devices - probing for devices
5530  * @ioc: per adapter object
5531  *
5532  * Called during initial loading of the driver.
5533  */
5534 static void
5535 _scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
5536 {
5537         u16 volume_mapping_flags =
5538             le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
5539             MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
5540
5541         if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
5542                 return;  /* return when IOC doesn't support initiator mode */
5543
5544         _scsih_probe_boot_devices(ioc);
5545
5546         if (ioc->ir_firmware) {
5547                 if ((volume_mapping_flags &
5548                      MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
5549                         _scsih_probe_sas(ioc);
5550                         _scsih_probe_raid(ioc);
5551                 } else {
5552                         _scsih_probe_raid(ioc);
5553                         _scsih_probe_sas(ioc);
5554                 }
5555         } else
5556                 _scsih_probe_sas(ioc);
5557 }
5558
5559 /**
5560  * scsih_probe - attach and add scsi host
5561  * @pdev: PCI device struct
5562  * @id: pci device id
5563  *
5564  * Returns 0 success, anything else error.
5565  */
5566 static int
5567 scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
5568 {
5569         struct MPT2SAS_ADAPTER *ioc;
5570         struct Scsi_Host *shost;
5571
5572         shost = scsi_host_alloc(&scsih_driver_template,
5573             sizeof(struct MPT2SAS_ADAPTER));
5574         if (!shost)
5575                 return -ENODEV;
5576
5577         /* init local params */
5578         ioc = shost_priv(shost);
5579         memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
5580         INIT_LIST_HEAD(&ioc->list);
5581         list_add_tail(&ioc->list, &mpt2sas_ioc_list);
5582         ioc->shost = shost;
5583         ioc->id = mpt_ids++;
5584         sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
5585         ioc->pdev = pdev;
5586         ioc->scsi_io_cb_idx = scsi_io_cb_idx;
5587         ioc->tm_cb_idx = tm_cb_idx;
5588         ioc->ctl_cb_idx = ctl_cb_idx;
5589         ioc->base_cb_idx = base_cb_idx;
5590         ioc->transport_cb_idx = transport_cb_idx;
5591         ioc->config_cb_idx = config_cb_idx;
5592         ioc->logging_level = logging_level;
5593         /* misc semaphores and spin locks */
5594         spin_lock_init(&ioc->ioc_reset_in_progress_lock);
5595         spin_lock_init(&ioc->scsi_lookup_lock);
5596         spin_lock_init(&ioc->sas_device_lock);
5597         spin_lock_init(&ioc->sas_node_lock);
5598         spin_lock_init(&ioc->fw_event_lock);
5599         spin_lock_init(&ioc->raid_device_lock);
5600
5601         INIT_LIST_HEAD(&ioc->sas_device_list);
5602         INIT_LIST_HEAD(&ioc->sas_device_init_list);
5603         INIT_LIST_HEAD(&ioc->sas_expander_list);
5604         INIT_LIST_HEAD(&ioc->fw_event_list);
5605         INIT_LIST_HEAD(&ioc->raid_device_list);
5606         INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
5607
5608         /* init shost parameters */
5609         shost->max_cmd_len = 16;
5610         shost->max_lun = max_lun;
5611         shost->transportt = mpt2sas_transport_template;
5612         shost->unique_id = ioc->id;
5613
5614         if ((scsi_add_host(shost, &pdev->dev))) {
5615                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5616                     ioc->name, __FILE__, __LINE__, __func__);
5617                 list_del(&ioc->list);
5618                 goto out_add_shost_fail;
5619         }
5620
5621         scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
5622             | SHOST_DIF_TYPE3_PROTECTION);
5623
5624         /* event thread */
5625         snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
5626             "fw_event%d", ioc->id);
5627         ioc->firmware_event_thread = create_singlethread_workqueue(
5628             ioc->firmware_event_name);
5629         if (!ioc->firmware_event_thread) {
5630                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5631                     ioc->name, __FILE__, __LINE__, __func__);
5632                 goto out_thread_fail;
5633         }
5634
5635         ioc->wait_for_port_enable_to_complete = 1;
5636         if ((mpt2sas_base_attach(ioc))) {
5637                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5638                     ioc->name, __FILE__, __LINE__, __func__);
5639                 goto out_attach_fail;
5640         }
5641
5642         ioc->wait_for_port_enable_to_complete = 0;
5643         _scsih_probe_devices(ioc);
5644         return 0;
5645
5646  out_attach_fail:
5647         destroy_workqueue(ioc->firmware_event_thread);
5648  out_thread_fail:
5649         list_del(&ioc->list);
5650         scsi_remove_host(shost);
5651  out_add_shost_fail:
5652         return -ENODEV;
5653 }
5654
5655 #ifdef CONFIG_PM
5656 /**
5657  * scsih_suspend - power management suspend main entry point
5658  * @pdev: PCI device struct
5659  * @state: PM state change to (usually PCI_D3)
5660  *
5661  * Returns 0 success, anything else error.
5662  */
5663 static int
5664 scsih_suspend(struct pci_dev *pdev, pm_message_t state)
5665 {
5666         struct Scsi_Host *shost = pci_get_drvdata(pdev);
5667         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5668         u32 device_state;
5669
5670         flush_scheduled_work();
5671         scsi_block_requests(shost);
5672         device_state = pci_choose_state(pdev, state);
5673         printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
5674             "operating state [D%d]\n", ioc->name, pdev,
5675             pci_name(pdev), device_state);
5676
5677         mpt2sas_base_free_resources(ioc);
5678         pci_save_state(pdev);
5679         pci_disable_device(pdev);
5680         pci_set_power_state(pdev, device_state);
5681         return 0;
5682 }
5683
5684 /**
5685  * scsih_resume - power management resume main entry point
5686  * @pdev: PCI device struct
5687  *
5688  * Returns 0 success, anything else error.
5689  */
5690 static int
5691 scsih_resume(struct pci_dev *pdev)
5692 {
5693         struct Scsi_Host *shost = pci_get_drvdata(pdev);
5694         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5695         u32 device_state = pdev->current_state;
5696         int r;
5697
5698         printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
5699             "operating state [D%d]\n", ioc->name, pdev,
5700             pci_name(pdev), device_state);
5701
5702         pci_set_power_state(pdev, PCI_D0);
5703         pci_enable_wake(pdev, PCI_D0, 0);
5704         pci_restore_state(pdev);
5705         ioc->pdev = pdev;
5706         r = mpt2sas_base_map_resources(ioc);
5707         if (r)
5708                 return r;
5709
5710         mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
5711         scsi_unblock_requests(shost);
5712         return 0;
5713 }
5714 #endif /* CONFIG_PM */
5715
5716
5717 static struct pci_driver scsih_driver = {
5718         .name           = MPT2SAS_DRIVER_NAME,
5719         .id_table       = scsih_pci_table,
5720         .probe          = scsih_probe,
5721         .remove         = __devexit_p(scsih_remove),
5722 #ifdef CONFIG_PM
5723         .suspend        = scsih_suspend,
5724         .resume         = scsih_resume,
5725 #endif
5726 };
5727
5728
5729 /**
5730  * scsih_init - main entry point for this driver.
5731  *
5732  * Returns 0 success, anything else error.
5733  */
5734 static int __init
5735 scsih_init(void)
5736 {
5737         int error;
5738
5739         mpt_ids = 0;
5740         printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
5741             MPT2SAS_DRIVER_VERSION);
5742
5743         mpt2sas_transport_template =
5744             sas_attach_transport(&mpt2sas_transport_functions);
5745         if (!mpt2sas_transport_template)
5746                 return -ENODEV;
5747
5748         mpt2sas_base_initialize_callback_handler();
5749
5750          /* queuecommand callback hander */
5751         scsi_io_cb_idx = mpt2sas_base_register_callback_handler(scsih_io_done);
5752
5753         /* task managment callback handler */
5754         tm_cb_idx = mpt2sas_base_register_callback_handler(scsih_tm_done);
5755
5756         /* base internal commands callback handler */
5757         base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
5758
5759         /* transport internal commands callback handler */
5760         transport_cb_idx = mpt2sas_base_register_callback_handler(
5761             mpt2sas_transport_done);
5762
5763         /* configuration page API internal commands callback handler */
5764         config_cb_idx = mpt2sas_base_register_callback_handler(
5765             mpt2sas_config_done);
5766
5767         /* ctl module callback handler */
5768         ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
5769
5770         mpt2sas_ctl_init();
5771
5772         error = pci_register_driver(&scsih_driver);
5773         if (error)
5774                 sas_release_transport(mpt2sas_transport_template);
5775
5776         return error;
5777 }
5778
5779 /**
5780  * scsih_exit - exit point for this driver (when it is a module).
5781  *
5782  * Returns 0 success, anything else error.
5783  */
5784 static void __exit
5785 scsih_exit(void)
5786 {
5787         printk(KERN_INFO "mpt2sas version %s unloading\n",
5788             MPT2SAS_DRIVER_VERSION);
5789
5790         pci_unregister_driver(&scsih_driver);
5791
5792         sas_release_transport(mpt2sas_transport_template);
5793         mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
5794         mpt2sas_base_release_callback_handler(tm_cb_idx);
5795         mpt2sas_base_release_callback_handler(base_cb_idx);
5796         mpt2sas_base_release_callback_handler(transport_cb_idx);
5797         mpt2sas_base_release_callback_handler(config_cb_idx);
5798         mpt2sas_base_release_callback_handler(ctl_cb_idx);
5799
5800         mpt2sas_ctl_exit();
5801 }
5802
5803 module_init(scsih_init);
5804 module_exit(scsih_exit);