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