blob: 6e6d5af6b365f07e57e406591347abe7d8893258 [file] [log] [blame]
Eric Moore635374e2009-03-09 01:21:12 -06001/*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
5 * Copyright (C) 2007-2008 LSI Corporation
6 * (mailto:DL-MPTFusionLinux@lsi.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * NO WARRANTY
19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 * solely responsible for determining the appropriateness of using and
24 * distributing the Program and assumes all risks associated with its
25 * exercise of rights under this Agreement, including but not limited to
26 * the risks and costs of program errors, damage to or loss of data,
27 * programs or equipment, and unavailability or interruption of operations.
28
29 * DISCLAIMER OF LIABILITY
30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
41 * USA.
42 */
43
44#include <linux/version.h>
45#include <linux/module.h>
46#include <linux/kernel.h>
47#include <linux/init.h>
48#include <linux/errno.h>
49#include <linux/blkdev.h>
50#include <linux/sched.h>
51#include <linux/workqueue.h>
52#include <linux/delay.h>
53#include <linux/pci.h>
54#include <linux/interrupt.h>
55
56#include "mpt2sas_base.h"
57
58MODULE_AUTHOR(MPT2SAS_AUTHOR);
59MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
60MODULE_LICENSE("GPL");
61MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
62
63#define RAID_CHANNEL 1
64
65/* forward proto's */
66static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
67 struct _sas_node *sas_expander);
68static void _firmware_event_work(struct work_struct *work);
69
70/* global parameters */
Eric Mooreba33fad2009-03-15 21:37:18 -060071LIST_HEAD(mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -060072
73/* local parameters */
Eric Moore635374e2009-03-09 01:21:12 -060074static u8 scsi_io_cb_idx = -1;
75static u8 tm_cb_idx = -1;
76static u8 ctl_cb_idx = -1;
77static u8 base_cb_idx = -1;
78static u8 transport_cb_idx = -1;
79static u8 config_cb_idx = -1;
80static int mpt_ids;
81
82/* command line options */
Eric Mooreba33fad2009-03-15 21:37:18 -060083static u32 logging_level;
Eric Moore635374e2009-03-09 01:21:12 -060084MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
85 "(default=0)");
86
87/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
88#define MPT2SAS_MAX_LUN (16895)
89static int max_lun = MPT2SAS_MAX_LUN;
90module_param(max_lun, int, 0);
91MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
92
93/**
94 * struct sense_info - common structure for obtaining sense keys
95 * @skey: sense key
96 * @asc: additional sense code
97 * @ascq: additional sense code qualifier
98 */
99struct sense_info {
100 u8 skey;
101 u8 asc;
102 u8 ascq;
103};
104
105
Eric Moore635374e2009-03-09 01:21:12 -0600106/**
107 * struct fw_event_work - firmware event struct
108 * @list: link list framework
109 * @work: work object (ioc->fault_reset_work_q)
110 * @ioc: per adapter object
111 * @VF_ID: virtual function id
112 * @host_reset_handling: handling events during host reset
113 * @ignore: flag meaning this event has been marked to ignore
114 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
115 * @event_data: reply event data payload follows
116 *
117 * This object stored on ioc->fw_event_list.
118 */
119struct fw_event_work {
120 struct list_head list;
Eric Moore6f92a7a2009-04-21 15:43:33 -0600121 struct work_struct work;
Eric Moore635374e2009-03-09 01:21:12 -0600122 struct MPT2SAS_ADAPTER *ioc;
123 u8 VF_ID;
124 u8 host_reset_handling;
125 u8 ignore;
126 u16 event;
127 void *event_data;
128};
129
130/**
131 * struct _scsi_io_transfer - scsi io transfer
132 * @handle: sas device handle (assigned by firmware)
133 * @is_raid: flag set for hidden raid components
134 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
135 * @data_length: data transfer length
136 * @data_dma: dma pointer to data
137 * @sense: sense data
138 * @lun: lun number
139 * @cdb_length: cdb length
140 * @cdb: cdb contents
141 * @valid_reply: flag set for reply message
142 * @timeout: timeout for this command
143 * @sense_length: sense length
144 * @ioc_status: ioc status
145 * @scsi_state: scsi state
146 * @scsi_status: scsi staus
147 * @log_info: log information
148 * @transfer_length: data length transfer when there is a reply message
149 *
150 * Used for sending internal scsi commands to devices within this module.
151 * Refer to _scsi_send_scsi_io().
152 */
153struct _scsi_io_transfer {
154 u16 handle;
155 u8 is_raid;
156 enum dma_data_direction dir;
157 u32 data_length;
158 dma_addr_t data_dma;
159 u8 sense[SCSI_SENSE_BUFFERSIZE];
160 u32 lun;
161 u8 cdb_length;
162 u8 cdb[32];
163 u8 timeout;
164 u8 valid_reply;
165 /* the following bits are only valid when 'valid_reply = 1' */
166 u32 sense_length;
167 u16 ioc_status;
168 u8 scsi_state;
169 u8 scsi_status;
170 u32 log_info;
171 u32 transfer_length;
172};
173
174/*
175 * The pci device ids are defined in mpi/mpi2_cnfg.h.
176 */
177static struct pci_device_id scsih_pci_table[] = {
178 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
179 PCI_ANY_ID, PCI_ANY_ID },
180 /* Falcon ~ 2008*/
181 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
182 PCI_ANY_ID, PCI_ANY_ID },
183 /* Liberator ~ 2108 */
184 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
185 PCI_ANY_ID, PCI_ANY_ID },
186 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
187 PCI_ANY_ID, PCI_ANY_ID },
188 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
189 PCI_ANY_ID, PCI_ANY_ID },
190 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
191 PCI_ANY_ID, PCI_ANY_ID },
192 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
193 PCI_ANY_ID, PCI_ANY_ID },
194 {0} /* Terminating entry */
195};
196MODULE_DEVICE_TABLE(pci, scsih_pci_table);
197
198/**
Eric Moored5d135b2009-05-18 13:02:08 -0600199 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600200 *
201 * Note: The logging levels are defined in mpt2sas_debug.h.
202 */
203static int
Eric Moored5d135b2009-05-18 13:02:08 -0600204_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600205{
206 int ret = param_set_int(val, kp);
207 struct MPT2SAS_ADAPTER *ioc;
208
209 if (ret)
210 return ret;
211
212 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600213 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600214 ioc->logging_level = logging_level;
215 return 0;
216}
Eric Moored5d135b2009-05-18 13:02:08 -0600217module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600218 &logging_level, 0644);
219
220/**
221 * _scsih_srch_boot_sas_address - search based on sas_address
222 * @sas_address: sas address
223 * @boot_device: boot device object from bios page 2
224 *
225 * Returns 1 when there's a match, 0 means no match.
226 */
227static inline int
228_scsih_srch_boot_sas_address(u64 sas_address,
229 Mpi2BootDeviceSasWwid_t *boot_device)
230{
231 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
232}
233
234/**
235 * _scsih_srch_boot_device_name - search based on device name
236 * @device_name: device name specified in INDENTIFY fram
237 * @boot_device: boot device object from bios page 2
238 *
239 * Returns 1 when there's a match, 0 means no match.
240 */
241static inline int
242_scsih_srch_boot_device_name(u64 device_name,
243 Mpi2BootDeviceDeviceName_t *boot_device)
244{
245 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
246}
247
248/**
249 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
250 * @enclosure_logical_id: enclosure logical id
251 * @slot_number: slot number
252 * @boot_device: boot device object from bios page 2
253 *
254 * Returns 1 when there's a match, 0 means no match.
255 */
256static inline int
257_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
258 Mpi2BootDeviceEnclosureSlot_t *boot_device)
259{
260 return (enclosure_logical_id == le64_to_cpu(boot_device->
261 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
262 SlotNumber)) ? 1 : 0;
263}
264
265/**
266 * _scsih_is_boot_device - search for matching boot device.
267 * @sas_address: sas address
268 * @device_name: device name specified in INDENTIFY fram
269 * @enclosure_logical_id: enclosure logical id
270 * @slot_number: slot number
271 * @form: specifies boot device form
272 * @boot_device: boot device object from bios page 2
273 *
274 * Returns 1 when there's a match, 0 means no match.
275 */
276static int
277_scsih_is_boot_device(u64 sas_address, u64 device_name,
278 u64 enclosure_logical_id, u16 slot, u8 form,
279 Mpi2BiosPage2BootDevice_t *boot_device)
280{
281 int rc = 0;
282
283 switch (form) {
284 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
285 if (!sas_address)
286 break;
287 rc = _scsih_srch_boot_sas_address(
288 sas_address, &boot_device->SasWwid);
289 break;
290 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
291 if (!enclosure_logical_id)
292 break;
293 rc = _scsih_srch_boot_encl_slot(
294 enclosure_logical_id,
295 slot, &boot_device->EnclosureSlot);
296 break;
297 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
298 if (!device_name)
299 break;
300 rc = _scsih_srch_boot_device_name(
301 device_name, &boot_device->DeviceName);
302 break;
303 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
304 break;
305 }
306
307 return rc;
308}
309
310/**
311 * _scsih_determine_boot_device - determine boot device.
312 * @ioc: per adapter object
313 * @device: either sas_device or raid_device object
314 * @is_raid: [flag] 1 = raid object, 0 = sas object
315 *
316 * Determines whether this device should be first reported device to
317 * to scsi-ml or sas transport, this purpose is for persistant boot device.
318 * There are primary, alternate, and current entries in bios page 2. The order
319 * priority is primary, alternate, then current. This routine saves
320 * the corresponding device object and is_raid flag in the ioc object.
321 * The saved data to be used later in _scsih_probe_boot_devices().
322 */
323static void
324_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
325 void *device, u8 is_raid)
326{
327 struct _sas_device *sas_device;
328 struct _raid_device *raid_device;
329 u64 sas_address;
330 u64 device_name;
331 u64 enclosure_logical_id;
332 u16 slot;
333
334 /* only process this function when driver loads */
335 if (!ioc->wait_for_port_enable_to_complete)
336 return;
337
338 if (!is_raid) {
339 sas_device = device;
340 sas_address = sas_device->sas_address;
341 device_name = sas_device->device_name;
342 enclosure_logical_id = sas_device->enclosure_logical_id;
343 slot = sas_device->slot;
344 } else {
345 raid_device = device;
346 sas_address = raid_device->wwid;
347 device_name = 0;
348 enclosure_logical_id = 0;
349 slot = 0;
350 }
351
352 if (!ioc->req_boot_device.device) {
353 if (_scsih_is_boot_device(sas_address, device_name,
354 enclosure_logical_id, slot,
355 (ioc->bios_pg2.ReqBootDeviceForm &
356 MPI2_BIOSPAGE2_FORM_MASK),
357 &ioc->bios_pg2.RequestedBootDevice)) {
358 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
359 "%s: req_boot_device(0x%016llx)\n",
360 ioc->name, __func__,
361 (unsigned long long)sas_address));
362 ioc->req_boot_device.device = device;
363 ioc->req_boot_device.is_raid = is_raid;
364 }
365 }
366
367 if (!ioc->req_alt_boot_device.device) {
368 if (_scsih_is_boot_device(sas_address, device_name,
369 enclosure_logical_id, slot,
370 (ioc->bios_pg2.ReqAltBootDeviceForm &
371 MPI2_BIOSPAGE2_FORM_MASK),
372 &ioc->bios_pg2.RequestedAltBootDevice)) {
373 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
374 "%s: req_alt_boot_device(0x%016llx)\n",
375 ioc->name, __func__,
376 (unsigned long long)sas_address));
377 ioc->req_alt_boot_device.device = device;
378 ioc->req_alt_boot_device.is_raid = is_raid;
379 }
380 }
381
382 if (!ioc->current_boot_device.device) {
383 if (_scsih_is_boot_device(sas_address, device_name,
384 enclosure_logical_id, slot,
385 (ioc->bios_pg2.CurrentBootDeviceForm &
386 MPI2_BIOSPAGE2_FORM_MASK),
387 &ioc->bios_pg2.CurrentBootDevice)) {
388 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
389 "%s: current_boot_device(0x%016llx)\n",
390 ioc->name, __func__,
391 (unsigned long long)sas_address));
392 ioc->current_boot_device.device = device;
393 ioc->current_boot_device.is_raid = is_raid;
394 }
395 }
396}
397
398/**
399 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
400 * @ioc: per adapter object
401 * @sas_address: sas address
402 * Context: Calling function should acquire ioc->sas_device_lock
403 *
404 * This searches for sas_device based on sas_address, then return sas_device
405 * object.
406 */
407struct _sas_device *
408mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
409 u64 sas_address)
410{
411 struct _sas_device *sas_device, *r;
412
413 r = NULL;
414 /* check the sas_device_init_list */
415 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
416 list) {
417 if (sas_device->sas_address != sas_address)
418 continue;
419 r = sas_device;
420 goto out;
421 }
422
423 /* then check the sas_device_list */
424 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
425 if (sas_device->sas_address != sas_address)
426 continue;
427 r = sas_device;
428 goto out;
429 }
430 out:
431 return r;
432}
433
434/**
435 * _scsih_sas_device_find_by_handle - sas device search
436 * @ioc: per adapter object
437 * @handle: sas device handle (assigned by firmware)
438 * Context: Calling function should acquire ioc->sas_device_lock
439 *
440 * This searches for sas_device based on sas_address, then return sas_device
441 * object.
442 */
443static struct _sas_device *
444_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
445{
446 struct _sas_device *sas_device, *r;
447
448 r = NULL;
449 if (ioc->wait_for_port_enable_to_complete) {
450 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
451 list) {
452 if (sas_device->handle != handle)
453 continue;
454 r = sas_device;
455 goto out;
456 }
457 } else {
458 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
459 if (sas_device->handle != handle)
460 continue;
461 r = sas_device;
462 goto out;
463 }
464 }
465
466 out:
467 return r;
468}
469
470/**
471 * _scsih_sas_device_remove - remove sas_device from list.
472 * @ioc: per adapter object
473 * @sas_device: the sas_device object
474 * Context: This function will acquire ioc->sas_device_lock.
475 *
476 * Removing object and freeing associated memory from the ioc->sas_device_list.
477 */
478static void
479_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
480 struct _sas_device *sas_device)
481{
482 unsigned long flags;
483
484 spin_lock_irqsave(&ioc->sas_device_lock, flags);
485 list_del(&sas_device->list);
486 memset(sas_device, 0, sizeof(struct _sas_device));
487 kfree(sas_device);
488 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
489}
490
491/**
492 * _scsih_sas_device_add - insert sas_device to the list.
493 * @ioc: per adapter object
494 * @sas_device: the sas_device object
495 * Context: This function will acquire ioc->sas_device_lock.
496 *
497 * Adding new object to the ioc->sas_device_list.
498 */
499static void
500_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
501 struct _sas_device *sas_device)
502{
503 unsigned long flags;
504 u16 handle, parent_handle;
505 u64 sas_address;
506
507 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
508 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
509 sas_device->handle, (unsigned long long)sas_device->sas_address));
510
511 spin_lock_irqsave(&ioc->sas_device_lock, flags);
512 list_add_tail(&sas_device->list, &ioc->sas_device_list);
513 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
514
515 handle = sas_device->handle;
516 parent_handle = sas_device->parent_handle;
517 sas_address = sas_device->sas_address;
Eric Moore8901cbb2009-04-21 15:41:32 -0600518 if (!mpt2sas_transport_port_add(ioc, handle, parent_handle))
Eric Moore635374e2009-03-09 01:21:12 -0600519 _scsih_sas_device_remove(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -0600520}
521
522/**
523 * _scsih_sas_device_init_add - insert sas_device to the list.
524 * @ioc: per adapter object
525 * @sas_device: the sas_device object
526 * Context: This function will acquire ioc->sas_device_lock.
527 *
528 * Adding new object at driver load time to the ioc->sas_device_init_list.
529 */
530static void
531_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
532 struct _sas_device *sas_device)
533{
534 unsigned long flags;
535
536 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
537 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
538 sas_device->handle, (unsigned long long)sas_device->sas_address));
539
540 spin_lock_irqsave(&ioc->sas_device_lock, flags);
541 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
542 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
543 _scsih_determine_boot_device(ioc, sas_device, 0);
544}
545
546/**
547 * mpt2sas_scsih_expander_find_by_handle - expander device search
548 * @ioc: per adapter object
549 * @handle: expander handle (assigned by firmware)
550 * Context: Calling function should acquire ioc->sas_device_lock
551 *
552 * This searches for expander device based on handle, then returns the
553 * sas_node object.
554 */
555struct _sas_node *
556mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
557{
558 struct _sas_node *sas_expander, *r;
559
560 r = NULL;
561 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
562 if (sas_expander->handle != handle)
563 continue;
564 r = sas_expander;
565 goto out;
566 }
567 out:
568 return r;
569}
570
571/**
572 * _scsih_raid_device_find_by_id - raid device search
573 * @ioc: per adapter object
574 * @id: sas device target id
575 * @channel: sas device channel
576 * Context: Calling function should acquire ioc->raid_device_lock
577 *
578 * This searches for raid_device based on target id, then return raid_device
579 * object.
580 */
581static struct _raid_device *
582_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
583{
584 struct _raid_device *raid_device, *r;
585
586 r = NULL;
587 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
588 if (raid_device->id == id && raid_device->channel == channel) {
589 r = raid_device;
590 goto out;
591 }
592 }
593
594 out:
595 return r;
596}
597
598/**
599 * _scsih_raid_device_find_by_handle - raid device search
600 * @ioc: per adapter object
601 * @handle: sas device handle (assigned by firmware)
602 * Context: Calling function should acquire ioc->raid_device_lock
603 *
604 * This searches for raid_device based on handle, then return raid_device
605 * object.
606 */
607static struct _raid_device *
608_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
609{
610 struct _raid_device *raid_device, *r;
611
612 r = NULL;
613 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
614 if (raid_device->handle != handle)
615 continue;
616 r = raid_device;
617 goto out;
618 }
619
620 out:
621 return r;
622}
623
624/**
625 * _scsih_raid_device_find_by_wwid - raid device search
626 * @ioc: per adapter object
627 * @handle: sas device handle (assigned by firmware)
628 * Context: Calling function should acquire ioc->raid_device_lock
629 *
630 * This searches for raid_device based on wwid, then return raid_device
631 * object.
632 */
633static struct _raid_device *
634_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
635{
636 struct _raid_device *raid_device, *r;
637
638 r = NULL;
639 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
640 if (raid_device->wwid != wwid)
641 continue;
642 r = raid_device;
643 goto out;
644 }
645
646 out:
647 return r;
648}
649
650/**
651 * _scsih_raid_device_add - add raid_device object
652 * @ioc: per adapter object
653 * @raid_device: raid_device object
654 *
655 * This is added to the raid_device_list link list.
656 */
657static void
658_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
659 struct _raid_device *raid_device)
660{
661 unsigned long flags;
662
663 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
664 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
665 raid_device->handle, (unsigned long long)raid_device->wwid));
666
667 spin_lock_irqsave(&ioc->raid_device_lock, flags);
668 list_add_tail(&raid_device->list, &ioc->raid_device_list);
669 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
670}
671
672/**
673 * _scsih_raid_device_remove - delete raid_device object
674 * @ioc: per adapter object
675 * @raid_device: raid_device object
676 *
677 * This is removed from the raid_device_list link list.
678 */
679static void
680_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
681 struct _raid_device *raid_device)
682{
683 unsigned long flags;
684
685 spin_lock_irqsave(&ioc->raid_device_lock, flags);
686 list_del(&raid_device->list);
687 memset(raid_device, 0, sizeof(struct _raid_device));
688 kfree(raid_device);
689 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
690}
691
692/**
693 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
694 * @ioc: per adapter object
695 * @sas_address: sas address
696 * Context: Calling function should acquire ioc->sas_node_lock.
697 *
698 * This searches for expander device based on sas_address, then returns the
699 * sas_node object.
700 */
701struct _sas_node *
702mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
703 u64 sas_address)
704{
705 struct _sas_node *sas_expander, *r;
706
707 r = NULL;
708 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
709 if (sas_expander->sas_address != sas_address)
710 continue;
711 r = sas_expander;
712 goto out;
713 }
714 out:
715 return r;
716}
717
718/**
719 * _scsih_expander_node_add - insert expander device to the list.
720 * @ioc: per adapter object
721 * @sas_expander: the sas_device object
722 * Context: This function will acquire ioc->sas_node_lock.
723 *
724 * Adding new object to the ioc->sas_expander_list.
725 *
726 * Return nothing.
727 */
728static void
729_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
730 struct _sas_node *sas_expander)
731{
732 unsigned long flags;
733
734 spin_lock_irqsave(&ioc->sas_node_lock, flags);
735 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
736 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
737}
738
739/**
740 * _scsih_is_end_device - determines if device is an end device
741 * @device_info: bitfield providing information about the device.
742 * Context: none
743 *
744 * Returns 1 if end device.
745 */
746static int
747_scsih_is_end_device(u32 device_info)
748{
749 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
750 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
751 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
752 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
753 return 1;
754 else
755 return 0;
756}
757
758/**
759 * _scsih_scsi_lookup_get - returns scmd entry
760 * @ioc: per adapter object
761 * @smid: system request message index
762 * Context: This function will acquire ioc->scsi_lookup_lock.
763 *
764 * Returns the smid stored scmd pointer.
765 */
766static struct scsi_cmnd *
767_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
768{
769 unsigned long flags;
770 struct scsi_cmnd *scmd;
771
772 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
773 scmd = ioc->scsi_lookup[smid - 1].scmd;
774 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
775 return scmd;
776}
777
778/**
779 * mptscsih_getclear_scsi_lookup - returns scmd entry
780 * @ioc: per adapter object
781 * @smid: system request message index
782 * Context: This function will acquire ioc->scsi_lookup_lock.
783 *
784 * Returns the smid stored scmd pointer, as well as clearing the scmd pointer.
785 */
786static struct scsi_cmnd *
787_scsih_scsi_lookup_getclear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
788{
789 unsigned long flags;
790 struct scsi_cmnd *scmd;
791
792 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
793 scmd = ioc->scsi_lookup[smid - 1].scmd;
794 ioc->scsi_lookup[smid - 1].scmd = NULL;
795 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
796 return scmd;
797}
798
799/**
800 * _scsih_scsi_lookup_set - updates scmd entry in lookup
801 * @ioc: per adapter object
802 * @smid: system request message index
803 * @scmd: pointer to scsi command object
804 * Context: This function will acquire ioc->scsi_lookup_lock.
805 *
806 * This will save scmd pointer in the scsi_lookup array.
807 *
808 * Return nothing.
809 */
810static void
811_scsih_scsi_lookup_set(struct MPT2SAS_ADAPTER *ioc, u16 smid,
812 struct scsi_cmnd *scmd)
813{
814 unsigned long flags;
815
816 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
817 ioc->scsi_lookup[smid - 1].scmd = scmd;
818 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
819}
820
821/**
822 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
823 * @ioc: per adapter object
824 * @smid: system request message index
825 * @scmd: pointer to scsi command object
826 * Context: This function will acquire ioc->scsi_lookup_lock.
827 *
828 * This will search for a scmd pointer in the scsi_lookup array,
829 * returning the revelent smid. A returned value of zero means invalid.
830 */
831static u16
832_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
833 *scmd)
834{
835 u16 smid;
836 unsigned long flags;
837 int i;
838
839 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
840 smid = 0;
841 for (i = 0; i < ioc->request_depth; i++) {
842 if (ioc->scsi_lookup[i].scmd == scmd) {
843 smid = i + 1;
844 goto out;
845 }
846 }
847 out:
848 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
849 return smid;
850}
851
852/**
853 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
854 * @ioc: per adapter object
855 * @id: target id
856 * @channel: channel
857 * Context: This function will acquire ioc->scsi_lookup_lock.
858 *
859 * This will search for a matching channel:id in the scsi_lookup array,
860 * returning 1 if found.
861 */
862static u8
863_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
864 int channel)
865{
866 u8 found;
867 unsigned long flags;
868 int i;
869
870 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
871 found = 0;
872 for (i = 0 ; i < ioc->request_depth; i++) {
873 if (ioc->scsi_lookup[i].scmd &&
874 (ioc->scsi_lookup[i].scmd->device->id == id &&
875 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
876 found = 1;
877 goto out;
878 }
879 }
880 out:
881 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
882 return found;
883}
884
885/**
Eric Moore993e0da2009-05-18 13:00:45 -0600886 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
887 * @ioc: per adapter object
888 * @id: target id
889 * @lun: lun number
890 * @channel: channel
891 * Context: This function will acquire ioc->scsi_lookup_lock.
892 *
893 * This will search for a matching channel:id:lun in the scsi_lookup array,
894 * returning 1 if found.
895 */
896static u8
897_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
898 unsigned int lun, int channel)
899{
900 u8 found;
901 unsigned long flags;
902 int i;
903
904 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
905 found = 0;
906 for (i = 0 ; i < ioc->request_depth; i++) {
907 if (ioc->scsi_lookup[i].scmd &&
908 (ioc->scsi_lookup[i].scmd->device->id == id &&
909 ioc->scsi_lookup[i].scmd->device->channel == channel &&
910 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
911 found = 1;
912 goto out;
913 }
914 }
915 out:
916 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
917 return found;
918}
919
920/**
Eric Moore635374e2009-03-09 01:21:12 -0600921 * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
922 * @ioc: per adapter object
923 * @smid: system request message index
924 *
925 * Returns phys pointer to chain buffer.
926 */
927static dma_addr_t
928_scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
929{
930 return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
931 ioc->chains_needed_per_io));
932}
933
934/**
935 * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
936 * @ioc: per adapter object
937 * @smid: system request message index
938 *
939 * Returns virt pointer to chain buffer.
940 */
941static void *
942_scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
943{
944 return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
945 ioc->chains_needed_per_io)));
946}
947
948/**
949 * _scsih_build_scatter_gather - main sg creation routine
950 * @ioc: per adapter object
951 * @scmd: scsi command
952 * @smid: system request message index
953 * Context: none.
954 *
955 * The main routine that builds scatter gather table from a given
956 * scsi request sent via the .queuecommand main handler.
957 *
958 * Returns 0 success, anything else error
959 */
960static int
961_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
962 struct scsi_cmnd *scmd, u16 smid)
963{
964 Mpi2SCSIIORequest_t *mpi_request;
965 dma_addr_t chain_dma;
966 struct scatterlist *sg_scmd;
967 void *sg_local, *chain;
968 u32 chain_offset;
969 u32 chain_length;
970 u32 chain_flags;
971 u32 sges_left;
972 u32 sges_in_segment;
973 u32 sgl_flags;
974 u32 sgl_flags_last_element;
975 u32 sgl_flags_end_buffer;
976
977 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
978
979 /* init scatter gather flags */
980 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
981 if (scmd->sc_data_direction == DMA_TO_DEVICE)
982 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
983 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
984 << MPI2_SGE_FLAGS_SHIFT;
985 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
986 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
987 << MPI2_SGE_FLAGS_SHIFT;
988 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
989
990 sg_scmd = scsi_sglist(scmd);
991 sges_left = scsi_dma_map(scmd);
992 if (!sges_left) {
993 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
994 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
995 return -ENOMEM;
996 }
997
998 sg_local = &mpi_request->SGL;
999 sges_in_segment = ioc->max_sges_in_main_message;
1000 if (sges_left <= sges_in_segment)
1001 goto fill_in_last_segment;
1002
1003 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1004 (sges_in_segment * ioc->sge_size))/4;
1005
1006 /* fill in main message segment when there is a chain following */
1007 while (sges_in_segment) {
1008 if (sges_in_segment == 1)
1009 ioc->base_add_sg_single(sg_local,
1010 sgl_flags_last_element | sg_dma_len(sg_scmd),
1011 sg_dma_address(sg_scmd));
1012 else
1013 ioc->base_add_sg_single(sg_local, sgl_flags |
1014 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1015 sg_scmd = sg_next(sg_scmd);
1016 sg_local += ioc->sge_size;
1017 sges_left--;
1018 sges_in_segment--;
1019 }
1020
1021 /* initializing the chain flags and pointers */
1022 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1023 chain = _scsih_get_chain_buffer(ioc, smid);
1024 chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
1025 do {
1026 sges_in_segment = (sges_left <=
1027 ioc->max_sges_in_chain_message) ? sges_left :
1028 ioc->max_sges_in_chain_message;
1029 chain_offset = (sges_left == sges_in_segment) ?
1030 0 : (sges_in_segment * ioc->sge_size)/4;
1031 chain_length = sges_in_segment * ioc->sge_size;
1032 if (chain_offset) {
1033 chain_offset = chain_offset <<
1034 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1035 chain_length += ioc->sge_size;
1036 }
1037 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1038 chain_length, chain_dma);
1039 sg_local = chain;
1040 if (!chain_offset)
1041 goto fill_in_last_segment;
1042
1043 /* fill in chain segments */
1044 while (sges_in_segment) {
1045 if (sges_in_segment == 1)
1046 ioc->base_add_sg_single(sg_local,
1047 sgl_flags_last_element |
1048 sg_dma_len(sg_scmd),
1049 sg_dma_address(sg_scmd));
1050 else
1051 ioc->base_add_sg_single(sg_local, sgl_flags |
1052 sg_dma_len(sg_scmd),
1053 sg_dma_address(sg_scmd));
1054 sg_scmd = sg_next(sg_scmd);
1055 sg_local += ioc->sge_size;
1056 sges_left--;
1057 sges_in_segment--;
1058 }
1059
1060 chain_dma += ioc->request_sz;
1061 chain += ioc->request_sz;
1062 } while (1);
1063
1064
1065 fill_in_last_segment:
1066
1067 /* fill the last segment */
1068 while (sges_left) {
1069 if (sges_left == 1)
1070 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1071 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1072 else
1073 ioc->base_add_sg_single(sg_local, sgl_flags |
1074 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1075 sg_scmd = sg_next(sg_scmd);
1076 sg_local += ioc->sge_size;
1077 sges_left--;
1078 }
1079
1080 return 0;
1081}
1082
1083/**
Eric Moored5d135b2009-05-18 13:02:08 -06001084 * _scsih_change_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001085 * @sdev: scsi device struct
1086 * @qdepth: requested queue depth
1087 *
1088 * Returns queue depth.
1089 */
1090static int
Eric Moored5d135b2009-05-18 13:02:08 -06001091_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
Eric Moore635374e2009-03-09 01:21:12 -06001092{
1093 struct Scsi_Host *shost = sdev->host;
1094 int max_depth;
1095 int tag_type;
1096
1097 max_depth = shost->can_queue;
1098 if (!sdev->tagged_supported)
1099 max_depth = 1;
1100 if (qdepth > max_depth)
1101 qdepth = max_depth;
1102 tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
1103 scsi_adjust_queue_depth(sdev, tag_type, qdepth);
1104
1105 if (sdev->inquiry_len > 7)
1106 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1107 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1108 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1109 sdev->ordered_tags, sdev->scsi_level,
1110 (sdev->inquiry[7] & 2) >> 1);
1111
1112 return sdev->queue_depth;
1113}
1114
1115/**
Eric Moored5d135b2009-05-18 13:02:08 -06001116 * _scsih_change_queue_depth - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001117 * @sdev: scsi device struct
1118 * @tag_type: requested tag type
1119 *
1120 * Returns queue tag type.
1121 */
1122static int
Eric Moored5d135b2009-05-18 13:02:08 -06001123_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001124{
1125 if (sdev->tagged_supported) {
1126 scsi_set_tag_type(sdev, tag_type);
1127 if (tag_type)
1128 scsi_activate_tcq(sdev, sdev->queue_depth);
1129 else
1130 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1131 } else
1132 tag_type = 0;
1133
1134 return tag_type;
1135}
1136
1137/**
Eric Moored5d135b2009-05-18 13:02:08 -06001138 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001139 * @starget: scsi target struct
1140 *
1141 * Returns 0 if ok. Any other return is assumed to be an error and
1142 * the device is ignored.
1143 */
1144static int
Eric Moored5d135b2009-05-18 13:02:08 -06001145_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001146{
1147 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1148 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1149 struct MPT2SAS_TARGET *sas_target_priv_data;
1150 struct _sas_device *sas_device;
1151 struct _raid_device *raid_device;
1152 unsigned long flags;
1153 struct sas_rphy *rphy;
1154
1155 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1156 if (!sas_target_priv_data)
1157 return -ENOMEM;
1158
1159 starget->hostdata = sas_target_priv_data;
1160 sas_target_priv_data->starget = starget;
1161 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1162
1163 /* RAID volumes */
1164 if (starget->channel == RAID_CHANNEL) {
1165 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1166 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1167 starget->channel);
1168 if (raid_device) {
1169 sas_target_priv_data->handle = raid_device->handle;
1170 sas_target_priv_data->sas_address = raid_device->wwid;
1171 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1172 raid_device->starget = starget;
1173 }
1174 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1175 return 0;
1176 }
1177
1178 /* sas/sata devices */
1179 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1180 rphy = dev_to_rphy(starget->dev.parent);
1181 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1182 rphy->identify.sas_address);
1183
1184 if (sas_device) {
1185 sas_target_priv_data->handle = sas_device->handle;
1186 sas_target_priv_data->sas_address = sas_device->sas_address;
1187 sas_device->starget = starget;
1188 sas_device->id = starget->id;
1189 sas_device->channel = starget->channel;
1190 if (sas_device->hidden_raid_component)
1191 sas_target_priv_data->flags |=
1192 MPT_TARGET_FLAGS_RAID_COMPONENT;
1193 }
1194 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1195
1196 return 0;
1197}
1198
1199/**
Eric Moored5d135b2009-05-18 13:02:08 -06001200 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001201 * @starget: scsi target struct
1202 *
1203 * Returns nothing.
1204 */
1205static void
Eric Moored5d135b2009-05-18 13:02:08 -06001206_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001207{
1208 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1209 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1210 struct MPT2SAS_TARGET *sas_target_priv_data;
1211 struct _sas_device *sas_device;
1212 struct _raid_device *raid_device;
1213 unsigned long flags;
1214 struct sas_rphy *rphy;
1215
1216 sas_target_priv_data = starget->hostdata;
1217 if (!sas_target_priv_data)
1218 return;
1219
1220 if (starget->channel == RAID_CHANNEL) {
1221 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1222 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1223 starget->channel);
1224 if (raid_device) {
1225 raid_device->starget = NULL;
1226 raid_device->sdev = NULL;
1227 }
1228 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1229 goto out;
1230 }
1231
1232 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1233 rphy = dev_to_rphy(starget->dev.parent);
1234 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1235 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001236 if (sas_device && (sas_device->starget == starget) &&
1237 (sas_device->id == starget->id) &&
1238 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001239 sas_device->starget = NULL;
1240
1241 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1242
1243 out:
1244 kfree(sas_target_priv_data);
1245 starget->hostdata = NULL;
1246}
1247
1248/**
Eric Moored5d135b2009-05-18 13:02:08 -06001249 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001250 * @sdev: scsi device struct
1251 *
1252 * Returns 0 if ok. Any other return is assumed to be an error and
1253 * the device is ignored.
1254 */
1255static int
Eric Moored5d135b2009-05-18 13:02:08 -06001256_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001257{
1258 struct Scsi_Host *shost;
1259 struct MPT2SAS_ADAPTER *ioc;
1260 struct MPT2SAS_TARGET *sas_target_priv_data;
1261 struct MPT2SAS_DEVICE *sas_device_priv_data;
1262 struct scsi_target *starget;
1263 struct _raid_device *raid_device;
1264 struct _sas_device *sas_device;
1265 unsigned long flags;
1266
1267 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1268 if (!sas_device_priv_data)
1269 return -ENOMEM;
1270
1271 sas_device_priv_data->lun = sdev->lun;
1272 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1273
1274 starget = scsi_target(sdev);
1275 sas_target_priv_data = starget->hostdata;
1276 sas_target_priv_data->num_luns++;
1277 sas_device_priv_data->sas_target = sas_target_priv_data;
1278 sdev->hostdata = sas_device_priv_data;
1279 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1280 sdev->no_uld_attach = 1;
1281
1282 shost = dev_to_shost(&starget->dev);
1283 ioc = shost_priv(shost);
1284 if (starget->channel == RAID_CHANNEL) {
1285 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1286 raid_device = _scsih_raid_device_find_by_id(ioc,
1287 starget->id, starget->channel);
1288 if (raid_device)
1289 raid_device->sdev = sdev; /* raid is single lun */
1290 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1291 } else {
1292 /* set TLR bit for SSP devices */
1293 if (!(ioc->facts.IOCCapabilities &
1294 MPI2_IOCFACTS_CAPABILITY_TLR))
1295 goto out;
1296 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1297 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1298 sas_device_priv_data->sas_target->sas_address);
1299 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1300 if (sas_device && sas_device->device_info &
1301 MPI2_SAS_DEVICE_INFO_SSP_TARGET)
1302 sas_device_priv_data->flags |= MPT_DEVICE_TLR_ON;
1303 }
1304
1305 out:
1306 return 0;
1307}
1308
1309/**
Eric Moored5d135b2009-05-18 13:02:08 -06001310 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001311 * @sdev: scsi device struct
1312 *
1313 * Returns nothing.
1314 */
1315static void
Eric Moored5d135b2009-05-18 13:02:08 -06001316_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001317{
1318 struct MPT2SAS_TARGET *sas_target_priv_data;
1319 struct scsi_target *starget;
1320
1321 if (!sdev->hostdata)
1322 return;
1323
1324 starget = scsi_target(sdev);
1325 sas_target_priv_data = starget->hostdata;
1326 sas_target_priv_data->num_luns--;
1327 kfree(sdev->hostdata);
1328 sdev->hostdata = NULL;
1329}
1330
1331/**
Eric Moored5d135b2009-05-18 13:02:08 -06001332 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001333 * @ioc: per adapter object
1334 * @sas_device: the sas_device object
1335 * @sdev: scsi device struct
1336 */
1337static void
Eric Moored5d135b2009-05-18 13:02:08 -06001338_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001339 struct _sas_device *sas_device, struct scsi_device *sdev)
1340{
1341 Mpi2ConfigReply_t mpi_reply;
1342 Mpi2SasDevicePage0_t sas_device_pg0;
1343 u32 ioc_status;
1344 u16 flags;
1345 u32 device_info;
1346
1347 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1348 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1349 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1350 ioc->name, __FILE__, __LINE__, __func__);
1351 return;
1352 }
1353
1354 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1355 MPI2_IOCSTATUS_MASK;
1356 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1357 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1358 ioc->name, __FILE__, __LINE__, __func__);
1359 return;
1360 }
1361
1362 flags = le16_to_cpu(sas_device_pg0.Flags);
1363 device_info = le16_to_cpu(sas_device_pg0.DeviceInfo);
1364
1365 sdev_printk(KERN_INFO, sdev,
1366 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1367 "sw_preserve(%s)\n",
1368 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1369 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1370 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1371 "n",
1372 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1373 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1374 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1375}
1376
1377/**
1378 * _scsih_get_volume_capabilities - volume capabilities
1379 * @ioc: per adapter object
1380 * @sas_device: the raid_device object
1381 */
1382static void
1383_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1384 struct _raid_device *raid_device)
1385{
1386 Mpi2RaidVolPage0_t *vol_pg0;
1387 Mpi2RaidPhysDiskPage0_t pd_pg0;
1388 Mpi2SasDevicePage0_t sas_device_pg0;
1389 Mpi2ConfigReply_t mpi_reply;
1390 u16 sz;
1391 u8 num_pds;
1392
1393 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1394 &num_pds)) || !num_pds) {
1395 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1396 ioc->name, __FILE__, __LINE__, __func__);
1397 return;
1398 }
1399
1400 raid_device->num_pds = num_pds;
1401 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1402 sizeof(Mpi2RaidVol0PhysDisk_t));
1403 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1404 if (!vol_pg0) {
1405 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1406 ioc->name, __FILE__, __LINE__, __func__);
1407 return;
1408 }
1409
1410 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1411 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1412 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1413 ioc->name, __FILE__, __LINE__, __func__);
1414 kfree(vol_pg0);
1415 return;
1416 }
1417
1418 raid_device->volume_type = vol_pg0->VolumeType;
1419
1420 /* figure out what the underlying devices are by
1421 * obtaining the device_info bits for the 1st device
1422 */
1423 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1424 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1425 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1426 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1427 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1428 le16_to_cpu(pd_pg0.DevHandle)))) {
1429 raid_device->device_info =
1430 le32_to_cpu(sas_device_pg0.DeviceInfo);
1431 }
1432 }
1433
1434 kfree(vol_pg0);
1435}
1436
1437/**
Eric Moored5d135b2009-05-18 13:02:08 -06001438 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001439 * @sdev: scsi device struct
1440 *
1441 * Returns 0 if ok. Any other return is assumed to be an error and
1442 * the device is ignored.
1443 */
1444static int
Eric Moored5d135b2009-05-18 13:02:08 -06001445_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001446{
1447 struct Scsi_Host *shost = sdev->host;
1448 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1449 struct MPT2SAS_DEVICE *sas_device_priv_data;
1450 struct MPT2SAS_TARGET *sas_target_priv_data;
1451 struct _sas_device *sas_device;
1452 struct _raid_device *raid_device;
1453 unsigned long flags;
1454 int qdepth;
1455 u8 ssp_target = 0;
1456 char *ds = "";
1457 char *r_level = "";
1458
1459 qdepth = 1;
1460 sas_device_priv_data = sdev->hostdata;
1461 sas_device_priv_data->configured_lun = 1;
1462 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1463 sas_target_priv_data = sas_device_priv_data->sas_target;
1464
1465 /* raid volume handling */
1466 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1467
1468 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1469 raid_device = _scsih_raid_device_find_by_handle(ioc,
1470 sas_target_priv_data->handle);
1471 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1472 if (!raid_device) {
1473 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1474 ioc->name, __FILE__, __LINE__, __func__);
1475 return 0;
1476 }
1477
1478 _scsih_get_volume_capabilities(ioc, raid_device);
1479
1480 /* RAID Queue Depth Support
1481 * IS volume = underlying qdepth of drive type, either
1482 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1483 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1484 */
1485 if (raid_device->device_info &
1486 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1487 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1488 ds = "SSP";
1489 } else {
1490 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1491 if (raid_device->device_info &
1492 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1493 ds = "SATA";
1494 else
1495 ds = "STP";
1496 }
1497
1498 switch (raid_device->volume_type) {
1499 case MPI2_RAID_VOL_TYPE_RAID0:
1500 r_level = "RAID0";
1501 break;
1502 case MPI2_RAID_VOL_TYPE_RAID1E:
1503 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1504 r_level = "RAID1E";
1505 break;
1506 case MPI2_RAID_VOL_TYPE_RAID1:
1507 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1508 r_level = "RAID1";
1509 break;
1510 case MPI2_RAID_VOL_TYPE_RAID10:
1511 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1512 r_level = "RAID10";
1513 break;
1514 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1515 default:
1516 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1517 r_level = "RAIDX";
1518 break;
1519 }
1520
1521 sdev_printk(KERN_INFO, sdev, "%s: "
1522 "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1523 r_level, raid_device->handle,
1524 (unsigned long long)raid_device->wwid,
1525 raid_device->num_pds, ds);
Eric Moored5d135b2009-05-18 13:02:08 -06001526 _scsih_change_queue_depth(sdev, qdepth);
Eric Moore635374e2009-03-09 01:21:12 -06001527 return 0;
1528 }
1529
1530 /* non-raid handling */
1531 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1532 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1533 sas_device_priv_data->sas_target->sas_address);
1534 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1535 if (sas_device) {
1536 if (sas_target_priv_data->flags &
1537 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1538 mpt2sas_config_get_volume_handle(ioc,
1539 sas_device->handle, &sas_device->volume_handle);
1540 mpt2sas_config_get_volume_wwid(ioc,
1541 sas_device->volume_handle,
1542 &sas_device->volume_wwid);
1543 }
1544 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1545 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1546 ssp_target = 1;
1547 ds = "SSP";
1548 } else {
1549 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1550 if (sas_device->device_info &
1551 MPI2_SAS_DEVICE_INFO_STP_TARGET)
1552 ds = "STP";
1553 else if (sas_device->device_info &
1554 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1555 ds = "SATA";
1556 }
1557
1558 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1559 "sas_addr(0x%016llx), device_name(0x%016llx)\n",
1560 ds, sas_device->handle,
1561 (unsigned long long)sas_device->sas_address,
1562 (unsigned long long)sas_device->device_name);
1563 sdev_printk(KERN_INFO, sdev, "%s: "
1564 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1565 (unsigned long long) sas_device->enclosure_logical_id,
1566 sas_device->slot);
1567
1568 if (!ssp_target)
Eric Moored5d135b2009-05-18 13:02:08 -06001569 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
Eric Moore635374e2009-03-09 01:21:12 -06001570 }
1571
Eric Moored5d135b2009-05-18 13:02:08 -06001572 _scsih_change_queue_depth(sdev, qdepth);
Eric Moore635374e2009-03-09 01:21:12 -06001573
1574 if (ssp_target)
1575 sas_read_port_mode_page(sdev);
1576 return 0;
1577}
1578
1579/**
Eric Moored5d135b2009-05-18 13:02:08 -06001580 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06001581 * @sdev: scsi device struct
1582 * @bdev: pointer to block device context
1583 * @capacity: device size (in 512 byte sectors)
1584 * @params: three element array to place output:
1585 * params[0] number of heads (max 255)
1586 * params[1] number of sectors (max 63)
1587 * params[2] number of cylinders
1588 *
1589 * Return nothing.
1590 */
1591static int
Eric Moored5d135b2009-05-18 13:02:08 -06001592_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06001593 sector_t capacity, int params[])
1594{
1595 int heads;
1596 int sectors;
1597 sector_t cylinders;
1598 ulong dummy;
1599
1600 heads = 64;
1601 sectors = 32;
1602
1603 dummy = heads * sectors;
1604 cylinders = capacity;
1605 sector_div(cylinders, dummy);
1606
1607 /*
1608 * Handle extended translation size for logical drives
1609 * > 1Gb
1610 */
1611 if ((ulong)capacity >= 0x200000) {
1612 heads = 255;
1613 sectors = 63;
1614 dummy = heads * sectors;
1615 cylinders = capacity;
1616 sector_div(cylinders, dummy);
1617 }
1618
1619 /* return result */
1620 params[0] = heads;
1621 params[1] = sectors;
1622 params[2] = cylinders;
1623
1624 return 0;
1625}
1626
1627/**
1628 * _scsih_response_code - translation of device response code
1629 * @ioc: per adapter object
1630 * @response_code: response code returned by the device
1631 *
1632 * Return nothing.
1633 */
1634static void
1635_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1636{
1637 char *desc;
1638
1639 switch (response_code) {
1640 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1641 desc = "task management request completed";
1642 break;
1643 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1644 desc = "invalid frame";
1645 break;
1646 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1647 desc = "task management request not supported";
1648 break;
1649 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1650 desc = "task management request failed";
1651 break;
1652 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1653 desc = "task management request succeeded";
1654 break;
1655 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1656 desc = "invalid lun";
1657 break;
1658 case 0xA:
1659 desc = "overlapped tag attempted";
1660 break;
1661 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1662 desc = "task queued, however not sent to target";
1663 break;
1664 default:
1665 desc = "unknown";
1666 break;
1667 }
1668 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1669 ioc->name, response_code, desc);
1670}
1671
1672/**
Eric Moored5d135b2009-05-18 13:02:08 -06001673 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06001674 * @ioc: per adapter object
1675 * @smid: system request message index
1676 * @VF_ID: virtual function id
1677 * @reply: reply message frame(lower 32bit addr)
1678 * Context: none.
1679 *
1680 * The callback handler when using scsih_issue_tm.
1681 *
1682 * Return nothing.
1683 */
1684static void
Eric Moored5d135b2009-05-18 13:02:08 -06001685_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06001686{
1687 MPI2DefaultReply_t *mpi_reply;
1688
1689 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
1690 return;
1691 if (ioc->tm_cmds.smid != smid)
1692 return;
1693 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1694 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
1695 if (mpi_reply) {
1696 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1697 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1698 }
1699 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1700 complete(&ioc->tm_cmds.done);
1701}
1702
1703/**
1704 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1705 * @ioc: per adapter object
1706 * @handle: device handle
1707 *
1708 * During taskmangement request, we need to freeze the device queue.
1709 */
1710void
1711mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1712{
1713 struct MPT2SAS_DEVICE *sas_device_priv_data;
1714 struct scsi_device *sdev;
1715 u8 skip = 0;
1716
1717 shost_for_each_device(sdev, ioc->shost) {
1718 if (skip)
1719 continue;
1720 sas_device_priv_data = sdev->hostdata;
1721 if (!sas_device_priv_data)
1722 continue;
1723 if (sas_device_priv_data->sas_target->handle == handle) {
1724 sas_device_priv_data->sas_target->tm_busy = 1;
1725 skip = 1;
1726 ioc->ignore_loginfos = 1;
1727 }
1728 }
1729}
1730
1731/**
1732 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1733 * @ioc: per adapter object
1734 * @handle: device handle
1735 *
1736 * During taskmangement request, we need to freeze the device queue.
1737 */
1738void
1739mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1740{
1741 struct MPT2SAS_DEVICE *sas_device_priv_data;
1742 struct scsi_device *sdev;
1743 u8 skip = 0;
1744
1745 shost_for_each_device(sdev, ioc->shost) {
1746 if (skip)
1747 continue;
1748 sas_device_priv_data = sdev->hostdata;
1749 if (!sas_device_priv_data)
1750 continue;
1751 if (sas_device_priv_data->sas_target->handle == handle) {
1752 sas_device_priv_data->sas_target->tm_busy = 0;
1753 skip = 1;
1754 ioc->ignore_loginfos = 0;
1755 }
1756 }
1757}
1758
1759/**
1760 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1761 * @ioc: per adapter struct
1762 * @device_handle: device handle
1763 * @lun: lun number
1764 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1765 * @smid_task: smid assigned to the task
1766 * @timeout: timeout in seconds
1767 * Context: The calling function needs to acquire the tm_cmds.mutex
1768 *
1769 * A generic API for sending task management requests to firmware.
1770 *
1771 * The ioc->tm_cmds.status flag should be MPT2_CMD_NOT_USED before calling
1772 * this API.
1773 *
1774 * The callback index is set inside `ioc->tm_cb_idx`.
1775 *
1776 * Return nothing.
1777 */
1778void
1779mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
1780 u8 type, u16 smid_task, ulong timeout)
1781{
1782 Mpi2SCSITaskManagementRequest_t *mpi_request;
1783 Mpi2SCSITaskManagementReply_t *mpi_reply;
1784 u16 smid = 0;
1785 u32 ioc_state;
1786 unsigned long timeleft;
1787 u8 VF_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001788
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05301789 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
1790 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
1791 __func__, ioc->name);
1792 return;
1793 }
1794
1795 if (ioc->shost_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06001796 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1797 __func__, ioc->name);
1798 return;
1799 }
Eric Moore635374e2009-03-09 01:21:12 -06001800
1801 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
1802 if (ioc_state & MPI2_DOORBELL_USED) {
1803 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
1804 "active!\n", ioc->name));
1805 goto issue_host_reset;
1806 }
1807
1808 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
1809 mpt2sas_base_fault_info(ioc, ioc_state &
1810 MPI2_DOORBELL_DATA_MASK);
1811 goto issue_host_reset;
1812 }
1813
1814 smid = mpt2sas_base_get_smid(ioc, ioc->tm_cb_idx);
1815 if (!smid) {
1816 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1817 ioc->name, __func__);
1818 return;
1819 }
1820
1821 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
1822 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type, smid));
1823 ioc->tm_cmds.status = MPT2_CMD_PENDING;
1824 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1825 ioc->tm_cmds.smid = smid;
1826 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
1827 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1828 mpi_request->DevHandle = cpu_to_le16(handle);
1829 mpi_request->TaskType = type;
1830 mpi_request->TaskMID = cpu_to_le16(smid_task);
1831 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
1832 mpt2sas_scsih_set_tm_flag(ioc, handle);
1833 mpt2sas_base_put_smid_hi_priority(ioc, smid, VF_ID);
1834 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
1835 mpt2sas_scsih_clear_tm_flag(ioc, handle);
1836 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
1837 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1838 ioc->name, __func__);
1839 _debug_dump_mf(mpi_request,
1840 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
1841 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET))
1842 goto issue_host_reset;
1843 }
1844
1845 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
1846 mpi_reply = ioc->tm_cmds.reply;
1847 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
1848 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
1849 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
1850 le32_to_cpu(mpi_reply->IOCLogInfo),
1851 le32_to_cpu(mpi_reply->TerminationCount)));
1852 if (ioc->logging_level & MPT_DEBUG_TM)
1853 _scsih_response_code(ioc, mpi_reply->ResponseCode);
1854 }
1855 return;
1856 issue_host_reset:
1857 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, FORCE_BIG_HAMMER);
1858}
1859
1860/**
Eric Moored5d135b2009-05-18 13:02:08 -06001861 * _scsih_abort - eh threads main abort routine
Eric Moore635374e2009-03-09 01:21:12 -06001862 * @sdev: scsi device struct
1863 *
1864 * Returns SUCCESS if command aborted else FAILED
1865 */
1866static int
Eric Moored5d135b2009-05-18 13:02:08 -06001867_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06001868{
1869 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1870 struct MPT2SAS_DEVICE *sas_device_priv_data;
1871 u16 smid;
1872 u16 handle;
1873 int r;
1874 struct scsi_cmnd *scmd_lookup;
1875
1876 printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
1877 ioc->name, scmd);
1878 scsi_print_command(scmd);
1879
1880 sas_device_priv_data = scmd->device->hostdata;
1881 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1882 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1883 ioc->name, scmd);
1884 scmd->result = DID_NO_CONNECT << 16;
1885 scmd->scsi_done(scmd);
1886 r = SUCCESS;
1887 goto out;
1888 }
1889
1890 /* search for the command */
1891 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
1892 if (!smid) {
1893 scmd->result = DID_RESET << 16;
1894 r = SUCCESS;
1895 goto out;
1896 }
1897
1898 /* for hidden raid components and volumes this is not supported */
1899 if (sas_device_priv_data->sas_target->flags &
1900 MPT_TARGET_FLAGS_RAID_COMPONENT ||
1901 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
1902 scmd->result = DID_RESET << 16;
1903 r = FAILED;
1904 goto out;
1905 }
1906
1907 mutex_lock(&ioc->tm_cmds.mutex);
1908 handle = sas_device_priv_data->sas_target->handle;
1909 mpt2sas_scsih_issue_tm(ioc, handle, sas_device_priv_data->lun,
1910 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
1911
1912 /* sanity check - see whether command actually completed */
1913 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid);
1914 if (scmd_lookup && (scmd_lookup->serial_number == scmd->serial_number))
1915 r = FAILED;
1916 else
1917 r = SUCCESS;
1918 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1919 mutex_unlock(&ioc->tm_cmds.mutex);
1920
1921 out:
1922 printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
1923 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1924 return r;
1925}
1926
Eric Moore635374e2009-03-09 01:21:12 -06001927/**
Eric Moored5d135b2009-05-18 13:02:08 -06001928 * _scsih_dev_reset - eh threads main device reset routine
Eric Moore635374e2009-03-09 01:21:12 -06001929 * @sdev: scsi device struct
1930 *
1931 * Returns SUCCESS if command aborted else FAILED
1932 */
1933static int
Eric Moored5d135b2009-05-18 13:02:08 -06001934_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06001935{
1936 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1937 struct MPT2SAS_DEVICE *sas_device_priv_data;
1938 struct _sas_device *sas_device;
1939 unsigned long flags;
1940 u16 handle;
1941 int r;
1942
Eric Moore993e0da2009-05-18 13:00:45 -06001943 printk(MPT2SAS_INFO_FMT "attempting device reset! scmd(%p)\n",
Eric Moore635374e2009-03-09 01:21:12 -06001944 ioc->name, scmd);
1945 scsi_print_command(scmd);
1946
1947 sas_device_priv_data = scmd->device->hostdata;
1948 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1949 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1950 ioc->name, scmd);
1951 scmd->result = DID_NO_CONNECT << 16;
1952 scmd->scsi_done(scmd);
1953 r = SUCCESS;
1954 goto out;
1955 }
1956
1957 /* for hidden raid components obtain the volume_handle */
1958 handle = 0;
1959 if (sas_device_priv_data->sas_target->flags &
1960 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1961 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1962 sas_device = _scsih_sas_device_find_by_handle(ioc,
1963 sas_device_priv_data->sas_target->handle);
1964 if (sas_device)
1965 handle = sas_device->volume_handle;
1966 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1967 } else
1968 handle = sas_device_priv_data->sas_target->handle;
1969
1970 if (!handle) {
1971 scmd->result = DID_RESET << 16;
1972 r = FAILED;
1973 goto out;
1974 }
1975
1976 mutex_lock(&ioc->tm_cmds.mutex);
1977 mpt2sas_scsih_issue_tm(ioc, handle, 0,
Eric Moore993e0da2009-05-18 13:00:45 -06001978 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, scmd->device->lun,
1979 30);
1980
1981 /*
1982 * sanity check see whether all commands to this device been
1983 * completed
1984 */
1985 if (_scsih_scsi_lookup_find_by_lun(ioc, scmd->device->id,
1986 scmd->device->lun, scmd->device->channel))
1987 r = FAILED;
1988 else
1989 r = SUCCESS;
1990 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1991 mutex_unlock(&ioc->tm_cmds.mutex);
1992
1993 out:
1994 printk(MPT2SAS_INFO_FMT "device reset: %s scmd(%p)\n",
1995 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1996 return r;
1997}
1998
1999/**
Eric Moored5d135b2009-05-18 13:02:08 -06002000 * _scsih_target_reset - eh threads main target reset routine
Eric Moore993e0da2009-05-18 13:00:45 -06002001 * @sdev: scsi device struct
2002 *
2003 * Returns SUCCESS if command aborted else FAILED
2004 */
2005static int
Eric Moored5d135b2009-05-18 13:02:08 -06002006_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002007{
2008 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2009 struct MPT2SAS_DEVICE *sas_device_priv_data;
2010 struct _sas_device *sas_device;
2011 unsigned long flags;
2012 u16 handle;
2013 int r;
2014
2015 printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
2016 ioc->name, scmd);
2017 scsi_print_command(scmd);
2018
2019 sas_device_priv_data = scmd->device->hostdata;
2020 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2021 printk(MPT2SAS_INFO_FMT "target been deleted! scmd(%p)\n",
2022 ioc->name, scmd);
2023 scmd->result = DID_NO_CONNECT << 16;
2024 scmd->scsi_done(scmd);
2025 r = SUCCESS;
2026 goto out;
2027 }
2028
2029 /* for hidden raid components obtain the volume_handle */
2030 handle = 0;
2031 if (sas_device_priv_data->sas_target->flags &
2032 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2033 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2034 sas_device = _scsih_sas_device_find_by_handle(ioc,
2035 sas_device_priv_data->sas_target->handle);
2036 if (sas_device)
2037 handle = sas_device->volume_handle;
2038 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2039 } else
2040 handle = sas_device_priv_data->sas_target->handle;
2041
2042 if (!handle) {
2043 scmd->result = DID_RESET << 16;
2044 r = FAILED;
2045 goto out;
2046 }
2047
2048 mutex_lock(&ioc->tm_cmds.mutex);
2049 mpt2sas_scsih_issue_tm(ioc, handle, 0,
Eric Moore635374e2009-03-09 01:21:12 -06002050 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
2051
2052 /*
2053 * sanity check see whether all commands to this target been
2054 * completed
2055 */
2056 if (_scsih_scsi_lookup_find_by_target(ioc, scmd->device->id,
2057 scmd->device->channel))
2058 r = FAILED;
2059 else
2060 r = SUCCESS;
2061 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2062 mutex_unlock(&ioc->tm_cmds.mutex);
2063
2064 out:
2065 printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
2066 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2067 return r;
2068}
2069
2070/**
Eric Moored5d135b2009-05-18 13:02:08 -06002071 * _scsih_abort - eh threads main host reset routine
Eric Moore635374e2009-03-09 01:21:12 -06002072 * @sdev: scsi device struct
2073 *
2074 * Returns SUCCESS if command aborted else FAILED
2075 */
2076static int
Eric Moored5d135b2009-05-18 13:02:08 -06002077_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002078{
2079 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2080 int r, retval;
2081
2082 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2083 ioc->name, scmd);
2084 scsi_print_command(scmd);
2085
2086 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2087 FORCE_BIG_HAMMER);
2088 r = (retval < 0) ? FAILED : SUCCESS;
2089 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2090 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2091
2092 return r;
2093}
2094
2095/**
2096 * _scsih_fw_event_add - insert and queue up fw_event
2097 * @ioc: per adapter object
2098 * @fw_event: object describing the event
2099 * Context: This function will acquire ioc->fw_event_lock.
2100 *
2101 * This adds the firmware event object into link list, then queues it up to
2102 * be processed from user context.
2103 *
2104 * Return nothing.
2105 */
2106static void
2107_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2108{
2109 unsigned long flags;
2110
2111 if (ioc->firmware_event_thread == NULL)
2112 return;
2113
2114 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2115 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Eric Moore6f92a7a2009-04-21 15:43:33 -06002116 INIT_WORK(&fw_event->work, _firmware_event_work);
2117 queue_work(ioc->firmware_event_thread, &fw_event->work);
Eric Moore635374e2009-03-09 01:21:12 -06002118 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2119}
2120
2121/**
2122 * _scsih_fw_event_free - delete fw_event
2123 * @ioc: per adapter object
2124 * @fw_event: object describing the event
2125 * Context: This function will acquire ioc->fw_event_lock.
2126 *
2127 * This removes firmware event object from link list, frees associated memory.
2128 *
2129 * Return nothing.
2130 */
2131static void
2132_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2133 *fw_event)
2134{
2135 unsigned long flags;
2136
2137 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2138 list_del(&fw_event->list);
2139 kfree(fw_event->event_data);
2140 kfree(fw_event);
2141 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2142}
2143
2144/**
2145 * _scsih_fw_event_add - requeue an event
2146 * @ioc: per adapter object
2147 * @fw_event: object describing the event
2148 * Context: This function will acquire ioc->fw_event_lock.
2149 *
2150 * Return nothing.
2151 */
2152static void
2153_scsih_fw_event_requeue(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2154 *fw_event, unsigned long delay)
2155{
2156 unsigned long flags;
2157 if (ioc->firmware_event_thread == NULL)
2158 return;
2159
2160 spin_lock_irqsave(&ioc->fw_event_lock, flags);
Eric Moore6f92a7a2009-04-21 15:43:33 -06002161 queue_work(ioc->firmware_event_thread, &fw_event->work);
Eric Moore635374e2009-03-09 01:21:12 -06002162 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2163}
2164
2165/**
2166 * _scsih_fw_event_off - turn flag off preventing event handling
2167 * @ioc: per adapter object
2168 *
2169 * Used to prevent handling of firmware events during adapter reset
2170 * driver unload.
2171 *
2172 * Return nothing.
2173 */
2174static void
2175_scsih_fw_event_off(struct MPT2SAS_ADAPTER *ioc)
2176{
2177 unsigned long flags;
2178
2179 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2180 ioc->fw_events_off = 1;
2181 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2182
2183}
2184
2185/**
2186 * _scsih_fw_event_on - turn flag on allowing firmware event handling
2187 * @ioc: per adapter object
2188 *
2189 * Returns nothing.
2190 */
2191static void
2192_scsih_fw_event_on(struct MPT2SAS_ADAPTER *ioc)
2193{
2194 unsigned long flags;
2195
2196 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2197 ioc->fw_events_off = 0;
2198 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2199}
2200
2201/**
2202 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2203 * @ioc: per adapter object
2204 * @handle: device handle
2205 *
2206 * During device pull we need to appropiately set the sdev state.
2207 */
2208static void
2209_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2210{
2211 struct MPT2SAS_DEVICE *sas_device_priv_data;
2212 struct scsi_device *sdev;
2213
2214 shost_for_each_device(sdev, ioc->shost) {
2215 sas_device_priv_data = sdev->hostdata;
2216 if (!sas_device_priv_data)
2217 continue;
2218 if (!sas_device_priv_data->block)
2219 continue;
2220 if (sas_device_priv_data->sas_target->handle == handle) {
2221 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2222 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2223 "handle(0x%04x)\n", ioc->name, handle));
2224 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302225 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002226 }
2227 }
2228}
2229
2230/**
2231 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2232 * @ioc: per adapter object
2233 * @handle: device handle
2234 *
2235 * During device pull we need to appropiately set the sdev state.
2236 */
2237static void
2238_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2239{
2240 struct MPT2SAS_DEVICE *sas_device_priv_data;
2241 struct scsi_device *sdev;
2242
2243 shost_for_each_device(sdev, ioc->shost) {
2244 sas_device_priv_data = sdev->hostdata;
2245 if (!sas_device_priv_data)
2246 continue;
2247 if (sas_device_priv_data->block)
2248 continue;
2249 if (sas_device_priv_data->sas_target->handle == handle) {
2250 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2251 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2252 "handle(0x%04x)\n", ioc->name, handle));
2253 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302254 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002255 }
2256 }
2257}
2258
2259/**
2260 * _scsih_block_io_to_children_attached_to_ex
2261 * @ioc: per adapter object
2262 * @sas_expander: the sas_device object
2263 *
2264 * This routine set sdev state to SDEV_BLOCK for all devices
2265 * attached to this expander. This function called when expander is
2266 * pulled.
2267 */
2268static void
2269_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2270 struct _sas_node *sas_expander)
2271{
2272 struct _sas_port *mpt2sas_port;
2273 struct _sas_device *sas_device;
2274 struct _sas_node *expander_sibling;
2275 unsigned long flags;
2276
2277 if (!sas_expander)
2278 return;
2279
2280 list_for_each_entry(mpt2sas_port,
2281 &sas_expander->sas_port_list, port_list) {
2282 if (mpt2sas_port->remote_identify.device_type ==
2283 SAS_END_DEVICE) {
2284 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2285 sas_device =
2286 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2287 mpt2sas_port->remote_identify.sas_address);
2288 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2289 if (!sas_device)
2290 continue;
2291 _scsih_block_io_device(ioc, sas_device->handle);
2292 }
2293 }
2294
2295 list_for_each_entry(mpt2sas_port,
2296 &sas_expander->sas_port_list, port_list) {
2297
2298 if (mpt2sas_port->remote_identify.device_type ==
2299 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2300 mpt2sas_port->remote_identify.device_type ==
2301 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2302
2303 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2304 expander_sibling =
2305 mpt2sas_scsih_expander_find_by_sas_address(
2306 ioc, mpt2sas_port->remote_identify.sas_address);
2307 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2308 _scsih_block_io_to_children_attached_to_ex(ioc,
2309 expander_sibling);
2310 }
2311 }
2312}
2313
2314/**
2315 * _scsih_block_io_to_children_attached_directly
2316 * @ioc: per adapter object
2317 * @event_data: topology change event data
2318 *
2319 * This routine set sdev state to SDEV_BLOCK for all devices
2320 * direct attached during device pull.
2321 */
2322static void
2323_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2324 Mpi2EventDataSasTopologyChangeList_t *event_data)
2325{
2326 int i;
2327 u16 handle;
2328 u16 reason_code;
2329 u8 phy_number;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302330 u8 link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06002331
2332 for (i = 0; i < event_data->NumEntries; i++) {
2333 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2334 if (!handle)
2335 continue;
2336 phy_number = event_data->StartPhyNum + i;
2337 reason_code = event_data->PHY[i].PhyStatus &
2338 MPI2_EVENT_SAS_TOPO_RC_MASK;
2339 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2340 _scsih_block_io_device(ioc, handle);
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302341 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED) {
2342 link_rate = event_data->PHY[i].LinkRate >> 4;
2343 if (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)
2344 _scsih_ublock_io_device(ioc, handle);
2345 }
Eric Moore635374e2009-03-09 01:21:12 -06002346 }
2347}
2348
2349/**
2350 * _scsih_check_topo_delete_events - sanity check on topo events
2351 * @ioc: per adapter object
2352 * @event_data: the event data payload
2353 *
2354 * This routine added to better handle cable breaker.
2355 *
2356 * This handles the case where driver recieves multiple expander
2357 * add and delete events in a single shot. When there is a delete event
2358 * the routine will void any pending add events waiting in the event queue.
2359 *
2360 * Return nothing.
2361 */
2362static void
2363_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2364 Mpi2EventDataSasTopologyChangeList_t *event_data)
2365{
2366 struct fw_event_work *fw_event;
2367 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2368 u16 expander_handle;
2369 struct _sas_node *sas_expander;
2370 unsigned long flags;
2371
2372 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2373 if (expander_handle < ioc->sas_hba.num_phys) {
2374 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2375 return;
2376 }
2377
2378 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2379 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2380 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2381 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
2382 expander_handle);
2383 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2384 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
2385 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
2386 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2387
2388 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
2389 return;
2390
2391 /* mark ignore flag for pending events */
2392 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2393 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
2394 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
2395 fw_event->ignore)
2396 continue;
2397 local_event_data = fw_event->event_data;
2398 if (local_event_data->ExpStatus ==
2399 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
2400 local_event_data->ExpStatus ==
2401 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
2402 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
2403 expander_handle) {
2404 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2405 "setting ignoring flag\n", ioc->name));
2406 fw_event->ignore = 1;
2407 }
2408 }
2409 }
2410 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2411}
2412
2413/**
Eric Moore635374e2009-03-09 01:21:12 -06002414 * _scsih_flush_running_cmds - completing outstanding commands.
2415 * @ioc: per adapter object
2416 *
2417 * The flushing out of all pending scmd commands following host reset,
2418 * where all IO is dropped to the floor.
2419 *
2420 * Return nothing.
2421 */
2422static void
2423_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
2424{
2425 struct scsi_cmnd *scmd;
2426 u16 smid;
2427 u16 count = 0;
2428
2429 for (smid = 1; smid <= ioc->request_depth; smid++) {
2430 scmd = _scsih_scsi_lookup_getclear(ioc, smid);
2431 if (!scmd)
2432 continue;
2433 count++;
2434 mpt2sas_base_free_smid(ioc, smid);
2435 scsi_dma_unmap(scmd);
2436 scmd->result = DID_RESET << 16;
2437 scmd->scsi_done(scmd);
2438 }
2439 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
2440 ioc->name, count));
2441}
2442
2443/**
Eric Moore3c621b32009-05-18 12:59:41 -06002444 * _scsih_setup_eedp - setup MPI request for EEDP transfer
2445 * @scmd: pointer to scsi command object
2446 * @mpi_request: pointer to the SCSI_IO reqest message frame
2447 *
2448 * Supporting protection 1 and 3.
2449 *
2450 * Returns nothing
2451 */
2452static void
2453_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
2454{
2455 u16 eedp_flags;
2456 unsigned char prot_op = scsi_get_prot_op(scmd);
2457 unsigned char prot_type = scsi_get_prot_type(scmd);
2458
2459 if (prot_type == SCSI_PROT_DIF_TYPE0 ||
2460 prot_type == SCSI_PROT_DIF_TYPE2 ||
2461 prot_op == SCSI_PROT_NORMAL)
2462 return;
2463
2464 if (prot_op == SCSI_PROT_READ_STRIP)
2465 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
2466 else if (prot_op == SCSI_PROT_WRITE_INSERT)
2467 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
2468 else
2469 return;
2470
2471 mpi_request->EEDPBlockSize = scmd->device->sector_size;
2472
2473 switch (prot_type) {
2474 case SCSI_PROT_DIF_TYPE1:
2475
2476 /*
2477 * enable ref/guard checking
2478 * auto increment ref tag
2479 */
2480 mpi_request->EEDPFlags = eedp_flags |
2481 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2482 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2483 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2484 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
2485 cpu_to_be32(scsi_get_lba(scmd));
2486
2487 break;
2488
2489 case SCSI_PROT_DIF_TYPE3:
2490
2491 /*
2492 * enable guard checking
2493 */
2494 mpi_request->EEDPFlags = eedp_flags |
2495 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2496
2497 break;
2498 }
2499}
2500
2501/**
2502 * _scsih_eedp_error_handling - return sense code for EEDP errors
2503 * @scmd: pointer to scsi command object
2504 * @ioc_status: ioc status
2505 *
2506 * Returns nothing
2507 */
2508static void
2509_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
2510{
2511 u8 ascq;
2512 u8 sk;
2513 u8 host_byte;
2514
2515 switch (ioc_status) {
2516 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2517 ascq = 0x01;
2518 break;
2519 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2520 ascq = 0x02;
2521 break;
2522 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2523 ascq = 0x03;
2524 break;
2525 default:
2526 ascq = 0x00;
2527 break;
2528 }
2529
2530 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2531 sk = ILLEGAL_REQUEST;
2532 host_byte = DID_ABORT;
2533 } else {
2534 sk = ABORTED_COMMAND;
2535 host_byte = DID_OK;
2536 }
2537
2538 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
2539 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
2540 SAM_STAT_CHECK_CONDITION;
2541}
2542
2543/**
Eric Moored5d135b2009-05-18 13:02:08 -06002544 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06002545 * @scmd: pointer to scsi command object
2546 * @done: function pointer to be invoked on completion
2547 *
2548 * The callback index is set inside `ioc->scsi_io_cb_idx`.
2549 *
2550 * Returns 0 on success. If there's a failure, return either:
2551 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
2552 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
2553 */
2554static int
Eric Moored5d135b2009-05-18 13:02:08 -06002555_scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06002556{
2557 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2558 struct MPT2SAS_DEVICE *sas_device_priv_data;
2559 struct MPT2SAS_TARGET *sas_target_priv_data;
2560 Mpi2SCSIIORequest_t *mpi_request;
2561 u32 mpi_control;
2562 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06002563
2564 scmd->scsi_done = done;
2565 sas_device_priv_data = scmd->device->hostdata;
2566 if (!sas_device_priv_data) {
2567 scmd->result = DID_NO_CONNECT << 16;
2568 scmd->scsi_done(scmd);
2569 return 0;
2570 }
2571
2572 sas_target_priv_data = sas_device_priv_data->sas_target;
2573 if (!sas_target_priv_data || sas_target_priv_data->handle ==
2574 MPT2SAS_INVALID_DEVICE_HANDLE || sas_target_priv_data->deleted) {
2575 scmd->result = DID_NO_CONNECT << 16;
2576 scmd->scsi_done(scmd);
2577 return 0;
2578 }
2579
2580 /* see if we are busy with task managment stuff */
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302581 if (sas_target_priv_data->tm_busy)
2582 return SCSI_MLQUEUE_DEVICE_BUSY;
2583 else if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06002584 return SCSI_MLQUEUE_HOST_BUSY;
Eric Moore635374e2009-03-09 01:21:12 -06002585
2586 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
2587 mpi_control = MPI2_SCSIIO_CONTROL_READ;
2588 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
2589 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
2590 else
2591 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
2592
2593 /* set tags */
2594 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
2595 if (scmd->device->tagged_supported) {
2596 if (scmd->device->ordered_tags)
2597 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
2598 else
2599 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2600 } else
2601/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
2602/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
2603 */
2604 mpi_control |= (0x500);
2605
2606 } else
2607 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2608
2609 if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON))
2610 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
2611
2612 smid = mpt2sas_base_get_smid(ioc, ioc->scsi_io_cb_idx);
2613 if (!smid) {
2614 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2615 ioc->name, __func__);
2616 goto out;
2617 }
2618 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2619 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06002620 _scsih_setup_eedp(scmd, mpi_request);
Eric Moore635374e2009-03-09 01:21:12 -06002621 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2622 if (sas_device_priv_data->sas_target->flags &
2623 MPT_TARGET_FLAGS_RAID_COMPONENT)
2624 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
2625 else
2626 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2627 mpi_request->DevHandle =
2628 cpu_to_le16(sas_device_priv_data->sas_target->handle);
2629 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2630 mpi_request->Control = cpu_to_le32(mpi_control);
2631 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
2632 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
2633 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
2634 mpi_request->SenseBufferLowAddress =
2635 (u32)mpt2sas_base_get_sense_buffer_dma(ioc, smid);
2636 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
2637 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
2638 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
2639
2640 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
2641 mpi_request->LUN);
2642 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
2643
2644 if (!mpi_request->DataLength) {
2645 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
2646 } else {
2647 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
2648 mpt2sas_base_free_smid(ioc, smid);
2649 goto out;
2650 }
2651 }
2652
2653 _scsih_scsi_lookup_set(ioc, smid, scmd);
2654 mpt2sas_base_put_smid_scsi_io(ioc, smid, 0,
2655 sas_device_priv_data->sas_target->handle);
2656 return 0;
2657
2658 out:
2659 return SCSI_MLQUEUE_HOST_BUSY;
2660}
2661
2662/**
2663 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
2664 * @sense_buffer: sense data returned by target
2665 * @data: normalized skey/asc/ascq
2666 *
2667 * Return nothing.
2668 */
2669static void
2670_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
2671{
2672 if ((sense_buffer[0] & 0x7F) >= 0x72) {
2673 /* descriptor format */
2674 data->skey = sense_buffer[1] & 0x0F;
2675 data->asc = sense_buffer[2];
2676 data->ascq = sense_buffer[3];
2677 } else {
2678 /* fixed format */
2679 data->skey = sense_buffer[2] & 0x0F;
2680 data->asc = sense_buffer[12];
2681 data->ascq = sense_buffer[13];
2682 }
2683}
2684
2685#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
2686/**
2687 * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
2688 * @ioc: per adapter object
2689 * @scmd: pointer to scsi command object
2690 * @mpi_reply: reply mf payload returned from firmware
2691 *
2692 * scsi_status - SCSI Status code returned from target device
2693 * scsi_state - state info associated with SCSI_IO determined by ioc
2694 * ioc_status - ioc supplied status info
2695 *
2696 * Return nothing.
2697 */
2698static void
2699_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
2700 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
2701{
2702 u32 response_info;
2703 u8 *response_bytes;
2704 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
2705 MPI2_IOCSTATUS_MASK;
2706 u8 scsi_state = mpi_reply->SCSIState;
2707 u8 scsi_status = mpi_reply->SCSIStatus;
2708 char *desc_ioc_state = NULL;
2709 char *desc_scsi_status = NULL;
2710 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05302711 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
2712
2713 if (log_info == 0x31170000)
2714 return;
Eric Moore635374e2009-03-09 01:21:12 -06002715
2716 switch (ioc_status) {
2717 case MPI2_IOCSTATUS_SUCCESS:
2718 desc_ioc_state = "success";
2719 break;
2720 case MPI2_IOCSTATUS_INVALID_FUNCTION:
2721 desc_ioc_state = "invalid function";
2722 break;
2723 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
2724 desc_ioc_state = "scsi recovered error";
2725 break;
2726 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
2727 desc_ioc_state = "scsi invalid dev handle";
2728 break;
2729 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2730 desc_ioc_state = "scsi device not there";
2731 break;
2732 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
2733 desc_ioc_state = "scsi data overrun";
2734 break;
2735 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
2736 desc_ioc_state = "scsi data underrun";
2737 break;
2738 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
2739 desc_ioc_state = "scsi io data error";
2740 break;
2741 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2742 desc_ioc_state = "scsi protocol error";
2743 break;
2744 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
2745 desc_ioc_state = "scsi task terminated";
2746 break;
2747 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2748 desc_ioc_state = "scsi residual mismatch";
2749 break;
2750 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2751 desc_ioc_state = "scsi task mgmt failed";
2752 break;
2753 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
2754 desc_ioc_state = "scsi ioc terminated";
2755 break;
2756 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
2757 desc_ioc_state = "scsi ext terminated";
2758 break;
Eric Moore3c621b32009-05-18 12:59:41 -06002759 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2760 desc_ioc_state = "eedp guard error";
2761 break;
2762 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2763 desc_ioc_state = "eedp ref tag error";
2764 break;
2765 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2766 desc_ioc_state = "eedp app tag error";
2767 break;
Eric Moore635374e2009-03-09 01:21:12 -06002768 default:
2769 desc_ioc_state = "unknown";
2770 break;
2771 }
2772
2773 switch (scsi_status) {
2774 case MPI2_SCSI_STATUS_GOOD:
2775 desc_scsi_status = "good";
2776 break;
2777 case MPI2_SCSI_STATUS_CHECK_CONDITION:
2778 desc_scsi_status = "check condition";
2779 break;
2780 case MPI2_SCSI_STATUS_CONDITION_MET:
2781 desc_scsi_status = "condition met";
2782 break;
2783 case MPI2_SCSI_STATUS_BUSY:
2784 desc_scsi_status = "busy";
2785 break;
2786 case MPI2_SCSI_STATUS_INTERMEDIATE:
2787 desc_scsi_status = "intermediate";
2788 break;
2789 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
2790 desc_scsi_status = "intermediate condmet";
2791 break;
2792 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
2793 desc_scsi_status = "reservation conflict";
2794 break;
2795 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
2796 desc_scsi_status = "command terminated";
2797 break;
2798 case MPI2_SCSI_STATUS_TASK_SET_FULL:
2799 desc_scsi_status = "task set full";
2800 break;
2801 case MPI2_SCSI_STATUS_ACA_ACTIVE:
2802 desc_scsi_status = "aca active";
2803 break;
2804 case MPI2_SCSI_STATUS_TASK_ABORTED:
2805 desc_scsi_status = "task aborted";
2806 break;
2807 default:
2808 desc_scsi_status = "unknown";
2809 break;
2810 }
2811
2812 desc_scsi_state[0] = '\0';
2813 if (!scsi_state)
2814 desc_scsi_state = " ";
2815 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
2816 strcat(desc_scsi_state, "response info ");
2817 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
2818 strcat(desc_scsi_state, "state terminated ");
2819 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
2820 strcat(desc_scsi_state, "no status ");
2821 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
2822 strcat(desc_scsi_state, "autosense failed ");
2823 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
2824 strcat(desc_scsi_state, "autosense valid ");
2825
2826 scsi_print_command(scmd);
2827 printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
2828 "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
2829 le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
2830 ioc_status, smid);
2831 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
2832 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
2833 scsi_get_resid(scmd));
2834 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
2835 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
2836 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
2837 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
2838 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
2839 scsi_status, desc_scsi_state, scsi_state);
2840
2841 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2842 struct sense_info data;
2843 _scsih_normalize_sense(scmd->sense_buffer, &data);
2844 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
2845 "[0x%02x,0x%02x,0x%02x]\n", ioc->name, data.skey,
2846 data.asc, data.ascq);
2847 }
2848
2849 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
2850 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
2851 response_bytes = (u8 *)&response_info;
2852 _scsih_response_code(ioc, response_bytes[3]);
2853 }
2854}
2855#endif
2856
2857/**
2858 * _scsih_smart_predicted_fault - illuminate Fault LED
2859 * @ioc: per adapter object
2860 * @handle: device handle
2861 *
2862 * Return nothing.
2863 */
2864static void
2865_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2866{
2867 Mpi2SepReply_t mpi_reply;
2868 Mpi2SepRequest_t mpi_request;
2869 struct scsi_target *starget;
2870 struct MPT2SAS_TARGET *sas_target_priv_data;
2871 Mpi2EventNotificationReply_t *event_reply;
2872 Mpi2EventDataSasDeviceStatusChange_t *event_data;
2873 struct _sas_device *sas_device;
2874 ssize_t sz;
2875 unsigned long flags;
2876
2877 /* only handle non-raid devices */
2878 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2879 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2880 if (!sas_device) {
2881 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2882 return;
2883 }
2884 starget = sas_device->starget;
2885 sas_target_priv_data = starget->hostdata;
2886
2887 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
2888 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
2889 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2890 return;
2891 }
2892 starget_printk(KERN_WARNING, starget, "predicted fault\n");
2893 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2894
2895 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
2896 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
2897 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
2898 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
2899 mpi_request.SlotStatus =
2900 MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
2901 mpi_request.DevHandle = cpu_to_le16(handle);
2902 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
2903 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
2904 &mpi_request)) != 0) {
2905 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2906 ioc->name, __FILE__, __LINE__, __func__);
2907 return;
2908 }
2909
2910 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
2911 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2912 "enclosure_processor: ioc_status (0x%04x), "
2913 "loginfo(0x%08x)\n", ioc->name,
2914 le16_to_cpu(mpi_reply.IOCStatus),
2915 le32_to_cpu(mpi_reply.IOCLogInfo)));
2916 return;
2917 }
2918 }
2919
2920 /* insert into event log */
2921 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
2922 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
2923 event_reply = kzalloc(sz, GFP_KERNEL);
2924 if (!event_reply) {
2925 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2926 ioc->name, __FILE__, __LINE__, __func__);
2927 return;
2928 }
2929
2930 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
2931 event_reply->Event =
2932 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
2933 event_reply->MsgLength = sz/4;
2934 event_reply->EventDataLength =
2935 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
2936 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
2937 event_reply->EventData;
2938 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
2939 event_data->ASC = 0x5D;
2940 event_data->DevHandle = cpu_to_le16(handle);
2941 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
2942 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
2943 kfree(event_reply);
2944}
2945
2946/**
Eric Moored5d135b2009-05-18 13:02:08 -06002947 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06002948 * @ioc: per adapter object
2949 * @smid: system request message index
2950 * @VF_ID: virtual function id
2951 * @reply: reply message frame(lower 32bit addr)
2952 *
2953 * Callback handler when using scsih_qcmd.
2954 *
2955 * Return nothing.
2956 */
2957static void
Eric Moored5d135b2009-05-18 13:02:08 -06002958_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06002959{
2960 Mpi2SCSIIORequest_t *mpi_request;
2961 Mpi2SCSIIOReply_t *mpi_reply;
2962 struct scsi_cmnd *scmd;
2963 u16 ioc_status;
2964 u32 xfer_cnt;
2965 u8 scsi_state;
2966 u8 scsi_status;
2967 u32 log_info;
2968 struct MPT2SAS_DEVICE *sas_device_priv_data;
2969 u32 response_code;
2970
2971 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2972 scmd = _scsih_scsi_lookup_getclear(ioc, smid);
2973 if (scmd == NULL)
2974 return;
2975
2976 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2977
2978 if (mpi_reply == NULL) {
2979 scmd->result = DID_OK << 16;
2980 goto out;
2981 }
2982
2983 sas_device_priv_data = scmd->device->hostdata;
2984 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
2985 sas_device_priv_data->sas_target->deleted) {
2986 scmd->result = DID_NO_CONNECT << 16;
2987 goto out;
2988 }
2989
2990 /* turning off TLR */
2991 if (!sas_device_priv_data->tlr_snoop_check) {
2992 sas_device_priv_data->tlr_snoop_check++;
2993 if (sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) {
2994 response_code = (le32_to_cpu(mpi_reply->ResponseInfo)
2995 >> 24);
2996 if (response_code ==
2997 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
2998 sas_device_priv_data->flags &=
2999 ~MPT_DEVICE_TLR_ON;
3000 }
3001 }
3002
3003 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3004 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3005 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3006 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3007 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3008 else
3009 log_info = 0;
3010 ioc_status &= MPI2_IOCSTATUS_MASK;
3011 scsi_state = mpi_reply->SCSIState;
3012 scsi_status = mpi_reply->SCSIStatus;
3013
3014 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3015 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3016 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3017 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3018 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3019 }
3020
3021 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3022 struct sense_info data;
3023 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3024 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06003025 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06003026 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06003027 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06003028 _scsih_normalize_sense(scmd->sense_buffer, &data);
3029 /* failure prediction threshold exceeded */
3030 if (data.asc == 0x5D)
3031 _scsih_smart_predicted_fault(ioc,
3032 le16_to_cpu(mpi_reply->DevHandle));
3033 }
3034
3035 switch (ioc_status) {
3036 case MPI2_IOCSTATUS_BUSY:
3037 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3038 scmd->result = SAM_STAT_BUSY;
3039 break;
3040
3041 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3042 scmd->result = DID_NO_CONNECT << 16;
3043 break;
3044
3045 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3046 if (sas_device_priv_data->block) {
3047 scmd->result = (DID_BUS_BUSY << 16);
3048 break;
3049 }
3050
3051 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3052 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3053 scmd->result = DID_RESET << 16;
3054 break;
3055
3056 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3057 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3058 scmd->result = DID_SOFT_ERROR << 16;
3059 else
3060 scmd->result = (DID_OK << 16) | scsi_status;
3061 break;
3062
3063 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3064 scmd->result = (DID_OK << 16) | scsi_status;
3065
3066 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3067 break;
3068
3069 if (xfer_cnt < scmd->underflow) {
3070 if (scsi_status == SAM_STAT_BUSY)
3071 scmd->result = SAM_STAT_BUSY;
3072 else
3073 scmd->result = DID_SOFT_ERROR << 16;
3074 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3075 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3076 scmd->result = DID_SOFT_ERROR << 16;
3077 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3078 scmd->result = DID_RESET << 16;
3079 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3080 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3081 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3082 scmd->result = (DRIVER_SENSE << 24) |
3083 SAM_STAT_CHECK_CONDITION;
3084 scmd->sense_buffer[0] = 0x70;
3085 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3086 scmd->sense_buffer[12] = 0x20;
3087 scmd->sense_buffer[13] = 0;
3088 }
3089 break;
3090
3091 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3092 scsi_set_resid(scmd, 0);
3093 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3094 case MPI2_IOCSTATUS_SUCCESS:
3095 scmd->result = (DID_OK << 16) | scsi_status;
3096 if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3097 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3098 scmd->result = DID_SOFT_ERROR << 16;
3099 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3100 scmd->result = DID_RESET << 16;
3101 break;
3102
Eric Moore3c621b32009-05-18 12:59:41 -06003103 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3104 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3105 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3106 _scsih_eedp_error_handling(scmd, ioc_status);
3107 break;
Eric Moore635374e2009-03-09 01:21:12 -06003108 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3109 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3110 case MPI2_IOCSTATUS_INVALID_SGL:
3111 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3112 case MPI2_IOCSTATUS_INVALID_FIELD:
3113 case MPI2_IOCSTATUS_INVALID_STATE:
3114 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3115 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3116 default:
3117 scmd->result = DID_SOFT_ERROR << 16;
3118 break;
3119
3120 }
3121
3122#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3123 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3124 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3125#endif
3126
3127 out:
3128 scsi_dma_unmap(scmd);
3129 scmd->scsi_done(scmd);
3130}
3131
3132/**
Eric Moore635374e2009-03-09 01:21:12 -06003133 * _scsih_sas_host_refresh - refreshing sas host object contents
3134 * @ioc: per adapter object
3135 * @update: update link information
3136 * Context: user
3137 *
3138 * During port enable, fw will send topology events for every device. Its
3139 * possible that the handles may change from the previous setting, so this
3140 * code keeping handles updating if changed.
3141 *
3142 * Return nothing.
3143 */
3144static void
3145_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc, u8 update)
3146{
3147 u16 sz;
3148 u16 ioc_status;
3149 int i;
3150 Mpi2ConfigReply_t mpi_reply;
3151 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3152
3153 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3154 "updating handles for sas_host(0x%016llx)\n",
3155 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3156
3157 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3158 * sizeof(Mpi2SasIOUnit0PhyData_t));
3159 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3160 if (!sas_iounit_pg0) {
3161 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3162 ioc->name, __FILE__, __LINE__, __func__);
3163 return;
3164 }
3165 if (!(mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3166 sas_iounit_pg0, sz))) {
3167 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3168 MPI2_IOCSTATUS_MASK;
3169 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3170 goto out;
3171 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3172 ioc->sas_hba.phy[i].handle =
3173 le16_to_cpu(sas_iounit_pg0->PhyData[i].
3174 ControllerDevHandle);
3175 if (update)
Kashyap, Desaicc0f5202009-08-20 13:22:39 +05303176 mpt2sas_transport_update_links(
3177 ioc,
Eric Moore635374e2009-03-09 01:21:12 -06003178 ioc->sas_hba.phy[i].handle,
3179 le16_to_cpu(sas_iounit_pg0->PhyData[i].
3180 AttachedDevHandle), i,
3181 sas_iounit_pg0->PhyData[i].
3182 NegotiatedLinkRate >> 4);
3183 }
3184 }
3185
3186 out:
3187 kfree(sas_iounit_pg0);
3188}
3189
3190/**
3191 * _scsih_sas_host_add - create sas host object
3192 * @ioc: per adapter object
3193 *
3194 * Creating host side data object, stored in ioc->sas_hba
3195 *
3196 * Return nothing.
3197 */
3198static void
3199_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
3200{
3201 int i;
3202 Mpi2ConfigReply_t mpi_reply;
3203 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3204 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3205 Mpi2SasPhyPage0_t phy_pg0;
3206 Mpi2SasDevicePage0_t sas_device_pg0;
3207 Mpi2SasEnclosurePage0_t enclosure_pg0;
3208 u16 ioc_status;
3209 u16 sz;
3210 u16 device_missing_delay;
3211
3212 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
3213 if (!ioc->sas_hba.num_phys) {
3214 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3215 ioc->name, __FILE__, __LINE__, __func__);
3216 return;
3217 }
3218
3219 /* sas_iounit page 0 */
3220 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
3221 sizeof(Mpi2SasIOUnit0PhyData_t));
3222 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3223 if (!sas_iounit_pg0) {
3224 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3225 ioc->name, __FILE__, __LINE__, __func__);
3226 return;
3227 }
3228 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3229 sas_iounit_pg0, sz))) {
3230 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3231 ioc->name, __FILE__, __LINE__, __func__);
3232 goto out;
3233 }
3234 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3235 MPI2_IOCSTATUS_MASK;
3236 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3237 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3238 ioc->name, __FILE__, __LINE__, __func__);
3239 goto out;
3240 }
3241
3242 /* sas_iounit page 1 */
3243 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
3244 sizeof(Mpi2SasIOUnit1PhyData_t));
3245 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3246 if (!sas_iounit_pg1) {
3247 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3248 ioc->name, __FILE__, __LINE__, __func__);
3249 goto out;
3250 }
3251 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3252 sas_iounit_pg1, sz))) {
3253 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3254 ioc->name, __FILE__, __LINE__, __func__);
3255 goto out;
3256 }
3257 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3258 MPI2_IOCSTATUS_MASK;
3259 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3260 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3261 ioc->name, __FILE__, __LINE__, __func__);
3262 goto out;
3263 }
3264
3265 ioc->io_missing_delay =
3266 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
3267 device_missing_delay =
3268 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
3269 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3270 ioc->device_missing_delay = (device_missing_delay &
3271 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3272 else
3273 ioc->device_missing_delay = device_missing_delay &
3274 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3275
3276 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
3277 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
3278 sizeof(struct _sas_phy), GFP_KERNEL);
3279 if (!ioc->sas_hba.phy) {
3280 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3281 ioc->name, __FILE__, __LINE__, __func__);
3282 goto out;
3283 }
3284 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3285 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
3286 i))) {
3287 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3288 ioc->name, __FILE__, __LINE__, __func__);
3289 goto out;
3290 }
3291 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3292 MPI2_IOCSTATUS_MASK;
3293 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3294 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3295 ioc->name, __FILE__, __LINE__, __func__);
3296 goto out;
3297 }
3298 ioc->sas_hba.phy[i].handle =
3299 le16_to_cpu(sas_iounit_pg0->PhyData[i].ControllerDevHandle);
3300 ioc->sas_hba.phy[i].phy_id = i;
3301 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
3302 phy_pg0, ioc->sas_hba.parent_dev);
3303 }
3304 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3305 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.phy[0].handle))) {
3306 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3307 ioc->name, __FILE__, __LINE__, __func__);
3308 goto out;
3309 }
3310 ioc->sas_hba.handle = le16_to_cpu(sas_device_pg0.DevHandle);
3311 ioc->sas_hba.enclosure_handle =
3312 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3313 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3314 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
3315 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
3316 (unsigned long long) ioc->sas_hba.sas_address,
3317 ioc->sas_hba.num_phys) ;
3318
3319 if (ioc->sas_hba.enclosure_handle) {
3320 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3321 &enclosure_pg0,
3322 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3323 ioc->sas_hba.enclosure_handle))) {
3324 ioc->sas_hba.enclosure_logical_id =
3325 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3326 }
3327 }
3328
3329 out:
3330 kfree(sas_iounit_pg1);
3331 kfree(sas_iounit_pg0);
3332}
3333
3334/**
3335 * _scsih_expander_add - creating expander object
3336 * @ioc: per adapter object
3337 * @handle: expander handle
3338 *
3339 * Creating expander object, stored in ioc->sas_expander_list.
3340 *
3341 * Return 0 for success, else error.
3342 */
3343static int
3344_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3345{
3346 struct _sas_node *sas_expander;
3347 Mpi2ConfigReply_t mpi_reply;
3348 Mpi2ExpanderPage0_t expander_pg0;
3349 Mpi2ExpanderPage1_t expander_pg1;
3350 Mpi2SasEnclosurePage0_t enclosure_pg0;
3351 u32 ioc_status;
3352 u16 parent_handle;
3353 __le64 sas_address;
3354 int i;
3355 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05303356 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06003357 int rc = 0;
3358
3359 if (!handle)
3360 return -1;
3361
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303362 if (ioc->shost_recovery)
3363 return -1;
3364
Eric Moore635374e2009-03-09 01:21:12 -06003365 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
3366 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
3367 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3368 ioc->name, __FILE__, __LINE__, __func__);
3369 return -1;
3370 }
3371
3372 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3373 MPI2_IOCSTATUS_MASK;
3374 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3375 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3376 ioc->name, __FILE__, __LINE__, __func__);
3377 return -1;
3378 }
3379
3380 /* handle out of order topology events */
3381 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
3382 if (parent_handle >= ioc->sas_hba.num_phys) {
3383 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3384 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3385 parent_handle);
3386 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3387 if (!sas_expander) {
3388 rc = _scsih_expander_add(ioc, parent_handle);
3389 if (rc != 0)
3390 return rc;
3391 }
3392 }
3393
3394 sas_address = le64_to_cpu(expander_pg0.SASAddress);
3395
3396 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3397 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3398 sas_address);
3399 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3400
3401 if (sas_expander)
3402 return 0;
3403
3404 sas_expander = kzalloc(sizeof(struct _sas_node),
3405 GFP_KERNEL);
3406 if (!sas_expander) {
3407 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3408 ioc->name, __FILE__, __LINE__, __func__);
3409 return -1;
3410 }
3411
3412 sas_expander->handle = handle;
3413 sas_expander->num_phys = expander_pg0.NumPhys;
3414 sas_expander->parent_handle = parent_handle;
3415 sas_expander->enclosure_handle =
3416 le16_to_cpu(expander_pg0.EnclosureHandle);
3417 sas_expander->sas_address = sas_address;
3418
3419 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
3420 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
3421 handle, sas_expander->parent_handle, (unsigned long long)
3422 sas_expander->sas_address, sas_expander->num_phys);
3423
3424 if (!sas_expander->num_phys)
3425 goto out_fail;
3426 sas_expander->phy = kcalloc(sas_expander->num_phys,
3427 sizeof(struct _sas_phy), GFP_KERNEL);
3428 if (!sas_expander->phy) {
3429 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3430 ioc->name, __FILE__, __LINE__, __func__);
3431 rc = -1;
3432 goto out_fail;
3433 }
3434
3435 INIT_LIST_HEAD(&sas_expander->sas_port_list);
3436 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
3437 sas_expander->parent_handle);
3438 if (!mpt2sas_port) {
3439 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3440 ioc->name, __FILE__, __LINE__, __func__);
3441 rc = -1;
3442 goto out_fail;
3443 }
3444 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
3445
3446 for (i = 0 ; i < sas_expander->num_phys ; i++) {
3447 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
3448 &expander_pg1, i, handle))) {
3449 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3450 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05303451 rc = -1;
3452 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06003453 }
3454 sas_expander->phy[i].handle = handle;
3455 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05303456
3457 if ((mpt2sas_transport_add_expander_phy(ioc,
3458 &sas_expander->phy[i], expander_pg1,
3459 sas_expander->parent_dev))) {
3460 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3461 ioc->name, __FILE__, __LINE__, __func__);
3462 rc = -1;
3463 goto out_fail;
3464 }
Eric Moore635374e2009-03-09 01:21:12 -06003465 }
3466
3467 if (sas_expander->enclosure_handle) {
3468 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3469 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3470 sas_expander->enclosure_handle))) {
3471 sas_expander->enclosure_logical_id =
3472 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3473 }
3474 }
3475
3476 _scsih_expander_node_add(ioc, sas_expander);
3477 return 0;
3478
3479 out_fail:
3480
Kashyap, Desai20f58952009-08-07 19:34:26 +05303481 if (mpt2sas_port)
3482 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
3483 sas_expander->parent_handle);
Eric Moore635374e2009-03-09 01:21:12 -06003484 kfree(sas_expander);
3485 return rc;
3486}
3487
3488/**
3489 * _scsih_expander_remove - removing expander object
3490 * @ioc: per adapter object
3491 * @handle: expander handle
3492 *
3493 * Return nothing.
3494 */
3495static void
3496_scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3497{
3498 struct _sas_node *sas_expander;
3499 unsigned long flags;
3500
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303501 if (ioc->shost_recovery)
3502 return;
3503
Eric Moore635374e2009-03-09 01:21:12 -06003504 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3505 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc, handle);
3506 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3507 _scsih_expander_node_remove(ioc, sas_expander);
3508}
3509
3510/**
3511 * _scsih_add_device - creating sas device object
3512 * @ioc: per adapter object
3513 * @handle: sas device handle
3514 * @phy_num: phy number end device attached to
3515 * @is_pd: is this hidden raid component
3516 *
3517 * Creating end device object, stored in ioc->sas_device_list.
3518 *
3519 * Returns 0 for success, non-zero for failure.
3520 */
3521static int
3522_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
3523{
3524 Mpi2ConfigReply_t mpi_reply;
3525 Mpi2SasDevicePage0_t sas_device_pg0;
3526 Mpi2SasEnclosurePage0_t enclosure_pg0;
3527 struct _sas_device *sas_device;
3528 u32 ioc_status;
3529 __le64 sas_address;
3530 u32 device_info;
3531 unsigned long flags;
3532
3533 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3534 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
3535 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3536 ioc->name, __FILE__, __LINE__, __func__);
3537 return -1;
3538 }
3539
3540 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3541 MPI2_IOCSTATUS_MASK;
3542 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3543 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3544 ioc->name, __FILE__, __LINE__, __func__);
3545 return -1;
3546 }
3547
3548 /* check if device is present */
3549 if (!(le16_to_cpu(sas_device_pg0.Flags) &
3550 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
3551 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3552 ioc->name, __FILE__, __LINE__, __func__);
3553 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
3554 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
3555 return -1;
3556 }
3557
3558 /* check if there were any issus with discovery */
3559 if (sas_device_pg0.AccessStatus ==
3560 MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED) {
3561 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3562 ioc->name, __FILE__, __LINE__, __func__);
3563 printk(MPT2SAS_ERR_FMT "AccessStatus = 0x%02x\n",
3564 ioc->name, sas_device_pg0.AccessStatus);
3565 return -1;
3566 }
3567
3568 /* check if this is end device */
3569 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
3570 if (!(_scsih_is_end_device(device_info))) {
3571 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3572 ioc->name, __FILE__, __LINE__, __func__);
3573 return -1;
3574 }
3575
3576 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3577
3578 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3579 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3580 sas_address);
3581 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3582
3583 if (sas_device) {
3584 _scsih_ublock_io_device(ioc, handle);
3585 return 0;
3586 }
3587
3588 sas_device = kzalloc(sizeof(struct _sas_device),
3589 GFP_KERNEL);
3590 if (!sas_device) {
3591 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3592 ioc->name, __FILE__, __LINE__, __func__);
3593 return -1;
3594 }
3595
3596 sas_device->handle = handle;
3597 sas_device->parent_handle =
3598 le16_to_cpu(sas_device_pg0.ParentDevHandle);
3599 sas_device->enclosure_handle =
3600 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3601 sas_device->slot =
3602 le16_to_cpu(sas_device_pg0.Slot);
3603 sas_device->device_info = device_info;
3604 sas_device->sas_address = sas_address;
3605 sas_device->hidden_raid_component = is_pd;
3606
3607 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05303608 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
3609 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3610 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06003611 sas_device->enclosure_logical_id =
3612 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06003613
3614 /* get device name */
3615 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
3616
3617 if (ioc->wait_for_port_enable_to_complete)
3618 _scsih_sas_device_init_add(ioc, sas_device);
3619 else
3620 _scsih_sas_device_add(ioc, sas_device);
3621
3622 return 0;
3623}
3624
3625/**
3626 * _scsih_remove_device - removing sas device object
3627 * @ioc: per adapter object
3628 * @handle: sas device handle
3629 *
3630 * Return nothing.
3631 */
3632static void
3633_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3634{
3635 struct MPT2SAS_TARGET *sas_target_priv_data;
3636 struct _sas_device *sas_device;
3637 unsigned long flags;
3638 Mpi2SasIoUnitControlReply_t mpi_reply;
3639 Mpi2SasIoUnitControlRequest_t mpi_request;
3640 u16 device_handle;
3641
3642 /* lookup sas_device */
3643 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3644 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3645 if (!sas_device) {
3646 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3647 return;
3648 }
3649
3650 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle"
3651 "(0x%04x)\n", ioc->name, __func__, handle));
3652
3653 if (sas_device->starget && sas_device->starget->hostdata) {
3654 sas_target_priv_data = sas_device->starget->hostdata;
3655 sas_target_priv_data->deleted = 1;
3656 }
3657 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3658
3659 if (ioc->remove_host)
3660 goto out;
3661
3662 /* Target Reset to flush out all the outstanding IO */
3663 device_handle = (sas_device->hidden_raid_component) ?
3664 sas_device->volume_handle : handle;
3665 if (device_handle) {
3666 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
3667 "handle(0x%04x)\n", ioc->name, device_handle));
3668 mutex_lock(&ioc->tm_cmds.mutex);
3669 mpt2sas_scsih_issue_tm(ioc, device_handle, 0,
3670 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
3671 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
3672 mutex_unlock(&ioc->tm_cmds.mutex);
3673 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
3674 "done: handle(0x%04x)\n", ioc->name, device_handle));
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303675 if (ioc->shost_recovery)
3676 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06003677 }
3678
3679 /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
3680 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: handle"
3681 "(0x%04x)\n", ioc->name, handle));
3682 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3683 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3684 mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3685 mpi_request.DevHandle = handle;
3686 mpi_request.VF_ID = 0;
3687 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
3688 &mpi_request)) != 0) {
3689 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3690 ioc->name, __FILE__, __LINE__, __func__);
3691 }
3692
3693 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: ioc_status"
3694 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
3695 le16_to_cpu(mpi_reply.IOCStatus),
3696 le32_to_cpu(mpi_reply.IOCLogInfo)));
3697
3698 out:
Kashyap, Desai34a03be2009-08-20 13:23:19 +05303699
3700 _scsih_ublock_io_device(ioc, handle);
3701
Eric Moore635374e2009-03-09 01:21:12 -06003702 mpt2sas_transport_port_remove(ioc, sas_device->sas_address,
3703 sas_device->parent_handle);
3704
3705 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
3706 "(0x%016llx)\n", ioc->name, sas_device->handle,
3707 (unsigned long long) sas_device->sas_address);
3708 _scsih_sas_device_remove(ioc, sas_device);
3709
3710 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle"
3711 "(0x%04x)\n", ioc->name, __func__, handle));
3712}
3713
3714#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3715/**
3716 * _scsih_sas_topology_change_event_debug - debug for topology event
3717 * @ioc: per adapter object
3718 * @event_data: event data payload
3719 * Context: user.
3720 */
3721static void
3722_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3723 Mpi2EventDataSasTopologyChangeList_t *event_data)
3724{
3725 int i;
3726 u16 handle;
3727 u16 reason_code;
3728 u8 phy_number;
3729 char *status_str = NULL;
3730 char link_rate[25];
3731
3732 switch (event_data->ExpStatus) {
3733 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
3734 status_str = "add";
3735 break;
3736 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
3737 status_str = "remove";
3738 break;
3739 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
3740 status_str = "responding";
3741 break;
3742 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
3743 status_str = "remove delay";
3744 break;
3745 default:
3746 status_str = "unknown status";
3747 break;
3748 }
3749 printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
3750 ioc->name, status_str);
3751 printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
3752 "start_phy(%02d), count(%d)\n",
3753 le16_to_cpu(event_data->ExpanderDevHandle),
3754 le16_to_cpu(event_data->EnclosureHandle),
3755 event_data->StartPhyNum, event_data->NumEntries);
3756 for (i = 0; i < event_data->NumEntries; i++) {
3757 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3758 if (!handle)
3759 continue;
3760 phy_number = event_data->StartPhyNum + i;
3761 reason_code = event_data->PHY[i].PhyStatus &
3762 MPI2_EVENT_SAS_TOPO_RC_MASK;
3763 switch (reason_code) {
3764 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
3765 snprintf(link_rate, 25, ": add, link(0x%02x)",
3766 (event_data->PHY[i].LinkRate >> 4));
3767 status_str = link_rate;
3768 break;
3769 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
3770 status_str = ": remove";
3771 break;
3772 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
3773 status_str = ": remove_delay";
3774 break;
3775 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
3776 snprintf(link_rate, 25, ": link(0x%02x)",
3777 (event_data->PHY[i].LinkRate >> 4));
3778 status_str = link_rate;
3779 break;
3780 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
3781 status_str = ": responding";
3782 break;
3783 default:
3784 status_str = ": unknown";
3785 break;
3786 }
3787 printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x)%s\n",
3788 phy_number, handle, status_str);
3789 }
3790}
3791#endif
3792
3793/**
3794 * _scsih_sas_topology_change_event - handle topology changes
3795 * @ioc: per adapter object
3796 * @VF_ID:
3797 * @event_data: event data payload
3798 * fw_event:
3799 * Context: user.
3800 *
3801 */
3802static void
3803_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
3804 Mpi2EventDataSasTopologyChangeList_t *event_data,
3805 struct fw_event_work *fw_event)
3806{
3807 int i;
3808 u16 parent_handle, handle;
3809 u16 reason_code;
3810 u8 phy_number;
3811 struct _sas_node *sas_expander;
3812 unsigned long flags;
3813 u8 link_rate_;
3814
3815#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3816 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
3817 _scsih_sas_topology_change_event_debug(ioc, event_data);
3818#endif
3819
3820 if (!ioc->sas_hba.num_phys)
3821 _scsih_sas_host_add(ioc);
3822 else
3823 _scsih_sas_host_refresh(ioc, 0);
3824
3825 if (fw_event->ignore) {
3826 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
3827 "event\n", ioc->name));
3828 return;
3829 }
3830
3831 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3832
3833 /* handle expander add */
3834 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
3835 if (_scsih_expander_add(ioc, parent_handle) != 0)
3836 return;
3837
3838 /* handle siblings events */
3839 for (i = 0; i < event_data->NumEntries; i++) {
3840 if (fw_event->ignore) {
3841 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
3842 "expander event\n", ioc->name));
3843 return;
3844 }
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303845 if (ioc->shost_recovery)
3846 return;
Eric Moore635374e2009-03-09 01:21:12 -06003847 if (event_data->PHY[i].PhyStatus &
3848 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
3849 continue;
3850 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3851 if (!handle)
3852 continue;
3853 phy_number = event_data->StartPhyNum + i;
3854 reason_code = event_data->PHY[i].PhyStatus &
3855 MPI2_EVENT_SAS_TOPO_RC_MASK;
3856 link_rate_ = event_data->PHY[i].LinkRate >> 4;
3857 switch (reason_code) {
3858 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
3859 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
3860 if (!parent_handle) {
3861 if (phy_number < ioc->sas_hba.num_phys)
Kashyap, Desaicc0f5202009-08-20 13:22:39 +05303862 mpt2sas_transport_update_links(
3863 ioc,
3864 ioc->sas_hba.phy[phy_number].handle,
3865 handle, phy_number, link_rate_);
Eric Moore635374e2009-03-09 01:21:12 -06003866 } else {
3867 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3868 sas_expander =
3869 mpt2sas_scsih_expander_find_by_handle(ioc,
3870 parent_handle);
3871 spin_unlock_irqrestore(&ioc->sas_node_lock,
3872 flags);
3873 if (sas_expander) {
3874 if (phy_number < sas_expander->num_phys)
Kashyap, Desaicc0f5202009-08-20 13:22:39 +05303875 mpt2sas_transport_update_links(
3876 ioc,
3877 sas_expander->
3878 phy[phy_number].handle,
3879 handle, phy_number,
3880 link_rate_);
Eric Moore635374e2009-03-09 01:21:12 -06003881 }
3882 }
Eric Moore635374e2009-03-09 01:21:12 -06003883 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED) {
3884 if (link_rate_ < MPI2_SAS_NEG_LINK_RATE_1_5)
3885 break;
3886 _scsih_add_device(ioc, handle, phy_number, 0);
3887 }
3888 break;
3889 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
3890 _scsih_remove_device(ioc, handle);
3891 break;
3892 }
3893 }
3894
3895 /* handle expander removal */
3896 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3897 _scsih_expander_remove(ioc, parent_handle);
3898
3899}
3900
3901#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3902/**
3903 * _scsih_sas_device_status_change_event_debug - debug for device event
3904 * @event_data: event data payload
3905 * Context: user.
3906 *
3907 * Return nothing.
3908 */
3909static void
3910_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3911 Mpi2EventDataSasDeviceStatusChange_t *event_data)
3912{
3913 char *reason_str = NULL;
3914
3915 switch (event_data->ReasonCode) {
3916 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
3917 reason_str = "smart data";
3918 break;
3919 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
3920 reason_str = "unsupported device discovered";
3921 break;
3922 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
3923 reason_str = "internal device reset";
3924 break;
3925 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
3926 reason_str = "internal task abort";
3927 break;
3928 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
3929 reason_str = "internal task abort set";
3930 break;
3931 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
3932 reason_str = "internal clear task set";
3933 break;
3934 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
3935 reason_str = "internal query task";
3936 break;
3937 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
3938 reason_str = "sata init failure";
3939 break;
3940 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
3941 reason_str = "internal device reset complete";
3942 break;
3943 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
3944 reason_str = "internal task abort complete";
3945 break;
3946 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
3947 reason_str = "internal async notification";
3948 break;
3949 default:
3950 reason_str = "unknown reason";
3951 break;
3952 }
3953 printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
3954 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
3955 reason_str, le16_to_cpu(event_data->DevHandle),
3956 (unsigned long long)le64_to_cpu(event_data->SASAddress));
3957 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
3958 printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
3959 event_data->ASC, event_data->ASCQ);
3960 printk(KERN_INFO "\n");
3961}
3962#endif
3963
3964/**
3965 * _scsih_sas_device_status_change_event - handle device status change
3966 * @ioc: per adapter object
3967 * @VF_ID:
3968 * @event_data: event data payload
3969 * Context: user.
3970 *
3971 * Return nothing.
3972 */
3973static void
3974_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
3975 Mpi2EventDataSasDeviceStatusChange_t *event_data)
3976{
3977#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3978 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
3979 _scsih_sas_device_status_change_event_debug(ioc, event_data);
3980#endif
3981}
3982
3983#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3984/**
3985 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
3986 * @ioc: per adapter object
3987 * @event_data: event data payload
3988 * Context: user.
3989 *
3990 * Return nothing.
3991 */
3992static void
3993_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3994 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
3995{
3996 char *reason_str = NULL;
3997
3998 switch (event_data->ReasonCode) {
3999 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
4000 reason_str = "enclosure add";
4001 break;
4002 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
4003 reason_str = "enclosure remove";
4004 break;
4005 default:
4006 reason_str = "unknown reason";
4007 break;
4008 }
4009
4010 printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
4011 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
4012 " number slots(%d)\n", ioc->name, reason_str,
4013 le16_to_cpu(event_data->EnclosureHandle),
4014 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
4015 le16_to_cpu(event_data->StartSlot));
4016}
4017#endif
4018
4019/**
4020 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
4021 * @ioc: per adapter object
4022 * @VF_ID:
4023 * @event_data: event data payload
4024 * Context: user.
4025 *
4026 * Return nothing.
4027 */
4028static void
4029_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4030 u8 VF_ID, Mpi2EventDataSasEnclDevStatusChange_t *event_data)
4031{
4032#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4033 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4034 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
4035 event_data);
4036#endif
4037}
4038
4039/**
4040 * _scsih_sas_broadcast_primative_event - handle broadcast events
4041 * @ioc: per adapter object
4042 * @event_data: event data payload
4043 * Context: user.
4044 *
4045 * Return nothing.
4046 */
4047static void
4048_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4049 Mpi2EventDataSasBroadcastPrimitive_t *event_data)
4050{
4051 struct scsi_cmnd *scmd;
4052 u16 smid, handle;
4053 u32 lun;
4054 struct MPT2SAS_DEVICE *sas_device_priv_data;
4055 u32 termination_count;
4056 u32 query_count;
4057 Mpi2SCSITaskManagementReply_t *mpi_reply;
4058
4059 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
4060 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
4061 event_data->PortWidth));
4062
4063 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
4064 __func__));
4065
4066 mutex_lock(&ioc->tm_cmds.mutex);
4067 termination_count = 0;
4068 query_count = 0;
4069 mpi_reply = ioc->tm_cmds.reply;
4070 for (smid = 1; smid <= ioc->request_depth; smid++) {
4071 scmd = _scsih_scsi_lookup_get(ioc, smid);
4072 if (!scmd)
4073 continue;
4074 sas_device_priv_data = scmd->device->hostdata;
4075 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
4076 continue;
4077 /* skip hidden raid components */
4078 if (sas_device_priv_data->sas_target->flags &
4079 MPT_TARGET_FLAGS_RAID_COMPONENT)
4080 continue;
4081 /* skip volumes */
4082 if (sas_device_priv_data->sas_target->flags &
4083 MPT_TARGET_FLAGS_VOLUME)
4084 continue;
4085
4086 handle = sas_device_priv_data->sas_target->handle;
4087 lun = sas_device_priv_data->lun;
4088 query_count++;
4089
4090 mpt2sas_scsih_issue_tm(ioc, handle, lun,
4091 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
Eric Moore8901cbb2009-04-21 15:41:32 -06004092 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Eric Moore635374e2009-03-09 01:21:12 -06004093
4094 if ((mpi_reply->IOCStatus == MPI2_IOCSTATUS_SUCCESS) &&
4095 (mpi_reply->ResponseCode ==
4096 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
4097 mpi_reply->ResponseCode ==
4098 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
4099 continue;
4100
4101 mpt2sas_scsih_issue_tm(ioc, handle, lun,
Eric Moore8901cbb2009-04-21 15:41:32 -06004102 MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30);
4103 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Eric Moore635374e2009-03-09 01:21:12 -06004104 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
4105 }
Eric Moore635374e2009-03-09 01:21:12 -06004106 ioc->broadcast_aen_busy = 0;
4107 mutex_unlock(&ioc->tm_cmds.mutex);
4108
4109 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
4110 "%s - exit, query_count = %d termination_count = %d\n",
4111 ioc->name, __func__, query_count, termination_count));
4112}
4113
4114/**
4115 * _scsih_sas_discovery_event - handle discovery events
4116 * @ioc: per adapter object
4117 * @event_data: event data payload
4118 * Context: user.
4119 *
4120 * Return nothing.
4121 */
4122static void
4123_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4124 Mpi2EventDataSasDiscovery_t *event_data)
4125{
4126#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4127 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
4128 printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
4129 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
4130 "start" : "stop");
4131 if (event_data->DiscoveryStatus)
4132 printk(MPT2SAS_DEBUG_FMT ", discovery_status(0x%08x)",
4133 ioc->name, le32_to_cpu(event_data->DiscoveryStatus));
4134 printk("\n");
4135 }
4136#endif
4137
4138 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
4139 !ioc->sas_hba.num_phys)
4140 _scsih_sas_host_add(ioc);
4141}
4142
4143/**
4144 * _scsih_reprobe_lun - reprobing lun
4145 * @sdev: scsi device struct
4146 * @no_uld_attach: sdev->no_uld_attach flag setting
4147 *
4148 **/
4149static void
4150_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
4151{
4152 int rc;
4153
4154 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
4155 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
4156 sdev->no_uld_attach ? "hidding" : "exposing");
4157 rc = scsi_device_reprobe(sdev);
4158}
4159
4160/**
4161 * _scsih_reprobe_target - reprobing target
4162 * @starget: scsi target struct
4163 * @no_uld_attach: sdev->no_uld_attach flag setting
4164 *
4165 * Note: no_uld_attach flag determines whether the disk device is attached
4166 * to block layer. A value of `1` means to not attach.
4167 **/
4168static void
4169_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
4170{
4171 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
4172
4173 if (no_uld_attach)
4174 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
4175 else
4176 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
4177
4178 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
4179 _scsih_reprobe_lun);
4180}
4181/**
4182 * _scsih_sas_volume_add - add new volume
4183 * @ioc: per adapter object
4184 * @element: IR config element data
4185 * Context: user.
4186 *
4187 * Return nothing.
4188 */
4189static void
4190_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
4191 Mpi2EventIrConfigElement_t *element)
4192{
4193 struct _raid_device *raid_device;
4194 unsigned long flags;
4195 u64 wwid;
4196 u16 handle = le16_to_cpu(element->VolDevHandle);
4197 int rc;
4198
Eric Moore635374e2009-03-09 01:21:12 -06004199 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4200 if (!wwid) {
4201 printk(MPT2SAS_ERR_FMT
4202 "failure at %s:%d/%s()!\n", ioc->name,
4203 __FILE__, __LINE__, __func__);
4204 return;
4205 }
4206
4207 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4208 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
4209 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4210
4211 if (raid_device)
4212 return;
4213
4214 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4215 if (!raid_device) {
4216 printk(MPT2SAS_ERR_FMT
4217 "failure at %s:%d/%s()!\n", ioc->name,
4218 __FILE__, __LINE__, __func__);
4219 return;
4220 }
4221
4222 raid_device->id = ioc->sas_id++;
4223 raid_device->channel = RAID_CHANNEL;
4224 raid_device->handle = handle;
4225 raid_device->wwid = wwid;
4226 _scsih_raid_device_add(ioc, raid_device);
4227 if (!ioc->wait_for_port_enable_to_complete) {
4228 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4229 raid_device->id, 0);
4230 if (rc)
4231 _scsih_raid_device_remove(ioc, raid_device);
4232 } else
4233 _scsih_determine_boot_device(ioc, raid_device, 1);
4234}
4235
4236/**
4237 * _scsih_sas_volume_delete - delete volume
4238 * @ioc: per adapter object
4239 * @element: IR config element data
4240 * Context: user.
4241 *
4242 * Return nothing.
4243 */
4244static void
4245_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
4246 Mpi2EventIrConfigElement_t *element)
4247{
4248 struct _raid_device *raid_device;
4249 u16 handle = le16_to_cpu(element->VolDevHandle);
4250 unsigned long flags;
4251 struct MPT2SAS_TARGET *sas_target_priv_data;
4252
Eric Moore635374e2009-03-09 01:21:12 -06004253 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4254 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4255 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4256 if (!raid_device)
4257 return;
4258 if (raid_device->starget) {
4259 sas_target_priv_data = raid_device->starget->hostdata;
4260 sas_target_priv_data->deleted = 1;
4261 scsi_remove_target(&raid_device->starget->dev);
4262 }
4263 _scsih_raid_device_remove(ioc, raid_device);
4264}
4265
4266/**
4267 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
4268 * @ioc: per adapter object
4269 * @element: IR config element data
4270 * Context: user.
4271 *
4272 * Return nothing.
4273 */
4274static void
4275_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
4276 Mpi2EventIrConfigElement_t *element)
4277{
4278 struct _sas_device *sas_device;
4279 unsigned long flags;
4280 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4281
4282 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4283 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4284 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4285 if (!sas_device)
4286 return;
4287
4288 /* exposing raid component */
4289 sas_device->volume_handle = 0;
4290 sas_device->volume_wwid = 0;
4291 sas_device->hidden_raid_component = 0;
4292 _scsih_reprobe_target(sas_device->starget, 0);
4293}
4294
4295/**
4296 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
4297 * @ioc: per adapter object
4298 * @element: IR config element data
4299 * Context: user.
4300 *
4301 * Return nothing.
4302 */
4303static void
4304_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
4305 Mpi2EventIrConfigElement_t *element)
4306{
4307 struct _sas_device *sas_device;
4308 unsigned long flags;
4309 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4310
4311 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4312 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4313 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4314 if (!sas_device)
4315 return;
4316
4317 /* hiding raid component */
4318 mpt2sas_config_get_volume_handle(ioc, handle,
4319 &sas_device->volume_handle);
4320 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
4321 &sas_device->volume_wwid);
4322 sas_device->hidden_raid_component = 1;
4323 _scsih_reprobe_target(sas_device->starget, 1);
4324}
4325
4326/**
4327 * _scsih_sas_pd_delete - delete pd component
4328 * @ioc: per adapter object
4329 * @element: IR config element data
4330 * Context: user.
4331 *
4332 * Return nothing.
4333 */
4334static void
4335_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
4336 Mpi2EventIrConfigElement_t *element)
4337{
4338 struct _sas_device *sas_device;
4339 unsigned long flags;
4340 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4341
4342 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4343 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4344 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4345 if (!sas_device)
4346 return;
4347 _scsih_remove_device(ioc, handle);
4348}
4349
4350/**
4351 * _scsih_sas_pd_add - remove pd component
4352 * @ioc: per adapter object
4353 * @element: IR config element data
4354 * Context: user.
4355 *
4356 * Return nothing.
4357 */
4358static void
4359_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
4360 Mpi2EventIrConfigElement_t *element)
4361{
4362 struct _sas_device *sas_device;
4363 unsigned long flags;
4364 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05304365 Mpi2ConfigReply_t mpi_reply;
4366 Mpi2SasDevicePage0_t sas_device_pg0;
4367 u32 ioc_status;
Eric Moore635374e2009-03-09 01:21:12 -06004368
4369 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4370 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4371 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai62727a72009-08-07 19:35:18 +05304372 if (sas_device) {
Eric Moore635374e2009-03-09 01:21:12 -06004373 sas_device->hidden_raid_component = 1;
Kashyap, Desai62727a72009-08-07 19:35:18 +05304374 return;
4375 }
4376
4377 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4378 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4379 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4380 ioc->name, __FILE__, __LINE__, __func__);
4381 return;
4382 }
4383
4384 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4385 MPI2_IOCSTATUS_MASK;
4386 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4387 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4388 ioc->name, __FILE__, __LINE__, __func__);
4389 return;
4390 }
4391
Kashyap, Desaicc0f5202009-08-20 13:22:39 +05304392 mpt2sas_transport_update_links(ioc,
Kashyap, Desai62727a72009-08-07 19:35:18 +05304393 le16_to_cpu(sas_device_pg0.ParentDevHandle),
4394 handle, sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
4395
4396 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06004397}
4398
4399#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4400/**
4401 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
4402 * @ioc: per adapter object
4403 * @event_data: event data payload
4404 * Context: user.
4405 *
4406 * Return nothing.
4407 */
4408static void
4409_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4410 Mpi2EventDataIrConfigChangeList_t *event_data)
4411{
4412 Mpi2EventIrConfigElement_t *element;
4413 u8 element_type;
4414 int i;
4415 char *reason_str = NULL, *element_str = NULL;
4416
4417 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4418
4419 printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
4420 ioc->name, (le32_to_cpu(event_data->Flags) &
4421 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
4422 "foreign" : "native", event_data->NumElements);
4423 for (i = 0; i < event_data->NumElements; i++, element++) {
4424 switch (element->ReasonCode) {
4425 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4426 reason_str = "add";
4427 break;
4428 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4429 reason_str = "remove";
4430 break;
4431 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
4432 reason_str = "no change";
4433 break;
4434 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4435 reason_str = "hide";
4436 break;
4437 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4438 reason_str = "unhide";
4439 break;
4440 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4441 reason_str = "volume_created";
4442 break;
4443 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4444 reason_str = "volume_deleted";
4445 break;
4446 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4447 reason_str = "pd_created";
4448 break;
4449 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4450 reason_str = "pd_deleted";
4451 break;
4452 default:
4453 reason_str = "unknown reason";
4454 break;
4455 }
4456 element_type = le16_to_cpu(element->ElementFlags) &
4457 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
4458 switch (element_type) {
4459 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
4460 element_str = "volume";
4461 break;
4462 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
4463 element_str = "phys disk";
4464 break;
4465 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
4466 element_str = "hot spare";
4467 break;
4468 default:
4469 element_str = "unknown element";
4470 break;
4471 }
4472 printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
4473 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
4474 reason_str, le16_to_cpu(element->VolDevHandle),
4475 le16_to_cpu(element->PhysDiskDevHandle),
4476 element->PhysDiskNum);
4477 }
4478}
4479#endif
4480
4481/**
4482 * _scsih_sas_ir_config_change_event - handle ir configuration change events
4483 * @ioc: per adapter object
4484 * @VF_ID:
4485 * @event_data: event data payload
4486 * Context: user.
4487 *
4488 * Return nothing.
4489 */
4490static void
4491_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4492 Mpi2EventDataIrConfigChangeList_t *event_data)
4493{
4494 Mpi2EventIrConfigElement_t *element;
4495 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05304496 u8 foreign_config;
Eric Moore635374e2009-03-09 01:21:12 -06004497
4498#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4499 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4500 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
4501
4502#endif
Kashyap, Desai62727a72009-08-07 19:35:18 +05304503 foreign_config = (le32_to_cpu(event_data->Flags) &
4504 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06004505
4506 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4507 for (i = 0; i < event_data->NumElements; i++, element++) {
4508
4509 switch (element->ReasonCode) {
4510 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4511 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05304512 if (!foreign_config)
4513 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06004514 break;
4515 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4516 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05304517 if (!foreign_config)
4518 _scsih_sas_volume_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06004519 break;
4520 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4521 _scsih_sas_pd_hide(ioc, element);
4522 break;
4523 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4524 _scsih_sas_pd_expose(ioc, element);
4525 break;
4526 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4527 _scsih_sas_pd_add(ioc, element);
4528 break;
4529 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4530 _scsih_sas_pd_delete(ioc, element);
4531 break;
4532 }
4533 }
4534}
4535
4536/**
4537 * _scsih_sas_ir_volume_event - IR volume event
4538 * @ioc: per adapter object
4539 * @event_data: event data payload
4540 * Context: user.
4541 *
4542 * Return nothing.
4543 */
4544static void
4545_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4546 Mpi2EventDataIrVolume_t *event_data)
4547{
4548 u64 wwid;
4549 unsigned long flags;
4550 struct _raid_device *raid_device;
4551 u16 handle;
4552 u32 state;
4553 int rc;
4554 struct MPT2SAS_TARGET *sas_target_priv_data;
4555
4556 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
4557 return;
4558
4559 handle = le16_to_cpu(event_data->VolDevHandle);
4560 state = le32_to_cpu(event_data->NewValue);
4561 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4562 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
4563 le32_to_cpu(event_data->PreviousValue), state));
4564
4565 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4566 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4567 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4568
4569 switch (state) {
4570 case MPI2_RAID_VOL_STATE_MISSING:
4571 case MPI2_RAID_VOL_STATE_FAILED:
4572 if (!raid_device)
4573 break;
4574 if (raid_device->starget) {
4575 sas_target_priv_data = raid_device->starget->hostdata;
4576 sas_target_priv_data->deleted = 1;
4577 scsi_remove_target(&raid_device->starget->dev);
4578 }
4579 _scsih_raid_device_remove(ioc, raid_device);
4580 break;
4581
4582 case MPI2_RAID_VOL_STATE_ONLINE:
4583 case MPI2_RAID_VOL_STATE_DEGRADED:
4584 case MPI2_RAID_VOL_STATE_OPTIMAL:
4585 if (raid_device)
4586 break;
4587
4588 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4589 if (!wwid) {
4590 printk(MPT2SAS_ERR_FMT
4591 "failure at %s:%d/%s()!\n", ioc->name,
4592 __FILE__, __LINE__, __func__);
4593 break;
4594 }
4595
4596 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4597 if (!raid_device) {
4598 printk(MPT2SAS_ERR_FMT
4599 "failure at %s:%d/%s()!\n", ioc->name,
4600 __FILE__, __LINE__, __func__);
4601 break;
4602 }
4603
4604 raid_device->id = ioc->sas_id++;
4605 raid_device->channel = RAID_CHANNEL;
4606 raid_device->handle = handle;
4607 raid_device->wwid = wwid;
4608 _scsih_raid_device_add(ioc, raid_device);
4609 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4610 raid_device->id, 0);
4611 if (rc)
4612 _scsih_raid_device_remove(ioc, raid_device);
4613 break;
4614
4615 case MPI2_RAID_VOL_STATE_INITIALIZING:
4616 default:
4617 break;
4618 }
4619}
4620
4621/**
4622 * _scsih_sas_ir_physical_disk_event - PD event
4623 * @ioc: per adapter object
4624 * @event_data: event data payload
4625 * Context: user.
4626 *
4627 * Return nothing.
4628 */
4629static void
4630_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4631 Mpi2EventDataIrPhysicalDisk_t *event_data)
4632{
4633 u16 handle;
4634 u32 state;
4635 struct _sas_device *sas_device;
4636 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05304637 Mpi2ConfigReply_t mpi_reply;
4638 Mpi2SasDevicePage0_t sas_device_pg0;
4639 u32 ioc_status;
Eric Moore635374e2009-03-09 01:21:12 -06004640
4641 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
4642 return;
4643
4644 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
4645 state = le32_to_cpu(event_data->NewValue);
4646
4647 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4648 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
4649 le32_to_cpu(event_data->PreviousValue), state));
4650
4651 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4652 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4653 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4654
4655 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06004656 case MPI2_RAID_PD_STATE_ONLINE:
4657 case MPI2_RAID_PD_STATE_DEGRADED:
4658 case MPI2_RAID_PD_STATE_REBUILDING:
4659 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desai62727a72009-08-07 19:35:18 +05304660 if (sas_device) {
Eric Moore635374e2009-03-09 01:21:12 -06004661 sas_device->hidden_raid_component = 1;
Kashyap, Desai62727a72009-08-07 19:35:18 +05304662 return;
4663 }
4664
4665 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
4666 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
4667 handle))) {
4668 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4669 ioc->name, __FILE__, __LINE__, __func__);
4670 return;
4671 }
4672
4673 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4674 MPI2_IOCSTATUS_MASK;
4675 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4676 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4677 ioc->name, __FILE__, __LINE__, __func__);
4678 return;
4679 }
4680
Kashyap, Desaicc0f5202009-08-20 13:22:39 +05304681 mpt2sas_transport_update_links(ioc,
Kashyap, Desai62727a72009-08-07 19:35:18 +05304682 le16_to_cpu(sas_device_pg0.ParentDevHandle),
4683 handle, sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
4684
4685 _scsih_add_device(ioc, handle, 0, 1);
4686
Eric Moore635374e2009-03-09 01:21:12 -06004687 break;
4688
Kashyap, Desai62727a72009-08-07 19:35:18 +05304689 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06004690 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
4691 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
4692 case MPI2_RAID_PD_STATE_HOT_SPARE:
4693 default:
4694 break;
4695 }
4696}
4697
4698#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4699/**
4700 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
4701 * @ioc: per adapter object
4702 * @event_data: event data payload
4703 * Context: user.
4704 *
4705 * Return nothing.
4706 */
4707static void
4708_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
4709 Mpi2EventDataIrOperationStatus_t *event_data)
4710{
4711 char *reason_str = NULL;
4712
4713 switch (event_data->RAIDOperation) {
4714 case MPI2_EVENT_IR_RAIDOP_RESYNC:
4715 reason_str = "resync";
4716 break;
4717 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
4718 reason_str = "online capacity expansion";
4719 break;
4720 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
4721 reason_str = "consistency check";
4722 break;
4723 default:
4724 reason_str = "unknown reason";
4725 break;
4726 }
4727
4728 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
4729 "\thandle(0x%04x), percent complete(%d)\n",
4730 ioc->name, reason_str,
4731 le16_to_cpu(event_data->VolDevHandle),
4732 event_data->PercentComplete);
4733}
4734#endif
4735
4736/**
4737 * _scsih_sas_ir_operation_status_event - handle RAID operation events
4738 * @ioc: per adapter object
4739 * @VF_ID:
4740 * @event_data: event data payload
4741 * Context: user.
4742 *
4743 * Return nothing.
4744 */
4745static void
4746_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4747 Mpi2EventDataIrOperationStatus_t *event_data)
4748{
4749#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4750 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4751 _scsih_sas_ir_operation_status_event_debug(ioc, event_data);
4752#endif
4753}
4754
4755/**
4756 * _scsih_task_set_full - handle task set full
4757 * @ioc: per adapter object
4758 * @event_data: event data payload
4759 * Context: user.
4760 *
4761 * Throttle back qdepth.
4762 */
4763static void
4764_scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4765 Mpi2EventDataTaskSetFull_t *event_data)
4766{
4767 unsigned long flags;
4768 struct _sas_device *sas_device;
4769 static struct _raid_device *raid_device;
4770 struct scsi_device *sdev;
4771 int depth;
4772 u16 current_depth;
4773 u16 handle;
4774 int id, channel;
4775 u64 sas_address;
4776
4777 current_depth = le16_to_cpu(event_data->CurrentDepth);
4778 handle = le16_to_cpu(event_data->DevHandle);
4779 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4780 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4781 if (!sas_device) {
4782 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4783 return;
4784 }
4785 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4786 id = sas_device->id;
4787 channel = sas_device->channel;
4788 sas_address = sas_device->sas_address;
4789
4790 /* if hidden raid component, then change to volume characteristics */
4791 if (sas_device->hidden_raid_component && sas_device->volume_handle) {
4792 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4793 raid_device = _scsih_raid_device_find_by_handle(
4794 ioc, sas_device->volume_handle);
4795 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4796 if (raid_device) {
4797 id = raid_device->id;
4798 channel = raid_device->channel;
4799 handle = raid_device->handle;
4800 sas_address = raid_device->wwid;
4801 }
4802 }
4803
4804 if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
4805 starget_printk(KERN_DEBUG, sas_device->starget, "task set "
4806 "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
4807 handle, (unsigned long long)sas_address, current_depth);
4808
4809 shost_for_each_device(sdev, ioc->shost) {
4810 if (sdev->id == id && sdev->channel == channel) {
4811 if (current_depth > sdev->queue_depth) {
4812 if (ioc->logging_level &
4813 MPT_DEBUG_TASK_SET_FULL)
4814 sdev_printk(KERN_INFO, sdev, "strange "
4815 "observation, the queue depth is"
4816 " (%d) meanwhile fw queue depth "
4817 "is (%d)\n", sdev->queue_depth,
4818 current_depth);
4819 continue;
4820 }
4821 depth = scsi_track_queue_full(sdev,
4822 current_depth - 1);
4823 if (depth > 0)
4824 sdev_printk(KERN_INFO, sdev, "Queue depth "
4825 "reduced to (%d)\n", depth);
4826 else if (depth < 0)
4827 sdev_printk(KERN_INFO, sdev, "Tagged Command "
4828 "Queueing is being disabled\n");
4829 else if (depth == 0)
4830 if (ioc->logging_level &
4831 MPT_DEBUG_TASK_SET_FULL)
4832 sdev_printk(KERN_INFO, sdev,
4833 "Queue depth not changed yet\n");
4834 }
4835 }
4836}
4837
4838/**
4839 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
4840 * @ioc: per adapter object
4841 * @sas_address: sas address
4842 * @slot: enclosure slot id
4843 * @handle: device handle
4844 *
4845 * After host reset, find out whether devices are still responding.
4846 * Used in _scsi_remove_unresponsive_sas_devices.
4847 *
4848 * Return nothing.
4849 */
4850static void
4851_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4852 u16 slot, u16 handle)
4853{
4854 struct MPT2SAS_TARGET *sas_target_priv_data;
4855 struct scsi_target *starget;
4856 struct _sas_device *sas_device;
4857 unsigned long flags;
4858
4859 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4860 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
4861 if (sas_device->sas_address == sas_address &&
4862 sas_device->slot == slot && sas_device->starget) {
4863 sas_device->responding = 1;
4864 starget_printk(KERN_INFO, sas_device->starget,
4865 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
4866 "logical id(0x%016llx), slot(%d)\n", handle,
4867 (unsigned long long)sas_device->sas_address,
4868 (unsigned long long)
4869 sas_device->enclosure_logical_id,
4870 sas_device->slot);
4871 if (sas_device->handle == handle)
4872 goto out;
4873 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
4874 sas_device->handle);
4875 sas_device->handle = handle;
4876 starget = sas_device->starget;
4877 sas_target_priv_data = starget->hostdata;
4878 sas_target_priv_data->handle = handle;
4879 goto out;
4880 }
4881 }
4882 out:
4883 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4884}
4885
4886/**
4887 * _scsih_search_responding_sas_devices -
4888 * @ioc: per adapter object
4889 *
4890 * After host reset, find out whether devices are still responding.
4891 * If not remove.
4892 *
4893 * Return nothing.
4894 */
4895static void
4896_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
4897{
4898 Mpi2SasDevicePage0_t sas_device_pg0;
4899 Mpi2ConfigReply_t mpi_reply;
4900 u16 ioc_status;
4901 __le64 sas_address;
4902 u16 handle;
4903 u32 device_info;
4904 u16 slot;
4905
4906 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
4907
4908 if (list_empty(&ioc->sas_device_list))
4909 return;
4910
4911 handle = 0xFFFF;
4912 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
4913 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
4914 handle))) {
4915 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4916 MPI2_IOCSTATUS_MASK;
4917 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
4918 break;
4919 handle = le16_to_cpu(sas_device_pg0.DevHandle);
4920 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4921 if (!(_scsih_is_end_device(device_info)))
4922 continue;
4923 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4924 slot = le16_to_cpu(sas_device_pg0.Slot);
4925 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
4926 handle);
4927 }
4928}
4929
4930/**
4931 * _scsih_mark_responding_raid_device - mark a raid_device as responding
4932 * @ioc: per adapter object
4933 * @wwid: world wide identifier for raid volume
4934 * @handle: device handle
4935 *
4936 * After host reset, find out whether devices are still responding.
4937 * Used in _scsi_remove_unresponsive_raid_devices.
4938 *
4939 * Return nothing.
4940 */
4941static void
4942_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
4943 u16 handle)
4944{
4945 struct MPT2SAS_TARGET *sas_target_priv_data;
4946 struct scsi_target *starget;
4947 struct _raid_device *raid_device;
4948 unsigned long flags;
4949
4950 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4951 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
4952 if (raid_device->wwid == wwid && raid_device->starget) {
4953 raid_device->responding = 1;
4954 starget_printk(KERN_INFO, raid_device->starget,
4955 "handle(0x%04x), wwid(0x%016llx)\n", handle,
4956 (unsigned long long)raid_device->wwid);
4957 if (raid_device->handle == handle)
4958 goto out;
4959 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
4960 raid_device->handle);
4961 raid_device->handle = handle;
4962 starget = raid_device->starget;
4963 sas_target_priv_data = starget->hostdata;
4964 sas_target_priv_data->handle = handle;
4965 goto out;
4966 }
4967 }
4968 out:
4969 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4970}
4971
4972/**
4973 * _scsih_search_responding_raid_devices -
4974 * @ioc: per adapter object
4975 *
4976 * After host reset, find out whether devices are still responding.
4977 * If not remove.
4978 *
4979 * Return nothing.
4980 */
4981static void
4982_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
4983{
4984 Mpi2RaidVolPage1_t volume_pg1;
4985 Mpi2ConfigReply_t mpi_reply;
4986 u16 ioc_status;
4987 u16 handle;
4988
4989 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
4990
4991 if (list_empty(&ioc->raid_device_list))
4992 return;
4993
4994 handle = 0xFFFF;
4995 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
4996 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
4997 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4998 MPI2_IOCSTATUS_MASK;
4999 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5000 break;
5001 handle = le16_to_cpu(volume_pg1.DevHandle);
5002 _scsih_mark_responding_raid_device(ioc,
5003 le64_to_cpu(volume_pg1.WWID), handle);
5004 }
5005}
5006
5007/**
5008 * _scsih_mark_responding_expander - mark a expander as responding
5009 * @ioc: per adapter object
5010 * @sas_address: sas address
5011 * @handle:
5012 *
5013 * After host reset, find out whether devices are still responding.
5014 * Used in _scsi_remove_unresponsive_expanders.
5015 *
5016 * Return nothing.
5017 */
5018static void
5019_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5020 u16 handle)
5021{
5022 struct _sas_node *sas_expander;
5023 unsigned long flags;
5024
5025 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5026 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
5027 if (sas_expander->sas_address == sas_address) {
5028 sas_expander->responding = 1;
5029 if (sas_expander->handle != handle) {
5030 printk(KERN_INFO "old handle(0x%04x)\n",
5031 sas_expander->handle);
5032 sas_expander->handle = handle;
5033 }
5034 goto out;
5035 }
5036 }
5037 out:
5038 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5039}
5040
5041/**
5042 * _scsih_search_responding_expanders -
5043 * @ioc: per adapter object
5044 *
5045 * After host reset, find out whether devices are still responding.
5046 * If not remove.
5047 *
5048 * Return nothing.
5049 */
5050static void
5051_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
5052{
5053 Mpi2ExpanderPage0_t expander_pg0;
5054 Mpi2ConfigReply_t mpi_reply;
5055 u16 ioc_status;
5056 __le64 sas_address;
5057 u16 handle;
5058
5059 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5060
5061 if (list_empty(&ioc->sas_expander_list))
5062 return;
5063
5064 handle = 0xFFFF;
5065 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5066 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
5067
5068 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5069 MPI2_IOCSTATUS_MASK;
5070 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5071 break;
5072
5073 handle = le16_to_cpu(expander_pg0.DevHandle);
5074 sas_address = le64_to_cpu(expander_pg0.SASAddress);
5075 printk(KERN_INFO "\texpander present: handle(0x%04x), "
5076 "sas_addr(0x%016llx)\n", handle,
5077 (unsigned long long)sas_address);
5078 _scsih_mark_responding_expander(ioc, sas_address, handle);
5079 }
5080
5081}
5082
5083/**
5084 * _scsih_remove_unresponding_devices - removing unresponding devices
5085 * @ioc: per adapter object
5086 *
5087 * Return nothing.
5088 */
5089static void
5090_scsih_remove_unresponding_devices(struct MPT2SAS_ADAPTER *ioc)
5091{
5092 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305093 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06005094 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06005095
Eric Moore635374e2009-03-09 01:21:12 -06005096
5097 list_for_each_entry_safe(sas_device, sas_device_next,
5098 &ioc->sas_device_list, list) {
5099 if (sas_device->responding) {
5100 sas_device->responding = 0;
5101 continue;
5102 }
5103 if (sas_device->starget)
5104 starget_printk(KERN_INFO, sas_device->starget,
5105 "removing: handle(0x%04x), sas_addr(0x%016llx), "
5106 "enclosure logical id(0x%016llx), slot(%d)\n",
5107 sas_device->handle,
5108 (unsigned long long)sas_device->sas_address,
5109 (unsigned long long)
5110 sas_device->enclosure_logical_id,
5111 sas_device->slot);
5112 _scsih_remove_device(ioc, sas_device->handle);
5113 }
5114
5115 list_for_each_entry_safe(raid_device, raid_device_next,
5116 &ioc->raid_device_list, list) {
5117 if (raid_device->responding) {
5118 raid_device->responding = 0;
5119 continue;
5120 }
5121 if (raid_device->starget) {
5122 starget_printk(KERN_INFO, raid_device->starget,
5123 "removing: handle(0x%04x), wwid(0x%016llx)\n",
5124 raid_device->handle,
5125 (unsigned long long)raid_device->wwid);
5126 scsi_remove_target(&raid_device->starget->dev);
5127 }
5128 _scsih_raid_device_remove(ioc, raid_device);
5129 }
5130
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305131 retry_expander_search:
5132 sas_expander = NULL;
5133 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06005134 if (sas_expander->responding) {
5135 sas_expander->responding = 0;
5136 continue;
5137 }
Eric Moore635374e2009-03-09 01:21:12 -06005138 _scsih_expander_remove(ioc, sas_expander->handle);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305139 goto retry_expander_search;
5140 }
5141}
5142
5143/**
5144 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
5145 * @ioc: per adapter object
5146 * @reset_phase: phase
5147 *
5148 * The handler for doing any required cleanup or initialization.
5149 *
5150 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
5151 * MPT2_IOC_DONE_RESET
5152 *
5153 * Return nothing.
5154 */
5155void
5156mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
5157{
5158 switch (reset_phase) {
5159 case MPT2_IOC_PRE_RESET:
5160 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5161 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
5162 _scsih_fw_event_off(ioc);
5163 break;
5164 case MPT2_IOC_AFTER_RESET:
5165 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5166 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
5167 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
5168 ioc->tm_cmds.status |= MPT2_CMD_RESET;
5169 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
5170 complete(&ioc->tm_cmds.done);
5171 }
5172 _scsih_fw_event_on(ioc);
5173 _scsih_flush_running_cmds(ioc);
5174 break;
5175 case MPT2_IOC_DONE_RESET:
5176 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5177 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
5178 _scsih_sas_host_refresh(ioc, 0);
5179 _scsih_search_responding_sas_devices(ioc);
5180 _scsih_search_responding_raid_devices(ioc);
5181 _scsih_search_responding_expanders(ioc);
5182 break;
5183 case MPT2_IOC_RUNNING:
5184 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5185 "MPT2_IOC_RUNNING\n", ioc->name, __func__));
5186 _scsih_remove_unresponding_devices(ioc);
5187 break;
Eric Moore635374e2009-03-09 01:21:12 -06005188 }
5189}
5190
5191/**
5192 * _firmware_event_work - delayed task for processing firmware events
5193 * @ioc: per adapter object
5194 * @work: equal to the fw_event_work object
5195 * Context: user.
5196 *
5197 * Return nothing.
5198 */
5199static void
5200_firmware_event_work(struct work_struct *work)
5201{
5202 struct fw_event_work *fw_event = container_of(work,
Eric Moore6f92a7a2009-04-21 15:43:33 -06005203 struct fw_event_work, work);
Eric Moore635374e2009-03-09 01:21:12 -06005204 unsigned long flags;
5205 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
5206
Eric Moore635374e2009-03-09 01:21:12 -06005207 /* the queue is being flushed so ignore this event */
5208 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5209 if (ioc->fw_events_off || ioc->remove_host) {
5210 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5211 _scsih_fw_event_free(ioc, fw_event);
5212 return;
5213 }
5214 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5215
Eric Moore635374e2009-03-09 01:21:12 -06005216 if (ioc->shost_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06005217 _scsih_fw_event_requeue(ioc, fw_event, 1000);
5218 return;
5219 }
Eric Moore635374e2009-03-09 01:21:12 -06005220
5221 switch (fw_event->event) {
5222 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
5223 _scsih_sas_topology_change_event(ioc, fw_event->VF_ID,
5224 fw_event->event_data, fw_event);
5225 break;
5226 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5227 _scsih_sas_device_status_change_event(ioc, fw_event->VF_ID,
5228 fw_event->event_data);
5229 break;
5230 case MPI2_EVENT_SAS_DISCOVERY:
5231 _scsih_sas_discovery_event(ioc, fw_event->VF_ID,
5232 fw_event->event_data);
5233 break;
5234 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5235 _scsih_sas_broadcast_primative_event(ioc, fw_event->VF_ID,
5236 fw_event->event_data);
5237 break;
5238 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5239 _scsih_sas_enclosure_dev_status_change_event(ioc,
5240 fw_event->VF_ID, fw_event->event_data);
5241 break;
5242 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5243 _scsih_sas_ir_config_change_event(ioc, fw_event->VF_ID,
5244 fw_event->event_data);
5245 break;
5246 case MPI2_EVENT_IR_VOLUME:
5247 _scsih_sas_ir_volume_event(ioc, fw_event->VF_ID,
5248 fw_event->event_data);
5249 break;
5250 case MPI2_EVENT_IR_PHYSICAL_DISK:
5251 _scsih_sas_ir_physical_disk_event(ioc, fw_event->VF_ID,
5252 fw_event->event_data);
5253 break;
5254 case MPI2_EVENT_IR_OPERATION_STATUS:
5255 _scsih_sas_ir_operation_status_event(ioc, fw_event->VF_ID,
5256 fw_event->event_data);
5257 break;
5258 case MPI2_EVENT_TASK_SET_FULL:
5259 _scsih_task_set_full(ioc, fw_event->VF_ID,
5260 fw_event->event_data);
5261 break;
5262 }
5263 _scsih_fw_event_free(ioc, fw_event);
5264}
5265
5266/**
5267 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
5268 * @ioc: per adapter object
5269 * @VF_ID: virtual function id
5270 * @reply: reply message frame(lower 32bit addr)
5271 * Context: interrupt.
5272 *
5273 * This function merely adds a new work task into ioc->firmware_event_thread.
5274 * The tasks are worked from _firmware_event_work in user context.
5275 *
5276 * Return nothing.
5277 */
5278void
5279mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, u32 reply)
5280{
5281 struct fw_event_work *fw_event;
5282 Mpi2EventNotificationReply_t *mpi_reply;
5283 unsigned long flags;
5284 u16 event;
5285
5286 /* events turned off due to host reset or driver unloading */
5287 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5288 if (ioc->fw_events_off || ioc->remove_host) {
5289 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5290 return;
5291 }
5292 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5293
5294 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
5295 event = le16_to_cpu(mpi_reply->Event);
5296
5297 switch (event) {
5298 /* handle these */
5299 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5300 {
5301 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
5302 (Mpi2EventDataSasBroadcastPrimitive_t *)
5303 mpi_reply->EventData;
5304
5305 if (baen_data->Primitive !=
5306 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
5307 ioc->broadcast_aen_busy)
5308 return;
5309 ioc->broadcast_aen_busy = 1;
5310 break;
5311 }
5312
5313 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
5314 _scsih_check_topo_delete_events(ioc,
5315 (Mpi2EventDataSasTopologyChangeList_t *)
5316 mpi_reply->EventData);
5317 break;
5318
5319 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5320 case MPI2_EVENT_IR_OPERATION_STATUS:
5321 case MPI2_EVENT_SAS_DISCOVERY:
5322 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5323 case MPI2_EVENT_IR_VOLUME:
5324 case MPI2_EVENT_IR_PHYSICAL_DISK:
5325 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5326 case MPI2_EVENT_TASK_SET_FULL:
5327 break;
5328
5329 default: /* ignore the rest */
5330 return;
5331 }
5332
5333 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
5334 if (!fw_event) {
5335 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5336 ioc->name, __FILE__, __LINE__, __func__);
5337 return;
5338 }
5339 fw_event->event_data =
5340 kzalloc(mpi_reply->EventDataLength*4, GFP_ATOMIC);
5341 if (!fw_event->event_data) {
5342 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5343 ioc->name, __FILE__, __LINE__, __func__);
5344 kfree(fw_event);
5345 return;
5346 }
5347
5348 memcpy(fw_event->event_data, mpi_reply->EventData,
5349 mpi_reply->EventDataLength*4);
5350 fw_event->ioc = ioc;
5351 fw_event->VF_ID = VF_ID;
5352 fw_event->event = event;
5353 _scsih_fw_event_add(ioc, fw_event);
5354}
5355
5356/* shost template */
5357static struct scsi_host_template scsih_driver_template = {
5358 .module = THIS_MODULE,
5359 .name = "Fusion MPT SAS Host",
5360 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06005361 .queuecommand = _scsih_qcmd,
5362 .target_alloc = _scsih_target_alloc,
5363 .slave_alloc = _scsih_slave_alloc,
5364 .slave_configure = _scsih_slave_configure,
5365 .target_destroy = _scsih_target_destroy,
5366 .slave_destroy = _scsih_slave_destroy,
5367 .change_queue_depth = _scsih_change_queue_depth,
5368 .change_queue_type = _scsih_change_queue_type,
5369 .eh_abort_handler = _scsih_abort,
5370 .eh_device_reset_handler = _scsih_dev_reset,
5371 .eh_target_reset_handler = _scsih_target_reset,
5372 .eh_host_reset_handler = _scsih_host_reset,
5373 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06005374 .can_queue = 1,
5375 .this_id = -1,
5376 .sg_tablesize = MPT2SAS_SG_DEPTH,
5377 .max_sectors = 8192,
5378 .cmd_per_lun = 7,
5379 .use_clustering = ENABLE_CLUSTERING,
5380 .shost_attrs = mpt2sas_host_attrs,
5381 .sdev_attrs = mpt2sas_dev_attrs,
5382};
5383
5384/**
5385 * _scsih_expander_node_remove - removing expander device from list.
5386 * @ioc: per adapter object
5387 * @sas_expander: the sas_device object
5388 * Context: Calling function should acquire ioc->sas_node_lock.
5389 *
5390 * Removing object and freeing associated memory from the
5391 * ioc->sas_expander_list.
5392 *
5393 * Return nothing.
5394 */
5395static void
5396_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
5397 struct _sas_node *sas_expander)
5398{
5399 struct _sas_port *mpt2sas_port;
5400 struct _sas_device *sas_device;
5401 struct _sas_node *expander_sibling;
5402 unsigned long flags;
5403
5404 if (!sas_expander)
5405 return;
5406
5407 /* remove sibling ports attached to this expander */
5408 retry_device_search:
5409 list_for_each_entry(mpt2sas_port,
5410 &sas_expander->sas_port_list, port_list) {
5411 if (mpt2sas_port->remote_identify.device_type ==
5412 SAS_END_DEVICE) {
5413 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5414 sas_device =
5415 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5416 mpt2sas_port->remote_identify.sas_address);
5417 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5418 if (!sas_device)
5419 continue;
5420 _scsih_remove_device(ioc, sas_device->handle);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305421 if (ioc->shost_recovery)
5422 return;
Eric Moore635374e2009-03-09 01:21:12 -06005423 goto retry_device_search;
5424 }
5425 }
5426
5427 retry_expander_search:
5428 list_for_each_entry(mpt2sas_port,
5429 &sas_expander->sas_port_list, port_list) {
5430
5431 if (mpt2sas_port->remote_identify.device_type ==
5432 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
5433 mpt2sas_port->remote_identify.device_type ==
5434 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
5435
5436 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5437 expander_sibling =
5438 mpt2sas_scsih_expander_find_by_sas_address(
5439 ioc, mpt2sas_port->remote_identify.sas_address);
5440 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5441 if (!expander_sibling)
5442 continue;
5443 _scsih_expander_remove(ioc, expander_sibling->handle);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305444 if (ioc->shost_recovery)
5445 return;
Eric Moore635374e2009-03-09 01:21:12 -06005446 goto retry_expander_search;
5447 }
5448 }
5449
5450 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
5451 sas_expander->parent_handle);
5452
5453 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
5454 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
5455 sas_expander->handle, (unsigned long long)
5456 sas_expander->sas_address);
5457
5458 list_del(&sas_expander->list);
5459 kfree(sas_expander->phy);
5460 kfree(sas_expander);
5461}
5462
5463/**
Eric Moored5d135b2009-05-18 13:02:08 -06005464 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06005465 * @pdev: PCI device struct
5466 *
5467 * Return nothing.
5468 */
5469static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06005470_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06005471{
5472 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5473 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5474 struct _sas_port *mpt2sas_port;
5475 struct _sas_device *sas_device;
5476 struct _sas_node *expander_sibling;
5477 struct workqueue_struct *wq;
5478 unsigned long flags;
5479
5480 ioc->remove_host = 1;
5481 _scsih_fw_event_off(ioc);
5482
5483 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5484 wq = ioc->firmware_event_thread;
5485 ioc->firmware_event_thread = NULL;
5486 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5487 if (wq)
5488 destroy_workqueue(wq);
5489
5490 /* free ports attached to the sas_host */
5491 retry_again:
5492 list_for_each_entry(mpt2sas_port,
5493 &ioc->sas_hba.sas_port_list, port_list) {
5494 if (mpt2sas_port->remote_identify.device_type ==
5495 SAS_END_DEVICE) {
5496 sas_device =
5497 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5498 mpt2sas_port->remote_identify.sas_address);
5499 if (sas_device) {
5500 _scsih_remove_device(ioc, sas_device->handle);
5501 goto retry_again;
5502 }
5503 } else {
5504 expander_sibling =
5505 mpt2sas_scsih_expander_find_by_sas_address(ioc,
5506 mpt2sas_port->remote_identify.sas_address);
5507 if (expander_sibling) {
5508 _scsih_expander_remove(ioc,
5509 expander_sibling->handle);
5510 goto retry_again;
5511 }
5512 }
5513 }
5514
5515 /* free phys attached to the sas_host */
5516 if (ioc->sas_hba.num_phys) {
5517 kfree(ioc->sas_hba.phy);
5518 ioc->sas_hba.phy = NULL;
5519 ioc->sas_hba.num_phys = 0;
5520 }
5521
5522 sas_remove_host(shost);
5523 mpt2sas_base_detach(ioc);
5524 list_del(&ioc->list);
5525 scsi_remove_host(shost);
5526 scsi_host_put(shost);
5527}
5528
5529/**
5530 * _scsih_probe_boot_devices - reports 1st device
5531 * @ioc: per adapter object
5532 *
5533 * If specified in bios page 2, this routine reports the 1st
5534 * device scsi-ml or sas transport for persistent boot device
5535 * purposes. Please refer to function _scsih_determine_boot_device()
5536 */
5537static void
5538_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
5539{
5540 u8 is_raid;
5541 void *device;
5542 struct _sas_device *sas_device;
5543 struct _raid_device *raid_device;
5544 u16 handle, parent_handle;
5545 u64 sas_address;
5546 unsigned long flags;
5547 int rc;
5548
5549 device = NULL;
5550 if (ioc->req_boot_device.device) {
5551 device = ioc->req_boot_device.device;
5552 is_raid = ioc->req_boot_device.is_raid;
5553 } else if (ioc->req_alt_boot_device.device) {
5554 device = ioc->req_alt_boot_device.device;
5555 is_raid = ioc->req_alt_boot_device.is_raid;
5556 } else if (ioc->current_boot_device.device) {
5557 device = ioc->current_boot_device.device;
5558 is_raid = ioc->current_boot_device.is_raid;
5559 }
5560
5561 if (!device)
5562 return;
5563
5564 if (is_raid) {
5565 raid_device = device;
5566 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5567 raid_device->id, 0);
5568 if (rc)
5569 _scsih_raid_device_remove(ioc, raid_device);
5570 } else {
5571 sas_device = device;
5572 handle = sas_device->handle;
5573 parent_handle = sas_device->parent_handle;
5574 sas_address = sas_device->sas_address;
5575 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5576 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5577 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5578 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
5579 sas_device->parent_handle)) {
5580 _scsih_sas_device_remove(ioc, sas_device);
5581 } else if (!sas_device->starget) {
5582 mpt2sas_transport_port_remove(ioc, sas_address,
5583 parent_handle);
5584 _scsih_sas_device_remove(ioc, sas_device);
5585 }
5586 }
5587}
5588
5589/**
5590 * _scsih_probe_raid - reporting raid volumes to scsi-ml
5591 * @ioc: per adapter object
5592 *
5593 * Called during initial loading of the driver.
5594 */
5595static void
5596_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
5597{
5598 struct _raid_device *raid_device, *raid_next;
5599 int rc;
5600
5601 list_for_each_entry_safe(raid_device, raid_next,
5602 &ioc->raid_device_list, list) {
5603 if (raid_device->starget)
5604 continue;
5605 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5606 raid_device->id, 0);
5607 if (rc)
5608 _scsih_raid_device_remove(ioc, raid_device);
5609 }
5610}
5611
5612/**
5613 * _scsih_probe_sas - reporting raid volumes to sas transport
5614 * @ioc: per adapter object
5615 *
5616 * Called during initial loading of the driver.
5617 */
5618static void
5619_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
5620{
5621 struct _sas_device *sas_device, *next;
5622 unsigned long flags;
5623 u16 handle, parent_handle;
5624 u64 sas_address;
5625
5626 /* SAS Device List */
5627 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
5628 list) {
5629 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5630 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5631 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5632
5633 handle = sas_device->handle;
5634 parent_handle = sas_device->parent_handle;
5635 sas_address = sas_device->sas_address;
5636 if (!mpt2sas_transport_port_add(ioc, handle, parent_handle)) {
5637 _scsih_sas_device_remove(ioc, sas_device);
5638 } else if (!sas_device->starget) {
5639 mpt2sas_transport_port_remove(ioc, sas_address,
5640 parent_handle);
5641 _scsih_sas_device_remove(ioc, sas_device);
5642 }
5643 }
5644}
5645
5646/**
5647 * _scsih_probe_devices - probing for devices
5648 * @ioc: per adapter object
5649 *
5650 * Called during initial loading of the driver.
5651 */
5652static void
5653_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
5654{
5655 u16 volume_mapping_flags =
5656 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
5657 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
5658
5659 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
5660 return; /* return when IOC doesn't support initiator mode */
5661
5662 _scsih_probe_boot_devices(ioc);
5663
5664 if (ioc->ir_firmware) {
5665 if ((volume_mapping_flags &
5666 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
5667 _scsih_probe_sas(ioc);
5668 _scsih_probe_raid(ioc);
5669 } else {
5670 _scsih_probe_raid(ioc);
5671 _scsih_probe_sas(ioc);
5672 }
5673 } else
5674 _scsih_probe_sas(ioc);
5675}
5676
5677/**
Eric Moored5d135b2009-05-18 13:02:08 -06005678 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06005679 * @pdev: PCI device struct
5680 * @id: pci device id
5681 *
5682 * Returns 0 success, anything else error.
5683 */
5684static int
Eric Moored5d135b2009-05-18 13:02:08 -06005685_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06005686{
5687 struct MPT2SAS_ADAPTER *ioc;
5688 struct Scsi_Host *shost;
5689
5690 shost = scsi_host_alloc(&scsih_driver_template,
5691 sizeof(struct MPT2SAS_ADAPTER));
5692 if (!shost)
5693 return -ENODEV;
5694
5695 /* init local params */
5696 ioc = shost_priv(shost);
5697 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
5698 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06005699 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06005700 ioc->shost = shost;
5701 ioc->id = mpt_ids++;
5702 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
5703 ioc->pdev = pdev;
5704 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
5705 ioc->tm_cb_idx = tm_cb_idx;
5706 ioc->ctl_cb_idx = ctl_cb_idx;
5707 ioc->base_cb_idx = base_cb_idx;
5708 ioc->transport_cb_idx = transport_cb_idx;
5709 ioc->config_cb_idx = config_cb_idx;
5710 ioc->logging_level = logging_level;
5711 /* misc semaphores and spin locks */
5712 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
5713 spin_lock_init(&ioc->scsi_lookup_lock);
5714 spin_lock_init(&ioc->sas_device_lock);
5715 spin_lock_init(&ioc->sas_node_lock);
5716 spin_lock_init(&ioc->fw_event_lock);
5717 spin_lock_init(&ioc->raid_device_lock);
5718
5719 INIT_LIST_HEAD(&ioc->sas_device_list);
5720 INIT_LIST_HEAD(&ioc->sas_device_init_list);
5721 INIT_LIST_HEAD(&ioc->sas_expander_list);
5722 INIT_LIST_HEAD(&ioc->fw_event_list);
5723 INIT_LIST_HEAD(&ioc->raid_device_list);
5724 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
5725
5726 /* init shost parameters */
5727 shost->max_cmd_len = 16;
5728 shost->max_lun = max_lun;
5729 shost->transportt = mpt2sas_transport_template;
5730 shost->unique_id = ioc->id;
5731
5732 if ((scsi_add_host(shost, &pdev->dev))) {
5733 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5734 ioc->name, __FILE__, __LINE__, __func__);
5735 list_del(&ioc->list);
5736 goto out_add_shost_fail;
5737 }
5738
Eric Moore3c621b32009-05-18 12:59:41 -06005739 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
5740 | SHOST_DIF_TYPE3_PROTECTION);
5741
Eric Moore635374e2009-03-09 01:21:12 -06005742 /* event thread */
5743 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
5744 "fw_event%d", ioc->id);
5745 ioc->firmware_event_thread = create_singlethread_workqueue(
5746 ioc->firmware_event_name);
5747 if (!ioc->firmware_event_thread) {
5748 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5749 ioc->name, __FILE__, __LINE__, __func__);
5750 goto out_thread_fail;
5751 }
5752
5753 ioc->wait_for_port_enable_to_complete = 1;
5754 if ((mpt2sas_base_attach(ioc))) {
5755 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5756 ioc->name, __FILE__, __LINE__, __func__);
5757 goto out_attach_fail;
5758 }
5759
5760 ioc->wait_for_port_enable_to_complete = 0;
5761 _scsih_probe_devices(ioc);
5762 return 0;
5763
5764 out_attach_fail:
5765 destroy_workqueue(ioc->firmware_event_thread);
5766 out_thread_fail:
5767 list_del(&ioc->list);
5768 scsi_remove_host(shost);
5769 out_add_shost_fail:
5770 return -ENODEV;
5771}
5772
5773#ifdef CONFIG_PM
5774/**
Eric Moored5d135b2009-05-18 13:02:08 -06005775 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06005776 * @pdev: PCI device struct
5777 * @state: PM state change to (usually PCI_D3)
5778 *
5779 * Returns 0 success, anything else error.
5780 */
5781static int
Eric Moored5d135b2009-05-18 13:02:08 -06005782_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06005783{
5784 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5785 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5786 u32 device_state;
5787
Kashyap, Desaie4750c92009-08-07 19:37:59 +05305788 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005789 flush_scheduled_work();
5790 scsi_block_requests(shost);
5791 device_state = pci_choose_state(pdev, state);
5792 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
5793 "operating state [D%d]\n", ioc->name, pdev,
5794 pci_name(pdev), device_state);
5795
5796 mpt2sas_base_free_resources(ioc);
5797 pci_save_state(pdev);
5798 pci_disable_device(pdev);
5799 pci_set_power_state(pdev, device_state);
5800 return 0;
5801}
5802
5803/**
Eric Moored5d135b2009-05-18 13:02:08 -06005804 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06005805 * @pdev: PCI device struct
5806 *
5807 * Returns 0 success, anything else error.
5808 */
5809static int
Eric Moored5d135b2009-05-18 13:02:08 -06005810_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06005811{
5812 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5813 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5814 u32 device_state = pdev->current_state;
5815 int r;
5816
5817 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
5818 "operating state [D%d]\n", ioc->name, pdev,
5819 pci_name(pdev), device_state);
5820
5821 pci_set_power_state(pdev, PCI_D0);
5822 pci_enable_wake(pdev, PCI_D0, 0);
5823 pci_restore_state(pdev);
5824 ioc->pdev = pdev;
5825 r = mpt2sas_base_map_resources(ioc);
5826 if (r)
5827 return r;
5828
5829 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
5830 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05305831 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005832 return 0;
5833}
5834#endif /* CONFIG_PM */
5835
5836
5837static struct pci_driver scsih_driver = {
5838 .name = MPT2SAS_DRIVER_NAME,
5839 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06005840 .probe = _scsih_probe,
5841 .remove = __devexit_p(_scsih_remove),
Eric Moore635374e2009-03-09 01:21:12 -06005842#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06005843 .suspend = _scsih_suspend,
5844 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06005845#endif
5846};
5847
5848
5849/**
Eric Moored5d135b2009-05-18 13:02:08 -06005850 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06005851 *
5852 * Returns 0 success, anything else error.
5853 */
5854static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06005855_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06005856{
5857 int error;
5858
5859 mpt_ids = 0;
5860 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
5861 MPT2SAS_DRIVER_VERSION);
5862
5863 mpt2sas_transport_template =
5864 sas_attach_transport(&mpt2sas_transport_functions);
5865 if (!mpt2sas_transport_template)
5866 return -ENODEV;
5867
5868 mpt2sas_base_initialize_callback_handler();
5869
5870 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06005871 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06005872
5873 /* task managment callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06005874 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06005875
5876 /* base internal commands callback handler */
5877 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
5878
5879 /* transport internal commands callback handler */
5880 transport_cb_idx = mpt2sas_base_register_callback_handler(
5881 mpt2sas_transport_done);
5882
5883 /* configuration page API internal commands callback handler */
5884 config_cb_idx = mpt2sas_base_register_callback_handler(
5885 mpt2sas_config_done);
5886
5887 /* ctl module callback handler */
5888 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
5889
5890 mpt2sas_ctl_init();
5891
5892 error = pci_register_driver(&scsih_driver);
5893 if (error)
5894 sas_release_transport(mpt2sas_transport_template);
5895
5896 return error;
5897}
5898
5899/**
Eric Moored5d135b2009-05-18 13:02:08 -06005900 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06005901 *
5902 * Returns 0 success, anything else error.
5903 */
5904static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06005905_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06005906{
5907 printk(KERN_INFO "mpt2sas version %s unloading\n",
5908 MPT2SAS_DRIVER_VERSION);
5909
5910 pci_unregister_driver(&scsih_driver);
5911
5912 sas_release_transport(mpt2sas_transport_template);
5913 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
5914 mpt2sas_base_release_callback_handler(tm_cb_idx);
5915 mpt2sas_base_release_callback_handler(base_cb_idx);
5916 mpt2sas_base_release_callback_handler(transport_cb_idx);
5917 mpt2sas_base_release_callback_handler(config_cb_idx);
5918 mpt2sas_base_release_callback_handler(ctl_cb_idx);
5919
5920 mpt2sas_ctl_exit();
5921}
5922
Eric Moored5d135b2009-05-18 13:02:08 -06005923module_init(_scsih_init);
5924module_exit(_scsih_exit);