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