PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/scsi/snic/snic_disc.c

https://gitlab.com/dieselnutjob/linux-next
C | 565 lines | 391 code | 108 blank | 66 comment | 34 complexity | feb6e069ed0abfec7e33d42eb13b3206 MD5 | raw file
  1. /*
  2. * Copyright 2014 Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This program is free software; you may redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; version 2 of the License.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  9. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  10. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  11. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  12. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  13. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  14. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  15. * SOFTWARE.
  16. */
  17. #include <linux/errno.h>
  18. #include <linux/mempool.h>
  19. #include <scsi/scsi_tcq.h>
  20. #include "snic_disc.h"
  21. #include "snic.h"
  22. #include "snic_io.h"
  23. /* snic target types */
  24. static const char * const snic_tgt_type_str[] = {
  25. [SNIC_TGT_DAS] = "DAS",
  26. [SNIC_TGT_SAN] = "SAN",
  27. };
  28. static inline const char *
  29. snic_tgt_type_to_str(int typ)
  30. {
  31. return ((typ > SNIC_TGT_NONE && typ <= SNIC_TGT_SAN) ?
  32. snic_tgt_type_str[typ] : "Unknown");
  33. }
  34. static const char * const snic_tgt_state_str[] = {
  35. [SNIC_TGT_STAT_INIT] = "INIT",
  36. [SNIC_TGT_STAT_ONLINE] = "ONLINE",
  37. [SNIC_TGT_STAT_OFFLINE] = "OFFLINE",
  38. [SNIC_TGT_STAT_DEL] = "DELETION IN PROGRESS",
  39. };
  40. const char *
  41. snic_tgt_state_to_str(int state)
  42. {
  43. return ((state >= SNIC_TGT_STAT_INIT && state <= SNIC_TGT_STAT_DEL) ?
  44. snic_tgt_state_str[state] : "UNKNOWN");
  45. }
  46. /*
  47. * Initiate report_tgt req desc
  48. */
  49. static void
  50. snic_report_tgt_init(struct snic_host_req *req, u32 hid, u8 *buf, u32 len,
  51. dma_addr_t rsp_buf_pa, ulong ctx)
  52. {
  53. struct snic_sg_desc *sgd = NULL;
  54. snic_io_hdr_enc(&req->hdr, SNIC_REQ_REPORT_TGTS, 0, SCSI_NO_TAG, hid,
  55. 1, ctx);
  56. req->u.rpt_tgts.sg_cnt = cpu_to_le16(1);
  57. sgd = req_to_sgl(req);
  58. sgd[0].addr = cpu_to_le64(rsp_buf_pa);
  59. sgd[0].len = cpu_to_le32(len);
  60. sgd[0]._resvd = 0;
  61. req->u.rpt_tgts.sg_addr = cpu_to_le64((ulong)sgd);
  62. }
  63. /*
  64. * snic_queue_report_tgt_req: Queues report target request.
  65. */
  66. static int
  67. snic_queue_report_tgt_req(struct snic *snic)
  68. {
  69. struct snic_req_info *rqi = NULL;
  70. u32 ntgts, buf_len = 0;
  71. u8 *buf = NULL;
  72. dma_addr_t pa = 0;
  73. int ret = 0;
  74. rqi = snic_req_init(snic, 1);
  75. if (!rqi) {
  76. ret = -ENOMEM;
  77. goto error;
  78. }
  79. if (snic->fwinfo.max_tgts)
  80. ntgts = min_t(u32, snic->fwinfo.max_tgts, snic->shost->max_id);
  81. else
  82. ntgts = snic->shost->max_id;
  83. /* Allocate Response Buffer */
  84. SNIC_BUG_ON(ntgts == 0);
  85. buf_len = ntgts * sizeof(struct snic_tgt_id) + SNIC_SG_DESC_ALIGN;
  86. buf = kzalloc(buf_len, GFP_KERNEL);
  87. if (!buf) {
  88. snic_req_free(snic, rqi);
  89. SNIC_HOST_ERR(snic->shost, "Resp Buf Alloc Failed.\n");
  90. ret = -ENOMEM;
  91. goto error;
  92. }
  93. SNIC_BUG_ON((((unsigned long)buf) % SNIC_SG_DESC_ALIGN) != 0);
  94. pa = dma_map_single(&snic->pdev->dev, buf, buf_len, DMA_FROM_DEVICE);
  95. if (dma_mapping_error(&snic->pdev->dev, pa)) {
  96. SNIC_HOST_ERR(snic->shost,
  97. "Rpt-tgt rspbuf %p: PCI DMA Mapping Failed\n",
  98. buf);
  99. kfree(buf);
  100. snic_req_free(snic, rqi);
  101. ret = -EINVAL;
  102. goto error;
  103. }
  104. SNIC_BUG_ON(pa == 0);
  105. rqi->sge_va = (ulong) buf;
  106. snic_report_tgt_init(rqi->req,
  107. snic->config.hid,
  108. buf,
  109. buf_len,
  110. pa,
  111. (ulong)rqi);
  112. snic_handle_untagged_req(snic, rqi);
  113. ret = snic_queue_wq_desc(snic, rqi->req, rqi->req_len);
  114. if (ret) {
  115. dma_unmap_single(&snic->pdev->dev, pa, buf_len,
  116. DMA_FROM_DEVICE);
  117. kfree(buf);
  118. rqi->sge_va = 0;
  119. snic_release_untagged_req(snic, rqi);
  120. SNIC_HOST_ERR(snic->shost, "Queuing Report Tgts Failed.\n");
  121. goto error;
  122. }
  123. SNIC_DISC_DBG(snic->shost, "Report Targets Issued.\n");
  124. return ret;
  125. error:
  126. SNIC_HOST_ERR(snic->shost,
  127. "Queuing Report Targets Failed, err = %d\n",
  128. ret);
  129. return ret;
  130. } /* end of snic_queue_report_tgt_req */
  131. /* call into SML */
  132. static void
  133. snic_scsi_scan_tgt(struct work_struct *work)
  134. {
  135. struct snic_tgt *tgt = container_of(work, struct snic_tgt, scan_work);
  136. struct Scsi_Host *shost = dev_to_shost(&tgt->dev);
  137. unsigned long flags;
  138. SNIC_HOST_INFO(shost, "Scanning Target id 0x%x\n", tgt->id);
  139. scsi_scan_target(&tgt->dev,
  140. tgt->channel,
  141. tgt->scsi_tgt_id,
  142. SCAN_WILD_CARD,
  143. SCSI_SCAN_RESCAN);
  144. spin_lock_irqsave(shost->host_lock, flags);
  145. tgt->flags &= ~SNIC_TGT_SCAN_PENDING;
  146. spin_unlock_irqrestore(shost->host_lock, flags);
  147. } /* end of snic_scsi_scan_tgt */
  148. /*
  149. * snic_tgt_lookup :
  150. */
  151. static struct snic_tgt *
  152. snic_tgt_lookup(struct snic *snic, struct snic_tgt_id *tgtid)
  153. {
  154. struct list_head *cur, *nxt;
  155. struct snic_tgt *tgt = NULL;
  156. list_for_each_safe(cur, nxt, &snic->disc.tgt_list) {
  157. tgt = list_entry(cur, struct snic_tgt, list);
  158. if (tgt->id == le32_to_cpu(tgtid->tgt_id))
  159. return tgt;
  160. tgt = NULL;
  161. }
  162. return tgt;
  163. } /* end of snic_tgt_lookup */
  164. /*
  165. * snic_tgt_dev_release : Called on dropping last ref for snic_tgt object
  166. */
  167. void
  168. snic_tgt_dev_release(struct device *dev)
  169. {
  170. struct snic_tgt *tgt = dev_to_tgt(dev);
  171. SNIC_HOST_INFO(snic_tgt_to_shost(tgt),
  172. "Target Device ID %d (%s) Permanently Deleted.\n",
  173. tgt->id,
  174. dev_name(dev));
  175. SNIC_BUG_ON(!list_empty(&tgt->list));
  176. kfree(tgt);
  177. }
  178. /*
  179. * snic_tgt_del : work function to delete snic_tgt
  180. */
  181. static void
  182. snic_tgt_del(struct work_struct *work)
  183. {
  184. struct snic_tgt *tgt = container_of(work, struct snic_tgt, del_work);
  185. struct Scsi_Host *shost = snic_tgt_to_shost(tgt);
  186. if (tgt->flags & SNIC_TGT_SCAN_PENDING)
  187. scsi_flush_work(shost);
  188. /* Block IOs on child devices, stops new IOs */
  189. scsi_target_block(&tgt->dev);
  190. /* Cleanup IOs */
  191. snic_tgt_scsi_abort_io(tgt);
  192. /* Unblock IOs now, to flush if there are any. */
  193. scsi_target_unblock(&tgt->dev, SDEV_TRANSPORT_OFFLINE);
  194. /* Delete SCSI Target and sdevs */
  195. scsi_remove_target(&tgt->dev); /* ?? */
  196. device_del(&tgt->dev);
  197. put_device(&tgt->dev);
  198. } /* end of snic_tgt_del */
  199. /* snic_tgt_create: checks for existence of snic_tgt, if it doesn't
  200. * it creates one.
  201. */
  202. static struct snic_tgt *
  203. snic_tgt_create(struct snic *snic, struct snic_tgt_id *tgtid)
  204. {
  205. struct snic_tgt *tgt = NULL;
  206. unsigned long flags;
  207. int ret;
  208. tgt = snic_tgt_lookup(snic, tgtid);
  209. if (tgt) {
  210. /* update the information if required */
  211. return tgt;
  212. }
  213. tgt = kzalloc(sizeof(*tgt), GFP_KERNEL);
  214. if (!tgt) {
  215. SNIC_HOST_ERR(snic->shost, "Failure to allocate snic_tgt.\n");
  216. ret = -ENOMEM;
  217. return tgt;
  218. }
  219. INIT_LIST_HEAD(&tgt->list);
  220. tgt->id = le32_to_cpu(tgtid->tgt_id);
  221. tgt->channel = 0;
  222. SNIC_BUG_ON(le16_to_cpu(tgtid->tgt_type) > SNIC_TGT_SAN);
  223. tgt->tdata.typ = le16_to_cpu(tgtid->tgt_type);
  224. /*
  225. * Plugging into SML Device Tree
  226. */
  227. tgt->tdata.disc_id = 0;
  228. tgt->state = SNIC_TGT_STAT_INIT;
  229. device_initialize(&tgt->dev);
  230. tgt->dev.parent = get_device(&snic->shost->shost_gendev);
  231. tgt->dev.release = snic_tgt_dev_release;
  232. INIT_WORK(&tgt->scan_work, snic_scsi_scan_tgt);
  233. INIT_WORK(&tgt->del_work, snic_tgt_del);
  234. switch (tgt->tdata.typ) {
  235. case SNIC_TGT_DAS:
  236. dev_set_name(&tgt->dev, "snic_das_tgt:%d:%d-%d",
  237. snic->shost->host_no, tgt->channel, tgt->id);
  238. break;
  239. case SNIC_TGT_SAN:
  240. dev_set_name(&tgt->dev, "snic_san_tgt:%d:%d-%d",
  241. snic->shost->host_no, tgt->channel, tgt->id);
  242. break;
  243. default:
  244. SNIC_HOST_INFO(snic->shost, "Target type Unknown Detected.\n");
  245. dev_set_name(&tgt->dev, "snic_das_tgt:%d:%d-%d",
  246. snic->shost->host_no, tgt->channel, tgt->id);
  247. break;
  248. }
  249. spin_lock_irqsave(snic->shost->host_lock, flags);
  250. list_add_tail(&tgt->list, &snic->disc.tgt_list);
  251. tgt->scsi_tgt_id = snic->disc.nxt_tgt_id++;
  252. tgt->state = SNIC_TGT_STAT_ONLINE;
  253. spin_unlock_irqrestore(snic->shost->host_lock, flags);
  254. SNIC_HOST_INFO(snic->shost,
  255. "Tgt %d, type = %s detected. Adding..\n",
  256. tgt->id, snic_tgt_type_to_str(tgt->tdata.typ));
  257. ret = device_add(&tgt->dev);
  258. if (ret) {
  259. SNIC_HOST_ERR(snic->shost,
  260. "Snic Tgt: device_add, with err = %d\n",
  261. ret);
  262. put_device(&snic->shost->shost_gendev);
  263. kfree(tgt);
  264. tgt = NULL;
  265. return tgt;
  266. }
  267. SNIC_HOST_INFO(snic->shost, "Scanning %s.\n", dev_name(&tgt->dev));
  268. scsi_queue_work(snic->shost, &tgt->scan_work);
  269. return tgt;
  270. } /* end of snic_tgt_create */
  271. /* Handler for discovery */
  272. void
  273. snic_handle_tgt_disc(struct work_struct *work)
  274. {
  275. struct snic *snic = container_of(work, struct snic, tgt_work);
  276. struct snic_tgt_id *tgtid = NULL;
  277. struct snic_tgt *tgt = NULL;
  278. unsigned long flags;
  279. int i;
  280. spin_lock_irqsave(&snic->snic_lock, flags);
  281. if (snic->in_remove) {
  282. spin_unlock_irqrestore(&snic->snic_lock, flags);
  283. kfree(snic->disc.rtgt_info);
  284. return;
  285. }
  286. spin_unlock_irqrestore(&snic->snic_lock, flags);
  287. mutex_lock(&snic->disc.mutex);
  288. /* Discover triggered during disc in progress */
  289. if (snic->disc.req_cnt) {
  290. snic->disc.state = SNIC_DISC_DONE;
  291. snic->disc.req_cnt = 0;
  292. mutex_unlock(&snic->disc.mutex);
  293. kfree(snic->disc.rtgt_info);
  294. snic->disc.rtgt_info = NULL;
  295. SNIC_HOST_INFO(snic->shost, "tgt_disc: Discovery restart.\n");
  296. /* Start Discovery Again */
  297. snic_disc_start(snic);
  298. return;
  299. }
  300. tgtid = (struct snic_tgt_id *)snic->disc.rtgt_info;
  301. SNIC_BUG_ON(snic->disc.rtgt_cnt == 0 || tgtid == NULL);
  302. for (i = 0; i < snic->disc.rtgt_cnt; i++) {
  303. tgt = snic_tgt_create(snic, &tgtid[i]);
  304. if (!tgt) {
  305. int buf_sz = snic->disc.rtgt_cnt * sizeof(*tgtid);
  306. SNIC_HOST_ERR(snic->shost, "Failed to create tgt.\n");
  307. snic_hex_dump("rpt_tgt_rsp", (char *)tgtid, buf_sz);
  308. break;
  309. }
  310. }
  311. snic->disc.rtgt_info = NULL;
  312. snic->disc.state = SNIC_DISC_DONE;
  313. mutex_unlock(&snic->disc.mutex);
  314. SNIC_HOST_INFO(snic->shost, "Discovery Completed.\n");
  315. kfree(tgtid);
  316. } /* end of snic_handle_tgt_disc */
  317. int
  318. snic_report_tgt_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
  319. {
  320. u8 typ, cmpl_stat;
  321. u32 cmnd_id, hid, tgt_cnt = 0;
  322. ulong ctx;
  323. struct snic_req_info *rqi = NULL;
  324. struct snic_tgt_id *tgtid;
  325. int i, ret = 0;
  326. snic_io_hdr_dec(&fwreq->hdr, &typ, &cmpl_stat, &cmnd_id, &hid, &ctx);
  327. rqi = (struct snic_req_info *) ctx;
  328. tgtid = (struct snic_tgt_id *) rqi->sge_va;
  329. tgt_cnt = le32_to_cpu(fwreq->u.rpt_tgts_cmpl.tgt_cnt);
  330. if (tgt_cnt == 0) {
  331. SNIC_HOST_ERR(snic->shost, "No Targets Found on this host.\n");
  332. ret = 1;
  333. goto end;
  334. }
  335. /* printing list of targets here */
  336. SNIC_HOST_INFO(snic->shost, "Target Count = %d\n", tgt_cnt);
  337. SNIC_BUG_ON(tgt_cnt > snic->fwinfo.max_tgts);
  338. for (i = 0; i < tgt_cnt; i++)
  339. SNIC_HOST_INFO(snic->shost,
  340. "Tgt id = 0x%x\n",
  341. le32_to_cpu(tgtid[i].tgt_id));
  342. /*
  343. * Queue work for further processing,
  344. * Response Buffer Memory is freed after creating targets
  345. */
  346. snic->disc.rtgt_cnt = tgt_cnt;
  347. snic->disc.rtgt_info = (u8 *) tgtid;
  348. queue_work(snic_glob->event_q, &snic->tgt_work);
  349. ret = 0;
  350. end:
  351. /* Unmap Response Buffer */
  352. snic_pci_unmap_rsp_buf(snic, rqi);
  353. if (ret)
  354. kfree(tgtid);
  355. rqi->sge_va = 0;
  356. snic_release_untagged_req(snic, rqi);
  357. return ret;
  358. } /* end of snic_report_tgt_cmpl_handler */
  359. /* Discovery init fn */
  360. void
  361. snic_disc_init(struct snic_disc *disc)
  362. {
  363. INIT_LIST_HEAD(&disc->tgt_list);
  364. mutex_init(&disc->mutex);
  365. disc->disc_id = 0;
  366. disc->nxt_tgt_id = 0;
  367. disc->state = SNIC_DISC_INIT;
  368. disc->req_cnt = 0;
  369. disc->rtgt_cnt = 0;
  370. disc->rtgt_info = NULL;
  371. disc->cb = NULL;
  372. } /* end of snic_disc_init */
  373. /* Discovery, uninit fn */
  374. void
  375. snic_disc_term(struct snic *snic)
  376. {
  377. struct snic_disc *disc = &snic->disc;
  378. mutex_lock(&disc->mutex);
  379. if (disc->req_cnt) {
  380. disc->req_cnt = 0;
  381. SNIC_SCSI_DBG(snic->shost, "Terminating Discovery.\n");
  382. }
  383. mutex_unlock(&disc->mutex);
  384. }
  385. /*
  386. * snic_disc_start: Discovery Start ...
  387. */
  388. int
  389. snic_disc_start(struct snic *snic)
  390. {
  391. struct snic_disc *disc = &snic->disc;
  392. unsigned long flags;
  393. int ret = 0;
  394. SNIC_SCSI_DBG(snic->shost, "Discovery Start.\n");
  395. spin_lock_irqsave(&snic->snic_lock, flags);
  396. if (snic->in_remove) {
  397. spin_unlock_irqrestore(&snic->snic_lock, flags);
  398. SNIC_ERR("snic driver removal in progress ...\n");
  399. ret = 0;
  400. return ret;
  401. }
  402. spin_unlock_irqrestore(&snic->snic_lock, flags);
  403. mutex_lock(&disc->mutex);
  404. if (disc->state == SNIC_DISC_PENDING) {
  405. disc->req_cnt++;
  406. mutex_unlock(&disc->mutex);
  407. return ret;
  408. }
  409. disc->state = SNIC_DISC_PENDING;
  410. mutex_unlock(&disc->mutex);
  411. ret = snic_queue_report_tgt_req(snic);
  412. if (ret)
  413. SNIC_HOST_INFO(snic->shost, "Discovery Failed, err=%d.\n", ret);
  414. return ret;
  415. } /* end of snic_disc_start */
  416. /*
  417. * snic_disc_work :
  418. */
  419. void
  420. snic_handle_disc(struct work_struct *work)
  421. {
  422. struct snic *snic = container_of(work, struct snic, disc_work);
  423. int ret = 0;
  424. SNIC_HOST_INFO(snic->shost, "disc_work: Discovery\n");
  425. ret = snic_disc_start(snic);
  426. if (ret)
  427. goto disc_err;
  428. disc_err:
  429. SNIC_HOST_ERR(snic->shost,
  430. "disc_work: Discovery Failed w/ err = %d\n",
  431. ret);
  432. } /* end of snic_disc_work */
  433. /*
  434. * snic_tgt_del_all : cleanup all snic targets
  435. * Called on unbinding the interface
  436. */
  437. void
  438. snic_tgt_del_all(struct snic *snic)
  439. {
  440. struct snic_tgt *tgt = NULL;
  441. struct list_head *cur, *nxt;
  442. unsigned long flags;
  443. scsi_flush_work(snic->shost);
  444. mutex_lock(&snic->disc.mutex);
  445. spin_lock_irqsave(snic->shost->host_lock, flags);
  446. list_for_each_safe(cur, nxt, &snic->disc.tgt_list) {
  447. tgt = list_entry(cur, struct snic_tgt, list);
  448. tgt->state = SNIC_TGT_STAT_DEL;
  449. list_del_init(&tgt->list);
  450. SNIC_HOST_INFO(snic->shost, "Tgt %d q'ing for del\n", tgt->id);
  451. queue_work(snic_glob->event_q, &tgt->del_work);
  452. tgt = NULL;
  453. }
  454. spin_unlock_irqrestore(snic->shost->host_lock, flags);
  455. mutex_unlock(&snic->disc.mutex);
  456. flush_workqueue(snic_glob->event_q);
  457. } /* end of snic_tgt_del_all */