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