Merge git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc
[linux-2.6] / drivers / scsi / qla2xxx / qla_gs.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2005 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8
9 static inline struct ct_sns_req *
10 qla2x00_prep_ct_req(struct ct_sns_req *, uint16_t, uint16_t);
11
12 static inline struct sns_cmd_pkt *
13 qla2x00_prep_sns_cmd(scsi_qla_host_t *, uint16_t, uint16_t, uint16_t);
14
15 static int qla2x00_sns_ga_nxt(scsi_qla_host_t *, fc_port_t *);
16 static int qla2x00_sns_gid_pt(scsi_qla_host_t *, sw_info_t *);
17 static int qla2x00_sns_gpn_id(scsi_qla_host_t *, sw_info_t *);
18 static int qla2x00_sns_gnn_id(scsi_qla_host_t *, sw_info_t *);
19 static int qla2x00_sns_rft_id(scsi_qla_host_t *);
20 static int qla2x00_sns_rnn_id(scsi_qla_host_t *);
21
22 /**
23  * qla2x00_prep_ms_iocb() - Prepare common MS/CT IOCB fields for SNS CT query.
24  * @ha: HA context
25  * @req_size: request size in bytes
26  * @rsp_size: response size in bytes
27  *
28  * Returns a pointer to the @ha's ms_iocb.
29  */
30 void *
31 qla2x00_prep_ms_iocb(scsi_qla_host_t *ha, uint32_t req_size, uint32_t rsp_size)
32 {
33         ms_iocb_entry_t *ms_pkt;
34
35         ms_pkt = ha->ms_iocb;
36         memset(ms_pkt, 0, sizeof(ms_iocb_entry_t));
37
38         ms_pkt->entry_type = MS_IOCB_TYPE;
39         ms_pkt->entry_count = 1;
40         SET_TARGET_ID(ha, ms_pkt->loop_id, SIMPLE_NAME_SERVER);
41         ms_pkt->control_flags = __constant_cpu_to_le16(CF_READ | CF_HEAD_TAG);
42         ms_pkt->timeout = __constant_cpu_to_le16(25);
43         ms_pkt->cmd_dsd_count = __constant_cpu_to_le16(1);
44         ms_pkt->total_dsd_count = __constant_cpu_to_le16(2);
45         ms_pkt->rsp_bytecount = cpu_to_le32(rsp_size);
46         ms_pkt->req_bytecount = cpu_to_le32(req_size);
47
48         ms_pkt->dseg_req_address[0] = cpu_to_le32(LSD(ha->ct_sns_dma));
49         ms_pkt->dseg_req_address[1] = cpu_to_le32(MSD(ha->ct_sns_dma));
50         ms_pkt->dseg_req_length = ms_pkt->req_bytecount;
51
52         ms_pkt->dseg_rsp_address[0] = cpu_to_le32(LSD(ha->ct_sns_dma));
53         ms_pkt->dseg_rsp_address[1] = cpu_to_le32(MSD(ha->ct_sns_dma));
54         ms_pkt->dseg_rsp_length = ms_pkt->rsp_bytecount;
55
56         return (ms_pkt);
57 }
58
59 /**
60  * qla24xx_prep_ms_iocb() - Prepare common CT IOCB fields for SNS CT query.
61  * @ha: HA context
62  * @req_size: request size in bytes
63  * @rsp_size: response size in bytes
64  *
65  * Returns a pointer to the @ha's ms_iocb.
66  */
67 void *
68 qla24xx_prep_ms_iocb(scsi_qla_host_t *ha, uint32_t req_size, uint32_t rsp_size)
69 {
70         struct ct_entry_24xx *ct_pkt;
71
72         ct_pkt = (struct ct_entry_24xx *)ha->ms_iocb;
73         memset(ct_pkt, 0, sizeof(struct ct_entry_24xx));
74
75         ct_pkt->entry_type = CT_IOCB_TYPE;
76         ct_pkt->entry_count = 1;
77         ct_pkt->nport_handle = __constant_cpu_to_le16(NPH_SNS);
78         ct_pkt->timeout = __constant_cpu_to_le16(25);
79         ct_pkt->cmd_dsd_count = __constant_cpu_to_le16(1);
80         ct_pkt->rsp_dsd_count = __constant_cpu_to_le16(1);
81         ct_pkt->rsp_byte_count = cpu_to_le32(rsp_size);
82         ct_pkt->cmd_byte_count = cpu_to_le32(req_size);
83
84         ct_pkt->dseg_0_address[0] = cpu_to_le32(LSD(ha->ct_sns_dma));
85         ct_pkt->dseg_0_address[1] = cpu_to_le32(MSD(ha->ct_sns_dma));
86         ct_pkt->dseg_0_len = ct_pkt->cmd_byte_count;
87
88         ct_pkt->dseg_1_address[0] = cpu_to_le32(LSD(ha->ct_sns_dma));
89         ct_pkt->dseg_1_address[1] = cpu_to_le32(MSD(ha->ct_sns_dma));
90         ct_pkt->dseg_1_len = ct_pkt->rsp_byte_count;
91
92         return (ct_pkt);
93 }
94
95 /**
96  * qla2x00_prep_ct_req() - Prepare common CT request fields for SNS query.
97  * @ct_req: CT request buffer
98  * @cmd: GS command
99  * @rsp_size: response size in bytes
100  *
101  * Returns a pointer to the intitialized @ct_req.
102  */
103 static inline struct ct_sns_req *
104 qla2x00_prep_ct_req(struct ct_sns_req *ct_req, uint16_t cmd, uint16_t rsp_size)
105 {
106         memset(ct_req, 0, sizeof(struct ct_sns_pkt));
107
108         ct_req->header.revision = 0x01;
109         ct_req->header.gs_type = 0xFC;
110         ct_req->header.gs_subtype = 0x02;
111         ct_req->command = cpu_to_be16(cmd);
112         ct_req->max_rsp_size = cpu_to_be16((rsp_size - 16) / 4);
113
114         return (ct_req);
115 }
116
117 static int
118 qla2x00_chk_ms_status(scsi_qla_host_t *ha, ms_iocb_entry_t *ms_pkt,
119     struct ct_sns_rsp *ct_rsp, const char *routine)
120 {
121         int rval;
122         uint16_t comp_status;
123
124         rval = QLA_FUNCTION_FAILED;
125         if (ms_pkt->entry_status != 0) {
126                 DEBUG2_3(printk("scsi(%ld): %s failed, error status (%x).\n",
127                     ha->host_no, routine, ms_pkt->entry_status));
128         } else {
129                 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
130                         comp_status =
131                             ((struct ct_entry_24xx *)ms_pkt)->comp_status;
132                 else
133                         comp_status = le16_to_cpu(ms_pkt->status);
134                 switch (comp_status) {
135                 case CS_COMPLETE:
136                 case CS_DATA_UNDERRUN:
137                 case CS_DATA_OVERRUN:           /* Overrun? */
138                         if (ct_rsp->header.response !=
139                             __constant_cpu_to_be16(CT_ACCEPT_RESPONSE)) {
140                                 DEBUG2_3(printk("scsi(%ld): %s failed, "
141                                     "rejected request:\n", ha->host_no,
142                                     routine));
143                                 DEBUG2_3(qla2x00_dump_buffer(
144                                     (uint8_t *)&ct_rsp->header,
145                                     sizeof(struct ct_rsp_hdr)));
146                         } else
147                                 rval = QLA_SUCCESS;
148                         break;
149                 default:
150                         DEBUG2_3(printk("scsi(%ld): %s failed, completion "
151                             "status (%x).\n", ha->host_no, routine,
152                             comp_status));
153                         break;
154                 }
155         }
156         return rval;
157 }
158
159 /**
160  * qla2x00_ga_nxt() - SNS scan for fabric devices via GA_NXT command.
161  * @ha: HA context
162  * @fcport: fcport entry to updated
163  *
164  * Returns 0 on success.
165  */
166 int
167 qla2x00_ga_nxt(scsi_qla_host_t *ha, fc_port_t *fcport)
168 {
169         int             rval;
170
171         ms_iocb_entry_t *ms_pkt;
172         struct ct_sns_req       *ct_req;
173         struct ct_sns_rsp       *ct_rsp;
174
175         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
176                 return (qla2x00_sns_ga_nxt(ha, fcport));
177         }
178
179         /* Issue GA_NXT */
180         /* Prepare common MS IOCB */
181         ms_pkt = ha->isp_ops.prep_ms_iocb(ha, GA_NXT_REQ_SIZE, GA_NXT_RSP_SIZE);
182
183         /* Prepare CT request */
184         ct_req = qla2x00_prep_ct_req(&ha->ct_sns->p.req, GA_NXT_CMD,
185             GA_NXT_RSP_SIZE);
186         ct_rsp = &ha->ct_sns->p.rsp;
187
188         /* Prepare CT arguments -- port_id */
189         ct_req->req.port_id.port_id[0] = fcport->d_id.b.domain;
190         ct_req->req.port_id.port_id[1] = fcport->d_id.b.area;
191         ct_req->req.port_id.port_id[2] = fcport->d_id.b.al_pa;
192
193         /* Execute MS IOCB */
194         rval = qla2x00_issue_iocb(ha, ha->ms_iocb, ha->ms_iocb_dma,
195             sizeof(ms_iocb_entry_t));
196         if (rval != QLA_SUCCESS) {
197                 /*EMPTY*/
198                 DEBUG2_3(printk("scsi(%ld): GA_NXT issue IOCB failed (%d).\n",
199                     ha->host_no, rval));
200         } else if (qla2x00_chk_ms_status(ha, ms_pkt, ct_rsp, "GA_NXT") !=
201             QLA_SUCCESS) {
202                 rval = QLA_FUNCTION_FAILED;
203         } else {
204                 /* Populate fc_port_t entry. */
205                 fcport->d_id.b.domain = ct_rsp->rsp.ga_nxt.port_id[0];
206                 fcport->d_id.b.area = ct_rsp->rsp.ga_nxt.port_id[1];
207                 fcport->d_id.b.al_pa = ct_rsp->rsp.ga_nxt.port_id[2];
208
209                 memcpy(fcport->node_name, ct_rsp->rsp.ga_nxt.node_name,
210                     WWN_SIZE);
211                 memcpy(fcport->port_name, ct_rsp->rsp.ga_nxt.port_name,
212                     WWN_SIZE);
213
214                 if (ct_rsp->rsp.ga_nxt.port_type != NS_N_PORT_TYPE &&
215                     ct_rsp->rsp.ga_nxt.port_type != NS_NL_PORT_TYPE)
216                         fcport->d_id.b.domain = 0xf0;
217
218                 DEBUG2_3(printk("scsi(%ld): GA_NXT entry - "
219                     "nn %02x%02x%02x%02x%02x%02x%02x%02x "
220                     "pn %02x%02x%02x%02x%02x%02x%02x%02x "
221                     "portid=%02x%02x%02x.\n",
222                     ha->host_no,
223                     fcport->node_name[0], fcport->node_name[1],
224                     fcport->node_name[2], fcport->node_name[3],
225                     fcport->node_name[4], fcport->node_name[5],
226                     fcport->node_name[6], fcport->node_name[7],
227                     fcport->port_name[0], fcport->port_name[1],
228                     fcport->port_name[2], fcport->port_name[3],
229                     fcport->port_name[4], fcport->port_name[5],
230                     fcport->port_name[6], fcport->port_name[7],
231                     fcport->d_id.b.domain, fcport->d_id.b.area,
232                     fcport->d_id.b.al_pa));
233         }
234
235         return (rval);
236 }
237
238 /**
239  * qla2x00_gid_pt() - SNS scan for fabric devices via GID_PT command.
240  * @ha: HA context
241  * @list: switch info entries to populate
242  *
243  * NOTE: Non-Nx_Ports are not requested.
244  *
245  * Returns 0 on success.
246  */
247 int
248 qla2x00_gid_pt(scsi_qla_host_t *ha, sw_info_t *list)
249 {
250         int             rval;
251         uint16_t        i;
252
253         ms_iocb_entry_t *ms_pkt;
254         struct ct_sns_req       *ct_req;
255         struct ct_sns_rsp       *ct_rsp;
256
257         struct ct_sns_gid_pt_data *gid_data;
258
259         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
260                 return (qla2x00_sns_gid_pt(ha, list));
261         }
262
263         gid_data = NULL;
264
265         /* Issue GID_PT */
266         /* Prepare common MS IOCB */
267         ms_pkt = ha->isp_ops.prep_ms_iocb(ha, GID_PT_REQ_SIZE, GID_PT_RSP_SIZE);
268
269         /* Prepare CT request */
270         ct_req = qla2x00_prep_ct_req(&ha->ct_sns->p.req, GID_PT_CMD,
271             GID_PT_RSP_SIZE);
272         ct_rsp = &ha->ct_sns->p.rsp;
273
274         /* Prepare CT arguments -- port_type */
275         ct_req->req.gid_pt.port_type = NS_NX_PORT_TYPE;
276
277         /* Execute MS IOCB */
278         rval = qla2x00_issue_iocb(ha, ha->ms_iocb, ha->ms_iocb_dma,
279             sizeof(ms_iocb_entry_t));
280         if (rval != QLA_SUCCESS) {
281                 /*EMPTY*/
282                 DEBUG2_3(printk("scsi(%ld): GID_PT issue IOCB failed (%d).\n",
283                     ha->host_no, rval));
284         } else if (qla2x00_chk_ms_status(ha, ms_pkt, ct_rsp, "GID_PT") !=
285             QLA_SUCCESS) {
286                 rval = QLA_FUNCTION_FAILED;
287         } else {
288                 /* Set port IDs in switch info list. */
289                 for (i = 0; i < MAX_FIBRE_DEVICES; i++) {
290                         gid_data = &ct_rsp->rsp.gid_pt.entries[i];
291                         list[i].d_id.b.domain = gid_data->port_id[0];
292                         list[i].d_id.b.area = gid_data->port_id[1];
293                         list[i].d_id.b.al_pa = gid_data->port_id[2];
294
295                         /* Last one exit. */
296                         if (gid_data->control_byte & BIT_7) {
297                                 list[i].d_id.b.rsvd_1 = gid_data->control_byte;
298                                 break;
299                         }
300                 }
301
302                 /*
303                  * If we've used all available slots, then the switch is
304                  * reporting back more devices than we can handle with this
305                  * single call.  Return a failed status, and let GA_NXT handle
306                  * the overload.
307                  */
308                 if (i == MAX_FIBRE_DEVICES)
309                         rval = QLA_FUNCTION_FAILED;
310         }
311
312         return (rval);
313 }
314
315 /**
316  * qla2x00_gpn_id() - SNS Get Port Name (GPN_ID) query.
317  * @ha: HA context
318  * @list: switch info entries to populate
319  *
320  * Returns 0 on success.
321  */
322 int
323 qla2x00_gpn_id(scsi_qla_host_t *ha, sw_info_t *list)
324 {
325         int             rval;
326         uint16_t        i;
327
328         ms_iocb_entry_t *ms_pkt;
329         struct ct_sns_req       *ct_req;
330         struct ct_sns_rsp       *ct_rsp;
331
332         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
333                 return (qla2x00_sns_gpn_id(ha, list));
334         }
335
336         for (i = 0; i < MAX_FIBRE_DEVICES; i++) {
337                 /* Issue GPN_ID */
338                 /* Prepare common MS IOCB */
339                 ms_pkt = ha->isp_ops.prep_ms_iocb(ha, GPN_ID_REQ_SIZE,
340                     GPN_ID_RSP_SIZE);
341
342                 /* Prepare CT request */
343                 ct_req = qla2x00_prep_ct_req(&ha->ct_sns->p.req, GPN_ID_CMD,
344                     GPN_ID_RSP_SIZE);
345                 ct_rsp = &ha->ct_sns->p.rsp;
346
347                 /* Prepare CT arguments -- port_id */
348                 ct_req->req.port_id.port_id[0] = list[i].d_id.b.domain;
349                 ct_req->req.port_id.port_id[1] = list[i].d_id.b.area;
350                 ct_req->req.port_id.port_id[2] = list[i].d_id.b.al_pa;
351
352                 /* Execute MS IOCB */
353                 rval = qla2x00_issue_iocb(ha, ha->ms_iocb, ha->ms_iocb_dma,
354                     sizeof(ms_iocb_entry_t));
355                 if (rval != QLA_SUCCESS) {
356                         /*EMPTY*/
357                         DEBUG2_3(printk("scsi(%ld): GPN_ID issue IOCB failed "
358                             "(%d).\n", ha->host_no, rval));
359                 } else if (qla2x00_chk_ms_status(ha, ms_pkt, ct_rsp,
360                     "GPN_ID") != QLA_SUCCESS) {
361                         rval = QLA_FUNCTION_FAILED;
362                 } else {
363                         /* Save portname */
364                         memcpy(list[i].port_name,
365                             ct_rsp->rsp.gpn_id.port_name, WWN_SIZE);
366                 }
367
368                 /* Last device exit. */
369                 if (list[i].d_id.b.rsvd_1 != 0)
370                         break;
371         }
372
373         return (rval);
374 }
375
376 /**
377  * qla2x00_gnn_id() - SNS Get Node Name (GNN_ID) query.
378  * @ha: HA context
379  * @list: switch info entries to populate
380  *
381  * Returns 0 on success.
382  */
383 int
384 qla2x00_gnn_id(scsi_qla_host_t *ha, sw_info_t *list)
385 {
386         int             rval;
387         uint16_t        i;
388
389         ms_iocb_entry_t *ms_pkt;
390         struct ct_sns_req       *ct_req;
391         struct ct_sns_rsp       *ct_rsp;
392
393         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
394                 return (qla2x00_sns_gnn_id(ha, list));
395         }
396
397         for (i = 0; i < MAX_FIBRE_DEVICES; i++) {
398                 /* Issue GNN_ID */
399                 /* Prepare common MS IOCB */
400                 ms_pkt = ha->isp_ops.prep_ms_iocb(ha, GNN_ID_REQ_SIZE,
401                     GNN_ID_RSP_SIZE);
402
403                 /* Prepare CT request */
404                 ct_req = qla2x00_prep_ct_req(&ha->ct_sns->p.req, GNN_ID_CMD,
405                     GNN_ID_RSP_SIZE);
406                 ct_rsp = &ha->ct_sns->p.rsp;
407
408                 /* Prepare CT arguments -- port_id */
409                 ct_req->req.port_id.port_id[0] = list[i].d_id.b.domain;
410                 ct_req->req.port_id.port_id[1] = list[i].d_id.b.area;
411                 ct_req->req.port_id.port_id[2] = list[i].d_id.b.al_pa;
412
413                 /* Execute MS IOCB */
414                 rval = qla2x00_issue_iocb(ha, ha->ms_iocb, ha->ms_iocb_dma,
415                     sizeof(ms_iocb_entry_t));
416                 if (rval != QLA_SUCCESS) {
417                         /*EMPTY*/
418                         DEBUG2_3(printk("scsi(%ld): GNN_ID issue IOCB failed "
419                             "(%d).\n", ha->host_no, rval));
420                 } else if (qla2x00_chk_ms_status(ha, ms_pkt, ct_rsp,
421                     "GNN_ID") != QLA_SUCCESS) {
422                         rval = QLA_FUNCTION_FAILED;
423                 } else {
424                         /* Save nodename */
425                         memcpy(list[i].node_name,
426                             ct_rsp->rsp.gnn_id.node_name, WWN_SIZE);
427
428                         DEBUG2_3(printk("scsi(%ld): GID_PT entry - "
429                             "nn %02x%02x%02x%02x%02x%02x%02x%02x "
430                             "pn %02x%02x%02x%02x%02x%02x%02x%02x "
431                             "portid=%02x%02x%02x.\n",
432                             ha->host_no,
433                             list[i].node_name[0], list[i].node_name[1],
434                             list[i].node_name[2], list[i].node_name[3],
435                             list[i].node_name[4], list[i].node_name[5],
436                             list[i].node_name[6], list[i].node_name[7],
437                             list[i].port_name[0], list[i].port_name[1],
438                             list[i].port_name[2], list[i].port_name[3],
439                             list[i].port_name[4], list[i].port_name[5],
440                             list[i].port_name[6], list[i].port_name[7],
441                             list[i].d_id.b.domain, list[i].d_id.b.area,
442                             list[i].d_id.b.al_pa));
443                 }
444
445                 /* Last device exit. */
446                 if (list[i].d_id.b.rsvd_1 != 0)
447                         break;
448         }
449
450         return (rval);
451 }
452
453 /**
454  * qla2x00_rft_id() - SNS Register FC-4 TYPEs (RFT_ID) supported by the HBA.
455  * @ha: HA context
456  *
457  * Returns 0 on success.
458  */
459 int
460 qla2x00_rft_id(scsi_qla_host_t *ha)
461 {
462         int             rval;
463
464         ms_iocb_entry_t *ms_pkt;
465         struct ct_sns_req       *ct_req;
466         struct ct_sns_rsp       *ct_rsp;
467
468         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
469                 return (qla2x00_sns_rft_id(ha));
470         }
471
472         /* Issue RFT_ID */
473         /* Prepare common MS IOCB */
474         ms_pkt = ha->isp_ops.prep_ms_iocb(ha, RFT_ID_REQ_SIZE, RFT_ID_RSP_SIZE);
475
476         /* Prepare CT request */
477         ct_req = qla2x00_prep_ct_req(&ha->ct_sns->p.req, RFT_ID_CMD,
478             RFT_ID_RSP_SIZE);
479         ct_rsp = &ha->ct_sns->p.rsp;
480
481         /* Prepare CT arguments -- port_id, FC-4 types */
482         ct_req->req.rft_id.port_id[0] = ha->d_id.b.domain;
483         ct_req->req.rft_id.port_id[1] = ha->d_id.b.area;
484         ct_req->req.rft_id.port_id[2] = ha->d_id.b.al_pa;
485
486         ct_req->req.rft_id.fc4_types[2] = 0x01;         /* FCP-3 */
487
488         /* Execute MS IOCB */
489         rval = qla2x00_issue_iocb(ha, ha->ms_iocb, ha->ms_iocb_dma,
490             sizeof(ms_iocb_entry_t));
491         if (rval != QLA_SUCCESS) {
492                 /*EMPTY*/
493                 DEBUG2_3(printk("scsi(%ld): RFT_ID issue IOCB failed (%d).\n",
494                     ha->host_no, rval));
495         } else if (qla2x00_chk_ms_status(ha, ms_pkt, ct_rsp, "RFT_ID") !=
496             QLA_SUCCESS) {
497                 rval = QLA_FUNCTION_FAILED;
498         } else {
499                 DEBUG2(printk("scsi(%ld): RFT_ID exiting normally.\n",
500                     ha->host_no));
501         }
502
503         return (rval);
504 }
505
506 /**
507  * qla2x00_rff_id() - SNS Register FC-4 Features (RFF_ID) supported by the HBA.
508  * @ha: HA context
509  *
510  * Returns 0 on success.
511  */
512 int
513 qla2x00_rff_id(scsi_qla_host_t *ha)
514 {
515         int             rval;
516
517         ms_iocb_entry_t *ms_pkt;
518         struct ct_sns_req       *ct_req;
519         struct ct_sns_rsp       *ct_rsp;
520
521         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
522                 DEBUG2(printk("scsi(%ld): RFF_ID call unsupported on "
523                     "ISP2100/ISP2200.\n", ha->host_no));
524                 return (QLA_SUCCESS);
525         }
526
527         /* Issue RFF_ID */
528         /* Prepare common MS IOCB */
529         ms_pkt = ha->isp_ops.prep_ms_iocb(ha, RFF_ID_REQ_SIZE, RFF_ID_RSP_SIZE);
530
531         /* Prepare CT request */
532         ct_req = qla2x00_prep_ct_req(&ha->ct_sns->p.req, RFF_ID_CMD,
533             RFF_ID_RSP_SIZE);
534         ct_rsp = &ha->ct_sns->p.rsp;
535
536         /* Prepare CT arguments -- port_id, FC-4 feature, FC-4 type */
537         ct_req->req.rff_id.port_id[0] = ha->d_id.b.domain;
538         ct_req->req.rff_id.port_id[1] = ha->d_id.b.area;
539         ct_req->req.rff_id.port_id[2] = ha->d_id.b.al_pa;
540
541         ct_req->req.rff_id.fc4_feature = BIT_1;
542         ct_req->req.rff_id.fc4_type = 0x08;             /* SCSI - FCP */
543
544         /* Execute MS IOCB */
545         rval = qla2x00_issue_iocb(ha, ha->ms_iocb, ha->ms_iocb_dma,
546             sizeof(ms_iocb_entry_t));
547         if (rval != QLA_SUCCESS) {
548                 /*EMPTY*/
549                 DEBUG2_3(printk("scsi(%ld): RFF_ID issue IOCB failed (%d).\n",
550                     ha->host_no, rval));
551         } else if (qla2x00_chk_ms_status(ha, ms_pkt, ct_rsp, "RFF_ID") !=
552             QLA_SUCCESS) {
553                 rval = QLA_FUNCTION_FAILED;
554         } else {
555                 DEBUG2(printk("scsi(%ld): RFF_ID exiting normally.\n",
556                     ha->host_no));
557         }
558
559         return (rval);
560 }
561
562 /**
563  * qla2x00_rnn_id() - SNS Register Node Name (RNN_ID) of the HBA.
564  * @ha: HA context
565  *
566  * Returns 0 on success.
567  */
568 int
569 qla2x00_rnn_id(scsi_qla_host_t *ha)
570 {
571         int             rval;
572
573         ms_iocb_entry_t *ms_pkt;
574         struct ct_sns_req       *ct_req;
575         struct ct_sns_rsp       *ct_rsp;
576
577         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
578                 return (qla2x00_sns_rnn_id(ha));
579         }
580
581         /* Issue RNN_ID */
582         /* Prepare common MS IOCB */
583         ms_pkt = ha->isp_ops.prep_ms_iocb(ha, RNN_ID_REQ_SIZE, RNN_ID_RSP_SIZE);
584
585         /* Prepare CT request */
586         ct_req = qla2x00_prep_ct_req(&ha->ct_sns->p.req, RNN_ID_CMD,
587             RNN_ID_RSP_SIZE);
588         ct_rsp = &ha->ct_sns->p.rsp;
589
590         /* Prepare CT arguments -- port_id, node_name */
591         ct_req->req.rnn_id.port_id[0] = ha->d_id.b.domain;
592         ct_req->req.rnn_id.port_id[1] = ha->d_id.b.area;
593         ct_req->req.rnn_id.port_id[2] = ha->d_id.b.al_pa;
594
595         memcpy(ct_req->req.rnn_id.node_name, ha->node_name, WWN_SIZE);
596
597         /* Execute MS IOCB */
598         rval = qla2x00_issue_iocb(ha, ha->ms_iocb, ha->ms_iocb_dma,
599             sizeof(ms_iocb_entry_t));
600         if (rval != QLA_SUCCESS) {
601                 /*EMPTY*/
602                 DEBUG2_3(printk("scsi(%ld): RNN_ID issue IOCB failed (%d).\n",
603                     ha->host_no, rval));
604         } else if (qla2x00_chk_ms_status(ha, ms_pkt, ct_rsp, "RNN_ID") !=
605             QLA_SUCCESS) {
606                 rval = QLA_FUNCTION_FAILED;
607         } else {
608                 DEBUG2(printk("scsi(%ld): RNN_ID exiting normally.\n",
609                     ha->host_no));
610         }
611
612         return (rval);
613 }
614
615 void
616 qla2x00_get_sym_node_name(scsi_qla_host_t *ha, uint8_t *snn)
617 {
618         sprintf(snn, "%s FW:v%d.%02d.%02d DVR:v%s",ha->model_number,
619             ha->fw_major_version, ha->fw_minor_version,
620             ha->fw_subminor_version, qla2x00_version_str);
621 }
622
623 /**
624  * qla2x00_rsnn_nn() - SNS Register Symbolic Node Name (RSNN_NN) of the HBA.
625  * @ha: HA context
626  *
627  * Returns 0 on success.
628  */
629 int
630 qla2x00_rsnn_nn(scsi_qla_host_t *ha)
631 {
632         int             rval;
633         ms_iocb_entry_t *ms_pkt;
634         struct ct_sns_req       *ct_req;
635         struct ct_sns_rsp       *ct_rsp;
636
637         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
638                 DEBUG2(printk("scsi(%ld): RSNN_ID call unsupported on "
639                     "ISP2100/ISP2200.\n", ha->host_no));
640                 return (QLA_SUCCESS);
641         }
642
643         /* Issue RSNN_NN */
644         /* Prepare common MS IOCB */
645         /*   Request size adjusted after CT preparation */
646         ms_pkt = ha->isp_ops.prep_ms_iocb(ha, 0, RSNN_NN_RSP_SIZE);
647
648         /* Prepare CT request */
649         ct_req = qla2x00_prep_ct_req(&ha->ct_sns->p.req, RSNN_NN_CMD,
650             RSNN_NN_RSP_SIZE);
651         ct_rsp = &ha->ct_sns->p.rsp;
652
653         /* Prepare CT arguments -- node_name, symbolic node_name, size */
654         memcpy(ct_req->req.rsnn_nn.node_name, ha->node_name, WWN_SIZE);
655
656         /* Prepare the Symbolic Node Name */
657         qla2x00_get_sym_node_name(ha, ct_req->req.rsnn_nn.sym_node_name);
658
659         /* Calculate SNN length */
660         ct_req->req.rsnn_nn.name_len =
661             (uint8_t)strlen(ct_req->req.rsnn_nn.sym_node_name);
662
663         /* Update MS IOCB request */
664         ms_pkt->req_bytecount =
665             cpu_to_le32(24 + 1 + ct_req->req.rsnn_nn.name_len);
666         ms_pkt->dseg_req_length = ms_pkt->req_bytecount;
667
668         /* Execute MS IOCB */
669         rval = qla2x00_issue_iocb(ha, ha->ms_iocb, ha->ms_iocb_dma,
670             sizeof(ms_iocb_entry_t));
671         if (rval != QLA_SUCCESS) {
672                 /*EMPTY*/
673                 DEBUG2_3(printk("scsi(%ld): RSNN_NN issue IOCB failed (%d).\n",
674                     ha->host_no, rval));
675         } else if (qla2x00_chk_ms_status(ha, ms_pkt, ct_rsp, "RSNN_NN") !=
676             QLA_SUCCESS) {
677                 rval = QLA_FUNCTION_FAILED;
678         } else {
679                 DEBUG2(printk("scsi(%ld): RSNN_NN exiting normally.\n",
680                     ha->host_no));
681         }
682
683         return (rval);
684 }
685
686 /**
687  * qla2x00_prep_sns_cmd() - Prepare common SNS command request fields for query.
688  * @ha: HA context
689  * @cmd: GS command
690  * @scmd_len: Subcommand length
691  * @data_size: response size in bytes
692  *
693  * Returns a pointer to the @ha's sns_cmd.
694  */
695 static inline struct sns_cmd_pkt *
696 qla2x00_prep_sns_cmd(scsi_qla_host_t *ha, uint16_t cmd, uint16_t scmd_len,
697     uint16_t data_size)
698 {
699         uint16_t                wc;
700         struct sns_cmd_pkt      *sns_cmd;
701
702         sns_cmd = ha->sns_cmd;
703         memset(sns_cmd, 0, sizeof(struct sns_cmd_pkt));
704         wc = data_size / 2;                     /* Size in 16bit words. */
705         sns_cmd->p.cmd.buffer_length = cpu_to_le16(wc);
706         sns_cmd->p.cmd.buffer_address[0] = cpu_to_le32(LSD(ha->sns_cmd_dma));
707         sns_cmd->p.cmd.buffer_address[1] = cpu_to_le32(MSD(ha->sns_cmd_dma));
708         sns_cmd->p.cmd.subcommand_length = cpu_to_le16(scmd_len);
709         sns_cmd->p.cmd.subcommand = cpu_to_le16(cmd);
710         wc = (data_size - 16) / 4;              /* Size in 32bit words. */
711         sns_cmd->p.cmd.size = cpu_to_le16(wc);
712
713         return (sns_cmd);
714 }
715
716 /**
717  * qla2x00_sns_ga_nxt() - SNS scan for fabric devices via GA_NXT command.
718  * @ha: HA context
719  * @fcport: fcport entry to updated
720  *
721  * This command uses the old Exectute SNS Command mailbox routine.
722  *
723  * Returns 0 on success.
724  */
725 static int
726 qla2x00_sns_ga_nxt(scsi_qla_host_t *ha, fc_port_t *fcport)
727 {
728         int             rval;
729
730         struct sns_cmd_pkt      *sns_cmd;
731
732         /* Issue GA_NXT. */
733         /* Prepare SNS command request. */
734         sns_cmd = qla2x00_prep_sns_cmd(ha, GA_NXT_CMD, GA_NXT_SNS_SCMD_LEN,
735             GA_NXT_SNS_DATA_SIZE);
736
737         /* Prepare SNS command arguments -- port_id. */
738         sns_cmd->p.cmd.param[0] = fcport->d_id.b.al_pa;
739         sns_cmd->p.cmd.param[1] = fcport->d_id.b.area;
740         sns_cmd->p.cmd.param[2] = fcport->d_id.b.domain;
741
742         /* Execute SNS command. */
743         rval = qla2x00_send_sns(ha, ha->sns_cmd_dma, GA_NXT_SNS_CMD_SIZE / 2,
744             sizeof(struct sns_cmd_pkt));
745         if (rval != QLA_SUCCESS) {
746                 /*EMPTY*/
747                 DEBUG2_3(printk("scsi(%ld): GA_NXT Send SNS failed (%d).\n",
748                     ha->host_no, rval));
749         } else if (sns_cmd->p.gan_data[8] != 0x80 ||
750             sns_cmd->p.gan_data[9] != 0x02) {
751                 DEBUG2_3(printk("scsi(%ld): GA_NXT failed, rejected request, "
752                     "ga_nxt_rsp:\n", ha->host_no));
753                 DEBUG2_3(qla2x00_dump_buffer(sns_cmd->p.gan_data, 16));
754                 rval = QLA_FUNCTION_FAILED;
755         } else {
756                 /* Populate fc_port_t entry. */
757                 fcport->d_id.b.domain = sns_cmd->p.gan_data[17];
758                 fcport->d_id.b.area = sns_cmd->p.gan_data[18];
759                 fcport->d_id.b.al_pa = sns_cmd->p.gan_data[19];
760
761                 memcpy(fcport->node_name, &sns_cmd->p.gan_data[284], WWN_SIZE);
762                 memcpy(fcport->port_name, &sns_cmd->p.gan_data[20], WWN_SIZE);
763
764                 if (sns_cmd->p.gan_data[16] != NS_N_PORT_TYPE &&
765                     sns_cmd->p.gan_data[16] != NS_NL_PORT_TYPE)
766                         fcport->d_id.b.domain = 0xf0;
767
768                 DEBUG2_3(printk("scsi(%ld): GA_NXT entry - "
769                     "nn %02x%02x%02x%02x%02x%02x%02x%02x "
770                     "pn %02x%02x%02x%02x%02x%02x%02x%02x "
771                     "portid=%02x%02x%02x.\n",
772                     ha->host_no,
773                     fcport->node_name[0], fcport->node_name[1],
774                     fcport->node_name[2], fcport->node_name[3],
775                     fcport->node_name[4], fcport->node_name[5],
776                     fcport->node_name[6], fcport->node_name[7],
777                     fcport->port_name[0], fcport->port_name[1],
778                     fcport->port_name[2], fcport->port_name[3],
779                     fcport->port_name[4], fcport->port_name[5],
780                     fcport->port_name[6], fcport->port_name[7],
781                     fcport->d_id.b.domain, fcport->d_id.b.area,
782                     fcport->d_id.b.al_pa));
783         }
784
785         return (rval);
786 }
787
788 /**
789  * qla2x00_sns_gid_pt() - SNS scan for fabric devices via GID_PT command.
790  * @ha: HA context
791  * @list: switch info entries to populate
792  *
793  * This command uses the old Exectute SNS Command mailbox routine.
794  *
795  * NOTE: Non-Nx_Ports are not requested.
796  *
797  * Returns 0 on success.
798  */
799 static int
800 qla2x00_sns_gid_pt(scsi_qla_host_t *ha, sw_info_t *list)
801 {
802         int             rval;
803
804         uint16_t        i;
805         uint8_t         *entry;
806         struct sns_cmd_pkt      *sns_cmd;
807
808         /* Issue GID_PT. */
809         /* Prepare SNS command request. */
810         sns_cmd = qla2x00_prep_sns_cmd(ha, GID_PT_CMD, GID_PT_SNS_SCMD_LEN,
811             GID_PT_SNS_DATA_SIZE);
812
813         /* Prepare SNS command arguments -- port_type. */
814         sns_cmd->p.cmd.param[0] = NS_NX_PORT_TYPE;
815
816         /* Execute SNS command. */
817         rval = qla2x00_send_sns(ha, ha->sns_cmd_dma, GID_PT_SNS_CMD_SIZE / 2,
818             sizeof(struct sns_cmd_pkt));
819         if (rval != QLA_SUCCESS) {
820                 /*EMPTY*/
821                 DEBUG2_3(printk("scsi(%ld): GID_PT Send SNS failed (%d).\n",
822                     ha->host_no, rval));
823         } else if (sns_cmd->p.gid_data[8] != 0x80 ||
824             sns_cmd->p.gid_data[9] != 0x02) {
825                 DEBUG2_3(printk("scsi(%ld): GID_PT failed, rejected request, "
826                     "gid_rsp:\n", ha->host_no));
827                 DEBUG2_3(qla2x00_dump_buffer(sns_cmd->p.gid_data, 16));
828                 rval = QLA_FUNCTION_FAILED;
829         } else {
830                 /* Set port IDs in switch info list. */
831                 for (i = 0; i < MAX_FIBRE_DEVICES; i++) {
832                         entry = &sns_cmd->p.gid_data[(i * 4) + 16];
833                         list[i].d_id.b.domain = entry[1];
834                         list[i].d_id.b.area = entry[2];
835                         list[i].d_id.b.al_pa = entry[3];
836
837                         /* Last one exit. */
838                         if (entry[0] & BIT_7) {
839                                 list[i].d_id.b.rsvd_1 = entry[0];
840                                 break;
841                         }
842                 }
843
844                 /*
845                  * If we've used all available slots, then the switch is
846                  * reporting back more devices that we can handle with this
847                  * single call.  Return a failed status, and let GA_NXT handle
848                  * the overload.
849                  */
850                 if (i == MAX_FIBRE_DEVICES)
851                         rval = QLA_FUNCTION_FAILED;
852         }
853
854         return (rval);
855 }
856
857 /**
858  * qla2x00_sns_gpn_id() - SNS Get Port Name (GPN_ID) query.
859  * @ha: HA context
860  * @list: switch info entries to populate
861  *
862  * This command uses the old Exectute SNS Command mailbox routine.
863  *
864  * Returns 0 on success.
865  */
866 static int
867 qla2x00_sns_gpn_id(scsi_qla_host_t *ha, sw_info_t *list)
868 {
869         int             rval;
870
871         uint16_t        i;
872         struct sns_cmd_pkt      *sns_cmd;
873
874         for (i = 0; i < MAX_FIBRE_DEVICES; i++) {
875                 /* Issue GPN_ID */
876                 /* Prepare SNS command request. */
877                 sns_cmd = qla2x00_prep_sns_cmd(ha, GPN_ID_CMD,
878                     GPN_ID_SNS_SCMD_LEN, GPN_ID_SNS_DATA_SIZE);
879
880                 /* Prepare SNS command arguments -- port_id. */
881                 sns_cmd->p.cmd.param[0] = list[i].d_id.b.al_pa;
882                 sns_cmd->p.cmd.param[1] = list[i].d_id.b.area;
883                 sns_cmd->p.cmd.param[2] = list[i].d_id.b.domain;
884
885                 /* Execute SNS command. */
886                 rval = qla2x00_send_sns(ha, ha->sns_cmd_dma,
887                     GPN_ID_SNS_CMD_SIZE / 2, sizeof(struct sns_cmd_pkt));
888                 if (rval != QLA_SUCCESS) {
889                         /*EMPTY*/
890                         DEBUG2_3(printk("scsi(%ld): GPN_ID Send SNS failed "
891                             "(%d).\n", ha->host_no, rval));
892                 } else if (sns_cmd->p.gpn_data[8] != 0x80 ||
893                     sns_cmd->p.gpn_data[9] != 0x02) {
894                         DEBUG2_3(printk("scsi(%ld): GPN_ID failed, rejected "
895                             "request, gpn_rsp:\n", ha->host_no));
896                         DEBUG2_3(qla2x00_dump_buffer(sns_cmd->p.gpn_data, 16));
897                         rval = QLA_FUNCTION_FAILED;
898                 } else {
899                         /* Save portname */
900                         memcpy(list[i].port_name, &sns_cmd->p.gpn_data[16],
901                             WWN_SIZE);
902                 }
903
904                 /* Last device exit. */
905                 if (list[i].d_id.b.rsvd_1 != 0)
906                         break;
907         }
908
909         return (rval);
910 }
911
912 /**
913  * qla2x00_sns_gnn_id() - SNS Get Node Name (GNN_ID) query.
914  * @ha: HA context
915  * @list: switch info entries to populate
916  *
917  * This command uses the old Exectute SNS Command mailbox routine.
918  *
919  * Returns 0 on success.
920  */
921 static int
922 qla2x00_sns_gnn_id(scsi_qla_host_t *ha, sw_info_t *list)
923 {
924         int             rval;
925
926         uint16_t        i;
927         struct sns_cmd_pkt      *sns_cmd;
928
929         for (i = 0; i < MAX_FIBRE_DEVICES; i++) {
930                 /* Issue GNN_ID */
931                 /* Prepare SNS command request. */
932                 sns_cmd = qla2x00_prep_sns_cmd(ha, GNN_ID_CMD,
933                     GNN_ID_SNS_SCMD_LEN, GNN_ID_SNS_DATA_SIZE);
934
935                 /* Prepare SNS command arguments -- port_id. */
936                 sns_cmd->p.cmd.param[0] = list[i].d_id.b.al_pa;
937                 sns_cmd->p.cmd.param[1] = list[i].d_id.b.area;
938                 sns_cmd->p.cmd.param[2] = list[i].d_id.b.domain;
939
940                 /* Execute SNS command. */
941                 rval = qla2x00_send_sns(ha, ha->sns_cmd_dma,
942                     GNN_ID_SNS_CMD_SIZE / 2, sizeof(struct sns_cmd_pkt));
943                 if (rval != QLA_SUCCESS) {
944                         /*EMPTY*/
945                         DEBUG2_3(printk("scsi(%ld): GNN_ID Send SNS failed "
946                             "(%d).\n", ha->host_no, rval));
947                 } else if (sns_cmd->p.gnn_data[8] != 0x80 ||
948                     sns_cmd->p.gnn_data[9] != 0x02) {
949                         DEBUG2_3(printk("scsi(%ld): GNN_ID failed, rejected "
950                             "request, gnn_rsp:\n", ha->host_no));
951                         DEBUG2_3(qla2x00_dump_buffer(sns_cmd->p.gnn_data, 16));
952                         rval = QLA_FUNCTION_FAILED;
953                 } else {
954                         /* Save nodename */
955                         memcpy(list[i].node_name, &sns_cmd->p.gnn_data[16],
956                             WWN_SIZE);
957
958                         DEBUG2_3(printk("scsi(%ld): GID_PT entry - "
959                             "nn %02x%02x%02x%02x%02x%02x%02x%02x "
960                             "pn %02x%02x%02x%02x%02x%02x%02x%02x "
961                             "portid=%02x%02x%02x.\n",
962                             ha->host_no,
963                             list[i].node_name[0], list[i].node_name[1],
964                             list[i].node_name[2], list[i].node_name[3],
965                             list[i].node_name[4], list[i].node_name[5],
966                             list[i].node_name[6], list[i].node_name[7],
967                             list[i].port_name[0], list[i].port_name[1],
968                             list[i].port_name[2], list[i].port_name[3],
969                             list[i].port_name[4], list[i].port_name[5],
970                             list[i].port_name[6], list[i].port_name[7],
971                             list[i].d_id.b.domain, list[i].d_id.b.area,
972                             list[i].d_id.b.al_pa));
973                 }
974
975                 /* Last device exit. */
976                 if (list[i].d_id.b.rsvd_1 != 0)
977                         break;
978         }
979
980         return (rval);
981 }
982
983 /**
984  * qla2x00_snd_rft_id() - SNS Register FC-4 TYPEs (RFT_ID) supported by the HBA.
985  * @ha: HA context
986  *
987  * This command uses the old Exectute SNS Command mailbox routine.
988  *
989  * Returns 0 on success.
990  */
991 static int
992 qla2x00_sns_rft_id(scsi_qla_host_t *ha)
993 {
994         int             rval;
995
996         struct sns_cmd_pkt      *sns_cmd;
997
998         /* Issue RFT_ID. */
999         /* Prepare SNS command request. */
1000         sns_cmd = qla2x00_prep_sns_cmd(ha, RFT_ID_CMD, RFT_ID_SNS_SCMD_LEN,
1001             RFT_ID_SNS_DATA_SIZE);
1002
1003         /* Prepare SNS command arguments -- port_id, FC-4 types */
1004         sns_cmd->p.cmd.param[0] = ha->d_id.b.al_pa;
1005         sns_cmd->p.cmd.param[1] = ha->d_id.b.area;
1006         sns_cmd->p.cmd.param[2] = ha->d_id.b.domain;
1007
1008         sns_cmd->p.cmd.param[5] = 0x01;                 /* FCP-3 */
1009
1010         /* Execute SNS command. */
1011         rval = qla2x00_send_sns(ha, ha->sns_cmd_dma, RFT_ID_SNS_CMD_SIZE / 2,
1012             sizeof(struct sns_cmd_pkt));
1013         if (rval != QLA_SUCCESS) {
1014                 /*EMPTY*/
1015                 DEBUG2_3(printk("scsi(%ld): RFT_ID Send SNS failed (%d).\n",
1016                     ha->host_no, rval));
1017         } else if (sns_cmd->p.rft_data[8] != 0x80 ||
1018             sns_cmd->p.rft_data[9] != 0x02) {
1019                 DEBUG2_3(printk("scsi(%ld): RFT_ID failed, rejected request, "
1020                     "rft_rsp:\n", ha->host_no));
1021                 DEBUG2_3(qla2x00_dump_buffer(sns_cmd->p.rft_data, 16));
1022                 rval = QLA_FUNCTION_FAILED;
1023         } else {
1024                 DEBUG2(printk("scsi(%ld): RFT_ID exiting normally.\n",
1025                     ha->host_no));
1026         }
1027
1028         return (rval);
1029 }
1030
1031 /**
1032  * qla2x00_sns_rnn_id() - SNS Register Node Name (RNN_ID) of the HBA.
1033  * HBA.
1034  * @ha: HA context
1035  *
1036  * This command uses the old Exectute SNS Command mailbox routine.
1037  *
1038  * Returns 0 on success.
1039  */
1040 static int
1041 qla2x00_sns_rnn_id(scsi_qla_host_t *ha)
1042 {
1043         int             rval;
1044
1045         struct sns_cmd_pkt      *sns_cmd;
1046
1047         /* Issue RNN_ID. */
1048         /* Prepare SNS command request. */
1049         sns_cmd = qla2x00_prep_sns_cmd(ha, RNN_ID_CMD, RNN_ID_SNS_SCMD_LEN,
1050             RNN_ID_SNS_DATA_SIZE);
1051
1052         /* Prepare SNS command arguments -- port_id, nodename. */
1053         sns_cmd->p.cmd.param[0] = ha->d_id.b.al_pa;
1054         sns_cmd->p.cmd.param[1] = ha->d_id.b.area;
1055         sns_cmd->p.cmd.param[2] = ha->d_id.b.domain;
1056
1057         sns_cmd->p.cmd.param[4] = ha->node_name[7];
1058         sns_cmd->p.cmd.param[5] = ha->node_name[6];
1059         sns_cmd->p.cmd.param[6] = ha->node_name[5];
1060         sns_cmd->p.cmd.param[7] = ha->node_name[4];
1061         sns_cmd->p.cmd.param[8] = ha->node_name[3];
1062         sns_cmd->p.cmd.param[9] = ha->node_name[2];
1063         sns_cmd->p.cmd.param[10] = ha->node_name[1];
1064         sns_cmd->p.cmd.param[11] = ha->node_name[0];
1065
1066         /* Execute SNS command. */
1067         rval = qla2x00_send_sns(ha, ha->sns_cmd_dma, RNN_ID_SNS_CMD_SIZE / 2,
1068             sizeof(struct sns_cmd_pkt));
1069         if (rval != QLA_SUCCESS) {
1070                 /*EMPTY*/
1071                 DEBUG2_3(printk("scsi(%ld): RNN_ID Send SNS failed (%d).\n",
1072                     ha->host_no, rval));
1073         } else if (sns_cmd->p.rnn_data[8] != 0x80 ||
1074             sns_cmd->p.rnn_data[9] != 0x02) {
1075                 DEBUG2_3(printk("scsi(%ld): RNN_ID failed, rejected request, "
1076                     "rnn_rsp:\n", ha->host_no));
1077                 DEBUG2_3(qla2x00_dump_buffer(sns_cmd->p.rnn_data, 16));
1078                 rval = QLA_FUNCTION_FAILED;
1079         } else {
1080                 DEBUG2(printk("scsi(%ld): RNN_ID exiting normally.\n",
1081                     ha->host_no));
1082         }
1083
1084         return (rval);
1085 }
1086
1087 /**
1088  * qla2x00_mgmt_svr_login() - Login to fabric Managment Service.
1089  * @ha: HA context
1090  *
1091  * Returns 0 on success.
1092  */
1093 static int
1094 qla2x00_mgmt_svr_login(scsi_qla_host_t *ha)
1095 {
1096         int ret;
1097         uint16_t mb[MAILBOX_REGISTER_COUNT];
1098
1099         ret = QLA_SUCCESS;
1100         if (ha->flags.management_server_logged_in)
1101                 return ret;
1102
1103         ha->isp_ops.fabric_login(ha, ha->mgmt_svr_loop_id, 0xff, 0xff, 0xfa,
1104             mb, BIT_1);
1105         if (mb[0] != MBS_COMMAND_COMPLETE) {
1106                 DEBUG2_13(printk("%s(%ld): Failed MANAGEMENT_SERVER login: "
1107                     "loop_id=%x mb[0]=%x mb[1]=%x mb[2]=%x mb[6]=%x mb[7]=%x\n",
1108                     __func__, ha->host_no, ha->mgmt_svr_loop_id, mb[0], mb[1],
1109                     mb[2], mb[6], mb[7]));
1110                 ret = QLA_FUNCTION_FAILED;
1111         } else
1112                 ha->flags.management_server_logged_in = 1;
1113
1114         return ret;
1115 }
1116
1117 /**
1118  * qla2x00_prep_ms_fdmi_iocb() - Prepare common MS IOCB fields for FDMI query.
1119  * @ha: HA context
1120  * @req_size: request size in bytes
1121  * @rsp_size: response size in bytes
1122  *
1123  * Returns a pointer to the @ha's ms_iocb.
1124  */
1125 void *
1126 qla2x00_prep_ms_fdmi_iocb(scsi_qla_host_t *ha, uint32_t req_size,
1127     uint32_t rsp_size)
1128 {
1129         ms_iocb_entry_t *ms_pkt;
1130
1131         ms_pkt = ha->ms_iocb;
1132         memset(ms_pkt, 0, sizeof(ms_iocb_entry_t));
1133
1134         ms_pkt->entry_type = MS_IOCB_TYPE;
1135         ms_pkt->entry_count = 1;
1136         SET_TARGET_ID(ha, ms_pkt->loop_id, ha->mgmt_svr_loop_id);
1137         ms_pkt->control_flags = __constant_cpu_to_le16(CF_READ | CF_HEAD_TAG);
1138         ms_pkt->timeout = __constant_cpu_to_le16(59);
1139         ms_pkt->cmd_dsd_count = __constant_cpu_to_le16(1);
1140         ms_pkt->total_dsd_count = __constant_cpu_to_le16(2);
1141         ms_pkt->rsp_bytecount = cpu_to_le32(rsp_size);
1142         ms_pkt->req_bytecount = cpu_to_le32(req_size);
1143
1144         ms_pkt->dseg_req_address[0] = cpu_to_le32(LSD(ha->ct_sns_dma));
1145         ms_pkt->dseg_req_address[1] = cpu_to_le32(MSD(ha->ct_sns_dma));
1146         ms_pkt->dseg_req_length = ms_pkt->req_bytecount;
1147
1148         ms_pkt->dseg_rsp_address[0] = cpu_to_le32(LSD(ha->ct_sns_dma));
1149         ms_pkt->dseg_rsp_address[1] = cpu_to_le32(MSD(ha->ct_sns_dma));
1150         ms_pkt->dseg_rsp_length = ms_pkt->rsp_bytecount;
1151
1152         return ms_pkt;
1153 }
1154
1155 /**
1156  * qla24xx_prep_ms_fdmi_iocb() - Prepare common MS IOCB fields for FDMI query.
1157  * @ha: HA context
1158  * @req_size: request size in bytes
1159  * @rsp_size: response size in bytes
1160  *
1161  * Returns a pointer to the @ha's ms_iocb.
1162  */
1163 void *
1164 qla24xx_prep_ms_fdmi_iocb(scsi_qla_host_t *ha, uint32_t req_size,
1165     uint32_t rsp_size)
1166 {
1167         struct ct_entry_24xx *ct_pkt;
1168
1169         ct_pkt = (struct ct_entry_24xx *)ha->ms_iocb;
1170         memset(ct_pkt, 0, sizeof(struct ct_entry_24xx));
1171
1172         ct_pkt->entry_type = CT_IOCB_TYPE;
1173         ct_pkt->entry_count = 1;
1174         ct_pkt->nport_handle = cpu_to_le16(ha->mgmt_svr_loop_id);
1175         ct_pkt->timeout = __constant_cpu_to_le16(59);
1176         ct_pkt->cmd_dsd_count = __constant_cpu_to_le16(1);
1177         ct_pkt->rsp_dsd_count = __constant_cpu_to_le16(1);
1178         ct_pkt->rsp_byte_count = cpu_to_le32(rsp_size);
1179         ct_pkt->cmd_byte_count = cpu_to_le32(req_size);
1180
1181         ct_pkt->dseg_0_address[0] = cpu_to_le32(LSD(ha->ct_sns_dma));
1182         ct_pkt->dseg_0_address[1] = cpu_to_le32(MSD(ha->ct_sns_dma));
1183         ct_pkt->dseg_0_len = ct_pkt->cmd_byte_count;
1184
1185         ct_pkt->dseg_1_address[0] = cpu_to_le32(LSD(ha->ct_sns_dma));
1186         ct_pkt->dseg_1_address[1] = cpu_to_le32(MSD(ha->ct_sns_dma));
1187         ct_pkt->dseg_1_len = ct_pkt->rsp_byte_count;
1188
1189         return ct_pkt;
1190 }
1191
1192 static inline ms_iocb_entry_t *
1193 qla2x00_update_ms_fdmi_iocb(scsi_qla_host_t *ha, uint32_t req_size)
1194 {
1195         ms_iocb_entry_t *ms_pkt = ha->ms_iocb;
1196         struct ct_entry_24xx *ct_pkt = (struct ct_entry_24xx *)ha->ms_iocb;
1197
1198         if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
1199                 ct_pkt->cmd_byte_count = cpu_to_le32(req_size);
1200                 ct_pkt->dseg_0_len = ct_pkt->cmd_byte_count;
1201         } else {
1202                 ms_pkt->req_bytecount = cpu_to_le32(req_size);
1203                 ms_pkt->dseg_req_length = ms_pkt->req_bytecount;
1204         }
1205
1206         return ms_pkt;
1207 }
1208
1209 /**
1210  * qla2x00_prep_ct_req() - Prepare common CT request fields for SNS query.
1211  * @ct_req: CT request buffer
1212  * @cmd: GS command
1213  * @rsp_size: response size in bytes
1214  *
1215  * Returns a pointer to the intitialized @ct_req.
1216  */
1217 static inline struct ct_sns_req *
1218 qla2x00_prep_ct_fdmi_req(struct ct_sns_req *ct_req, uint16_t cmd,
1219     uint16_t rsp_size)
1220 {
1221         memset(ct_req, 0, sizeof(struct ct_sns_pkt));
1222
1223         ct_req->header.revision = 0x01;
1224         ct_req->header.gs_type = 0xFA;
1225         ct_req->header.gs_subtype = 0x10;
1226         ct_req->command = cpu_to_be16(cmd);
1227         ct_req->max_rsp_size = cpu_to_be16((rsp_size - 16) / 4);
1228
1229         return ct_req;
1230 }
1231
1232 /**
1233  * qla2x00_fdmi_rhba() -
1234  * @ha: HA context
1235  *
1236  * Returns 0 on success.
1237  */
1238 static int
1239 qla2x00_fdmi_rhba(scsi_qla_host_t *ha)
1240 {
1241         int rval, alen;
1242         uint32_t size, sn;
1243
1244         ms_iocb_entry_t *ms_pkt;
1245         struct ct_sns_req *ct_req;
1246         struct ct_sns_rsp *ct_rsp;
1247         uint8_t *entries;
1248         struct ct_fdmi_hba_attr *eiter;
1249
1250         /* Issue RHBA */
1251         /* Prepare common MS IOCB */
1252         /*   Request size adjusted after CT preparation */
1253         ms_pkt = ha->isp_ops.prep_ms_fdmi_iocb(ha, 0, RHBA_RSP_SIZE);
1254
1255         /* Prepare CT request */
1256         ct_req = qla2x00_prep_ct_fdmi_req(&ha->ct_sns->p.req, RHBA_CMD,
1257             RHBA_RSP_SIZE);
1258         ct_rsp = &ha->ct_sns->p.rsp;
1259
1260         /* Prepare FDMI command arguments -- attribute block, attributes. */
1261         memcpy(ct_req->req.rhba.hba_identifier, ha->port_name, WWN_SIZE);
1262         ct_req->req.rhba.entry_count = __constant_cpu_to_be32(1);
1263         memcpy(ct_req->req.rhba.port_name, ha->port_name, WWN_SIZE);
1264         size = 2 * WWN_SIZE + 4 + 4;
1265
1266         /* Attributes */
1267         ct_req->req.rhba.attrs.count =
1268             __constant_cpu_to_be32(FDMI_HBA_ATTR_COUNT);
1269         entries = ct_req->req.rhba.hba_identifier;
1270
1271         /* Nodename. */
1272         eiter = (struct ct_fdmi_hba_attr *) (entries + size);
1273         eiter->type = __constant_cpu_to_be16(FDMI_HBA_NODE_NAME);
1274         eiter->len = __constant_cpu_to_be16(4 + WWN_SIZE);
1275         memcpy(eiter->a.node_name, ha->node_name, WWN_SIZE);
1276         size += 4 + WWN_SIZE;
1277
1278         DEBUG13(printk("%s(%ld): NODENAME=%02x%02x%02x%02x%02x%02x%02x%02x.\n",
1279             __func__, ha->host_no,
1280             eiter->a.node_name[0], eiter->a.node_name[1], eiter->a.node_name[2],
1281             eiter->a.node_name[3], eiter->a.node_name[4], eiter->a.node_name[5],
1282             eiter->a.node_name[6], eiter->a.node_name[7]));
1283
1284         /* Manufacturer. */
1285         eiter = (struct ct_fdmi_hba_attr *) (entries + size);
1286         eiter->type = __constant_cpu_to_be16(FDMI_HBA_MANUFACTURER);
1287         strcpy(eiter->a.manufacturer, "QLogic Corporation");
1288         alen = strlen(eiter->a.manufacturer);
1289         alen += (alen & 3) ? (4 - (alen & 3)) : 4;
1290         eiter->len = cpu_to_be16(4 + alen);
1291         size += 4 + alen;
1292
1293         DEBUG13(printk("%s(%ld): MANUFACTURER=%s.\n", __func__, ha->host_no,
1294             eiter->a.manufacturer));
1295
1296         /* Serial number. */
1297         eiter = (struct ct_fdmi_hba_attr *) (entries + size);
1298         eiter->type = __constant_cpu_to_be16(FDMI_HBA_SERIAL_NUMBER);
1299         sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
1300         sprintf(eiter->a.serial_num, "%c%05d", 'A' + sn / 100000, sn % 100000);
1301         alen = strlen(eiter->a.serial_num);
1302         alen += (alen & 3) ? (4 - (alen & 3)) : 4;
1303         eiter->len = cpu_to_be16(4 + alen);
1304         size += 4 + alen;
1305
1306         DEBUG13(printk("%s(%ld): SERIALNO=%s.\n", __func__, ha->host_no,
1307             eiter->a.serial_num));
1308
1309         /* Model name. */
1310         eiter = (struct ct_fdmi_hba_attr *) (entries + size);
1311         eiter->type = __constant_cpu_to_be16(FDMI_HBA_MODEL);
1312         strcpy(eiter->a.model, ha->model_number);
1313         alen = strlen(eiter->a.model);
1314         alen += (alen & 3) ? (4 - (alen & 3)) : 4;
1315         eiter->len = cpu_to_be16(4 + alen);
1316         size += 4 + alen;
1317
1318         DEBUG13(printk("%s(%ld): MODEL_NAME=%s.\n", __func__, ha->host_no,
1319             eiter->a.model));
1320
1321         /* Model description. */
1322         eiter = (struct ct_fdmi_hba_attr *) (entries + size);
1323         eiter->type = __constant_cpu_to_be16(FDMI_HBA_MODEL_DESCRIPTION);
1324         if (ha->model_desc)
1325                 strncpy(eiter->a.model_desc, ha->model_desc, 80);
1326         alen = strlen(eiter->a.model_desc);
1327         alen += (alen & 3) ? (4 - (alen & 3)) : 4;
1328         eiter->len = cpu_to_be16(4 + alen);
1329         size += 4 + alen;
1330
1331         DEBUG13(printk("%s(%ld): MODEL_DESC=%s.\n", __func__, ha->host_no,
1332             eiter->a.model_desc));
1333
1334         /* Hardware version. */
1335         eiter = (struct ct_fdmi_hba_attr *) (entries + size);
1336         eiter->type = __constant_cpu_to_be16(FDMI_HBA_HARDWARE_VERSION);
1337         strcpy(eiter->a.hw_version, ha->adapter_id);
1338         alen = strlen(eiter->a.hw_version);
1339         alen += (alen & 3) ? (4 - (alen & 3)) : 4;
1340         eiter->len = cpu_to_be16(4 + alen);
1341         size += 4 + alen;
1342
1343         DEBUG13(printk("%s(%ld): HARDWAREVER=%s.\n", __func__, ha->host_no,
1344             eiter->a.hw_version));
1345
1346         /* Driver version. */
1347         eiter = (struct ct_fdmi_hba_attr *) (entries + size);
1348         eiter->type = __constant_cpu_to_be16(FDMI_HBA_DRIVER_VERSION);
1349         strcpy(eiter->a.driver_version, qla2x00_version_str);
1350         alen = strlen(eiter->a.driver_version);
1351         alen += (alen & 3) ? (4 - (alen & 3)) : 4;
1352         eiter->len = cpu_to_be16(4 + alen);
1353         size += 4 + alen;
1354
1355         DEBUG13(printk("%s(%ld): DRIVERVER=%s.\n", __func__, ha->host_no,
1356             eiter->a.driver_version));
1357
1358         /* Option ROM version. */
1359         eiter = (struct ct_fdmi_hba_attr *) (entries + size);
1360         eiter->type = __constant_cpu_to_be16(FDMI_HBA_OPTION_ROM_VERSION);
1361         strcpy(eiter->a.orom_version, "0.00");
1362         alen = strlen(eiter->a.orom_version);
1363         alen += (alen & 3) ? (4 - (alen & 3)) : 4;
1364         eiter->len = cpu_to_be16(4 + alen);
1365         size += 4 + alen;
1366
1367         DEBUG13(printk("%s(%ld): OPTROMVER=%s.\n", __func__, ha->host_no,
1368             eiter->a.orom_version));
1369
1370         /* Firmware version */
1371         eiter = (struct ct_fdmi_hba_attr *) (entries + size);
1372         eiter->type = __constant_cpu_to_be16(FDMI_HBA_FIRMWARE_VERSION);
1373         ha->isp_ops.fw_version_str(ha, eiter->a.fw_version);
1374         alen = strlen(eiter->a.fw_version);
1375         alen += (alen & 3) ? (4 - (alen & 3)) : 4;
1376         eiter->len = cpu_to_be16(4 + alen);
1377         size += 4 + alen;
1378
1379         DEBUG13(printk("%s(%ld): FIRMWAREVER=%s.\n", __func__, ha->host_no,
1380             eiter->a.fw_version));
1381
1382         /* Update MS request size. */
1383         qla2x00_update_ms_fdmi_iocb(ha, size + 16);
1384
1385         DEBUG13(printk("%s(%ld): RHBA identifier="
1386             "%02x%02x%02x%02x%02x%02x%02x%02x size=%d.\n", __func__,
1387             ha->host_no, ct_req->req.rhba.hba_identifier[0],
1388             ct_req->req.rhba.hba_identifier[1],
1389             ct_req->req.rhba.hba_identifier[2],
1390             ct_req->req.rhba.hba_identifier[3],
1391             ct_req->req.rhba.hba_identifier[4],
1392             ct_req->req.rhba.hba_identifier[5],
1393             ct_req->req.rhba.hba_identifier[6],
1394             ct_req->req.rhba.hba_identifier[7], size));
1395         DEBUG13(qla2x00_dump_buffer(entries, size));
1396
1397         /* Execute MS IOCB */
1398         rval = qla2x00_issue_iocb(ha, ha->ms_iocb, ha->ms_iocb_dma,
1399             sizeof(ms_iocb_entry_t));
1400         if (rval != QLA_SUCCESS) {
1401                 /*EMPTY*/
1402                 DEBUG2_3(printk("scsi(%ld): RHBA issue IOCB failed (%d).\n",
1403                     ha->host_no, rval));
1404         } else if (qla2x00_chk_ms_status(ha, ms_pkt, ct_rsp, "RHBA") !=
1405             QLA_SUCCESS) {
1406                 rval = QLA_FUNCTION_FAILED;
1407                 if (ct_rsp->header.reason_code == CT_REASON_CANNOT_PERFORM &&
1408                     ct_rsp->header.explanation_code ==
1409                     CT_EXPL_ALREADY_REGISTERED) {
1410                         DEBUG2_13(printk("%s(%ld): HBA already registered.\n",
1411                             __func__, ha->host_no));
1412                         rval = QLA_ALREADY_REGISTERED;
1413                 }
1414         } else {
1415                 DEBUG2(printk("scsi(%ld): RHBA exiting normally.\n",
1416                     ha->host_no));
1417         }
1418
1419         return rval;
1420 }
1421
1422 /**
1423  * qla2x00_fdmi_dhba() -
1424  * @ha: HA context
1425  *
1426  * Returns 0 on success.
1427  */
1428 static int
1429 qla2x00_fdmi_dhba(scsi_qla_host_t *ha)
1430 {
1431         int rval;
1432
1433         ms_iocb_entry_t *ms_pkt;
1434         struct ct_sns_req *ct_req;
1435         struct ct_sns_rsp *ct_rsp;
1436
1437         /* Issue RPA */
1438         /* Prepare common MS IOCB */
1439         ms_pkt = ha->isp_ops.prep_ms_fdmi_iocb(ha, DHBA_REQ_SIZE,
1440             DHBA_RSP_SIZE);
1441
1442         /* Prepare CT request */
1443         ct_req = qla2x00_prep_ct_fdmi_req(&ha->ct_sns->p.req, DHBA_CMD,
1444             DHBA_RSP_SIZE);
1445         ct_rsp = &ha->ct_sns->p.rsp;
1446
1447         /* Prepare FDMI command arguments -- portname. */
1448         memcpy(ct_req->req.dhba.port_name, ha->port_name, WWN_SIZE);
1449
1450         DEBUG13(printk("%s(%ld): DHBA portname="
1451             "%02x%02x%02x%02x%02x%02x%02x%02x.\n", __func__, ha->host_no,
1452             ct_req->req.dhba.port_name[0], ct_req->req.dhba.port_name[1],
1453             ct_req->req.dhba.port_name[2], ct_req->req.dhba.port_name[3],
1454             ct_req->req.dhba.port_name[4], ct_req->req.dhba.port_name[5],
1455             ct_req->req.dhba.port_name[6], ct_req->req.dhba.port_name[7]));
1456
1457         /* Execute MS IOCB */
1458         rval = qla2x00_issue_iocb(ha, ha->ms_iocb, ha->ms_iocb_dma,
1459             sizeof(ms_iocb_entry_t));
1460         if (rval != QLA_SUCCESS) {
1461                 /*EMPTY*/
1462                 DEBUG2_3(printk("scsi(%ld): DHBA issue IOCB failed (%d).\n",
1463                     ha->host_no, rval));
1464         } else if (qla2x00_chk_ms_status(ha, ms_pkt, ct_rsp, "DHBA") !=
1465             QLA_SUCCESS) {
1466                 rval = QLA_FUNCTION_FAILED;
1467         } else {
1468                 DEBUG2(printk("scsi(%ld): DHBA exiting normally.\n",
1469                     ha->host_no));
1470         }
1471
1472         return rval;
1473 }
1474
1475 /**
1476  * qla2x00_fdmi_rpa() -
1477  * @ha: HA context
1478  *
1479  * Returns 0 on success.
1480  */
1481 static int
1482 qla2x00_fdmi_rpa(scsi_qla_host_t *ha)
1483 {
1484         int rval, alen;
1485         uint32_t size, max_frame_size;
1486
1487         ms_iocb_entry_t *ms_pkt;
1488         struct ct_sns_req *ct_req;
1489         struct ct_sns_rsp *ct_rsp;
1490         uint8_t *entries;
1491         struct ct_fdmi_port_attr *eiter;
1492         struct init_cb_24xx *icb24 = (struct init_cb_24xx *)ha->init_cb;
1493
1494         /* Issue RPA */
1495         /* Prepare common MS IOCB */
1496         /*   Request size adjusted after CT preparation */
1497         ms_pkt = ha->isp_ops.prep_ms_fdmi_iocb(ha, 0, RPA_RSP_SIZE);
1498
1499         /* Prepare CT request */
1500         ct_req = qla2x00_prep_ct_fdmi_req(&ha->ct_sns->p.req, RPA_CMD,
1501             RPA_RSP_SIZE);
1502         ct_rsp = &ha->ct_sns->p.rsp;
1503
1504         /* Prepare FDMI command arguments -- attribute block, attributes. */
1505         memcpy(ct_req->req.rpa.port_name, ha->port_name, WWN_SIZE);
1506         size = WWN_SIZE + 4;
1507
1508         /* Attributes */
1509         ct_req->req.rpa.attrs.count =
1510             __constant_cpu_to_be32(FDMI_PORT_ATTR_COUNT);
1511         entries = ct_req->req.rpa.port_name;
1512
1513         /* FC4 types. */
1514         eiter = (struct ct_fdmi_port_attr *) (entries + size);
1515         eiter->type = __constant_cpu_to_be16(FDMI_PORT_FC4_TYPES);
1516         eiter->len = __constant_cpu_to_be16(4 + 32);
1517         eiter->a.fc4_types[2] = 0x01;
1518         size += 4 + 32;
1519
1520         DEBUG13(printk("%s(%ld): FC4_TYPES=%02x %02x.\n", __func__, ha->host_no,
1521             eiter->a.fc4_types[2], eiter->a.fc4_types[1]));
1522
1523         /* Supported speed. */
1524         eiter = (struct ct_fdmi_port_attr *) (entries + size);
1525         eiter->type = __constant_cpu_to_be16(FDMI_PORT_SUPPORT_SPEED);
1526         eiter->len = __constant_cpu_to_be16(4 + 4);
1527         if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
1528                 eiter->a.sup_speed = __constant_cpu_to_be32(4);
1529         else if (IS_QLA23XX(ha))
1530                 eiter->a.sup_speed = __constant_cpu_to_be32(2);
1531         else
1532                 eiter->a.sup_speed = __constant_cpu_to_be32(1);
1533         size += 4 + 4;
1534
1535         DEBUG13(printk("%s(%ld): SUPPORTED_SPEED=%x.\n", __func__, ha->host_no,
1536             eiter->a.sup_speed));
1537
1538         /* Current speed. */
1539         eiter = (struct ct_fdmi_port_attr *) (entries + size);
1540         eiter->type = __constant_cpu_to_be16(FDMI_PORT_CURRENT_SPEED);
1541         eiter->len = __constant_cpu_to_be16(4 + 4);
1542         switch (ha->link_data_rate) {
1543         case 0:
1544                 eiter->a.cur_speed = __constant_cpu_to_be32(1);
1545                 break;
1546         case 1:
1547                 eiter->a.cur_speed = __constant_cpu_to_be32(2);
1548                 break;
1549         case 3:
1550                 eiter->a.cur_speed = __constant_cpu_to_be32(4);
1551                 break;
1552         }
1553         size += 4 + 4;
1554
1555         DEBUG13(printk("%s(%ld): CURRENT_SPEED=%x.\n", __func__, ha->host_no,
1556             eiter->a.cur_speed));
1557
1558         /* Max frame size. */
1559         eiter = (struct ct_fdmi_port_attr *) (entries + size);
1560         eiter->type = __constant_cpu_to_be16(FDMI_PORT_MAX_FRAME_SIZE);
1561         eiter->len = __constant_cpu_to_be16(4 + 4);
1562         max_frame_size = IS_QLA24XX(ha) || IS_QLA54XX(ha) ?
1563                 (uint32_t) icb24->frame_payload_size:
1564                 (uint32_t) ha->init_cb->frame_payload_size;
1565         eiter->a.max_frame_size = cpu_to_be32(max_frame_size);
1566         size += 4 + 4;
1567
1568         DEBUG13(printk("%s(%ld): MAX_FRAME_SIZE=%x.\n", __func__, ha->host_no,
1569             eiter->a.max_frame_size));
1570
1571         /* OS device name. */
1572         eiter = (struct ct_fdmi_port_attr *) (entries + size);
1573         eiter->type = __constant_cpu_to_be16(FDMI_PORT_OS_DEVICE_NAME);
1574         sprintf(eiter->a.os_dev_name, "/proc/scsi/qla2xxx/%ld", ha->host_no);
1575         alen = strlen(eiter->a.os_dev_name);
1576         alen += (alen & 3) ? (4 - (alen & 3)) : 4;
1577         eiter->len = cpu_to_be16(4 + alen);
1578         size += 4 + alen;
1579
1580         DEBUG13(printk("%s(%ld): OS_DEVICE_NAME=%s.\n", __func__, ha->host_no,
1581             eiter->a.os_dev_name));
1582
1583         /* Hostname. */
1584         if (strlen(fc_host_system_hostname(ha->host))) {
1585                 eiter = (struct ct_fdmi_port_attr *) (entries + size);
1586                 eiter->type = __constant_cpu_to_be16(FDMI_PORT_HOST_NAME);
1587                 snprintf(eiter->a.host_name, sizeof(eiter->a.host_name),
1588                     "%s", fc_host_system_hostname(ha->host));
1589                 alen = strlen(eiter->a.host_name);
1590                 alen += (alen & 3) ? (4 - (alen & 3)) : 4;
1591                 eiter->len = cpu_to_be16(4 + alen);
1592                 size += 4 + alen;
1593
1594                 DEBUG13(printk("%s(%ld): HOSTNAME=%s.\n", __func__,
1595                     ha->host_no, eiter->a.host_name));
1596         }
1597
1598         /* Update MS request size. */
1599         qla2x00_update_ms_fdmi_iocb(ha, size + 16);
1600
1601         DEBUG13(printk("%s(%ld): RPA portname="
1602             "%02x%02x%02x%02x%02x%02x%02x%02x size=%d.\n", __func__,
1603             ha->host_no, ct_req->req.rpa.port_name[0],
1604             ct_req->req.rpa.port_name[1], ct_req->req.rpa.port_name[2],
1605             ct_req->req.rpa.port_name[3], ct_req->req.rpa.port_name[4],
1606             ct_req->req.rpa.port_name[5], ct_req->req.rpa.port_name[6],
1607             ct_req->req.rpa.port_name[7], size));
1608         DEBUG13(qla2x00_dump_buffer(entries, size));
1609
1610         /* Execute MS IOCB */
1611         rval = qla2x00_issue_iocb(ha, ha->ms_iocb, ha->ms_iocb_dma,
1612             sizeof(ms_iocb_entry_t));
1613         if (rval != QLA_SUCCESS) {
1614                 /*EMPTY*/
1615                 DEBUG2_3(printk("scsi(%ld): RPA issue IOCB failed (%d).\n",
1616                     ha->host_no, rval));
1617         } else if (qla2x00_chk_ms_status(ha, ms_pkt, ct_rsp, "RPA") !=
1618             QLA_SUCCESS) {
1619                 rval = QLA_FUNCTION_FAILED;
1620         } else {
1621                 DEBUG2(printk("scsi(%ld): RPA exiting normally.\n",
1622                     ha->host_no));
1623         }
1624
1625         return rval;
1626 }
1627
1628 /**
1629  * qla2x00_fdmi_register() -
1630  * @ha: HA context
1631  *
1632  * Returns 0 on success.
1633  */
1634 int
1635 qla2x00_fdmi_register(scsi_qla_host_t *ha)
1636 {
1637         int rval;
1638
1639         rval = qla2x00_mgmt_svr_login(ha);
1640         if (rval)
1641                 return rval;
1642
1643         rval = qla2x00_fdmi_rhba(ha);
1644         if (rval) {
1645                 if (rval != QLA_ALREADY_REGISTERED)
1646                         return rval;
1647
1648                 rval = qla2x00_fdmi_dhba(ha);
1649                 if (rval)
1650                         return rval;
1651
1652                 rval = qla2x00_fdmi_rhba(ha);
1653                 if (rval)
1654                         return rval;
1655         }
1656         rval = qla2x00_fdmi_rpa(ha);
1657
1658         return rval;
1659 }
1660
1661 /**
1662  * qla2x00_gfpn_id() - SNS Get Fabric Port Name (GFPN_ID) query.
1663  * @ha: HA context
1664  * @list: switch info entries to populate
1665  *
1666  * Returns 0 on success.
1667  */
1668 int
1669 qla2x00_gfpn_id(scsi_qla_host_t *ha, sw_info_t *list)
1670 {
1671         int             rval;
1672         uint16_t        i;
1673
1674         ms_iocb_entry_t *ms_pkt;
1675         struct ct_sns_req       *ct_req;
1676         struct ct_sns_rsp       *ct_rsp;
1677
1678         if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha))
1679                 return QLA_FUNCTION_FAILED;
1680
1681         for (i = 0; i < MAX_FIBRE_DEVICES; i++) {
1682                 /* Issue GFPN_ID */
1683                 memset(list[i].fabric_port_name, 0, WWN_SIZE);
1684
1685                 /* Prepare common MS IOCB */
1686                 ms_pkt = qla2x00_prep_ms_iocb(ha, GFPN_ID_REQ_SIZE,
1687                     GFPN_ID_RSP_SIZE);
1688
1689                 /* Prepare CT request */
1690                 ct_req = qla2x00_prep_ct_req(&ha->ct_sns->p.req, GFPN_ID_CMD,
1691                     GFPN_ID_RSP_SIZE);
1692                 ct_rsp = &ha->ct_sns->p.rsp;
1693
1694                 /* Prepare CT arguments -- port_id */
1695                 ct_req->req.port_id.port_id[0] = list[i].d_id.b.domain;
1696                 ct_req->req.port_id.port_id[1] = list[i].d_id.b.area;
1697                 ct_req->req.port_id.port_id[2] = list[i].d_id.b.al_pa;
1698
1699                 /* Execute MS IOCB */
1700                 rval = qla2x00_issue_iocb(ha, ha->ms_iocb, ha->ms_iocb_dma,
1701                     sizeof(ms_iocb_entry_t));
1702                 if (rval != QLA_SUCCESS) {
1703                         /*EMPTY*/
1704                         DEBUG2_3(printk("scsi(%ld): GFPN_ID issue IOCB "
1705                             "failed (%d).\n", ha->host_no, rval));
1706                 } else if (qla2x00_chk_ms_status(ha, ms_pkt, ct_rsp,
1707                     "GFPN_ID") != QLA_SUCCESS) {
1708                         rval = QLA_FUNCTION_FAILED;
1709                 } else {
1710                         /* Save fabric portname */
1711                         memcpy(list[i].fabric_port_name,
1712                             ct_rsp->rsp.gfpn_id.port_name, WWN_SIZE);
1713                 }
1714
1715                 /* Last device exit. */
1716                 if (list[i].d_id.b.rsvd_1 != 0)
1717                         break;
1718         }
1719
1720         return (rval);
1721 }
1722
1723 static inline void *
1724 qla24xx_prep_ms_fm_iocb(scsi_qla_host_t *ha, uint32_t req_size,
1725     uint32_t rsp_size)
1726 {
1727         struct ct_entry_24xx *ct_pkt;
1728
1729         ct_pkt = (struct ct_entry_24xx *)ha->ms_iocb;
1730         memset(ct_pkt, 0, sizeof(struct ct_entry_24xx));
1731
1732         ct_pkt->entry_type = CT_IOCB_TYPE;
1733         ct_pkt->entry_count = 1;
1734         ct_pkt->nport_handle = cpu_to_le16(ha->mgmt_svr_loop_id);
1735         ct_pkt->timeout = __constant_cpu_to_le16(59);
1736         ct_pkt->cmd_dsd_count = __constant_cpu_to_le16(1);
1737         ct_pkt->rsp_dsd_count = __constant_cpu_to_le16(1);
1738         ct_pkt->rsp_byte_count = cpu_to_le32(rsp_size);
1739         ct_pkt->cmd_byte_count = cpu_to_le32(req_size);
1740
1741         ct_pkt->dseg_0_address[0] = cpu_to_le32(LSD(ha->ct_sns_dma));
1742         ct_pkt->dseg_0_address[1] = cpu_to_le32(MSD(ha->ct_sns_dma));
1743         ct_pkt->dseg_0_len = ct_pkt->cmd_byte_count;
1744
1745         ct_pkt->dseg_1_address[0] = cpu_to_le32(LSD(ha->ct_sns_dma));
1746         ct_pkt->dseg_1_address[1] = cpu_to_le32(MSD(ha->ct_sns_dma));
1747         ct_pkt->dseg_1_len = ct_pkt->rsp_byte_count;
1748
1749         return ct_pkt;
1750 }
1751
1752
1753 static inline struct ct_sns_req *
1754 qla24xx_prep_ct_fm_req(struct ct_sns_req *ct_req, uint16_t cmd,
1755     uint16_t rsp_size)
1756 {
1757         memset(ct_req, 0, sizeof(struct ct_sns_pkt));
1758
1759         ct_req->header.revision = 0x01;
1760         ct_req->header.gs_type = 0xFA;
1761         ct_req->header.gs_subtype = 0x01;
1762         ct_req->command = cpu_to_be16(cmd);
1763         ct_req->max_rsp_size = cpu_to_be16((rsp_size - 16) / 4);
1764
1765         return ct_req;
1766 }
1767
1768 /**
1769  * qla2x00_gpsc() - FCS Get Port Speed Capabilities (GPSC) query.
1770  * @ha: HA context
1771  * @list: switch info entries to populate
1772  *
1773  * Returns 0 on success.
1774  */
1775 int
1776 qla2x00_gpsc(scsi_qla_host_t *ha, sw_info_t *list)
1777 {
1778         int             rval;
1779         uint16_t        i;
1780
1781         ms_iocb_entry_t *ms_pkt;
1782         struct ct_sns_req       *ct_req;
1783         struct ct_sns_rsp       *ct_rsp;
1784
1785         if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha))
1786                 return QLA_FUNCTION_FAILED;
1787
1788         rval = qla2x00_mgmt_svr_login(ha);
1789         if (rval)
1790                 return rval;
1791
1792         for (i = 0; i < MAX_FIBRE_DEVICES; i++) {
1793                 /* Issue GFPN_ID */
1794                 list[i].fp_speeds = list[i].fp_speed = 0;
1795
1796                 /* Prepare common MS IOCB */
1797                 ms_pkt = qla24xx_prep_ms_fm_iocb(ha, GPSC_REQ_SIZE,
1798                     GPSC_RSP_SIZE);
1799
1800                 /* Prepare CT request */
1801                 ct_req = qla24xx_prep_ct_fm_req(&ha->ct_sns->p.req,
1802                     GPSC_CMD, GPSC_RSP_SIZE);
1803                 ct_rsp = &ha->ct_sns->p.rsp;
1804
1805                 /* Prepare CT arguments -- port_name */
1806                 memcpy(ct_req->req.gpsc.port_name, list[i].fabric_port_name,
1807                     WWN_SIZE);
1808
1809                 /* Execute MS IOCB */
1810                 rval = qla2x00_issue_iocb(ha, ha->ms_iocb, ha->ms_iocb_dma,
1811                     sizeof(ms_iocb_entry_t));
1812                 if (rval != QLA_SUCCESS) {
1813                         /*EMPTY*/
1814                         DEBUG2_3(printk("scsi(%ld): GPSC issue IOCB "
1815                             "failed (%d).\n", ha->host_no, rval));
1816                 } else if (qla2x00_chk_ms_status(ha, ms_pkt, ct_rsp,
1817                     "GPSC") != QLA_SUCCESS) {
1818                         rval = QLA_FUNCTION_FAILED;
1819                 } else {
1820                         /* Save portname */
1821                         list[i].fp_speeds = ct_rsp->rsp.gpsc.speeds;
1822                         list[i].fp_speed = ct_rsp->rsp.gpsc.speed;
1823
1824                         DEBUG2_3(printk("scsi(%ld): GPSC ext entry - "
1825                             "fpn %02x%02x%02x%02x%02x%02x%02x%02x speeds=%04x "
1826                             "speed=%04x.\n", ha->host_no,
1827                             list[i].fabric_port_name[0],
1828                             list[i].fabric_port_name[1],
1829                             list[i].fabric_port_name[2],
1830                             list[i].fabric_port_name[3],
1831                             list[i].fabric_port_name[4],
1832                             list[i].fabric_port_name[5],
1833                             list[i].fabric_port_name[6],
1834                             list[i].fabric_port_name[7],
1835                             be16_to_cpu(list[i].fp_speeds),
1836                             be16_to_cpu(list[i].fp_speed)));
1837                 }
1838
1839                 /* Last device exit. */
1840                 if (list[i].d_id.b.rsvd_1 != 0)
1841                         break;
1842         }
1843
1844         return (rval);
1845 }