brintos

brintos / linux-shallow public Read only

0
0
Text · 12.8 KiB · 4db3ba6 Raw
555 lines · c
1// SPDX-License-Identifier: GPL-2.0-only2// Copyright 2014 Cisco Systems, Inc.  All rights reserved.3 4#include <linux/errno.h>5#include <linux/mempool.h>6 7#include <scsi/scsi_tcq.h>8 9#include "snic_disc.h"10#include "snic.h"11#include "snic_io.h"12 13 14/* snic target types */15static const char * const snic_tgt_type_str[] = {16	[SNIC_TGT_DAS] = "DAS",17	[SNIC_TGT_SAN] = "SAN",18};19 20static inline const char *21snic_tgt_type_to_str(int typ)22{23	return ((typ > SNIC_TGT_NONE && typ <= SNIC_TGT_SAN) ?24		 snic_tgt_type_str[typ] : "Unknown");25}26 27static const char * const snic_tgt_state_str[] = {28	[SNIC_TGT_STAT_INIT]	= "INIT",29	[SNIC_TGT_STAT_ONLINE]	= "ONLINE",30	[SNIC_TGT_STAT_OFFLINE]	= "OFFLINE",31	[SNIC_TGT_STAT_DEL]	= "DELETION IN PROGRESS",32};33 34const char *35snic_tgt_state_to_str(int state)36{37	return ((state >= SNIC_TGT_STAT_INIT && state <= SNIC_TGT_STAT_DEL) ?38		snic_tgt_state_str[state] : "UNKNOWN");39}40 41/*42 * Initiate report_tgt req desc43 */44static void45snic_report_tgt_init(struct snic_host_req *req, u32 hid, u8 *buf, u32 len,46		     dma_addr_t rsp_buf_pa, ulong ctx)47{48	struct snic_sg_desc *sgd = NULL;49 50 51	snic_io_hdr_enc(&req->hdr, SNIC_REQ_REPORT_TGTS, 0, SCSI_NO_TAG, hid,52			1, ctx);53 54	req->u.rpt_tgts.sg_cnt = cpu_to_le16(1);55	sgd = req_to_sgl(req);56	sgd[0].addr = cpu_to_le64(rsp_buf_pa);57	sgd[0].len = cpu_to_le32(len);58	sgd[0]._resvd = 0;59	req->u.rpt_tgts.sg_addr = cpu_to_le64((ulong)sgd);60}61 62/*63 * snic_queue_report_tgt_req: Queues report target request.64 */65static int66snic_queue_report_tgt_req(struct snic *snic)67{68	struct snic_req_info *rqi = NULL;69	u32 ntgts, buf_len = 0;70	u8 *buf = NULL;71	dma_addr_t pa = 0;72	int ret = 0;73 74	rqi = snic_req_init(snic, 1);75	if (!rqi) {76		ret = -ENOMEM;77		goto error;78	}79 80	if (snic->fwinfo.max_tgts)81		ntgts = min_t(u32, snic->fwinfo.max_tgts, snic->shost->max_id);82	else83		ntgts = snic->shost->max_id;84 85	/* Allocate Response Buffer */86	SNIC_BUG_ON(ntgts == 0);87	buf_len = ntgts * sizeof(struct snic_tgt_id) + SNIC_SG_DESC_ALIGN;88 89	buf = kzalloc(buf_len, GFP_KERNEL);90	if (!buf) {91		snic_req_free(snic, rqi);92		SNIC_HOST_ERR(snic->shost, "Resp Buf Alloc Failed.\n");93 94		ret = -ENOMEM;95		goto error;96	}97 98	SNIC_BUG_ON((((unsigned long)buf) % SNIC_SG_DESC_ALIGN) != 0);99 100	pa = dma_map_single(&snic->pdev->dev, buf, buf_len, DMA_FROM_DEVICE);101	if (dma_mapping_error(&snic->pdev->dev, pa)) {102		SNIC_HOST_ERR(snic->shost,103			      "Rpt-tgt rspbuf %p: PCI DMA Mapping Failed\n",104			      buf);105		kfree(buf);106		snic_req_free(snic, rqi);107		ret = -EINVAL;108 109		goto error;110	}111 112 113	SNIC_BUG_ON(pa == 0);114	rqi->sge_va = (ulong) buf;115 116	snic_report_tgt_init(rqi->req,117			     snic->config.hid,118			     buf,119			     buf_len,120			     pa,121			     (ulong)rqi);122 123	snic_handle_untagged_req(snic, rqi);124 125	ret = snic_queue_wq_desc(snic, rqi->req, rqi->req_len);126	if (ret) {127		dma_unmap_single(&snic->pdev->dev, pa, buf_len,128				 DMA_FROM_DEVICE);129		kfree(buf);130		rqi->sge_va = 0;131		snic_release_untagged_req(snic, rqi);132		SNIC_HOST_ERR(snic->shost, "Queuing Report Tgts Failed.\n");133 134		goto error;135	}136 137	SNIC_DISC_DBG(snic->shost, "Report Targets Issued.\n");138 139	return ret;140 141error:142	SNIC_HOST_ERR(snic->shost,143		      "Queuing Report Targets Failed, err = %d\n",144		      ret);145	return ret;146} /* end of snic_queue_report_tgt_req */147 148/* call into SML */149static void150snic_scsi_scan_tgt(struct work_struct *work)151{152	struct snic_tgt *tgt = container_of(work, struct snic_tgt, scan_work);153	struct Scsi_Host *shost = dev_to_shost(&tgt->dev);154	unsigned long flags;155 156	SNIC_HOST_INFO(shost, "Scanning Target id 0x%x\n", tgt->id);157	scsi_scan_target(&tgt->dev,158			 tgt->channel,159			 tgt->scsi_tgt_id,160			 SCAN_WILD_CARD,161			 SCSI_SCAN_RESCAN);162 163	spin_lock_irqsave(shost->host_lock, flags);164	tgt->flags &= ~SNIC_TGT_SCAN_PENDING;165	spin_unlock_irqrestore(shost->host_lock, flags);166} /* end of snic_scsi_scan_tgt */167 168/*169 * snic_tgt_lookup :170 */171static struct snic_tgt *172snic_tgt_lookup(struct snic *snic, struct snic_tgt_id *tgtid)173{174	struct list_head *cur, *nxt;175	struct snic_tgt *tgt = NULL;176 177	list_for_each_safe(cur, nxt, &snic->disc.tgt_list) {178		tgt = list_entry(cur, struct snic_tgt, list);179		if (tgt->id == le32_to_cpu(tgtid->tgt_id))180			return tgt;181		tgt = NULL;182	}183 184	return tgt;185} /* end of snic_tgt_lookup */186 187/*188 * snic_tgt_dev_release : Called on dropping last ref for snic_tgt object189 */190void191snic_tgt_dev_release(struct device *dev)192{193	struct snic_tgt *tgt = dev_to_tgt(dev);194 195	SNIC_HOST_INFO(snic_tgt_to_shost(tgt),196		       "Target Device ID %d (%s) Permanently Deleted.\n",197		       tgt->id,198		       dev_name(dev));199 200	SNIC_BUG_ON(!list_empty(&tgt->list));201	kfree(tgt);202}203 204/*205 * snic_tgt_del : work function to delete snic_tgt206 */207static void208snic_tgt_del(struct work_struct *work)209{210	struct snic_tgt *tgt = container_of(work, struct snic_tgt, del_work);211	struct Scsi_Host *shost = snic_tgt_to_shost(tgt);212 213	if (tgt->flags & SNIC_TGT_SCAN_PENDING)214		scsi_flush_work(shost);215 216	/* Block IOs on child devices, stops new IOs */217	scsi_block_targets(shost, &tgt->dev);218 219	/* Cleanup IOs */220	snic_tgt_scsi_abort_io(tgt);221 222	/* Unblock IOs now, to flush if there are any. */223	scsi_target_unblock(&tgt->dev, SDEV_TRANSPORT_OFFLINE);224 225	/* Delete SCSI Target and sdevs */226	scsi_remove_target(&tgt->dev);  /* ?? */227	device_del(&tgt->dev);228	put_device(&tgt->dev);229} /* end of snic_tgt_del */230 231/* snic_tgt_create: checks for existence of snic_tgt, if it doesn't232 * it creates one.233 */234static struct snic_tgt *235snic_tgt_create(struct snic *snic, struct snic_tgt_id *tgtid)236{237	struct snic_tgt *tgt = NULL;238	unsigned long flags;239	int ret;240 241	tgt = snic_tgt_lookup(snic, tgtid);242	if (tgt) {243		/* update the information if required */244		return tgt;245	}246 247	tgt = kzalloc(sizeof(*tgt), GFP_KERNEL);248	if (!tgt) {249		SNIC_HOST_ERR(snic->shost, "Failure to allocate snic_tgt.\n");250		ret = -ENOMEM;251 252		return tgt;253	}254 255	INIT_LIST_HEAD(&tgt->list);256	tgt->id = le32_to_cpu(tgtid->tgt_id);257	tgt->channel = 0;258 259	SNIC_BUG_ON(le16_to_cpu(tgtid->tgt_type) > SNIC_TGT_SAN);260	tgt->tdata.typ = le16_to_cpu(tgtid->tgt_type);261 262	/*263	 * Plugging into SML Device Tree264	 */265	tgt->tdata.disc_id = 0;266	tgt->state = SNIC_TGT_STAT_INIT;267	device_initialize(&tgt->dev);268	tgt->dev.parent = get_device(&snic->shost->shost_gendev);269	tgt->dev.release = snic_tgt_dev_release;270	INIT_WORK(&tgt->scan_work, snic_scsi_scan_tgt);271	INIT_WORK(&tgt->del_work, snic_tgt_del);272	switch (tgt->tdata.typ) {273	case SNIC_TGT_DAS:274		dev_set_name(&tgt->dev, "snic_das_tgt:%d:%d-%d",275			     snic->shost->host_no, tgt->channel, tgt->id);276		break;277 278	case SNIC_TGT_SAN:279		dev_set_name(&tgt->dev, "snic_san_tgt:%d:%d-%d",280			     snic->shost->host_no, tgt->channel, tgt->id);281		break;282 283	default:284		SNIC_HOST_INFO(snic->shost, "Target type Unknown Detected.\n");285		dev_set_name(&tgt->dev, "snic_das_tgt:%d:%d-%d",286			     snic->shost->host_no, tgt->channel, tgt->id);287		break;288	}289 290	spin_lock_irqsave(snic->shost->host_lock, flags);291	list_add_tail(&tgt->list, &snic->disc.tgt_list);292	tgt->scsi_tgt_id = snic->disc.nxt_tgt_id++;293	tgt->state = SNIC_TGT_STAT_ONLINE;294	spin_unlock_irqrestore(snic->shost->host_lock, flags);295 296	SNIC_HOST_INFO(snic->shost,297		       "Tgt %d, type = %s detected. Adding..\n",298		       tgt->id, snic_tgt_type_to_str(tgt->tdata.typ));299 300	ret = device_add(&tgt->dev);301	if (ret) {302		SNIC_HOST_ERR(snic->shost,303			      "Snic Tgt: device_add, with err = %d\n",304			      ret);305 306		put_device(&snic->shost->shost_gendev);307		spin_lock_irqsave(snic->shost->host_lock, flags);308		list_del(&tgt->list);309		spin_unlock_irqrestore(snic->shost->host_lock, flags);310		put_device(&tgt->dev);311		tgt = NULL;312 313		return tgt;314	}315 316	SNIC_HOST_INFO(snic->shost, "Scanning %s.\n", dev_name(&tgt->dev));317 318	scsi_queue_work(snic->shost, &tgt->scan_work);319 320	return tgt;321} /* end of snic_tgt_create */322 323/* Handler for discovery */324void325snic_handle_tgt_disc(struct work_struct *work)326{327	struct snic *snic = container_of(work, struct snic, tgt_work);328	struct snic_tgt_id *tgtid = NULL;329	struct snic_tgt *tgt = NULL;330	unsigned long flags;331	int i;332 333	spin_lock_irqsave(&snic->snic_lock, flags);334	if (snic->in_remove) {335		spin_unlock_irqrestore(&snic->snic_lock, flags);336		kfree(snic->disc.rtgt_info);337 338		return;339	}340	spin_unlock_irqrestore(&snic->snic_lock, flags);341 342	mutex_lock(&snic->disc.mutex);343	/* Discover triggered during disc in progress */344	if (snic->disc.req_cnt) {345		snic->disc.state = SNIC_DISC_DONE;346		snic->disc.req_cnt = 0;347		mutex_unlock(&snic->disc.mutex);348		kfree(snic->disc.rtgt_info);349		snic->disc.rtgt_info = NULL;350 351		SNIC_HOST_INFO(snic->shost, "tgt_disc: Discovery restart.\n");352		/* Start Discovery Again */353		snic_disc_start(snic);354 355		return;356	}357 358	tgtid = (struct snic_tgt_id *)snic->disc.rtgt_info;359 360	SNIC_BUG_ON(snic->disc.rtgt_cnt == 0 || tgtid == NULL);361 362	for (i = 0; i < snic->disc.rtgt_cnt; i++) {363		tgt = snic_tgt_create(snic, &tgtid[i]);364		if (!tgt) {365			int buf_sz = snic->disc.rtgt_cnt * sizeof(*tgtid);366 367			SNIC_HOST_ERR(snic->shost, "Failed to create tgt.\n");368			snic_hex_dump("rpt_tgt_rsp", (char *)tgtid, buf_sz);369			break;370		}371	}372 373	snic->disc.rtgt_info = NULL;374	snic->disc.state = SNIC_DISC_DONE;375	mutex_unlock(&snic->disc.mutex);376 377	SNIC_HOST_INFO(snic->shost, "Discovery Completed.\n");378 379	kfree(tgtid);380} /* end of snic_handle_tgt_disc */381 382 383int384snic_report_tgt_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)385{386 387	u8 typ, cmpl_stat;388	u32 cmnd_id, hid, tgt_cnt = 0;389	ulong ctx;390	struct snic_req_info *rqi = NULL;391	struct snic_tgt_id *tgtid;392	int i, ret = 0;393 394	snic_io_hdr_dec(&fwreq->hdr, &typ, &cmpl_stat, &cmnd_id, &hid, &ctx);395	rqi = (struct snic_req_info *) ctx;396	tgtid = (struct snic_tgt_id *) rqi->sge_va;397 398	tgt_cnt = le32_to_cpu(fwreq->u.rpt_tgts_cmpl.tgt_cnt);399	if (tgt_cnt == 0) {400		SNIC_HOST_ERR(snic->shost, "No Targets Found on this host.\n");401		ret = 1;402 403		goto end;404	}405 406	/* printing list of targets here */407	SNIC_HOST_INFO(snic->shost, "Target Count = %d\n", tgt_cnt);408 409	SNIC_BUG_ON(tgt_cnt > snic->fwinfo.max_tgts);410 411	for (i = 0; i < tgt_cnt; i++)412		SNIC_HOST_INFO(snic->shost,413			       "Tgt id = 0x%x\n",414			       le32_to_cpu(tgtid[i].tgt_id));415 416	/*417	 * Queue work for further processing,418	 * Response Buffer Memory is freed after creating targets419	 */420	snic->disc.rtgt_cnt = tgt_cnt;421	snic->disc.rtgt_info = (u8 *) tgtid;422	queue_work(snic_glob->event_q, &snic->tgt_work);423	ret = 0;424 425end:426	/* Unmap Response Buffer */427	snic_pci_unmap_rsp_buf(snic, rqi);428	if (ret)429		kfree(tgtid);430 431	rqi->sge_va = 0;432	snic_release_untagged_req(snic, rqi);433 434	return ret;435} /* end of snic_report_tgt_cmpl_handler */436 437/* Discovery init fn */438void439snic_disc_init(struct snic_disc *disc)440{441	INIT_LIST_HEAD(&disc->tgt_list);442	mutex_init(&disc->mutex);443	disc->disc_id = 0;444	disc->nxt_tgt_id = 0;445	disc->state = SNIC_DISC_INIT;446	disc->req_cnt = 0;447	disc->rtgt_cnt = 0;448	disc->rtgt_info = NULL;449	disc->cb = NULL;450} /* end of snic_disc_init */451 452/* Discovery, uninit fn */453void454snic_disc_term(struct snic *snic)455{456	struct snic_disc *disc = &snic->disc;457 458	mutex_lock(&disc->mutex);459	if (disc->req_cnt) {460		disc->req_cnt = 0;461		SNIC_SCSI_DBG(snic->shost, "Terminating Discovery.\n");462	}463	mutex_unlock(&disc->mutex);464}465 466/*467 * snic_disc_start: Discovery Start ...468 */469int470snic_disc_start(struct snic *snic)471{472	struct snic_disc *disc = &snic->disc;473	unsigned long flags;474	int ret = 0;475 476	SNIC_SCSI_DBG(snic->shost, "Discovery Start.\n");477 478	spin_lock_irqsave(&snic->snic_lock, flags);479	if (snic->in_remove) {480		spin_unlock_irqrestore(&snic->snic_lock, flags);481		SNIC_ERR("snic driver removal in progress ...\n");482		ret = 0;483 484		return ret;485	}486	spin_unlock_irqrestore(&snic->snic_lock, flags);487 488	mutex_lock(&disc->mutex);489	if (disc->state == SNIC_DISC_PENDING) {490		disc->req_cnt++;491		mutex_unlock(&disc->mutex);492 493		return ret;494	}495	disc->state = SNIC_DISC_PENDING;496	mutex_unlock(&disc->mutex);497 498	ret = snic_queue_report_tgt_req(snic);499	if (ret)500		SNIC_HOST_INFO(snic->shost, "Discovery Failed, err=%d.\n", ret);501 502	return ret;503} /* end of snic_disc_start */504 505/*506 * snic_disc_work :507 */508void509snic_handle_disc(struct work_struct *work)510{511	struct snic *snic = container_of(work, struct snic, disc_work);512	int ret = 0;513 514	SNIC_HOST_INFO(snic->shost, "disc_work: Discovery\n");515 516	ret = snic_disc_start(snic);517	if (ret)518		goto disc_err;519 520disc_err:521	SNIC_HOST_ERR(snic->shost,522		      "disc_work: Discovery Failed w/ err = %d\n",523		      ret);524} /* end of snic_disc_work */525 526/*527 * snic_tgt_del_all : cleanup all snic targets528 * Called on unbinding the interface529 */530void531snic_tgt_del_all(struct snic *snic)532{533	struct snic_tgt *tgt = NULL;534	struct list_head *cur, *nxt;535	unsigned long flags;536 537	scsi_flush_work(snic->shost);538 539	mutex_lock(&snic->disc.mutex);540	spin_lock_irqsave(snic->shost->host_lock, flags);541 542	list_for_each_safe(cur, nxt, &snic->disc.tgt_list) {543		tgt = list_entry(cur, struct snic_tgt, list);544		tgt->state = SNIC_TGT_STAT_DEL;545		list_del_init(&tgt->list);546		SNIC_HOST_INFO(snic->shost, "Tgt %d q'ing for del\n", tgt->id);547		queue_work(snic_glob->event_q, &tgt->del_work);548		tgt = NULL;549	}550	spin_unlock_irqrestore(snic->shost->host_lock, flags);551	mutex_unlock(&snic->disc.mutex);552 553	flush_workqueue(snic_glob->event_q);554} /* end of snic_tgt_del_all */555