xref: /openbmc/linux/drivers/scsi/snic/snic_disc.c (revision bc33f5e5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // 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 */
15 static const char * const snic_tgt_type_str[] = {
16 	[SNIC_TGT_DAS] = "DAS",
17 	[SNIC_TGT_SAN] = "SAN",
18 };
19 
20 static inline const char *
21 snic_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 
27 static 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 
34 const char *
35 snic_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 desc
43  */
44 static void
45 snic_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  */
65 static int
66 snic_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 	else
83 		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 
141 error:
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 */
149 static void
150 snic_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  */
171 static struct snic_tgt *
172 snic_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 object
189  */
190 void
191 snic_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_tgt
206  */
207 static void
208 snic_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_target_block(&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't
232  * it creates one.
233  */
234 static struct snic_tgt *
235 snic_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 Tree
264 	 */
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 		kfree(tgt);
308 		tgt = NULL;
309 
310 		return tgt;
311 	}
312 
313 	SNIC_HOST_INFO(snic->shost, "Scanning %s.\n", dev_name(&tgt->dev));
314 
315 	scsi_queue_work(snic->shost, &tgt->scan_work);
316 
317 	return tgt;
318 } /* end of snic_tgt_create */
319 
320 /* Handler for discovery */
321 void
322 snic_handle_tgt_disc(struct work_struct *work)
323 {
324 	struct snic *snic = container_of(work, struct snic, tgt_work);
325 	struct snic_tgt_id *tgtid = NULL;
326 	struct snic_tgt *tgt = NULL;
327 	unsigned long flags;
328 	int i;
329 
330 	spin_lock_irqsave(&snic->snic_lock, flags);
331 	if (snic->in_remove) {
332 		spin_unlock_irqrestore(&snic->snic_lock, flags);
333 		kfree(snic->disc.rtgt_info);
334 
335 		return;
336 	}
337 	spin_unlock_irqrestore(&snic->snic_lock, flags);
338 
339 	mutex_lock(&snic->disc.mutex);
340 	/* Discover triggered during disc in progress */
341 	if (snic->disc.req_cnt) {
342 		snic->disc.state = SNIC_DISC_DONE;
343 		snic->disc.req_cnt = 0;
344 		mutex_unlock(&snic->disc.mutex);
345 		kfree(snic->disc.rtgt_info);
346 		snic->disc.rtgt_info = NULL;
347 
348 		SNIC_HOST_INFO(snic->shost, "tgt_disc: Discovery restart.\n");
349 		/* Start Discovery Again */
350 		snic_disc_start(snic);
351 
352 		return;
353 	}
354 
355 	tgtid = (struct snic_tgt_id *)snic->disc.rtgt_info;
356 
357 	SNIC_BUG_ON(snic->disc.rtgt_cnt == 0 || tgtid == NULL);
358 
359 	for (i = 0; i < snic->disc.rtgt_cnt; i++) {
360 		tgt = snic_tgt_create(snic, &tgtid[i]);
361 		if (!tgt) {
362 			int buf_sz = snic->disc.rtgt_cnt * sizeof(*tgtid);
363 
364 			SNIC_HOST_ERR(snic->shost, "Failed to create tgt.\n");
365 			snic_hex_dump("rpt_tgt_rsp", (char *)tgtid, buf_sz);
366 			break;
367 		}
368 	}
369 
370 	snic->disc.rtgt_info = NULL;
371 	snic->disc.state = SNIC_DISC_DONE;
372 	mutex_unlock(&snic->disc.mutex);
373 
374 	SNIC_HOST_INFO(snic->shost, "Discovery Completed.\n");
375 
376 	kfree(tgtid);
377 } /* end of snic_handle_tgt_disc */
378 
379 
380 int
381 snic_report_tgt_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
382 {
383 
384 	u8 typ, cmpl_stat;
385 	u32 cmnd_id, hid, tgt_cnt = 0;
386 	ulong ctx;
387 	struct snic_req_info *rqi = NULL;
388 	struct snic_tgt_id *tgtid;
389 	int i, ret = 0;
390 
391 	snic_io_hdr_dec(&fwreq->hdr, &typ, &cmpl_stat, &cmnd_id, &hid, &ctx);
392 	rqi = (struct snic_req_info *) ctx;
393 	tgtid = (struct snic_tgt_id *) rqi->sge_va;
394 
395 	tgt_cnt = le32_to_cpu(fwreq->u.rpt_tgts_cmpl.tgt_cnt);
396 	if (tgt_cnt == 0) {
397 		SNIC_HOST_ERR(snic->shost, "No Targets Found on this host.\n");
398 		ret = 1;
399 
400 		goto end;
401 	}
402 
403 	/* printing list of targets here */
404 	SNIC_HOST_INFO(snic->shost, "Target Count = %d\n", tgt_cnt);
405 
406 	SNIC_BUG_ON(tgt_cnt > snic->fwinfo.max_tgts);
407 
408 	for (i = 0; i < tgt_cnt; i++)
409 		SNIC_HOST_INFO(snic->shost,
410 			       "Tgt id = 0x%x\n",
411 			       le32_to_cpu(tgtid[i].tgt_id));
412 
413 	/*
414 	 * Queue work for further processing,
415 	 * Response Buffer Memory is freed after creating targets
416 	 */
417 	snic->disc.rtgt_cnt = tgt_cnt;
418 	snic->disc.rtgt_info = (u8 *) tgtid;
419 	queue_work(snic_glob->event_q, &snic->tgt_work);
420 	ret = 0;
421 
422 end:
423 	/* Unmap Response Buffer */
424 	snic_pci_unmap_rsp_buf(snic, rqi);
425 	if (ret)
426 		kfree(tgtid);
427 
428 	rqi->sge_va = 0;
429 	snic_release_untagged_req(snic, rqi);
430 
431 	return ret;
432 } /* end of snic_report_tgt_cmpl_handler */
433 
434 /* Discovery init fn */
435 void
436 snic_disc_init(struct snic_disc *disc)
437 {
438 	INIT_LIST_HEAD(&disc->tgt_list);
439 	mutex_init(&disc->mutex);
440 	disc->disc_id = 0;
441 	disc->nxt_tgt_id = 0;
442 	disc->state = SNIC_DISC_INIT;
443 	disc->req_cnt = 0;
444 	disc->rtgt_cnt = 0;
445 	disc->rtgt_info = NULL;
446 	disc->cb = NULL;
447 } /* end of snic_disc_init */
448 
449 /* Discovery, uninit fn */
450 void
451 snic_disc_term(struct snic *snic)
452 {
453 	struct snic_disc *disc = &snic->disc;
454 
455 	mutex_lock(&disc->mutex);
456 	if (disc->req_cnt) {
457 		disc->req_cnt = 0;
458 		SNIC_SCSI_DBG(snic->shost, "Terminating Discovery.\n");
459 	}
460 	mutex_unlock(&disc->mutex);
461 }
462 
463 /*
464  * snic_disc_start: Discovery Start ...
465  */
466 int
467 snic_disc_start(struct snic *snic)
468 {
469 	struct snic_disc *disc = &snic->disc;
470 	unsigned long flags;
471 	int ret = 0;
472 
473 	SNIC_SCSI_DBG(snic->shost, "Discovery Start.\n");
474 
475 	spin_lock_irqsave(&snic->snic_lock, flags);
476 	if (snic->in_remove) {
477 		spin_unlock_irqrestore(&snic->snic_lock, flags);
478 		SNIC_ERR("snic driver removal in progress ...\n");
479 		ret = 0;
480 
481 		return ret;
482 	}
483 	spin_unlock_irqrestore(&snic->snic_lock, flags);
484 
485 	mutex_lock(&disc->mutex);
486 	if (disc->state == SNIC_DISC_PENDING) {
487 		disc->req_cnt++;
488 		mutex_unlock(&disc->mutex);
489 
490 		return ret;
491 	}
492 	disc->state = SNIC_DISC_PENDING;
493 	mutex_unlock(&disc->mutex);
494 
495 	ret = snic_queue_report_tgt_req(snic);
496 	if (ret)
497 		SNIC_HOST_INFO(snic->shost, "Discovery Failed, err=%d.\n", ret);
498 
499 	return ret;
500 } /* end of snic_disc_start */
501 
502 /*
503  * snic_disc_work :
504  */
505 void
506 snic_handle_disc(struct work_struct *work)
507 {
508 	struct snic *snic = container_of(work, struct snic, disc_work);
509 	int ret = 0;
510 
511 	SNIC_HOST_INFO(snic->shost, "disc_work: Discovery\n");
512 
513 	ret = snic_disc_start(snic);
514 	if (ret)
515 		goto disc_err;
516 
517 disc_err:
518 	SNIC_HOST_ERR(snic->shost,
519 		      "disc_work: Discovery Failed w/ err = %d\n",
520 		      ret);
521 } /* end of snic_disc_work */
522 
523 /*
524  * snic_tgt_del_all : cleanup all snic targets
525  * Called on unbinding the interface
526  */
527 void
528 snic_tgt_del_all(struct snic *snic)
529 {
530 	struct snic_tgt *tgt = NULL;
531 	struct list_head *cur, *nxt;
532 	unsigned long flags;
533 
534 	scsi_flush_work(snic->shost);
535 
536 	mutex_lock(&snic->disc.mutex);
537 	spin_lock_irqsave(snic->shost->host_lock, flags);
538 
539 	list_for_each_safe(cur, nxt, &snic->disc.tgt_list) {
540 		tgt = list_entry(cur, struct snic_tgt, list);
541 		tgt->state = SNIC_TGT_STAT_DEL;
542 		list_del_init(&tgt->list);
543 		SNIC_HOST_INFO(snic->shost, "Tgt %d q'ing for del\n", tgt->id);
544 		queue_work(snic_glob->event_q, &tgt->del_work);
545 		tgt = NULL;
546 	}
547 	spin_unlock_irqrestore(snic->shost->host_lock, flags);
548 	mutex_unlock(&snic->disc.mutex);
549 
550 	flush_workqueue(snic_glob->event_q);
551 } /* end of snic_tgt_del_all */
552