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