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