xref: /openbmc/linux/drivers/scsi/be2iscsi/be_mgmt.c (revision a44e4f3a)
1 /*
2  * This file is part of the Emulex Linux Device Driver for Enterprise iSCSI
3  * Host Bus Adapters. Refer to the README file included with this package
4  * for driver version and adapter compatibility.
5  *
6  * Copyright (c) 2018 Broadcom. All Rights Reserved.
7  * The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of version 2 of the GNU General Public License as published
11  * by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful. ALL EXPRESS
14  * OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
15  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
16  * OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH
17  * DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
18  * See the GNU General Public License for more details, a copy of which
19  * can be found in the file COPYING included with this package.
20  *
21  * Contact Information:
22  * linux-drivers@broadcom.com
23  *
24  */
25 
26 #include <linux/bsg-lib.h>
27 #include <scsi/scsi_transport_iscsi.h>
28 #include <scsi/scsi_bsg_iscsi.h>
29 #include "be_mgmt.h"
30 #include "be_iscsi.h"
31 #include "be_main.h"
32 
33 unsigned int mgmt_vendor_specific_fw_cmd(struct be_ctrl_info *ctrl,
34 					 struct beiscsi_hba *phba,
35 					 struct bsg_job *job,
36 					 struct be_dma_mem *nonemb_cmd)
37 {
38 	struct be_mcc_wrb *wrb;
39 	struct be_sge *mcc_sge;
40 	unsigned int tag = 0;
41 	struct iscsi_bsg_request *bsg_req = job->request;
42 	struct be_bsg_vendor_cmd *req = nonemb_cmd->va;
43 	unsigned short region, sector_size, sector, offset;
44 
45 	nonemb_cmd->size = job->request_payload.payload_len;
46 	memset(nonemb_cmd->va, 0, nonemb_cmd->size);
47 	region =  bsg_req->rqst_data.h_vendor.vendor_cmd[1];
48 	sector_size =  bsg_req->rqst_data.h_vendor.vendor_cmd[2];
49 	sector =  bsg_req->rqst_data.h_vendor.vendor_cmd[3];
50 	offset =  bsg_req->rqst_data.h_vendor.vendor_cmd[4];
51 	req->region = region;
52 	req->sector = sector;
53 	req->offset = offset;
54 
55 	if (mutex_lock_interruptible(&ctrl->mbox_lock))
56 		return 0;
57 	switch (bsg_req->rqst_data.h_vendor.vendor_cmd[0]) {
58 	case BEISCSI_WRITE_FLASH:
59 		offset = sector * sector_size + offset;
60 		be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
61 				   OPCODE_COMMON_WRITE_FLASH, sizeof(*req));
62 		sg_copy_to_buffer(job->request_payload.sg_list,
63 				  job->request_payload.sg_cnt,
64 				  nonemb_cmd->va + offset, job->request_len);
65 		break;
66 	case BEISCSI_READ_FLASH:
67 		be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
68 			   OPCODE_COMMON_READ_FLASH, sizeof(*req));
69 		break;
70 	default:
71 		beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
72 			    "BG_%d : Unsupported cmd = 0x%x\n\n",
73 			    bsg_req->rqst_data.h_vendor.vendor_cmd[0]);
74 
75 		mutex_unlock(&ctrl->mbox_lock);
76 		return -EPERM;
77 	}
78 
79 	wrb = alloc_mcc_wrb(phba, &tag);
80 	if (!wrb) {
81 		mutex_unlock(&ctrl->mbox_lock);
82 		return 0;
83 	}
84 
85 	mcc_sge = nonembedded_sgl(wrb);
86 	be_wrb_hdr_prepare(wrb, nonemb_cmd->size, false,
87 			   job->request_payload.sg_cnt);
88 	mcc_sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd->dma));
89 	mcc_sge->pa_lo = cpu_to_le32(nonemb_cmd->dma & 0xFFFFFFFF);
90 	mcc_sge->len = cpu_to_le32(nonemb_cmd->size);
91 
92 	be_mcc_notify(phba, tag);
93 
94 	mutex_unlock(&ctrl->mbox_lock);
95 	return tag;
96 }
97 
98 /**
99  * mgmt_open_connection()- Establish a TCP CXN
100  * @dst_addr: Destination Address
101  * @beiscsi_ep: ptr to device endpoint struct
102  * @nonemb_cmd: ptr to memory allocated for command
103  *
104  * return
105  *	Success: Tag number of the MBX Command issued
106  *	Failure: Error code
107  **/
108 int mgmt_open_connection(struct beiscsi_hba *phba,
109 			 struct sockaddr *dst_addr,
110 			 struct beiscsi_endpoint *beiscsi_ep,
111 			 struct be_dma_mem *nonemb_cmd)
112 {
113 	struct hwi_controller *phwi_ctrlr;
114 	struct hwi_context_memory *phwi_context;
115 	struct sockaddr_in *daddr_in = (struct sockaddr_in *)dst_addr;
116 	struct sockaddr_in6 *daddr_in6 = (struct sockaddr_in6 *)dst_addr;
117 	struct be_ctrl_info *ctrl = &phba->ctrl;
118 	struct be_mcc_wrb *wrb;
119 	struct tcp_connect_and_offload_in_v1 *req;
120 	unsigned short def_hdr_id;
121 	unsigned short def_data_id;
122 	struct phys_addr template_address = { 0, 0 };
123 	struct phys_addr *ptemplate_address;
124 	unsigned int tag = 0;
125 	unsigned int i, ulp_num;
126 	unsigned short cid = beiscsi_ep->ep_cid;
127 	struct be_sge *sge;
128 
129 	if (dst_addr->sa_family != PF_INET && dst_addr->sa_family != PF_INET6) {
130 		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
131 			    "BG_%d : unknown addr family %d\n",
132 			    dst_addr->sa_family);
133 		return 0;
134 	}
135 
136 	phwi_ctrlr = phba->phwi_ctrlr;
137 	phwi_context = phwi_ctrlr->phwi_ctxt;
138 
139 	ulp_num = phwi_ctrlr->wrb_context[BE_GET_CRI_FROM_CID(cid)].ulp_num;
140 
141 	def_hdr_id = (unsigned short)HWI_GET_DEF_HDRQ_ID(phba, ulp_num);
142 	def_data_id = (unsigned short)HWI_GET_DEF_BUFQ_ID(phba, ulp_num);
143 
144 	ptemplate_address = &template_address;
145 	ISCSI_GET_PDU_TEMPLATE_ADDRESS(phba, ptemplate_address);
146 	if (mutex_lock_interruptible(&ctrl->mbox_lock))
147 		return 0;
148 	wrb = alloc_mcc_wrb(phba, &tag);
149 	if (!wrb) {
150 		mutex_unlock(&ctrl->mbox_lock);
151 		return 0;
152 	}
153 
154 	sge = nonembedded_sgl(wrb);
155 	req = nonemb_cmd->va;
156 	memset(req, 0, sizeof(*req));
157 
158 	be_wrb_hdr_prepare(wrb, nonemb_cmd->size, false, 1);
159 	be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
160 			   OPCODE_COMMON_ISCSI_TCP_CONNECT_AND_OFFLOAD,
161 			   nonemb_cmd->size);
162 	if (dst_addr->sa_family == PF_INET) {
163 		__be32 s_addr = daddr_in->sin_addr.s_addr;
164 		req->ip_address.ip_type = BEISCSI_IP_TYPE_V4;
165 		req->ip_address.addr[0] = s_addr & 0x000000ff;
166 		req->ip_address.addr[1] = (s_addr & 0x0000ff00) >> 8;
167 		req->ip_address.addr[2] = (s_addr & 0x00ff0000) >> 16;
168 		req->ip_address.addr[3] = (s_addr & 0xff000000) >> 24;
169 		req->tcp_port = ntohs(daddr_in->sin_port);
170 		beiscsi_ep->dst_addr = daddr_in->sin_addr.s_addr;
171 		beiscsi_ep->dst_tcpport = ntohs(daddr_in->sin_port);
172 		beiscsi_ep->ip_type = BEISCSI_IP_TYPE_V4;
173 	} else {
174 		/* else its PF_INET6 family */
175 		req->ip_address.ip_type = BEISCSI_IP_TYPE_V6;
176 		memcpy(&req->ip_address.addr,
177 		       &daddr_in6->sin6_addr.in6_u.u6_addr8, 16);
178 		req->tcp_port = ntohs(daddr_in6->sin6_port);
179 		beiscsi_ep->dst_tcpport = ntohs(daddr_in6->sin6_port);
180 		memcpy(&beiscsi_ep->dst6_addr,
181 		       &daddr_in6->sin6_addr.in6_u.u6_addr8, 16);
182 		beiscsi_ep->ip_type = BEISCSI_IP_TYPE_V6;
183 	}
184 	req->cid = cid;
185 	i = phba->nxt_cqid++;
186 	if (phba->nxt_cqid == phba->num_cpus)
187 		phba->nxt_cqid = 0;
188 	req->cq_id = phwi_context->be_cq[i].id;
189 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
190 		    "BG_%d : i=%d cq_id=%d\n", i, req->cq_id);
191 	req->defq_id = def_hdr_id;
192 	req->hdr_ring_id = def_hdr_id;
193 	req->data_ring_id = def_data_id;
194 	req->do_offload = 1;
195 	req->dataout_template_pa.lo = ptemplate_address->lo;
196 	req->dataout_template_pa.hi = ptemplate_address->hi;
197 	sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd->dma));
198 	sge->pa_lo = cpu_to_le32(nonemb_cmd->dma & 0xFFFFFFFF);
199 	sge->len = cpu_to_le32(nonemb_cmd->size);
200 
201 	if (!is_chip_be2_be3r(phba)) {
202 		req->hdr.version = MBX_CMD_VER1;
203 		req->tcp_window_size = 0x8000;
204 		req->tcp_window_scale_count = 2;
205 	}
206 
207 	be_mcc_notify(phba, tag);
208 	mutex_unlock(&ctrl->mbox_lock);
209 	return tag;
210 }
211 
212 /*
213  * beiscsi_exec_nemb_cmd()- execute non-embedded MBX cmd
214  * @phba: driver priv structure
215  * @nonemb_cmd: DMA address of the MBX command to be issued
216  * @cbfn: callback func on MCC completion
217  * @resp_buf: buffer to copy the MBX cmd response
218  * @resp_buf_len: response length to be copied
219  *
220  **/
221 static int beiscsi_exec_nemb_cmd(struct beiscsi_hba *phba,
222 				 struct be_dma_mem *nonemb_cmd,
223 				 void (*cbfn)(struct beiscsi_hba *,
224 					      unsigned int),
225 				 void *resp_buf, u32 resp_buf_len)
226 {
227 	struct be_ctrl_info *ctrl = &phba->ctrl;
228 	struct be_mcc_wrb *wrb;
229 	struct be_sge *sge;
230 	unsigned int tag;
231 	int rc = 0;
232 
233 	mutex_lock(&ctrl->mbox_lock);
234 	wrb = alloc_mcc_wrb(phba, &tag);
235 	if (!wrb) {
236 		mutex_unlock(&ctrl->mbox_lock);
237 		rc = -ENOMEM;
238 		goto free_cmd;
239 	}
240 
241 	sge = nonembedded_sgl(wrb);
242 	be_wrb_hdr_prepare(wrb, nonemb_cmd->size, false, 1);
243 	sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd->dma));
244 	sge->pa_lo = cpu_to_le32(lower_32_bits(nonemb_cmd->dma));
245 	sge->len = cpu_to_le32(nonemb_cmd->size);
246 
247 	if (cbfn) {
248 		struct be_dma_mem *tag_mem;
249 
250 		set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
251 		ctrl->ptag_state[tag].cbfn = cbfn;
252 		tag_mem = &phba->ctrl.ptag_state[tag].tag_mem_state;
253 
254 		/* store DMA mem to be freed in callback */
255 		tag_mem->size = nonemb_cmd->size;
256 		tag_mem->va = nonemb_cmd->va;
257 		tag_mem->dma = nonemb_cmd->dma;
258 	}
259 	be_mcc_notify(phba, tag);
260 	mutex_unlock(&ctrl->mbox_lock);
261 
262 	/* with cbfn set, its async cmd, don't wait */
263 	if (cbfn)
264 		return 0;
265 
266 	rc = beiscsi_mccq_compl_wait(phba, tag, NULL, nonemb_cmd);
267 
268 	/* copy the response, if any */
269 	if (resp_buf)
270 		memcpy(resp_buf, nonemb_cmd->va, resp_buf_len);
271 	/**
272 	 * This is special case of NTWK_GET_IF_INFO where the size of
273 	 * response is not known. beiscsi_if_get_info checks the return
274 	 * value to free DMA buffer.
275 	 */
276 	if (rc == -EAGAIN)
277 		return rc;
278 
279 	/**
280 	 * If FW is busy that is driver timed out, DMA buffer is saved with
281 	 * the tag, only when the cmd completes this buffer is freed.
282 	 */
283 	if (rc == -EBUSY)
284 		return rc;
285 
286 free_cmd:
287 	dma_free_coherent(&ctrl->pdev->dev, nonemb_cmd->size,
288 			    nonemb_cmd->va, nonemb_cmd->dma);
289 	return rc;
290 }
291 
292 static int beiscsi_prep_nemb_cmd(struct beiscsi_hba *phba,
293 				 struct be_dma_mem *cmd,
294 				 u8 subsystem, u8 opcode, u32 size)
295 {
296 	cmd->va = dma_alloc_coherent(&phba->ctrl.pdev->dev, size, &cmd->dma,
297 				     GFP_KERNEL);
298 	if (!cmd->va) {
299 		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
300 			    "BG_%d : Failed to allocate memory for if info\n");
301 		return -ENOMEM;
302 	}
303 	cmd->size = size;
304 	be_cmd_hdr_prepare(cmd->va, subsystem, opcode, size);
305 	beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
306 		    "BG_%d : subsystem %u cmd %u size %u\n",
307 		    subsystem, opcode, size);
308 	return 0;
309 }
310 
311 static void __beiscsi_eq_delay_compl(struct beiscsi_hba *phba, unsigned int tag)
312 {
313 	struct be_dma_mem *tag_mem;
314 
315 	/* status is ignored */
316 	__beiscsi_mcc_compl_status(phba, tag, NULL, NULL);
317 	tag_mem = &phba->ctrl.ptag_state[tag].tag_mem_state;
318 	if (tag_mem->size) {
319 		dma_free_coherent(&phba->pcidev->dev, tag_mem->size,
320 				    tag_mem->va, tag_mem->dma);
321 		tag_mem->size = 0;
322 	}
323 }
324 
325 int beiscsi_modify_eq_delay(struct beiscsi_hba *phba,
326 			    struct be_set_eqd *set_eqd, int num)
327 {
328 	struct be_cmd_req_modify_eq_delay *req;
329 	struct be_dma_mem nonemb_cmd;
330 	int i, rc;
331 
332 	rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_COMMON,
333 			OPCODE_COMMON_MODIFY_EQ_DELAY, sizeof(*req));
334 	if (rc)
335 		return rc;
336 
337 	req = nonemb_cmd.va;
338 	req->num_eq = cpu_to_le32(num);
339 	for (i = 0; i < num; i++) {
340 		req->delay[i].eq_id = cpu_to_le32(set_eqd[i].eq_id);
341 		req->delay[i].phase = 0;
342 		req->delay[i].delay_multiplier =
343 				cpu_to_le32(set_eqd[i].delay_multiplier);
344 	}
345 
346 	return beiscsi_exec_nemb_cmd(phba, &nonemb_cmd,
347 				     __beiscsi_eq_delay_compl, NULL, 0);
348 }
349 
350 /**
351  * beiscsi_get_initiator_name - read initiator name from flash
352  * @phba: device priv structure
353  * @name: buffer pointer
354  * @cfg: fetch user configured
355  *
356  */
357 int beiscsi_get_initiator_name(struct beiscsi_hba *phba, char *name, bool cfg)
358 {
359 	struct be_dma_mem nonemb_cmd;
360 	struct be_cmd_hba_name resp;
361 	struct be_cmd_hba_name *req;
362 	int rc;
363 
364 	rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI_INI,
365 			OPCODE_ISCSI_INI_CFG_GET_HBA_NAME, sizeof(resp));
366 	if (rc)
367 		return rc;
368 
369 	req = nonemb_cmd.va;
370 	if (cfg)
371 		req->hdr.version = 1;
372 	rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL,
373 				   &resp, sizeof(resp));
374 	if (rc) {
375 		beiscsi_log(phba, KERN_ERR,
376 			    BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
377 			    "BS_%d : Initiator Name MBX Failed\n");
378 		return rc;
379 	}
380 	rc = sprintf(name, "%s\n", resp.initiator_name);
381 	return rc;
382 }
383 
384 unsigned int beiscsi_if_get_handle(struct beiscsi_hba *phba)
385 {
386 	struct be_ctrl_info *ctrl = &phba->ctrl;
387 	struct be_mcc_wrb *wrb;
388 	struct be_cmd_get_all_if_id_req *req;
389 	struct be_cmd_get_all_if_id_req *pbe_allid;
390 	unsigned int tag;
391 	int status = 0;
392 
393 	if (mutex_lock_interruptible(&ctrl->mbox_lock))
394 		return -EINTR;
395 	wrb = alloc_mcc_wrb(phba, &tag);
396 	if (!wrb) {
397 		mutex_unlock(&ctrl->mbox_lock);
398 		return -ENOMEM;
399 	}
400 
401 	req = embedded_payload(wrb);
402 	be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
403 	be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
404 			   OPCODE_COMMON_ISCSI_NTWK_GET_ALL_IF_ID,
405 			   sizeof(*req));
406 	be_mcc_notify(phba, tag);
407 	mutex_unlock(&ctrl->mbox_lock);
408 
409 	status = beiscsi_mccq_compl_wait(phba, tag, &wrb, NULL);
410 	if (status) {
411 		beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
412 			    "BG_%d : %s failed: %d\n", __func__, status);
413 		return -EBUSY;
414 	}
415 
416 	pbe_allid = embedded_payload(wrb);
417 	/* we now support only one interface per function */
418 	phba->interface_handle = pbe_allid->if_hndl_list[0];
419 
420 	return status;
421 }
422 
423 static inline bool beiscsi_if_zero_ip(u8 *ip, u32 ip_type)
424 {
425 	u32 len;
426 
427 	len = (ip_type < BEISCSI_IP_TYPE_V6) ? IP_V4_LEN : IP_V6_LEN;
428 	while (len && !ip[len - 1])
429 		len--;
430 	return (len == 0);
431 }
432 
433 static int beiscsi_if_mod_gw(struct beiscsi_hba *phba,
434 			     u32 action, u32 ip_type, u8 *gw)
435 {
436 	struct be_cmd_set_def_gateway_req *req;
437 	struct be_dma_mem nonemb_cmd;
438 	int rt_val;
439 
440 	rt_val = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
441 			OPCODE_COMMON_ISCSI_NTWK_MODIFY_DEFAULT_GATEWAY,
442 			sizeof(*req));
443 	if (rt_val)
444 		return rt_val;
445 
446 	req = nonemb_cmd.va;
447 	req->action = action;
448 	req->ip_addr.ip_type = ip_type;
449 	memcpy(req->ip_addr.addr, gw,
450 	       (ip_type < BEISCSI_IP_TYPE_V6) ? IP_V4_LEN : IP_V6_LEN);
451 	return beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, NULL, 0);
452 }
453 
454 int beiscsi_if_set_gw(struct beiscsi_hba *phba, u32 ip_type, u8 *gw)
455 {
456 	struct be_cmd_get_def_gateway_resp gw_resp;
457 	int rt_val;
458 
459 	memset(&gw_resp, 0, sizeof(gw_resp));
460 	rt_val = beiscsi_if_get_gw(phba, ip_type, &gw_resp);
461 	if (rt_val) {
462 		beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
463 			    "BG_%d : Failed to Get Gateway Addr\n");
464 		return rt_val;
465 	}
466 
467 	if (!beiscsi_if_zero_ip(gw_resp.ip_addr.addr, ip_type)) {
468 		rt_val = beiscsi_if_mod_gw(phba, IP_ACTION_DEL, ip_type,
469 					   gw_resp.ip_addr.addr);
470 		if (rt_val) {
471 			beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
472 				    "BG_%d : Failed to clear Gateway Addr Set\n");
473 			return rt_val;
474 		}
475 	}
476 
477 	rt_val = beiscsi_if_mod_gw(phba, IP_ACTION_ADD, ip_type, gw);
478 	if (rt_val)
479 		beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
480 			    "BG_%d : Failed to Set Gateway Addr\n");
481 
482 	return rt_val;
483 }
484 
485 int beiscsi_if_get_gw(struct beiscsi_hba *phba, u32 ip_type,
486 		      struct be_cmd_get_def_gateway_resp *resp)
487 {
488 	struct be_cmd_get_def_gateway_req *req;
489 	struct be_dma_mem nonemb_cmd;
490 	int rc;
491 
492 	rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
493 			OPCODE_COMMON_ISCSI_NTWK_GET_DEFAULT_GATEWAY,
494 			sizeof(*resp));
495 	if (rc)
496 		return rc;
497 
498 	req = nonemb_cmd.va;
499 	req->ip_type = ip_type;
500 
501 	return beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL,
502 				     resp, sizeof(*resp));
503 }
504 
505 static int
506 beiscsi_if_clr_ip(struct beiscsi_hba *phba,
507 		  struct be_cmd_get_if_info_resp *if_info)
508 {
509 	struct be_cmd_set_ip_addr_req *req;
510 	struct be_dma_mem nonemb_cmd;
511 	int rc;
512 
513 	rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
514 			OPCODE_COMMON_ISCSI_NTWK_MODIFY_IP_ADDR,
515 			sizeof(*req));
516 	if (rc)
517 		return rc;
518 
519 	req = nonemb_cmd.va;
520 	req->ip_params.record_entry_count = 1;
521 	req->ip_params.ip_record.action = IP_ACTION_DEL;
522 	req->ip_params.ip_record.interface_hndl =
523 		phba->interface_handle;
524 	req->ip_params.ip_record.ip_addr.size_of_structure =
525 		sizeof(struct be_ip_addr_subnet_format);
526 	req->ip_params.ip_record.ip_addr.ip_type = if_info->ip_addr.ip_type;
527 	memcpy(req->ip_params.ip_record.ip_addr.addr,
528 	       if_info->ip_addr.addr,
529 	       sizeof(if_info->ip_addr.addr));
530 	memcpy(req->ip_params.ip_record.ip_addr.subnet_mask,
531 	       if_info->ip_addr.subnet_mask,
532 	       sizeof(if_info->ip_addr.subnet_mask));
533 	rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, NULL, 0);
534 	if (rc < 0 || req->ip_params.ip_record.status) {
535 		beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
536 			    "BG_%d : failed to clear IP: rc %d status %d\n",
537 			    rc, req->ip_params.ip_record.status);
538 	}
539 	return rc;
540 }
541 
542 static int
543 beiscsi_if_set_ip(struct beiscsi_hba *phba, u8 *ip,
544 		  u8 *subnet, u32 ip_type)
545 {
546 	struct be_cmd_set_ip_addr_req *req;
547 	struct be_dma_mem nonemb_cmd;
548 	uint32_t ip_len;
549 	int rc;
550 
551 	rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
552 			OPCODE_COMMON_ISCSI_NTWK_MODIFY_IP_ADDR,
553 			sizeof(*req));
554 	if (rc)
555 		return rc;
556 
557 	req = nonemb_cmd.va;
558 	req->ip_params.record_entry_count = 1;
559 	req->ip_params.ip_record.action = IP_ACTION_ADD;
560 	req->ip_params.ip_record.interface_hndl =
561 		phba->interface_handle;
562 	req->ip_params.ip_record.ip_addr.size_of_structure =
563 		sizeof(struct be_ip_addr_subnet_format);
564 	req->ip_params.ip_record.ip_addr.ip_type = ip_type;
565 	ip_len = (ip_type < BEISCSI_IP_TYPE_V6) ? IP_V4_LEN : IP_V6_LEN;
566 	memcpy(req->ip_params.ip_record.ip_addr.addr, ip, ip_len);
567 	if (subnet)
568 		memcpy(req->ip_params.ip_record.ip_addr.subnet_mask,
569 		       subnet, ip_len);
570 
571 	rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, NULL, 0);
572 	/**
573 	 * In some cases, host needs to look into individual record status
574 	 * even though FW reported success for that IOCTL.
575 	 */
576 	if (rc < 0 || req->ip_params.ip_record.status) {
577 		__beiscsi_log(phba, KERN_ERR,
578 			    "BG_%d : failed to set IP: rc %d status %d\n",
579 			    rc, req->ip_params.ip_record.status);
580 		if (req->ip_params.ip_record.status)
581 			rc = -EINVAL;
582 	}
583 	return rc;
584 }
585 
586 int beiscsi_if_en_static(struct beiscsi_hba *phba, u32 ip_type,
587 			 u8 *ip, u8 *subnet)
588 {
589 	struct be_cmd_get_if_info_resp *if_info;
590 	struct be_cmd_rel_dhcp_req *reldhcp;
591 	struct be_dma_mem nonemb_cmd;
592 	int rc;
593 
594 	rc = beiscsi_if_get_info(phba, ip_type, &if_info);
595 	if (rc)
596 		return rc;
597 
598 	if (if_info->dhcp_state) {
599 		rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd,
600 				CMD_SUBSYSTEM_ISCSI,
601 				OPCODE_COMMON_ISCSI_NTWK_REL_STATELESS_IP_ADDR,
602 				sizeof(*reldhcp));
603 		if (rc)
604 			goto exit;
605 
606 		reldhcp = nonemb_cmd.va;
607 		reldhcp->interface_hndl = phba->interface_handle;
608 		reldhcp->ip_type = ip_type;
609 		rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, NULL, 0);
610 		if (rc < 0) {
611 			beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
612 				    "BG_%d : failed to release existing DHCP: %d\n",
613 				    rc);
614 			goto exit;
615 		}
616 	}
617 
618 	/* first delete any IP set */
619 	if (!beiscsi_if_zero_ip(if_info->ip_addr.addr, ip_type)) {
620 		rc = beiscsi_if_clr_ip(phba, if_info);
621 		if (rc)
622 			goto exit;
623 	}
624 
625 	/* if ip == NULL then this is called just to release DHCP IP */
626 	if (ip)
627 		rc = beiscsi_if_set_ip(phba, ip, subnet, ip_type);
628 exit:
629 	kfree(if_info);
630 	return rc;
631 }
632 
633 int beiscsi_if_en_dhcp(struct beiscsi_hba *phba, u32 ip_type)
634 {
635 	struct be_cmd_get_def_gateway_resp gw_resp;
636 	struct be_cmd_get_if_info_resp *if_info;
637 	struct be_cmd_set_dhcp_req *dhcpreq;
638 	struct be_dma_mem nonemb_cmd;
639 	u8 *gw;
640 	int rc;
641 
642 	rc = beiscsi_if_get_info(phba, ip_type, &if_info);
643 	if (rc)
644 		return rc;
645 
646 	if (if_info->dhcp_state) {
647 		beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
648 				"BG_%d : DHCP Already Enabled\n");
649 		goto exit;
650 	}
651 
652 	/* first delete any IP set */
653 	if (!beiscsi_if_zero_ip(if_info->ip_addr.addr, ip_type)) {
654 		rc = beiscsi_if_clr_ip(phba, if_info);
655 		if (rc)
656 			goto exit;
657 	}
658 
659 	/* delete gateway settings if mode change is to DHCP */
660 	memset(&gw_resp, 0, sizeof(gw_resp));
661 	/* use ip_type provided in if_info */
662 	rc = beiscsi_if_get_gw(phba, if_info->ip_addr.ip_type, &gw_resp);
663 	if (rc) {
664 		beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
665 			    "BG_%d : Failed to Get Gateway Addr\n");
666 		goto exit;
667 	}
668 	gw = (u8 *)&gw_resp.ip_addr.addr;
669 	if (!beiscsi_if_zero_ip(gw, if_info->ip_addr.ip_type)) {
670 		rc = beiscsi_if_mod_gw(phba, IP_ACTION_DEL,
671 				       if_info->ip_addr.ip_type, gw);
672 		if (rc) {
673 			beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
674 				    "BG_%d : Failed to clear Gateway Addr Set\n");
675 			goto exit;
676 		}
677 	}
678 
679 	rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
680 			OPCODE_COMMON_ISCSI_NTWK_CONFIG_STATELESS_IP_ADDR,
681 			sizeof(*dhcpreq));
682 	if (rc)
683 		goto exit;
684 
685 	dhcpreq = nonemb_cmd.va;
686 	dhcpreq->flags = 1; /* 1 - blocking; 0 - non-blocking */
687 	dhcpreq->retry_count = 1;
688 	dhcpreq->interface_hndl = phba->interface_handle;
689 	dhcpreq->ip_type = ip_type;
690 	rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, NULL, 0);
691 
692 exit:
693 	kfree(if_info);
694 	return rc;
695 }
696 
697 /**
698  * beiscsi_if_set_vlan()- Issue and wait for CMD completion
699  * @phba: device private structure instance
700  * @vlan_tag: VLAN tag
701  *
702  * Issue the MBX Cmd and wait for the completion of the
703  * command.
704  *
705  * returns
706  *	Success: 0
707  *	Failure: Non-Xero Value
708  **/
709 int beiscsi_if_set_vlan(struct beiscsi_hba *phba, uint16_t vlan_tag)
710 {
711 	int rc;
712 	unsigned int tag;
713 
714 	tag = be_cmd_set_vlan(phba, vlan_tag);
715 	if (!tag) {
716 		beiscsi_log(phba, KERN_ERR,
717 			    (BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX),
718 			    "BG_%d : VLAN Setting Failed\n");
719 		return -EBUSY;
720 	}
721 
722 	rc = beiscsi_mccq_compl_wait(phba, tag, NULL, NULL);
723 	if (rc) {
724 		beiscsi_log(phba, KERN_ERR,
725 			    (BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX),
726 			    "BS_%d : VLAN MBX Cmd Failed\n");
727 		return rc;
728 	}
729 	return rc;
730 }
731 
732 
733 int beiscsi_if_get_info(struct beiscsi_hba *phba, int ip_type,
734 			struct be_cmd_get_if_info_resp **if_info)
735 {
736 	struct be_cmd_get_if_info_req *req;
737 	struct be_dma_mem nonemb_cmd;
738 	uint32_t ioctl_size = sizeof(struct be_cmd_get_if_info_resp);
739 	int rc;
740 
741 	rc = beiscsi_if_get_handle(phba);
742 	if (rc)
743 		return rc;
744 
745 	do {
746 		rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd,
747 				CMD_SUBSYSTEM_ISCSI,
748 				OPCODE_COMMON_ISCSI_NTWK_GET_IF_INFO,
749 				ioctl_size);
750 		if (rc)
751 			return rc;
752 
753 		req = nonemb_cmd.va;
754 		req->interface_hndl = phba->interface_handle;
755 		req->ip_type = ip_type;
756 
757 		/* Allocate memory for if_info */
758 		*if_info = kzalloc(ioctl_size, GFP_KERNEL);
759 		if (!*if_info) {
760 			beiscsi_log(phba, KERN_ERR,
761 				    BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
762 				    "BG_%d : Memory Allocation Failure\n");
763 
764 				/* Free the DMA memory for the IOCTL issuing */
765 				dma_free_coherent(&phba->ctrl.pdev->dev,
766 						    nonemb_cmd.size,
767 						    nonemb_cmd.va,
768 						    nonemb_cmd.dma);
769 				return -ENOMEM;
770 		}
771 
772 		rc =  beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, *if_info,
773 					    ioctl_size);
774 
775 		/* Check if the error is because of Insufficent_Buffer */
776 		if (rc == -EAGAIN) {
777 
778 			/* Get the new memory size */
779 			ioctl_size = ((struct be_cmd_resp_hdr *)
780 				      nonemb_cmd.va)->actual_resp_len;
781 			ioctl_size += sizeof(struct be_cmd_req_hdr);
782 
783 			/* Free the previous allocated DMA memory */
784 			dma_free_coherent(&phba->ctrl.pdev->dev, nonemb_cmd.size,
785 					    nonemb_cmd.va,
786 					    nonemb_cmd.dma);
787 
788 			/* Free the virtual memory */
789 			kfree(*if_info);
790 		} else
791 			break;
792 	} while (true);
793 	return rc;
794 }
795 
796 int mgmt_get_nic_conf(struct beiscsi_hba *phba,
797 		      struct be_cmd_get_nic_conf_resp *nic)
798 {
799 	struct be_dma_mem nonemb_cmd;
800 	int rc;
801 
802 	rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
803 			OPCODE_COMMON_ISCSI_NTWK_GET_NIC_CONFIG,
804 			sizeof(*nic));
805 	if (rc)
806 		return rc;
807 
808 	return beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL,
809 				     nic, sizeof(*nic));
810 }
811 
812 static void beiscsi_boot_process_compl(struct beiscsi_hba *phba,
813 				       unsigned int tag)
814 {
815 	struct be_cmd_get_boot_target_resp *boot_resp;
816 	struct be_cmd_resp_logout_fw_sess *logo_resp;
817 	struct be_cmd_get_session_resp *sess_resp;
818 	struct be_mcc_wrb *wrb;
819 	struct boot_struct *bs;
820 	int boot_work, status;
821 
822 	if (!test_bit(BEISCSI_HBA_BOOT_WORK, &phba->state)) {
823 		__beiscsi_log(phba, KERN_ERR,
824 			      "BG_%d : %s no boot work %lx\n",
825 			      __func__, phba->state);
826 		return;
827 	}
828 
829 	if (phba->boot_struct.tag != tag) {
830 		__beiscsi_log(phba, KERN_ERR,
831 			      "BG_%d : %s tag mismatch %d:%d\n",
832 			      __func__, tag, phba->boot_struct.tag);
833 		return;
834 	}
835 	bs = &phba->boot_struct;
836 	boot_work = 1;
837 	status = 0;
838 	switch (bs->action) {
839 	case BEISCSI_BOOT_REOPEN_SESS:
840 		status = __beiscsi_mcc_compl_status(phba, tag, NULL, NULL);
841 		if (!status)
842 			bs->action = BEISCSI_BOOT_GET_SHANDLE;
843 		else
844 			bs->retry--;
845 		break;
846 	case BEISCSI_BOOT_GET_SHANDLE:
847 		status = __beiscsi_mcc_compl_status(phba, tag, &wrb, NULL);
848 		if (!status) {
849 			boot_resp = embedded_payload(wrb);
850 			bs->s_handle = boot_resp->boot_session_handle;
851 		}
852 		if (bs->s_handle == BE_BOOT_INVALID_SHANDLE) {
853 			bs->action = BEISCSI_BOOT_REOPEN_SESS;
854 			bs->retry--;
855 		} else {
856 			bs->action = BEISCSI_BOOT_GET_SINFO;
857 		}
858 		break;
859 	case BEISCSI_BOOT_GET_SINFO:
860 		status = __beiscsi_mcc_compl_status(phba, tag, NULL,
861 						    &bs->nonemb_cmd);
862 		if (!status) {
863 			sess_resp = bs->nonemb_cmd.va;
864 			memcpy(&bs->boot_sess, &sess_resp->session_info,
865 			       sizeof(struct mgmt_session_info));
866 			bs->action = BEISCSI_BOOT_LOGOUT_SESS;
867 		} else {
868 			__beiscsi_log(phba, KERN_ERR,
869 				      "BG_%d : get boot session info error : 0x%x\n",
870 				      status);
871 			boot_work = 0;
872 		}
873 		dma_free_coherent(&phba->ctrl.pdev->dev, bs->nonemb_cmd.size,
874 				    bs->nonemb_cmd.va, bs->nonemb_cmd.dma);
875 		bs->nonemb_cmd.va = NULL;
876 		break;
877 	case BEISCSI_BOOT_LOGOUT_SESS:
878 		status = __beiscsi_mcc_compl_status(phba, tag, &wrb, NULL);
879 		if (!status) {
880 			logo_resp = embedded_payload(wrb);
881 			if (logo_resp->session_status != BE_SESS_STATUS_CLOSE) {
882 				__beiscsi_log(phba, KERN_ERR,
883 					      "BG_%d : FW boot session logout error : 0x%x\n",
884 					      logo_resp->session_status);
885 			}
886 		}
887 		/* continue to create boot_kset even if logout failed? */
888 		bs->action = BEISCSI_BOOT_CREATE_KSET;
889 		break;
890 	default:
891 		break;
892 	}
893 
894 	/* clear the tag so no other completion matches this tag */
895 	bs->tag = 0;
896 	if (!bs->retry) {
897 		boot_work = 0;
898 		__beiscsi_log(phba, KERN_ERR,
899 			      "BG_%d : failed to setup boot target: status %d action %d\n",
900 			      status, bs->action);
901 	}
902 	if (!boot_work) {
903 		/* wait for next event to start boot_work */
904 		clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
905 		return;
906 	}
907 	schedule_work(&phba->boot_work);
908 }
909 
910 /**
911  * beiscsi_boot_logout_sess()- Logout from boot FW session
912  * @phba: Device priv structure instance
913  *
914  * return
915  *	the TAG used for MBOX Command
916  *
917  */
918 unsigned int beiscsi_boot_logout_sess(struct beiscsi_hba *phba)
919 {
920 	struct be_ctrl_info *ctrl = &phba->ctrl;
921 	struct be_mcc_wrb *wrb;
922 	struct be_cmd_req_logout_fw_sess *req;
923 	unsigned int tag;
924 
925 	mutex_lock(&ctrl->mbox_lock);
926 	wrb = alloc_mcc_wrb(phba, &tag);
927 	if (!wrb) {
928 		mutex_unlock(&ctrl->mbox_lock);
929 		return 0;
930 	}
931 
932 	req = embedded_payload(wrb);
933 	be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
934 	be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
935 			   OPCODE_ISCSI_INI_SESSION_LOGOUT_TARGET,
936 			   sizeof(struct be_cmd_req_logout_fw_sess));
937 	/* Use the session handle copied into boot_sess */
938 	req->session_handle = phba->boot_struct.boot_sess.session_handle;
939 
940 	phba->boot_struct.tag = tag;
941 	set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
942 	ctrl->ptag_state[tag].cbfn = beiscsi_boot_process_compl;
943 
944 	be_mcc_notify(phba, tag);
945 	mutex_unlock(&ctrl->mbox_lock);
946 
947 	return tag;
948 }
949 /**
950  * beiscsi_boot_reopen_sess()- Reopen boot session
951  * @phba: Device priv structure instance
952  *
953  * return
954  *	the TAG used for MBOX Command
955  *
956  **/
957 unsigned int beiscsi_boot_reopen_sess(struct beiscsi_hba *phba)
958 {
959 	struct be_ctrl_info *ctrl = &phba->ctrl;
960 	struct be_mcc_wrb *wrb;
961 	struct be_cmd_reopen_session_req *req;
962 	unsigned int tag;
963 
964 	mutex_lock(&ctrl->mbox_lock);
965 	wrb = alloc_mcc_wrb(phba, &tag);
966 	if (!wrb) {
967 		mutex_unlock(&ctrl->mbox_lock);
968 		return 0;
969 	}
970 
971 	req = embedded_payload(wrb);
972 	be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
973 	be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
974 			   OPCODE_ISCSI_INI_DRIVER_REOPEN_ALL_SESSIONS,
975 			   sizeof(struct be_cmd_reopen_session_resp));
976 	req->reopen_type = BE_REOPEN_BOOT_SESSIONS;
977 	req->session_handle = BE_BOOT_INVALID_SHANDLE;
978 
979 	phba->boot_struct.tag = tag;
980 	set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
981 	ctrl->ptag_state[tag].cbfn = beiscsi_boot_process_compl;
982 
983 	be_mcc_notify(phba, tag);
984 	mutex_unlock(&ctrl->mbox_lock);
985 	return tag;
986 }
987 
988 
989 /**
990  * beiscsi_boot_get_sinfo()- Get boot session info
991  * @phba: device priv structure instance
992  *
993  * Fetches the boot_struct.s_handle info from FW.
994  * return
995  *	the TAG used for MBOX Command
996  *
997  **/
998 unsigned int beiscsi_boot_get_sinfo(struct beiscsi_hba *phba)
999 {
1000 	struct be_ctrl_info *ctrl = &phba->ctrl;
1001 	struct be_cmd_get_session_req *req;
1002 	struct be_dma_mem *nonemb_cmd;
1003 	struct be_mcc_wrb *wrb;
1004 	struct be_sge *sge;
1005 	unsigned int tag;
1006 
1007 	mutex_lock(&ctrl->mbox_lock);
1008 	wrb = alloc_mcc_wrb(phba, &tag);
1009 	if (!wrb) {
1010 		mutex_unlock(&ctrl->mbox_lock);
1011 		return 0;
1012 	}
1013 
1014 	nonemb_cmd = &phba->boot_struct.nonemb_cmd;
1015 	nonemb_cmd->size = sizeof(struct be_cmd_get_session_resp);
1016 	nonemb_cmd->va = dma_alloc_coherent(&phba->ctrl.pdev->dev,
1017 					      nonemb_cmd->size,
1018 					      &nonemb_cmd->dma,
1019 					      GFP_KERNEL);
1020 	if (!nonemb_cmd->va) {
1021 		mutex_unlock(&ctrl->mbox_lock);
1022 		return 0;
1023 	}
1024 
1025 	req = nonemb_cmd->va;
1026 	memset(req, 0, sizeof(*req));
1027 	sge = nonembedded_sgl(wrb);
1028 	be_wrb_hdr_prepare(wrb, sizeof(*req), false, 1);
1029 	be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
1030 			   OPCODE_ISCSI_INI_SESSION_GET_A_SESSION,
1031 			   sizeof(struct be_cmd_get_session_resp));
1032 	req->session_handle = phba->boot_struct.s_handle;
1033 	sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd->dma));
1034 	sge->pa_lo = cpu_to_le32(nonemb_cmd->dma & 0xFFFFFFFF);
1035 	sge->len = cpu_to_le32(nonemb_cmd->size);
1036 
1037 	phba->boot_struct.tag = tag;
1038 	set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
1039 	ctrl->ptag_state[tag].cbfn = beiscsi_boot_process_compl;
1040 
1041 	be_mcc_notify(phba, tag);
1042 	mutex_unlock(&ctrl->mbox_lock);
1043 	return tag;
1044 }
1045 
1046 unsigned int __beiscsi_boot_get_shandle(struct beiscsi_hba *phba, int async)
1047 {
1048 	struct be_ctrl_info *ctrl = &phba->ctrl;
1049 	struct be_mcc_wrb *wrb;
1050 	struct be_cmd_get_boot_target_req *req;
1051 	unsigned int tag;
1052 
1053 	mutex_lock(&ctrl->mbox_lock);
1054 	wrb = alloc_mcc_wrb(phba, &tag);
1055 	if (!wrb) {
1056 		mutex_unlock(&ctrl->mbox_lock);
1057 		return 0;
1058 	}
1059 
1060 	req = embedded_payload(wrb);
1061 	be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
1062 	be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
1063 			   OPCODE_ISCSI_INI_BOOT_GET_BOOT_TARGET,
1064 			   sizeof(struct be_cmd_get_boot_target_resp));
1065 
1066 	if (async) {
1067 		phba->boot_struct.tag = tag;
1068 		set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
1069 		ctrl->ptag_state[tag].cbfn = beiscsi_boot_process_compl;
1070 	}
1071 
1072 	be_mcc_notify(phba, tag);
1073 	mutex_unlock(&ctrl->mbox_lock);
1074 	return tag;
1075 }
1076 
1077 /**
1078  * beiscsi_boot_get_shandle()- Get boot session handle
1079  * @phba: device priv structure instance
1080  * @s_handle: session handle returned for boot session.
1081  *
1082  * return
1083  *	Success: 1
1084  *	Failure: negative
1085  *
1086  **/
1087 int beiscsi_boot_get_shandle(struct beiscsi_hba *phba, unsigned int *s_handle)
1088 {
1089 	struct be_cmd_get_boot_target_resp *boot_resp;
1090 	struct be_mcc_wrb *wrb;
1091 	unsigned int tag;
1092 	int rc;
1093 
1094 	*s_handle = BE_BOOT_INVALID_SHANDLE;
1095 	/* get configured boot session count and handle */
1096 	tag = __beiscsi_boot_get_shandle(phba, 0);
1097 	if (!tag) {
1098 		beiscsi_log(phba, KERN_ERR,
1099 			    BEISCSI_LOG_CONFIG | BEISCSI_LOG_INIT,
1100 			    "BG_%d : Getting Boot Target Info Failed\n");
1101 		return -EAGAIN;
1102 	}
1103 
1104 	rc = beiscsi_mccq_compl_wait(phba, tag, &wrb, NULL);
1105 	if (rc) {
1106 		beiscsi_log(phba, KERN_ERR,
1107 			    BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
1108 			    "BG_%d : MBX CMD get_boot_target Failed\n");
1109 		return -EBUSY;
1110 	}
1111 
1112 	boot_resp = embedded_payload(wrb);
1113 	/* check if there are any boot targets configured */
1114 	if (!boot_resp->boot_session_count) {
1115 		__beiscsi_log(phba, KERN_INFO,
1116 			      "BG_%d : No boot targets configured\n");
1117 		return -ENXIO;
1118 	}
1119 
1120 	/* only if FW has logged in to the boot target, s_handle is valid */
1121 	*s_handle = boot_resp->boot_session_handle;
1122 	return 1;
1123 }
1124 
1125 /**
1126  * beiscsi_drvr_ver_disp()- Display the driver Name and Version
1127  * @dev: ptr to device not used.
1128  * @attr: device attribute, not used.
1129  * @buf: contains formatted text driver name and version
1130  *
1131  * return
1132  * size of the formatted string
1133  **/
1134 ssize_t
1135 beiscsi_drvr_ver_disp(struct device *dev, struct device_attribute *attr,
1136 		       char *buf)
1137 {
1138 	return snprintf(buf, PAGE_SIZE, BE_NAME "\n");
1139 }
1140 
1141 /**
1142  * beiscsi_fw_ver_disp()- Display Firmware Version
1143  * @dev: ptr to device not used.
1144  * @attr: device attribute, not used.
1145  * @buf: contains formatted text Firmware version
1146  *
1147  * return
1148  * size of the formatted string
1149  **/
1150 ssize_t
1151 beiscsi_fw_ver_disp(struct device *dev, struct device_attribute *attr,
1152 		     char *buf)
1153 {
1154 	struct Scsi_Host *shost = class_to_shost(dev);
1155 	struct beiscsi_hba *phba = iscsi_host_priv(shost);
1156 
1157 	return snprintf(buf, PAGE_SIZE, "%s\n", phba->fw_ver_str);
1158 }
1159 
1160 /**
1161  * beiscsi_active_session_disp()- Display Sessions Active
1162  * @dev: ptr to device not used.
1163  * @attr: device attribute, not used.
1164  * @buf: contains formatted text Session Count
1165  *
1166  * return
1167  * size of the formatted string
1168  **/
1169 ssize_t
1170 beiscsi_active_session_disp(struct device *dev, struct device_attribute *attr,
1171 			 char *buf)
1172 {
1173 	struct Scsi_Host *shost = class_to_shost(dev);
1174 	struct beiscsi_hba *phba = iscsi_host_priv(shost);
1175 	uint16_t avlbl_cids = 0, ulp_num, len = 0, total_cids = 0;
1176 
1177 	for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
1178 		if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
1179 			avlbl_cids = BEISCSI_ULP_AVLBL_CID(phba, ulp_num);
1180 			total_cids = BEISCSI_GET_CID_COUNT(phba, ulp_num);
1181 			len += snprintf(buf+len, PAGE_SIZE - len,
1182 					"ULP%d : %d\n", ulp_num,
1183 					(total_cids - avlbl_cids));
1184 		} else
1185 			len += snprintf(buf+len, PAGE_SIZE - len,
1186 					"ULP%d : %d\n", ulp_num, 0);
1187 	}
1188 
1189 	return len;
1190 }
1191 
1192 /**
1193  * beiscsi_free_session_disp()- Display Avaliable Session
1194  * @dev: ptr to device not used.
1195  * @attr: device attribute, not used.
1196  * @buf: contains formatted text Session Count
1197  *
1198  * return
1199  * size of the formatted string
1200  **/
1201 ssize_t
1202 beiscsi_free_session_disp(struct device *dev, struct device_attribute *attr,
1203 		       char *buf)
1204 {
1205 	struct Scsi_Host *shost = class_to_shost(dev);
1206 	struct beiscsi_hba *phba = iscsi_host_priv(shost);
1207 	uint16_t ulp_num, len = 0;
1208 
1209 	for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
1210 		if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported))
1211 			len += snprintf(buf+len, PAGE_SIZE - len,
1212 					"ULP%d : %d\n", ulp_num,
1213 					BEISCSI_ULP_AVLBL_CID(phba, ulp_num));
1214 		else
1215 			len += snprintf(buf+len, PAGE_SIZE - len,
1216 					"ULP%d : %d\n", ulp_num, 0);
1217 	}
1218 
1219 	return len;
1220 }
1221 
1222 /**
1223  * beiscsi_adap_family_disp()- Display adapter family.
1224  * @dev: ptr to device to get priv structure
1225  * @attr: device attribute, not used.
1226  * @buf: contains formatted text driver name and version
1227  *
1228  * return
1229  * size of the formatted string
1230  **/
1231 ssize_t
1232 beiscsi_adap_family_disp(struct device *dev, struct device_attribute *attr,
1233 			  char *buf)
1234 {
1235 	uint16_t dev_id = 0;
1236 	struct Scsi_Host *shost = class_to_shost(dev);
1237 	struct beiscsi_hba *phba = iscsi_host_priv(shost);
1238 
1239 	dev_id = phba->pcidev->device;
1240 	switch (dev_id) {
1241 	case BE_DEVICE_ID1:
1242 	case OC_DEVICE_ID1:
1243 	case OC_DEVICE_ID2:
1244 		return snprintf(buf, PAGE_SIZE,
1245 				"Obsolete/Unsupported BE2 Adapter Family\n");
1246 		break;
1247 	case BE_DEVICE_ID2:
1248 	case OC_DEVICE_ID3:
1249 		return snprintf(buf, PAGE_SIZE, "BE3-R Adapter Family\n");
1250 		break;
1251 	case OC_SKH_ID1:
1252 		return snprintf(buf, PAGE_SIZE, "Skyhawk-R Adapter Family\n");
1253 		break;
1254 	default:
1255 		return snprintf(buf, PAGE_SIZE,
1256 				"Unknown Adapter Family: 0x%x\n", dev_id);
1257 		break;
1258 	}
1259 }
1260 
1261 /**
1262  * beiscsi_phys_port()- Display Physical Port Identifier
1263  * @dev: ptr to device not used.
1264  * @attr: device attribute, not used.
1265  * @buf: contains formatted text port identifier
1266  *
1267  * return
1268  * size of the formatted string
1269  **/
1270 ssize_t
1271 beiscsi_phys_port_disp(struct device *dev, struct device_attribute *attr,
1272 			 char *buf)
1273 {
1274 	struct Scsi_Host *shost = class_to_shost(dev);
1275 	struct beiscsi_hba *phba = iscsi_host_priv(shost);
1276 
1277 	return snprintf(buf, PAGE_SIZE, "Port Identifier : %u\n",
1278 			phba->fw_config.phys_port);
1279 }
1280 
1281 void beiscsi_offload_cxn_v0(struct beiscsi_offload_params *params,
1282 			     struct wrb_handle *pwrb_handle,
1283 			     struct be_mem_descriptor *mem_descr,
1284 			     struct hwi_wrb_context *pwrb_context)
1285 {
1286 	struct iscsi_wrb *pwrb = pwrb_handle->pwrb;
1287 
1288 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1289 		      max_send_data_segment_length, pwrb,
1290 		      params->dw[offsetof(struct amap_beiscsi_offload_params,
1291 		      max_send_data_segment_length) / 32]);
1292 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, type, pwrb,
1293 		      BE_TGT_CTX_UPDT_CMD);
1294 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1295 		      first_burst_length,
1296 		      pwrb,
1297 		      params->dw[offsetof(struct amap_beiscsi_offload_params,
1298 		      first_burst_length) / 32]);
1299 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, erl, pwrb,
1300 		      (params->dw[offsetof(struct amap_beiscsi_offload_params,
1301 		      erl) / 32] & OFFLD_PARAMS_ERL));
1302 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, dde, pwrb,
1303 		      (params->dw[offsetof(struct amap_beiscsi_offload_params,
1304 		       dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
1305 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, hde, pwrb,
1306 		      (params->dw[offsetof(struct amap_beiscsi_offload_params,
1307 		      hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
1308 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ir2t, pwrb,
1309 		      (params->dw[offsetof(struct amap_beiscsi_offload_params,
1310 		      ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
1311 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, imd, pwrb,
1312 		      (params->dw[offsetof(struct amap_beiscsi_offload_params,
1313 		      imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
1314 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, stat_sn,
1315 		      pwrb,
1316 		      (params->dw[offsetof(struct amap_beiscsi_offload_params,
1317 		      exp_statsn) / 32] + 1));
1318 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, wrb_idx,
1319 		      pwrb, pwrb_handle->wrb_index);
1320 
1321 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1322 		      max_burst_length, pwrb, params->dw[offsetof
1323 		      (struct amap_beiscsi_offload_params,
1324 		      max_burst_length) / 32]);
1325 
1326 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ptr2nextwrb,
1327 		      pwrb, pwrb_handle->wrb_index);
1328 	if (pwrb_context->plast_wrb)
1329 		AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1330 			      ptr2nextwrb,
1331 			      pwrb_context->plast_wrb,
1332 			      pwrb_handle->wrb_index);
1333 	pwrb_context->plast_wrb = pwrb;
1334 
1335 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1336 		      session_state, pwrb, 0);
1337 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, compltonack,
1338 		      pwrb, 1);
1339 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, notpredblq,
1340 		      pwrb, 0);
1341 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, mode, pwrb,
1342 		      0);
1343 
1344 	mem_descr += ISCSI_MEM_GLOBAL_HEADER;
1345 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1346 		      pad_buffer_addr_hi, pwrb,
1347 		      mem_descr->mem_array[0].bus_address.u.a32.address_hi);
1348 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1349 		      pad_buffer_addr_lo, pwrb,
1350 		      mem_descr->mem_array[0].bus_address.u.a32.address_lo);
1351 }
1352 
1353 void beiscsi_offload_cxn_v2(struct beiscsi_offload_params *params,
1354 			     struct wrb_handle *pwrb_handle,
1355 			     struct hwi_wrb_context *pwrb_context)
1356 {
1357 	struct iscsi_wrb *pwrb = pwrb_handle->pwrb;
1358 
1359 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1360 		      max_burst_length, pwrb, params->dw[offsetof
1361 		      (struct amap_beiscsi_offload_params,
1362 		      max_burst_length) / 32]);
1363 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1364 		      type, pwrb,
1365 		      BE_TGT_CTX_UPDT_CMD);
1366 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1367 		      ptr2nextwrb,
1368 		      pwrb, pwrb_handle->wrb_index);
1369 	if (pwrb_context->plast_wrb)
1370 		AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1371 			      ptr2nextwrb,
1372 			      pwrb_context->plast_wrb,
1373 			      pwrb_handle->wrb_index);
1374 	pwrb_context->plast_wrb = pwrb;
1375 
1376 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, wrb_idx,
1377 		      pwrb, pwrb_handle->wrb_index);
1378 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1379 		      max_send_data_segment_length, pwrb,
1380 		      params->dw[offsetof(struct amap_beiscsi_offload_params,
1381 		      max_send_data_segment_length) / 32]);
1382 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1383 		      first_burst_length, pwrb,
1384 		      params->dw[offsetof(struct amap_beiscsi_offload_params,
1385 		      first_burst_length) / 32]);
1386 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1387 		      max_recv_dataseg_len, pwrb,
1388 		      params->dw[offsetof(struct amap_beiscsi_offload_params,
1389 		      max_recv_data_segment_length) / 32]);
1390 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1391 		      max_cxns, pwrb, BEISCSI_MAX_CXNS);
1392 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, erl, pwrb,
1393 		      (params->dw[offsetof(struct amap_beiscsi_offload_params,
1394 		      erl) / 32] & OFFLD_PARAMS_ERL));
1395 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, dde, pwrb,
1396 		      (params->dw[offsetof(struct amap_beiscsi_offload_params,
1397 		      dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
1398 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, hde, pwrb,
1399 		      (params->dw[offsetof(struct amap_beiscsi_offload_params,
1400 		      hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
1401 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1402 		      ir2t, pwrb,
1403 		      (params->dw[offsetof(struct amap_beiscsi_offload_params,
1404 		      ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
1405 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, imd, pwrb,
1406 		      (params->dw[offsetof(struct amap_beiscsi_offload_params,
1407 		      imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
1408 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1409 		      data_seq_inorder,
1410 		      pwrb,
1411 		      (params->dw[offsetof(struct amap_beiscsi_offload_params,
1412 		      data_seq_inorder) / 32] &
1413 		      OFFLD_PARAMS_DATA_SEQ_INORDER) >> 6);
1414 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1415 		      pdu_seq_inorder,
1416 		      pwrb,
1417 		      (params->dw[offsetof(struct amap_beiscsi_offload_params,
1418 		      pdu_seq_inorder) / 32] &
1419 		      OFFLD_PARAMS_PDU_SEQ_INORDER) >> 7);
1420 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, max_r2t,
1421 		      pwrb,
1422 		      (params->dw[offsetof(struct amap_beiscsi_offload_params,
1423 		      max_r2t) / 32] &
1424 		      OFFLD_PARAMS_MAX_R2T) >> 8);
1425 	AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, stat_sn,
1426 		      pwrb,
1427 		     (params->dw[offsetof(struct amap_beiscsi_offload_params,
1428 		      exp_statsn) / 32] + 1));
1429 }
1430 
1431 unsigned int beiscsi_invalidate_cxn(struct beiscsi_hba *phba,
1432 				    struct beiscsi_endpoint *beiscsi_ep)
1433 {
1434 	struct be_invalidate_connection_params_in *req;
1435 	struct be_ctrl_info *ctrl = &phba->ctrl;
1436 	struct be_mcc_wrb *wrb;
1437 	unsigned int tag = 0;
1438 
1439 	mutex_lock(&ctrl->mbox_lock);
1440 	wrb = alloc_mcc_wrb(phba, &tag);
1441 	if (!wrb) {
1442 		mutex_unlock(&ctrl->mbox_lock);
1443 		return 0;
1444 	}
1445 
1446 	req = embedded_payload(wrb);
1447 	be_wrb_hdr_prepare(wrb, sizeof(union be_invalidate_connection_params),
1448 			   true, 0);
1449 	be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
1450 			   OPCODE_ISCSI_INI_DRIVER_INVALIDATE_CONNECTION,
1451 			   sizeof(*req));
1452 	req->session_handle = beiscsi_ep->fw_handle;
1453 	req->cid = beiscsi_ep->ep_cid;
1454 	if (beiscsi_ep->conn)
1455 		req->cleanup_type = BE_CLEANUP_TYPE_INVALIDATE;
1456 	else
1457 		req->cleanup_type = BE_CLEANUP_TYPE_ISSUE_TCP_RST;
1458 	/**
1459 	 * 0 - non-persistent targets
1460 	 * 1 - save session info on flash
1461 	 */
1462 	req->save_cfg = 0;
1463 	be_mcc_notify(phba, tag);
1464 	mutex_unlock(&ctrl->mbox_lock);
1465 	return tag;
1466 }
1467 
1468 unsigned int beiscsi_upload_cxn(struct beiscsi_hba *phba,
1469 				struct beiscsi_endpoint *beiscsi_ep)
1470 {
1471 	struct be_ctrl_info *ctrl = &phba->ctrl;
1472 	struct be_mcc_wrb *wrb;
1473 	struct be_tcp_upload_params_in *req;
1474 	unsigned int tag;
1475 
1476 	mutex_lock(&ctrl->mbox_lock);
1477 	wrb = alloc_mcc_wrb(phba, &tag);
1478 	if (!wrb) {
1479 		mutex_unlock(&ctrl->mbox_lock);
1480 		return 0;
1481 	}
1482 
1483 	req = embedded_payload(wrb);
1484 	be_wrb_hdr_prepare(wrb, sizeof(union be_tcp_upload_params), true, 0);
1485 	be_cmd_hdr_prepare(&req->hdr, CMD_COMMON_TCP_UPLOAD,
1486 			   OPCODE_COMMON_TCP_UPLOAD, sizeof(*req));
1487 	req->id = beiscsi_ep->ep_cid;
1488 	if (beiscsi_ep->conn)
1489 		req->upload_type = BE_UPLOAD_TYPE_GRACEFUL;
1490 	else
1491 		req->upload_type = BE_UPLOAD_TYPE_ABORT;
1492 	be_mcc_notify(phba, tag);
1493 	mutex_unlock(&ctrl->mbox_lock);
1494 	return tag;
1495 }
1496 
1497 int beiscsi_mgmt_invalidate_icds(struct beiscsi_hba *phba,
1498 				 struct invldt_cmd_tbl *inv_tbl,
1499 				 unsigned int nents)
1500 {
1501 	struct be_ctrl_info *ctrl = &phba->ctrl;
1502 	struct invldt_cmds_params_in *req;
1503 	struct be_dma_mem nonemb_cmd;
1504 	struct be_mcc_wrb *wrb;
1505 	unsigned int i, tag;
1506 	struct be_sge *sge;
1507 	int rc;
1508 
1509 	if (!nents || nents > BE_INVLDT_CMD_TBL_SZ)
1510 		return -EINVAL;
1511 
1512 	nonemb_cmd.size = sizeof(union be_invldt_cmds_params);
1513 	nonemb_cmd.va = dma_alloc_coherent(&phba->ctrl.pdev->dev,
1514 					   nonemb_cmd.size, &nonemb_cmd.dma,
1515 					   GFP_KERNEL);
1516 	if (!nonemb_cmd.va) {
1517 		beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
1518 			    "BM_%d : invldt_cmds_params alloc failed\n");
1519 		return -ENOMEM;
1520 	}
1521 
1522 	mutex_lock(&ctrl->mbox_lock);
1523 	wrb = alloc_mcc_wrb(phba, &tag);
1524 	if (!wrb) {
1525 		mutex_unlock(&ctrl->mbox_lock);
1526 		dma_free_coherent(&phba->ctrl.pdev->dev, nonemb_cmd.size,
1527 				    nonemb_cmd.va, nonemb_cmd.dma);
1528 		return -ENOMEM;
1529 	}
1530 
1531 	req = nonemb_cmd.va;
1532 	be_wrb_hdr_prepare(wrb, nonemb_cmd.size, false, 1);
1533 	be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
1534 			OPCODE_COMMON_ISCSI_ERROR_RECOVERY_INVALIDATE_COMMANDS,
1535 			sizeof(*req));
1536 	req->ref_handle = 0;
1537 	req->cleanup_type = CMD_ISCSI_COMMAND_INVALIDATE;
1538 	for (i = 0; i < nents; i++) {
1539 		req->table[i].icd = inv_tbl[i].icd;
1540 		req->table[i].cid = inv_tbl[i].cid;
1541 		req->icd_count++;
1542 	}
1543 	sge = nonembedded_sgl(wrb);
1544 	sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd.dma));
1545 	sge->pa_lo = cpu_to_le32(lower_32_bits(nonemb_cmd.dma));
1546 	sge->len = cpu_to_le32(nonemb_cmd.size);
1547 
1548 	be_mcc_notify(phba, tag);
1549 	mutex_unlock(&ctrl->mbox_lock);
1550 
1551 	rc = beiscsi_mccq_compl_wait(phba, tag, NULL, &nonemb_cmd);
1552 	if (rc != -EBUSY)
1553 		dma_free_coherent(&phba->ctrl.pdev->dev, nonemb_cmd.size,
1554 				    nonemb_cmd.va, nonemb_cmd.dma);
1555 	return rc;
1556 }
1557