blob: 90da7d63b08e1070bdb13030e96780c688e8b79e [file] [log] [blame]
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001/*
2 * linux/drivers/message/fusion/mptfc.c
3 * For use with LSI Logic PCI chip/adapter(s)
4 * running LSI Logic Fusion MPT (Message Passing Technology) firmware.
5 *
6 * Copyright (c) 1999-2005 LSI Logic Corporation
7 * (mailto:mpt_linux_developer@lsil.com)
8 *
9 */
10/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
11/*
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; version 2 of the License.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 NO WARRANTY
22 THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
23 CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
24 LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
25 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
26 solely responsible for determining the appropriateness of using and
27 distributing the Program and assumes all risks associated with its
28 exercise of rights under this Agreement, including but not limited to
29 the risks and costs of program errors, damage to or loss of data,
30 programs or equipment, and unavailability or interruption of operations.
31
32 DISCLAIMER OF LIABILITY
33 NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
34 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
36 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
37 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
38 USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
39 HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
40
41 You should have received a copy of the GNU General Public License
42 along with this program; if not, write to the Free Software
43 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
44*/
45/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
46#include "linux_compat.h" /* linux-2.6 tweaks */
47#include <linux/module.h>
48#include <linux/kernel.h>
49#include <linux/init.h>
50#include <linux/errno.h>
51#include <linux/kdev_t.h>
52#include <linux/blkdev.h>
53#include <linux/delay.h> /* for mdelay */
54#include <linux/interrupt.h> /* needed for in_interrupt() proto */
55#include <linux/reboot.h> /* notifier code */
56#include <linux/sched.h>
57#include <linux/workqueue.h>
Michael Reed05e8ec12006-01-13 14:31:54 -060058#include <linux/sort.h>
Moore, Eric Dean 2496af32005-04-22 18:02:41 -040059
60#include <scsi/scsi.h>
61#include <scsi/scsi_cmnd.h>
62#include <scsi/scsi_device.h>
63#include <scsi/scsi_host.h>
64#include <scsi/scsi_tcq.h>
Michael Reed05e8ec12006-01-13 14:31:54 -060065#include <scsi/scsi_transport_fc.h>
Moore, Eric Dean 2496af32005-04-22 18:02:41 -040066
67#include "mptbase.h"
68#include "mptscsih.h"
69
70/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
71#define my_NAME "Fusion MPT FC Host driver"
72#define my_VERSION MPT_LINUX_VERSION_COMMON
73#define MYNAM "mptfc"
74
75MODULE_AUTHOR(MODULEAUTHOR);
76MODULE_DESCRIPTION(my_NAME);
77MODULE_LICENSE("GPL");
78
79/* Command line args */
Michael Reed05e8ec12006-01-13 14:31:54 -060080#define MPTFC_DEV_LOSS_TMO (60)
81static int mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO; /* reasonable default */
82module_param(mptfc_dev_loss_tmo, int, 0);
83MODULE_PARM_DESC(mptfc_dev_loss_tmo, " Initial time the driver programs the "
84 " transport to wait for an rport to "
85 " return following a device loss event."
86 " Default=60.");
87
Moore, Eric Dean 2496af32005-04-22 18:02:41 -040088static int mptfcDoneCtx = -1;
89static int mptfcTaskCtx = -1;
90static int mptfcInternalCtx = -1; /* Used only for internal commands */
91
Michael Reed3bc7bf12006-01-25 18:05:18 -070092static int mptfc_target_alloc(struct scsi_target *starget);
93static int mptfc_slave_alloc(struct scsi_device *sdev);
Michael Reed05e8ec12006-01-13 14:31:54 -060094static int mptfc_qcmd(struct scsi_cmnd *SCpnt,
Michael Reed3bc7bf12006-01-25 18:05:18 -070095 void (*done)(struct scsi_cmnd *));
96static void mptfc_target_destroy(struct scsi_target *starget);
Michael Reed05e8ec12006-01-13 14:31:54 -060097static void mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout);
98static void __devexit mptfc_remove(struct pci_dev *pdev);
99
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400100static struct scsi_host_template mptfc_driver_template = {
Moore, Eric Deanf78496d2005-11-16 18:54:14 -0700101 .module = THIS_MODULE,
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400102 .proc_name = "mptfc",
103 .proc_info = mptscsih_proc_info,
104 .name = "MPT FC Host",
105 .info = mptscsih_info,
Michael Reed05e8ec12006-01-13 14:31:54 -0600106 .queuecommand = mptfc_qcmd,
Michael Reed3bc7bf12006-01-25 18:05:18 -0700107 .target_alloc = mptfc_target_alloc,
Michael Reed05e8ec12006-01-13 14:31:54 -0600108 .slave_alloc = mptfc_slave_alloc,
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400109 .slave_configure = mptscsih_slave_configure,
Michael Reed3bc7bf12006-01-25 18:05:18 -0700110 .target_destroy = mptfc_target_destroy,
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400111 .slave_destroy = mptscsih_slave_destroy,
Moore, Eric Dean6e3815b2005-06-24 12:18:57 -0600112 .change_queue_depth = mptscsih_change_queue_depth,
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400113 .eh_abort_handler = mptscsih_abort,
114 .eh_device_reset_handler = mptscsih_dev_reset,
115 .eh_bus_reset_handler = mptscsih_bus_reset,
116 .eh_host_reset_handler = mptscsih_host_reset,
117 .bios_param = mptscsih_bios_param,
118 .can_queue = MPT_FC_CAN_QUEUE,
119 .this_id = -1,
120 .sg_tablesize = MPT_SCSI_SG_DEPTH,
121 .max_sectors = 8192,
122 .cmd_per_lun = 7,
123 .use_clustering = ENABLE_CLUSTERING,
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400124};
125
126/****************************************************************************
127 * Supported hardware
128 */
129
130static struct pci_device_id mptfc_pci_table[] = {
Eric Moore87cf8982006-06-27 16:09:26 -0600131 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC909,
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400132 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore87cf8982006-06-27 16:09:26 -0600133 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC919,
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400134 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore87cf8982006-06-27 16:09:26 -0600135 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC929,
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400136 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore87cf8982006-06-27 16:09:26 -0600137 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC919X,
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400138 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore87cf8982006-06-27 16:09:26 -0600139 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC929X,
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400140 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore87cf8982006-06-27 16:09:26 -0600141 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC939X,
Moore, Eric Dean 3fadc592005-05-11 17:46:52 -0600142 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore87cf8982006-06-27 16:09:26 -0600143 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC949X,
Moore, Eric Dean 3fadc592005-05-11 17:46:52 -0600144 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore87cf8982006-06-27 16:09:26 -0600145 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC949E,
Moore, Eric6d5b0c32006-01-13 16:25:26 -0700146 PCI_ANY_ID, PCI_ANY_ID },
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400147 {0} /* Terminating entry */
148};
149MODULE_DEVICE_TABLE(pci, mptfc_pci_table);
150
Michael Reed05e8ec12006-01-13 14:31:54 -0600151static struct scsi_transport_template *mptfc_transport_template = NULL;
152
Adrian Bunk03fbcbc2006-01-25 02:00:52 +0100153static struct fc_function_template mptfc_transport_functions = {
Michael Reed05e8ec12006-01-13 14:31:54 -0600154 .dd_fcrport_size = 8,
155 .show_host_node_name = 1,
156 .show_host_port_name = 1,
157 .show_host_supported_classes = 1,
158 .show_host_port_id = 1,
159 .show_rport_supported_classes = 1,
160 .show_starget_node_name = 1,
161 .show_starget_port_name = 1,
162 .show_starget_port_id = 1,
163 .set_rport_dev_loss_tmo = mptfc_set_rport_loss_tmo,
164 .show_rport_dev_loss_tmo = 1,
165
166};
167
Michael Reed05e8ec12006-01-13 14:31:54 -0600168static void
169mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
170{
171 if (timeout > 0)
172 rport->dev_loss_tmo = timeout;
173 else
174 rport->dev_loss_tmo = mptfc_dev_loss_tmo;
175}
176
177static int
178mptfc_FcDevPage0_cmp_func(const void *a, const void *b)
179{
180 FCDevicePage0_t **aa = (FCDevicePage0_t **)a;
181 FCDevicePage0_t **bb = (FCDevicePage0_t **)b;
182
183 if ((*aa)->CurrentBus == (*bb)->CurrentBus) {
184 if ((*aa)->CurrentTargetID == (*bb)->CurrentTargetID)
185 return 0;
186 if ((*aa)->CurrentTargetID < (*bb)->CurrentTargetID)
187 return -1;
188 return 1;
189 }
190 if ((*aa)->CurrentBus < (*bb)->CurrentBus)
191 return -1;
192 return 1;
193}
194
195static int
196mptfc_GetFcDevPage0(MPT_ADAPTER *ioc, int ioc_port,
197 void(*func)(MPT_ADAPTER *ioc,int channel, FCDevicePage0_t *arg))
198{
199 ConfigPageHeader_t hdr;
200 CONFIGPARMS cfg;
201 FCDevicePage0_t *ppage0_alloc, *fc;
202 dma_addr_t page0_dma;
203 int data_sz;
204 int ii;
205
206 FCDevicePage0_t *p0_array=NULL, *p_p0;
207 FCDevicePage0_t **pp0_array=NULL, **p_pp0;
208
209 int rc = -ENOMEM;
210 U32 port_id = 0xffffff;
211 int num_targ = 0;
212 int max_bus = ioc->facts.MaxBuses;
213 int max_targ = ioc->facts.MaxDevices;
214
215 if (max_bus == 0 || max_targ == 0)
216 goto out;
217
218 data_sz = sizeof(FCDevicePage0_t) * max_bus * max_targ;
219 p_p0 = p0_array = kzalloc(data_sz, GFP_KERNEL);
220 if (!p0_array)
221 goto out;
222
223 data_sz = sizeof(FCDevicePage0_t *) * max_bus * max_targ;
224 p_pp0 = pp0_array = kzalloc(data_sz, GFP_KERNEL);
225 if (!pp0_array)
226 goto out;
227
228 do {
229 /* Get FC Device Page 0 header */
230 hdr.PageVersion = 0;
231 hdr.PageLength = 0;
232 hdr.PageNumber = 0;
233 hdr.PageType = MPI_CONFIG_PAGETYPE_FC_DEVICE;
234 cfg.cfghdr.hdr = &hdr;
235 cfg.physAddr = -1;
236 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
237 cfg.dir = 0;
238 cfg.pageAddr = port_id;
239 cfg.timeout = 0;
240
241 if ((rc = mpt_config(ioc, &cfg)) != 0)
242 break;
243
244 if (hdr.PageLength <= 0)
245 break;
246
247 data_sz = hdr.PageLength * 4;
248 ppage0_alloc = pci_alloc_consistent(ioc->pcidev, data_sz,
249 &page0_dma);
250 rc = -ENOMEM;
251 if (!ppage0_alloc)
252 break;
253
254 cfg.physAddr = page0_dma;
255 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
256
257 if ((rc = mpt_config(ioc, &cfg)) == 0) {
258 ppage0_alloc->PortIdentifier =
259 le32_to_cpu(ppage0_alloc->PortIdentifier);
260
261 ppage0_alloc->WWNN.Low =
262 le32_to_cpu(ppage0_alloc->WWNN.Low);
263
264 ppage0_alloc->WWNN.High =
265 le32_to_cpu(ppage0_alloc->WWNN.High);
266
267 ppage0_alloc->WWPN.Low =
268 le32_to_cpu(ppage0_alloc->WWPN.Low);
269
270 ppage0_alloc->WWPN.High =
271 le32_to_cpu(ppage0_alloc->WWPN.High);
272
273 ppage0_alloc->BBCredit =
274 le16_to_cpu(ppage0_alloc->BBCredit);
275
276 ppage0_alloc->MaxRxFrameSize =
277 le16_to_cpu(ppage0_alloc->MaxRxFrameSize);
278
279 port_id = ppage0_alloc->PortIdentifier;
280 num_targ++;
281 *p_p0 = *ppage0_alloc; /* save data */
282 *p_pp0++ = p_p0++; /* save addr */
283 }
284 pci_free_consistent(ioc->pcidev, data_sz,
285 (u8 *) ppage0_alloc, page0_dma);
286 if (rc != 0)
287 break;
288
289 } while (port_id <= 0xff0000);
290
291 if (num_targ) {
292 /* sort array */
293 if (num_targ > 1)
294 sort (pp0_array, num_targ, sizeof(FCDevicePage0_t *),
295 mptfc_FcDevPage0_cmp_func, NULL);
296 /* call caller's func for each targ */
297 for (ii = 0; ii < num_targ; ii++) {
298 fc = *(pp0_array+ii);
299 func(ioc, ioc_port, fc);
300 }
301 }
302
303 out:
Jesper Juhl8f760782006-06-27 02:55:06 -0700304 kfree(pp0_array);
305 kfree(p0_array);
Michael Reed05e8ec12006-01-13 14:31:54 -0600306 return rc;
307}
308
309static int
310mptfc_generate_rport_ids(FCDevicePage0_t *pg0, struct fc_rport_identifiers *rid)
311{
312 /* not currently usable */
313 if (pg0->Flags & (MPI_FC_DEVICE_PAGE0_FLAGS_PLOGI_INVALID |
314 MPI_FC_DEVICE_PAGE0_FLAGS_PRLI_INVALID))
315 return -1;
316
317 if (!(pg0->Flags & MPI_FC_DEVICE_PAGE0_FLAGS_TARGETID_BUS_VALID))
318 return -1;
319
320 if (!(pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_TARGET))
321 return -1;
322
323 /*
324 * board data structure already normalized to platform endianness
325 * shifted to avoid unaligned access on 64 bit architecture
326 */
327 rid->node_name = ((u64)pg0->WWNN.High) << 32 | (u64)pg0->WWNN.Low;
328 rid->port_name = ((u64)pg0->WWPN.High) << 32 | (u64)pg0->WWPN.Low;
329 rid->port_id = pg0->PortIdentifier;
330 rid->roles = FC_RPORT_ROLE_UNKNOWN;
Michael Reed05e8ec12006-01-13 14:31:54 -0600331
332 return 0;
333}
334
335static void
336mptfc_register_dev(MPT_ADAPTER *ioc, int channel, FCDevicePage0_t *pg0)
337{
338 struct fc_rport_identifiers rport_ids;
339 struct fc_rport *rport;
340 struct mptfc_rport_info *ri;
Michael Reed3bc7bf12006-01-25 18:05:18 -0700341 int new_ri = 1;
Moore, Eric65207fe2006-04-21 16:14:35 -0600342 u64 pn, nn;
Michael Reed3bc7bf12006-01-25 18:05:18 -0700343 VirtTarget *vtarget;
mdr@sgi.com6dd727d2006-05-01 13:07:04 -0500344 u32 roles = FC_RPORT_ROLE_UNKNOWN;
Michael Reed05e8ec12006-01-13 14:31:54 -0600345
346 if (mptfc_generate_rport_ids(pg0, &rport_ids) < 0)
347 return;
348
mdr@sgi.com6dd727d2006-05-01 13:07:04 -0500349 roles |= FC_RPORT_ROLE_FCP_TARGET;
350 if (pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_INITIATOR)
351 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
352
Michael Reed05e8ec12006-01-13 14:31:54 -0600353 /* scan list looking for a match */
Michael Reed05e8ec12006-01-13 14:31:54 -0600354 list_for_each_entry(ri, &ioc->fc_rports, list) {
Michael Reed3bc7bf12006-01-25 18:05:18 -0700355 pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
356 if (pn == rport_ids.port_name) { /* match */
Michael Reed05e8ec12006-01-13 14:31:54 -0600357 list_move_tail(&ri->list, &ioc->fc_rports);
Michael Reed3bc7bf12006-01-25 18:05:18 -0700358 new_ri = 0;
Michael Reed05e8ec12006-01-13 14:31:54 -0600359 break;
360 }
361 }
Michael Reed3bc7bf12006-01-25 18:05:18 -0700362 if (new_ri) { /* allocate one */
Michael Reed05e8ec12006-01-13 14:31:54 -0600363 ri = kzalloc(sizeof(struct mptfc_rport_info), GFP_KERNEL);
364 if (!ri)
365 return;
Michael Reed05e8ec12006-01-13 14:31:54 -0600366 list_add_tail(&ri->list, &ioc->fc_rports);
367 }
368
369 ri->pg0 = *pg0; /* add/update pg0 data */
370 ri->flags &= ~MPT_RPORT_INFO_FLAGS_MISSING;
371
Michael Reed3bc7bf12006-01-25 18:05:18 -0700372 /* MPT_RPORT_INFO_FLAGS_REGISTERED - rport not previously deleted */
Michael Reed05e8ec12006-01-13 14:31:54 -0600373 if (!(ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED)) {
374 ri->flags |= MPT_RPORT_INFO_FLAGS_REGISTERED;
Michael Reed3bc7bf12006-01-25 18:05:18 -0700375 rport = fc_remote_port_add(ioc->sh, channel, &rport_ids);
Michael Reed05e8ec12006-01-13 14:31:54 -0600376 if (rport) {
Michael Reed3bc7bf12006-01-25 18:05:18 -0700377 ri->rport = rport;
378 if (new_ri) /* may have been reset by user */
379 rport->dev_loss_tmo = mptfc_dev_loss_tmo;
Michael Reed05e8ec12006-01-13 14:31:54 -0600380 /*
381 * if already mapped, remap here. If not mapped,
Michael Reed3bc7bf12006-01-25 18:05:18 -0700382 * target_alloc will allocate vtarget and map,
383 * slave_alloc will fill in vdev from vtarget.
Michael Reed05e8ec12006-01-13 14:31:54 -0600384 */
Michael Reed3bc7bf12006-01-25 18:05:18 -0700385 if (ri->starget) {
386 vtarget = ri->starget->hostdata;
387 if (vtarget) {
388 vtarget->target_id = pg0->CurrentTargetID;
389 vtarget->bus_id = pg0->CurrentBus;
Michael Reed3bc7bf12006-01-25 18:05:18 -0700390 }
Michael Reed05e8ec12006-01-13 14:31:54 -0600391 }
Moore, Eric65207fe2006-04-21 16:14:35 -0600392 *((struct mptfc_rport_info **)rport->dd_data) = ri;
mdr@sgi.com6dd727d2006-05-01 13:07:04 -0500393 /* scan will be scheduled once rport becomes a target */
394 fc_remote_port_rolechg(rport,roles);
Moore, Eric65207fe2006-04-21 16:14:35 -0600395
396 pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
397 nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
Michael Reed3bc7bf12006-01-25 18:05:18 -0700398 dfcprintk ((MYIOC_s_INFO_FMT
399 "mptfc_reg_dev.%d: %x, %llx / %llx, tid %d, "
Michael Reed05e8ec12006-01-13 14:31:54 -0600400 "rport tid %d, tmo %d\n",
Michael Reed3bc7bf12006-01-25 18:05:18 -0700401 ioc->name,
Moore, Eric914c2d82006-03-14 09:19:36 -0700402 ioc->sh->host_no,
Michael Reed05e8ec12006-01-13 14:31:54 -0600403 pg0->PortIdentifier,
Moore, Eric65207fe2006-04-21 16:14:35 -0600404 (unsigned long long)nn,
405 (unsigned long long)pn,
Michael Reed05e8ec12006-01-13 14:31:54 -0600406 pg0->CurrentTargetID,
407 ri->rport->scsi_target_id,
Michael Reed3bc7bf12006-01-25 18:05:18 -0700408 ri->rport->dev_loss_tmo));
Michael Reed05e8ec12006-01-13 14:31:54 -0600409 } else {
410 list_del(&ri->list);
411 kfree(ri);
412 ri = NULL;
413 }
414 }
Michael Reed05e8ec12006-01-13 14:31:54 -0600415}
416
417/*
Michael Reed3bc7bf12006-01-25 18:05:18 -0700418 * OS entry point to allow for host driver to free allocated memory
419 * Called if no device present or device being unloaded
420 */
421static void
422mptfc_target_destroy(struct scsi_target *starget)
423{
424 struct fc_rport *rport;
425 struct mptfc_rport_info *ri;
426
427 rport = starget_to_rport(starget);
428 if (rport) {
429 ri = *((struct mptfc_rport_info **)rport->dd_data);
430 if (ri) /* better be! */
431 ri->starget = NULL;
432 }
433 if (starget->hostdata)
434 kfree(starget->hostdata);
435 starget->hostdata = NULL;
436}
437
438/*
439 * OS entry point to allow host driver to alloc memory
440 * for each scsi target. Called once per device the bus scan.
441 * Return non-zero if allocation fails.
442 */
443static int
444mptfc_target_alloc(struct scsi_target *starget)
445{
446 VirtTarget *vtarget;
447 struct fc_rport *rport;
448 struct mptfc_rport_info *ri;
449 int rc;
450
451 vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
452 if (!vtarget)
453 return -ENOMEM;
454 starget->hostdata = vtarget;
455
456 rc = -ENODEV;
457 rport = starget_to_rport(starget);
458 if (rport) {
459 ri = *((struct mptfc_rport_info **)rport->dd_data);
460 if (ri) { /* better be! */
461 vtarget->target_id = ri->pg0.CurrentTargetID;
462 vtarget->bus_id = ri->pg0.CurrentBus;
463 ri->starget = starget;
Michael Reed3bc7bf12006-01-25 18:05:18 -0700464 rc = 0;
465 }
466 }
467 if (rc != 0) {
468 kfree(vtarget);
469 starget->hostdata = NULL;
470 }
471
472 return rc;
473}
474
475/*
Michael Reed05e8ec12006-01-13 14:31:54 -0600476 * OS entry point to allow host driver to alloc memory
477 * for each scsi device. Called once per device the bus scan.
478 * Return non-zero if allocation fails.
479 * Init memory once per LUN.
480 */
Adrian Bunk03fbcbc2006-01-25 02:00:52 +0100481static int
Michael Reed05e8ec12006-01-13 14:31:54 -0600482mptfc_slave_alloc(struct scsi_device *sdev)
483{
484 MPT_SCSI_HOST *hd;
485 VirtTarget *vtarget;
486 VirtDevice *vdev;
487 struct scsi_target *starget;
488 struct fc_rport *rport;
Michael Reed05e8ec12006-01-13 14:31:54 -0600489
490
Moore, Eric65207fe2006-04-21 16:14:35 -0600491 starget = scsi_target(sdev);
492 rport = starget_to_rport(starget);
Michael Reed05e8ec12006-01-13 14:31:54 -0600493
494 if (!rport || fc_remote_port_chkready(rport))
495 return -ENXIO;
496
497 hd = (MPT_SCSI_HOST *)sdev->host->hostdata;
498
Michael Reed3bc7bf12006-01-25 18:05:18 -0700499 vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
Michael Reed05e8ec12006-01-13 14:31:54 -0600500 if (!vdev) {
501 printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
502 hd->ioc->name, sizeof(VirtDevice));
503 return -ENOMEM;
504 }
Michael Reed05e8ec12006-01-13 14:31:54 -0600505
Michael Reed05e8ec12006-01-13 14:31:54 -0600506
Michael Reed05e8ec12006-01-13 14:31:54 -0600507 sdev->hostdata = vdev;
Michael Reed05e8ec12006-01-13 14:31:54 -0600508 vtarget = starget->hostdata;
Michael Reed3bc7bf12006-01-25 18:05:18 -0700509
Michael Reed05e8ec12006-01-13 14:31:54 -0600510 if (vtarget->num_luns == 0) {
Michael Reed3bc7bf12006-01-25 18:05:18 -0700511 vtarget->ioc_id = hd->ioc->id;
Eric Mooreba856d32006-07-11 17:34:01 -0600512 vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
Michael Reed05e8ec12006-01-13 14:31:54 -0600513 hd->Targets[sdev->id] = vtarget;
514 }
515
Michael Reed05e8ec12006-01-13 14:31:54 -0600516 vdev->vtarget = vtarget;
Michael Reed05e8ec12006-01-13 14:31:54 -0600517 vdev->lun = sdev->lun;
Michael Reed05e8ec12006-01-13 14:31:54 -0600518
Michael Reed05e8ec12006-01-13 14:31:54 -0600519 vtarget->num_luns++;
520
Moore, Eric65207fe2006-04-21 16:14:35 -0600521
Moore, Eric914c2d82006-03-14 09:19:36 -0700522#ifdef DMPT_DEBUG_FC
Moore, Eric65207fe2006-04-21 16:14:35 -0600523 {
524 u64 nn, pn;
Moore, Eric914c2d82006-03-14 09:19:36 -0700525 struct mptfc_rport_info *ri;
526 ri = *((struct mptfc_rport_info **)rport->dd_data);
Moore, Eric65207fe2006-04-21 16:14:35 -0600527 pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
528 nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
Michael Reed3bc7bf12006-01-25 18:05:18 -0700529 dfcprintk ((MYIOC_s_INFO_FMT
530 "mptfc_slv_alloc.%d: num_luns %d, sdev.id %d, "
Michael Reed05e8ec12006-01-13 14:31:54 -0600531 "CurrentTargetID %d, %x %llx %llx\n",
Moore, Eric914c2d82006-03-14 09:19:36 -0700532 hd->ioc->name,
Michael Reed3bc7bf12006-01-25 18:05:18 -0700533 sdev->host->host_no,
534 vtarget->num_luns,
535 sdev->id, ri->pg0.CurrentTargetID,
Moore, Eric65207fe2006-04-21 16:14:35 -0600536 ri->pg0.PortIdentifier,
537 (unsigned long long)pn,
538 (unsigned long long)nn));
Moore, Eric914c2d82006-03-14 09:19:36 -0700539 }
540#endif
Michael Reed05e8ec12006-01-13 14:31:54 -0600541
542 return 0;
543}
544
545static int
546mptfc_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
547{
Michael Reed3bc7bf12006-01-25 18:05:18 -0700548 struct mptfc_rport_info *ri;
Michael Reed05e8ec12006-01-13 14:31:54 -0600549 struct fc_rport *rport = starget_to_rport(scsi_target(SCpnt->device));
550 int err;
551
552 err = fc_remote_port_chkready(rport);
553 if (unlikely(err)) {
554 SCpnt->result = err;
555 done(SCpnt);
556 return 0;
557 }
Michael Reed3bc7bf12006-01-25 18:05:18 -0700558
Moore, Eric65207fe2006-04-21 16:14:35 -0600559 /* dd_data is null until finished adding target */
560 ri = *((struct mptfc_rport_info **)rport->dd_data);
561 if (unlikely(!ri)) {
562 dfcprintk ((MYIOC_s_INFO_FMT
563 "mptfc_qcmd.%d: %d:%d, dd_data is null.\n",
564 ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
565 ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
566 SCpnt->device->id,SCpnt->device->lun));
567 SCpnt->result = DID_IMM_RETRY << 16;
568 done(SCpnt);
569 return 0;
570 }
571
572 err = mptscsih_qcmd(SCpnt,done);
573#ifdef DMPT_DEBUG_FC
574 if (unlikely(err)) {
575 dfcprintk ((MYIOC_s_INFO_FMT
Michael Reed419835e2006-05-24 15:07:40 -0500576 "mptfc_qcmd.%d: %d:%d, mptscsih_qcmd returns non-zero, (%x).\n",
Moore, Eric65207fe2006-04-21 16:14:35 -0600577 ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
578 ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
Michael Reed419835e2006-05-24 15:07:40 -0500579 SCpnt->device->id,SCpnt->device->lun,err));
Moore, Eric65207fe2006-04-21 16:14:35 -0600580 }
581#endif
582 return err;
Michael Reed05e8ec12006-01-13 14:31:54 -0600583}
584
Michael Reed80d3ac72006-05-24 15:07:09 -0500585/*
586 * mptfc_GetFcPortPage0 - Fetch FCPort config Page0.
587 * @ioc: Pointer to MPT_ADAPTER structure
588 * @portnum: IOC Port number
589 *
590 * Return: 0 for success
591 * -ENOMEM if no memory available
592 * -EPERM if not allowed due to ISR context
593 * -EAGAIN if no msg frames currently available
594 * -EFAULT for non-successful reply or no reply (timeout)
595 * -EINVAL portnum arg out of range (hardwired to two elements)
596 */
597static int
598mptfc_GetFcPortPage0(MPT_ADAPTER *ioc, int portnum)
599{
600 ConfigPageHeader_t hdr;
601 CONFIGPARMS cfg;
602 FCPortPage0_t *ppage0_alloc;
603 FCPortPage0_t *pp0dest;
604 dma_addr_t page0_dma;
605 int data_sz;
606 int copy_sz;
607 int rc;
608 int count = 400;
609
610 if (portnum > 1)
611 return -EINVAL;
612
613 /* Get FCPort Page 0 header */
614 hdr.PageVersion = 0;
615 hdr.PageLength = 0;
616 hdr.PageNumber = 0;
617 hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
618 cfg.cfghdr.hdr = &hdr;
619 cfg.physAddr = -1;
620 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
621 cfg.dir = 0;
622 cfg.pageAddr = portnum;
623 cfg.timeout = 0;
624
625 if ((rc = mpt_config(ioc, &cfg)) != 0)
626 return rc;
627
628 if (hdr.PageLength == 0)
629 return 0;
630
631 data_sz = hdr.PageLength * 4;
632 rc = -ENOMEM;
633 ppage0_alloc = (FCPortPage0_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page0_dma);
634 if (ppage0_alloc) {
635
636 try_again:
637 memset((u8 *)ppage0_alloc, 0, data_sz);
638 cfg.physAddr = page0_dma;
639 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
640
641 if ((rc = mpt_config(ioc, &cfg)) == 0) {
642 /* save the data */
643 pp0dest = &ioc->fc_port_page0[portnum];
644 copy_sz = min_t(int, sizeof(FCPortPage0_t), data_sz);
645 memcpy(pp0dest, ppage0_alloc, copy_sz);
646
647 /*
648 * Normalize endianness of structure data,
649 * by byte-swapping all > 1 byte fields!
650 */
651 pp0dest->Flags = le32_to_cpu(pp0dest->Flags);
652 pp0dest->PortIdentifier = le32_to_cpu(pp0dest->PortIdentifier);
653 pp0dest->WWNN.Low = le32_to_cpu(pp0dest->WWNN.Low);
654 pp0dest->WWNN.High = le32_to_cpu(pp0dest->WWNN.High);
655 pp0dest->WWPN.Low = le32_to_cpu(pp0dest->WWPN.Low);
656 pp0dest->WWPN.High = le32_to_cpu(pp0dest->WWPN.High);
657 pp0dest->SupportedServiceClass = le32_to_cpu(pp0dest->SupportedServiceClass);
658 pp0dest->SupportedSpeeds = le32_to_cpu(pp0dest->SupportedSpeeds);
659 pp0dest->CurrentSpeed = le32_to_cpu(pp0dest->CurrentSpeed);
660 pp0dest->MaxFrameSize = le32_to_cpu(pp0dest->MaxFrameSize);
661 pp0dest->FabricWWNN.Low = le32_to_cpu(pp0dest->FabricWWNN.Low);
662 pp0dest->FabricWWNN.High = le32_to_cpu(pp0dest->FabricWWNN.High);
663 pp0dest->FabricWWPN.Low = le32_to_cpu(pp0dest->FabricWWPN.Low);
664 pp0dest->FabricWWPN.High = le32_to_cpu(pp0dest->FabricWWPN.High);
665 pp0dest->DiscoveredPortsCount = le32_to_cpu(pp0dest->DiscoveredPortsCount);
666 pp0dest->MaxInitiators = le32_to_cpu(pp0dest->MaxInitiators);
667
668 /*
669 * if still doing discovery,
670 * hang loose a while until finished
671 */
672 if (pp0dest->PortState == MPI_FCPORTPAGE0_PORTSTATE_UNKNOWN) {
673 if (count-- > 0) {
Michael Reedd6be06c2006-05-24 15:07:57 -0500674 msleep(100);
Michael Reed80d3ac72006-05-24 15:07:09 -0500675 goto try_again;
676 }
677 printk(MYIOC_s_INFO_FMT "Firmware discovery not"
678 " complete.\n",
679 ioc->name);
680 }
681 }
682
683 pci_free_consistent(ioc->pcidev, data_sz, (u8 *) ppage0_alloc, page0_dma);
684 }
685
686 return rc;
687}
688
Michael Reedca2f9382006-05-24 15:07:24 -0500689static int
690mptfc_WriteFcPortPage1(MPT_ADAPTER *ioc, int portnum)
691{
692 ConfigPageHeader_t hdr;
693 CONFIGPARMS cfg;
694 int rc;
695
696 if (portnum > 1)
697 return -EINVAL;
698
699 if (!(ioc->fc_data.fc_port_page1[portnum].data))
700 return -EINVAL;
701
702 /* get fcport page 1 header */
703 hdr.PageVersion = 0;
704 hdr.PageLength = 0;
705 hdr.PageNumber = 1;
706 hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
707 cfg.cfghdr.hdr = &hdr;
708 cfg.physAddr = -1;
709 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
710 cfg.dir = 0;
711 cfg.pageAddr = portnum;
712 cfg.timeout = 0;
713
714 if ((rc = mpt_config(ioc, &cfg)) != 0)
715 return rc;
716
717 if (hdr.PageLength == 0)
718 return -ENODEV;
719
720 if (hdr.PageLength*4 != ioc->fc_data.fc_port_page1[portnum].pg_sz)
721 return -EINVAL;
722
723 cfg.physAddr = ioc->fc_data.fc_port_page1[portnum].dma;
724 cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
725 cfg.dir = 1;
726
727 rc = mpt_config(ioc, &cfg);
728
729 return rc;
730}
731
732static int
733mptfc_GetFcPortPage1(MPT_ADAPTER *ioc, int portnum)
734{
735 ConfigPageHeader_t hdr;
736 CONFIGPARMS cfg;
737 FCPortPage1_t *page1_alloc;
738 dma_addr_t page1_dma;
739 int data_sz;
740 int rc;
741
742 if (portnum > 1)
743 return -EINVAL;
744
745 /* get fcport page 1 header */
746 hdr.PageVersion = 0;
747 hdr.PageLength = 0;
748 hdr.PageNumber = 1;
749 hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
750 cfg.cfghdr.hdr = &hdr;
751 cfg.physAddr = -1;
752 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
753 cfg.dir = 0;
754 cfg.pageAddr = portnum;
755 cfg.timeout = 0;
756
757 if ((rc = mpt_config(ioc, &cfg)) != 0)
758 return rc;
759
760 if (hdr.PageLength == 0)
761 return -ENODEV;
762
763start_over:
764
765 if (ioc->fc_data.fc_port_page1[portnum].data == NULL) {
766 data_sz = hdr.PageLength * 4;
767 if (data_sz < sizeof(FCPortPage1_t))
768 data_sz = sizeof(FCPortPage1_t);
769
770 page1_alloc = (FCPortPage1_t *) pci_alloc_consistent(ioc->pcidev,
771 data_sz,
772 &page1_dma);
773 if (!page1_alloc)
774 return -ENOMEM;
775 }
776 else {
777 page1_alloc = ioc->fc_data.fc_port_page1[portnum].data;
778 page1_dma = ioc->fc_data.fc_port_page1[portnum].dma;
779 data_sz = ioc->fc_data.fc_port_page1[portnum].pg_sz;
780 if (hdr.PageLength * 4 > data_sz) {
781 ioc->fc_data.fc_port_page1[portnum].data = NULL;
782 pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
783 page1_alloc, page1_dma);
784 goto start_over;
785 }
786 }
787
788 memset(page1_alloc,0,data_sz);
789
790 cfg.physAddr = page1_dma;
791 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
792
793 if ((rc = mpt_config(ioc, &cfg)) == 0) {
794 ioc->fc_data.fc_port_page1[portnum].data = page1_alloc;
795 ioc->fc_data.fc_port_page1[portnum].pg_sz = data_sz;
796 ioc->fc_data.fc_port_page1[portnum].dma = page1_dma;
797 }
798 else {
799 ioc->fc_data.fc_port_page1[portnum].data = NULL;
800 pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
801 page1_alloc, page1_dma);
802 }
803
804 return rc;
805}
806
807static void
808mptfc_SetFcPortPage1_defaults(MPT_ADAPTER *ioc)
809{
810 int ii;
811 FCPortPage1_t *pp1;
812
813 #define MPTFC_FW_DEVICE_TIMEOUT (1)
814 #define MPTFC_FW_IO_PEND_TIMEOUT (1)
815 #define ON_FLAGS (MPI_FCPORTPAGE1_FLAGS_IMMEDIATE_ERROR_REPLY)
816 #define OFF_FLAGS (MPI_FCPORTPAGE1_FLAGS_VERBOSE_RESCAN_EVENTS)
817
818 for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
819 if (mptfc_GetFcPortPage1(ioc, ii) != 0)
820 continue;
821 pp1 = ioc->fc_data.fc_port_page1[ii].data;
822 if ((pp1->InitiatorDeviceTimeout == MPTFC_FW_DEVICE_TIMEOUT)
823 && (pp1->InitiatorIoPendTimeout == MPTFC_FW_IO_PEND_TIMEOUT)
824 && ((pp1->Flags & ON_FLAGS) == ON_FLAGS)
825 && ((pp1->Flags & OFF_FLAGS) == 0))
826 continue;
827 pp1->InitiatorDeviceTimeout = MPTFC_FW_DEVICE_TIMEOUT;
828 pp1->InitiatorIoPendTimeout = MPTFC_FW_IO_PEND_TIMEOUT;
829 pp1->Flags &= ~OFF_FLAGS;
830 pp1->Flags |= ON_FLAGS;
831 mptfc_WriteFcPortPage1(ioc, ii);
832 }
833}
834
835
Michael Reed05e8ec12006-01-13 14:31:54 -0600836static void
837mptfc_init_host_attr(MPT_ADAPTER *ioc,int portnum)
838{
839 unsigned class = 0, cos = 0;
840
841 /* don't know what to do as only one scsi (fc) host was allocated */
842 if (portnum != 0)
843 return;
844
845 class = ioc->fc_port_page0[portnum].SupportedServiceClass;
846 if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_1)
847 cos |= FC_COS_CLASS1;
848 if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_2)
849 cos |= FC_COS_CLASS2;
850 if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_3)
851 cos |= FC_COS_CLASS3;
852
853 fc_host_node_name(ioc->sh) =
854 (u64)ioc->fc_port_page0[portnum].WWNN.High << 32
855 | (u64)ioc->fc_port_page0[portnum].WWNN.Low;
856
857 fc_host_port_name(ioc->sh) =
858 (u64)ioc->fc_port_page0[portnum].WWPN.High << 32
859 | (u64)ioc->fc_port_page0[portnum].WWPN.Low;
860
861 fc_host_port_id(ioc->sh) = ioc->fc_port_page0[portnum].PortIdentifier;
862
863 fc_host_supported_classes(ioc->sh) = cos;
864
865 fc_host_tgtid_bind_type(ioc->sh) = FC_TGTID_BIND_BY_WWPN;
866}
867
868static void
Michael Reed419835e2006-05-24 15:07:40 -0500869mptfc_setup_reset(void *arg)
870{
871 MPT_ADAPTER *ioc = (MPT_ADAPTER *)arg;
872 u64 pn;
873 struct mptfc_rport_info *ri;
874
875 /* reset about to happen, delete (block) all rports */
876 list_for_each_entry(ri, &ioc->fc_rports, list) {
877 if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
878 ri->flags &= ~MPT_RPORT_INFO_FLAGS_REGISTERED;
879 fc_remote_port_delete(ri->rport); /* won't sleep */
880 ri->rport = NULL;
881
882 pn = (u64)ri->pg0.WWPN.High << 32 |
883 (u64)ri->pg0.WWPN.Low;
884 dfcprintk ((MYIOC_s_INFO_FMT
885 "mptfc_setup_reset.%d: %llx deleted\n",
886 ioc->name,
887 ioc->sh->host_no,
888 (unsigned long long)pn));
889 }
890 }
891}
892
893static void
Michael Reed05e8ec12006-01-13 14:31:54 -0600894mptfc_rescan_devices(void *arg)
895{
896 MPT_ADAPTER *ioc = (MPT_ADAPTER *)arg;
897 int ii;
898 int work_to_do;
Moore, Eric65207fe2006-04-21 16:14:35 -0600899 u64 pn;
Michael Reed05e8ec12006-01-13 14:31:54 -0600900 unsigned long flags;
901 struct mptfc_rport_info *ri;
902
903 do {
904 /* start by tagging all ports as missing */
Michael Reed05e8ec12006-01-13 14:31:54 -0600905 list_for_each_entry(ri, &ioc->fc_rports, list) {
906 if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
907 ri->flags |= MPT_RPORT_INFO_FLAGS_MISSING;
908 }
909 }
Michael Reed05e8ec12006-01-13 14:31:54 -0600910
911 /*
912 * now rescan devices known to adapter,
913 * will reregister existing rports
914 */
915 for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
Michael Reed80d3ac72006-05-24 15:07:09 -0500916 (void) mptfc_GetFcPortPage0(ioc, ii);
Michael Reed05e8ec12006-01-13 14:31:54 -0600917 mptfc_init_host_attr(ioc,ii); /* refresh */
918 mptfc_GetFcDevPage0(ioc,ii,mptfc_register_dev);
919 }
920
921 /* delete devices still missing */
Michael Reed05e8ec12006-01-13 14:31:54 -0600922 list_for_each_entry(ri, &ioc->fc_rports, list) {
923 /* if newly missing, delete it */
Moore, Eric65207fe2006-04-21 16:14:35 -0600924 if (ri->flags & MPT_RPORT_INFO_FLAGS_MISSING) {
Michael Reed05e8ec12006-01-13 14:31:54 -0600925
926 ri->flags &= ~(MPT_RPORT_INFO_FLAGS_REGISTERED|
927 MPT_RPORT_INFO_FLAGS_MISSING);
Moore, Eric65207fe2006-04-21 16:14:35 -0600928 fc_remote_port_delete(ri->rport); /* won't sleep */
Michael Reed3bc7bf12006-01-25 18:05:18 -0700929 ri->rport = NULL;
Moore, Eric65207fe2006-04-21 16:14:35 -0600930
931 pn = (u64)ri->pg0.WWPN.High << 32 |
932 (u64)ri->pg0.WWPN.Low;
Michael Reed3bc7bf12006-01-25 18:05:18 -0700933 dfcprintk ((MYIOC_s_INFO_FMT
934 "mptfc_rescan.%d: %llx deleted\n",
935 ioc->name,
936 ioc->sh->host_no,
Moore, Eric65207fe2006-04-21 16:14:35 -0600937 (unsigned long long)pn));
Michael Reed05e8ec12006-01-13 14:31:54 -0600938 }
939 }
Michael Reed05e8ec12006-01-13 14:31:54 -0600940
941 /*
942 * allow multiple passes as target state
943 * might have changed during scan
944 */
945 spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
946 if (ioc->fc_rescan_work_count > 2) /* only need one more */
947 ioc->fc_rescan_work_count = 2;
948 work_to_do = --ioc->fc_rescan_work_count;
949 spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
950 } while (work_to_do);
951}
952
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400953static int
954mptfc_probe(struct pci_dev *pdev, const struct pci_device_id *id)
955{
956 struct Scsi_Host *sh;
957 MPT_SCSI_HOST *hd;
958 MPT_ADAPTER *ioc;
959 unsigned long flags;
Christoph Hellwig1ca00bb2006-01-13 18:27:50 +0100960 int ii;
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400961 int numSGE = 0;
962 int scale;
963 int ioc_cap;
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400964 int error=0;
965 int r;
Michael Reed05e8ec12006-01-13 14:31:54 -0600966
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400967 if ((r = mpt_attach(pdev,id)) != 0)
968 return r;
Michael Reed05e8ec12006-01-13 14:31:54 -0600969
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400970 ioc = pci_get_drvdata(pdev);
Moore, Eric Dean d335cc32005-04-30 17:09:38 -0500971 ioc->DoneCtx = mptfcDoneCtx;
972 ioc->TaskCtx = mptfcTaskCtx;
973 ioc->InternalCtx = mptfcInternalCtx;
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400974
975 /* Added sanity check on readiness of the MPT adapter.
976 */
977 if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
978 printk(MYIOC_s_WARN_FMT
979 "Skipping because it's not operational!\n",
980 ioc->name);
Moore, Eric Dean7acec1e2005-11-16 18:54:17 -0700981 error = -ENODEV;
982 goto out_mptfc_probe;
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400983 }
984
985 if (!ioc->active) {
986 printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
987 ioc->name);
Moore, Eric Dean7acec1e2005-11-16 18:54:17 -0700988 error = -ENODEV;
989 goto out_mptfc_probe;
Moore, Eric Dean 2496af32005-04-22 18:02:41 -0400990 }
991
992 /* Sanity check - ensure at least 1 port is INITIATOR capable
993 */
994 ioc_cap = 0;
995 for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
996 if (ioc->pfacts[ii].ProtocolFlags &
997 MPI_PORTFACTS_PROTOCOL_INITIATOR)
998 ioc_cap ++;
999 }
1000
1001 if (!ioc_cap) {
1002 printk(MYIOC_s_WARN_FMT
1003 "Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
1004 ioc->name, ioc);
Michael Reed05e8ec12006-01-13 14:31:54 -06001005 return -ENODEV;
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001006 }
1007
1008 sh = scsi_host_alloc(&mptfc_driver_template, sizeof(MPT_SCSI_HOST));
1009
1010 if (!sh) {
1011 printk(MYIOC_s_WARN_FMT
1012 "Unable to register controller with SCSI subsystem\n",
1013 ioc->name);
Moore, Eric Dean7acec1e2005-11-16 18:54:17 -07001014 error = -1;
1015 goto out_mptfc_probe;
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001016 }
1017
Michael Reed80d3ac72006-05-24 15:07:09 -05001018 spin_lock_init(&ioc->fc_rescan_work_lock);
Michael Reed05e8ec12006-01-13 14:31:54 -06001019 INIT_WORK(&ioc->fc_rescan_work, mptfc_rescan_devices,(void *)ioc);
Michael Reed419835e2006-05-24 15:07:40 -05001020 INIT_WORK(&ioc->fc_setup_reset_work, mptfc_setup_reset, (void *)ioc);
Michael Reed05e8ec12006-01-13 14:31:54 -06001021
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001022 spin_lock_irqsave(&ioc->FreeQlock, flags);
1023
1024 /* Attach the SCSI Host to the IOC structure
1025 */
1026 ioc->sh = sh;
1027
1028 sh->io_port = 0;
1029 sh->n_io_port = 0;
1030 sh->irq = 0;
1031
1032 /* set 16 byte cdb's */
1033 sh->max_cmd_len = 16;
1034
1035 sh->max_id = MPT_MAX_FC_DEVICES<256 ? MPT_MAX_FC_DEVICES : 255;
1036
1037 sh->max_lun = MPT_LAST_LUN + 1;
1038 sh->max_channel = 0;
1039 sh->this_id = ioc->pfacts[0].PortSCSIID;
1040
1041 /* Required entry.
1042 */
1043 sh->unique_id = ioc->id;
1044
1045 /* Verify that we won't exceed the maximum
1046 * number of chain buffers
1047 * We can optimize: ZZ = req_sz/sizeof(SGE)
1048 * For 32bit SGE's:
1049 * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
1050 * + (req_sz - 64)/sizeof(SGE)
1051 * A slightly different algorithm is required for
1052 * 64bit SGEs.
1053 */
1054 scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
1055 if (sizeof(dma_addr_t) == sizeof(u64)) {
1056 numSGE = (scale - 1) *
1057 (ioc->facts.MaxChainDepth-1) + scale +
1058 (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
1059 sizeof(u32));
1060 } else {
1061 numSGE = 1 + (scale - 1) *
1062 (ioc->facts.MaxChainDepth-1) + scale +
1063 (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
1064 sizeof(u32));
1065 }
1066
1067 if (numSGE < sh->sg_tablesize) {
1068 /* Reset this value */
1069 dprintk((MYIOC_s_INFO_FMT
1070 "Resetting sg_tablesize to %d from %d\n",
1071 ioc->name, numSGE, sh->sg_tablesize));
1072 sh->sg_tablesize = numSGE;
1073 }
1074
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001075 spin_unlock_irqrestore(&ioc->FreeQlock, flags);
1076
1077 hd = (MPT_SCSI_HOST *) sh->hostdata;
1078 hd->ioc = ioc;
1079
1080 /* SCSI needs scsi_cmnd lookup table!
1081 * (with size equal to req_depth*PtrSz!)
1082 */
Christoph Hellwig1ca00bb2006-01-13 18:27:50 +01001083 hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
1084 if (!hd->ScsiLookup) {
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001085 error = -ENOMEM;
Moore, Eric Dean7acec1e2005-11-16 18:54:17 -07001086 goto out_mptfc_probe;
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001087 }
1088
Christoph Hellwig1ca00bb2006-01-13 18:27:50 +01001089 dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
1090 ioc->name, hd->ScsiLookup));
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001091
1092 /* Allocate memory for the device structures.
1093 * A non-Null pointer at an offset
1094 * indicates a device exists.
1095 * max_id = 1 + maximum id (hosts.h)
1096 */
Christoph Hellwig1ca00bb2006-01-13 18:27:50 +01001097 hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
1098 if (!hd->Targets) {
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001099 error = -ENOMEM;
Moore, Eric Dean7acec1e2005-11-16 18:54:17 -07001100 goto out_mptfc_probe;
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001101 }
1102
Christoph Hellwig1ca00bb2006-01-13 18:27:50 +01001103 dprintk((KERN_INFO " vdev @ %p\n", hd->Targets));
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001104
1105 /* Clear the TM flags
1106 */
1107 hd->tmPending = 0;
1108 hd->tmState = TM_STATE_NONE;
1109 hd->resetPending = 0;
1110 hd->abortSCpnt = NULL;
1111
1112 /* Clear the pointer used to store
1113 * single-threaded commands, i.e., those
1114 * issued during a bus scan, dv and
1115 * configuration pages.
1116 */
1117 hd->cmdPtr = NULL;
1118
1119 /* Initialize this SCSI Hosts' timers
1120 * To use, set the timer expires field
1121 * and add_timer
1122 */
1123 init_timer(&hd->timer);
1124 hd->timer.data = (unsigned long) hd;
1125 hd->timer.function = mptscsih_timer_expired;
1126
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001127 init_waitqueue_head(&hd->scandv_waitq);
1128 hd->scandv_wait_done = 0;
1129 hd->last_queue_full = 0;
1130
Michael Reed05e8ec12006-01-13 14:31:54 -06001131 sh->transportt = mptfc_transport_template;
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001132 error = scsi_add_host (sh, &ioc->pcidev->dev);
1133 if(error) {
1134 dprintk((KERN_ERR MYNAM
1135 "scsi_add_host failed\n"));
Moore, Eric Dean7acec1e2005-11-16 18:54:17 -07001136 goto out_mptfc_probe;
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001137 }
1138
Moore, Eric65207fe2006-04-21 16:14:35 -06001139 /* initialize workqueue */
1140
1141 snprintf(ioc->fc_rescan_work_q_name, KOBJ_NAME_LEN, "mptfc_wq_%d",
1142 sh->host_no);
1143 ioc->fc_rescan_work_q =
1144 create_singlethread_workqueue(ioc->fc_rescan_work_q_name);
1145 if (!ioc->fc_rescan_work_q)
1146 goto out_mptfc_probe;
1147
1148 /*
Michael Reed80d3ac72006-05-24 15:07:09 -05001149 * Pre-fetch FC port WWN and stuff...
1150 * (FCPortPage0_t stuff)
1151 */
1152 for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
1153 (void) mptfc_GetFcPortPage0(ioc, ii);
1154 }
Michael Reedca2f9382006-05-24 15:07:24 -05001155 mptfc_SetFcPortPage1_defaults(ioc);
Michael Reed80d3ac72006-05-24 15:07:09 -05001156
1157 /*
Moore, Eric65207fe2006-04-21 16:14:35 -06001158 * scan for rports -
1159 * by doing it via the workqueue, some locking is eliminated
1160 */
1161
1162 ioc->fc_rescan_work_count = 1;
1163 queue_work(ioc->fc_rescan_work_q, &ioc->fc_rescan_work);
1164 flush_workqueue(ioc->fc_rescan_work_q);
Michael Reed05e8ec12006-01-13 14:31:54 -06001165
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001166 return 0;
1167
Moore, Eric Dean7acec1e2005-11-16 18:54:17 -07001168out_mptfc_probe:
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001169
1170 mptscsih_remove(pdev);
1171 return error;
1172}
1173
1174static struct pci_driver mptfc_driver = {
1175 .name = "mptfc",
1176 .id_table = mptfc_pci_table,
1177 .probe = mptfc_probe,
Michael Reed05e8ec12006-01-13 14:31:54 -06001178 .remove = __devexit_p(mptfc_remove),
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07001179 .shutdown = mptscsih_shutdown,
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001180#ifdef CONFIG_PM
1181 .suspend = mptscsih_suspend,
1182 .resume = mptscsih_resume,
1183#endif
1184};
1185
Michael Reed80d3ac72006-05-24 15:07:09 -05001186static int
1187mptfc_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
1188{
1189 MPT_SCSI_HOST *hd;
1190 u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
1191 unsigned long flags;
1192 int rc=1;
1193
1194 devtverboseprintk((MYIOC_s_INFO_FMT "MPT event (=%02Xh) routed to SCSI host driver!\n",
1195 ioc->name, event));
1196
1197 if (ioc->sh == NULL ||
1198 ((hd = (MPT_SCSI_HOST *)ioc->sh->hostdata) == NULL))
1199 return 1;
1200
1201 switch (event) {
1202 case MPI_EVENT_RESCAN:
1203 spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
1204 if (ioc->fc_rescan_work_q) {
1205 if (ioc->fc_rescan_work_count++ == 0) {
1206 queue_work(ioc->fc_rescan_work_q,
1207 &ioc->fc_rescan_work);
1208 }
1209 }
1210 spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
1211 break;
1212 default:
1213 rc = mptscsih_event_process(ioc,pEvReply);
1214 break;
1215 }
1216 return rc;
1217}
1218
1219static int
1220mptfc_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
1221{
1222 int rc;
1223 unsigned long flags;
1224
1225 rc = mptscsih_ioc_reset(ioc,reset_phase);
1226 if (rc == 0)
1227 return rc;
1228
1229
1230 dtmprintk((KERN_WARNING MYNAM
1231 ": IOC %s_reset routed to FC host driver!\n",
1232 reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
1233 reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
1234
1235 if (reset_phase == MPT_IOC_SETUP_RESET) {
Michael Reed419835e2006-05-24 15:07:40 -05001236 spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
1237 if (ioc->fc_rescan_work_q) {
1238 queue_work(ioc->fc_rescan_work_q,
1239 &ioc->fc_setup_reset_work);
1240 }
1241 spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
Michael Reed80d3ac72006-05-24 15:07:09 -05001242 }
1243
1244 else if (reset_phase == MPT_IOC_PRE_RESET) {
1245 }
1246
1247 else { /* MPT_IOC_POST_RESET */
Michael Reedca2f9382006-05-24 15:07:24 -05001248 mptfc_SetFcPortPage1_defaults(ioc);
Michael Reed80d3ac72006-05-24 15:07:09 -05001249 spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
1250 if (ioc->fc_rescan_work_q) {
1251 if (ioc->fc_rescan_work_count++ == 0) {
1252 queue_work(ioc->fc_rescan_work_q,
1253 &ioc->fc_rescan_work);
1254 }
1255 }
1256 spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
1257 }
1258 return 1;
1259}
1260
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001261/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1262/**
1263 * mptfc_init - Register MPT adapter(s) as SCSI host(s) with
1264 * linux scsi mid-layer.
1265 *
1266 * Returns 0 for success, non-zero for failure.
1267 */
1268static int __init
1269mptfc_init(void)
1270{
Michael Reed05e8ec12006-01-13 14:31:54 -06001271 int error;
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001272
1273 show_mptmod_ver(my_NAME, my_VERSION);
1274
Michael Reedca2f9382006-05-24 15:07:24 -05001275 /* sanity check module parameters */
1276 if (mptfc_dev_loss_tmo <= 0)
Michael Reed05e8ec12006-01-13 14:31:54 -06001277 mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO;
1278
1279 mptfc_transport_template =
1280 fc_attach_transport(&mptfc_transport_functions);
1281
1282 if (!mptfc_transport_template)
1283 return -ENODEV;
1284
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001285 mptfcDoneCtx = mpt_register(mptscsih_io_done, MPTFC_DRIVER);
1286 mptfcTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTFC_DRIVER);
1287 mptfcInternalCtx = mpt_register(mptscsih_scandv_complete, MPTFC_DRIVER);
1288
Michael Reed80d3ac72006-05-24 15:07:09 -05001289 if (mpt_event_register(mptfcDoneCtx, mptfc_event_process) == 0) {
Moore, Eric3a892be2006-03-14 09:14:03 -07001290 devtverboseprintk((KERN_INFO MYNAM
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001291 ": Registered for IOC event notifications\n"));
1292 }
1293
Michael Reed80d3ac72006-05-24 15:07:09 -05001294 if (mpt_reset_register(mptfcDoneCtx, mptfc_ioc_reset) == 0) {
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001295 dprintk((KERN_INFO MYNAM
1296 ": Registered for IOC reset notifications\n"));
1297 }
1298
Michael Reed05e8ec12006-01-13 14:31:54 -06001299 error = pci_register_driver(&mptfc_driver);
Michael Reed3bc7bf12006-01-25 18:05:18 -07001300 if (error)
Michael Reed05e8ec12006-01-13 14:31:54 -06001301 fc_release_transport(mptfc_transport_template);
Michael Reed05e8ec12006-01-13 14:31:54 -06001302
1303 return error;
1304}
1305
1306/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1307/**
1308 * mptfc_remove - Removed fc infrastructure for devices
1309 * @pdev: Pointer to pci_dev structure
1310 *
1311 */
Michael Reed3bc7bf12006-01-25 18:05:18 -07001312static void __devexit
1313mptfc_remove(struct pci_dev *pdev)
Michael Reed05e8ec12006-01-13 14:31:54 -06001314{
Moore, Eric65207fe2006-04-21 16:14:35 -06001315 MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
1316 struct mptfc_rport_info *p, *n;
1317 struct workqueue_struct *work_q;
1318 unsigned long flags;
Michael Reedca2f9382006-05-24 15:07:24 -05001319 int ii;
Moore, Eric65207fe2006-04-21 16:14:35 -06001320
1321 /* destroy workqueue */
1322 if ((work_q=ioc->fc_rescan_work_q)) {
1323 spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
1324 ioc->fc_rescan_work_q = NULL;
1325 spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
1326 destroy_workqueue(work_q);
1327 }
Michael Reed05e8ec12006-01-13 14:31:54 -06001328
1329 fc_remove_host(ioc->sh);
1330
1331 list_for_each_entry_safe(p, n, &ioc->fc_rports, list) {
1332 list_del(&p->list);
1333 kfree(p);
1334 }
1335
Michael Reedca2f9382006-05-24 15:07:24 -05001336 for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
1337 if (ioc->fc_data.fc_port_page1[ii].data) {
1338 pci_free_consistent(ioc->pcidev,
1339 ioc->fc_data.fc_port_page1[ii].pg_sz,
1340 (u8 *) ioc->fc_data.fc_port_page1[ii].data,
1341 ioc->fc_data.fc_port_page1[ii].dma);
1342 ioc->fc_data.fc_port_page1[ii].data = NULL;
1343 }
1344 }
1345
Michael Reed05e8ec12006-01-13 14:31:54 -06001346 mptscsih_remove(pdev);
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001347}
1348
1349/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1350/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1351/**
1352 * mptfc_exit - Unregisters MPT adapter(s)
1353 *
1354 */
1355static void __exit
1356mptfc_exit(void)
1357{
1358 pci_unregister_driver(&mptfc_driver);
Michael Reed05e8ec12006-01-13 14:31:54 -06001359 fc_release_transport(mptfc_transport_template);
1360
Moore, Eric Dean 2496af32005-04-22 18:02:41 -04001361 mpt_reset_deregister(mptfcDoneCtx);
1362 dprintk((KERN_INFO MYNAM
1363 ": Deregistered for IOC reset notifications\n"));
1364
1365 mpt_event_deregister(mptfcDoneCtx);
1366 dprintk((KERN_INFO MYNAM
1367 ": Deregistered for IOC event notifications\n"));
1368
1369 mpt_deregister(mptfcInternalCtx);
1370 mpt_deregister(mptfcTaskCtx);
1371 mpt_deregister(mptfcDoneCtx);
1372}
1373
1374module_init(mptfc_init);
1375module_exit(mptfc_exit);