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