bfad_im.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
  4. * Copyright (c) 2014- QLogic Corporation.
  5. * All rights reserved
  6. * www.qlogic.com
  7. *
  8. * Linux driver for QLogic BR-series Fibre Channel Host Bus Adapter.
  9. */
  10. /*
  11. * bfad_im.c Linux driver IM module.
  12. */
  13. #include <linux/export.h>
  14. #include "bfad_drv.h"
  15. #include "bfad_im.h"
  16. #include "bfa_fcs.h"
  17. BFA_TRC_FILE(LDRV, IM);
  18. DEFINE_IDR(bfad_im_port_index);
  19. struct scsi_transport_template *bfad_im_scsi_transport_template;
  20. struct scsi_transport_template *bfad_im_scsi_vport_transport_template;
  21. static void bfad_im_itnim_work_handler(struct work_struct *work);
  22. static int bfad_im_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmnd);
  23. static int bfad_im_slave_alloc(struct scsi_device *sdev);
  24. static void bfad_im_fc_rport_add(struct bfad_im_port_s *im_port,
  25. struct bfad_itnim_s *itnim);
  26. void
  27. bfa_cb_ioim_done(void *drv, struct bfad_ioim_s *dio,
  28. enum bfi_ioim_status io_status, u8 scsi_status,
  29. int sns_len, u8 *sns_info, s32 residue)
  30. {
  31. struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
  32. struct bfad_s *bfad = drv;
  33. struct bfad_itnim_data_s *itnim_data;
  34. struct bfad_itnim_s *itnim;
  35. u8 host_status = DID_OK;
  36. switch (io_status) {
  37. case BFI_IOIM_STS_OK:
  38. bfa_trc(bfad, scsi_status);
  39. scsi_set_resid(cmnd, 0);
  40. if (sns_len > 0) {
  41. bfa_trc(bfad, sns_len);
  42. if (sns_len > SCSI_SENSE_BUFFERSIZE)
  43. sns_len = SCSI_SENSE_BUFFERSIZE;
  44. memcpy(cmnd->sense_buffer, sns_info, sns_len);
  45. }
  46. if (residue > 0) {
  47. bfa_trc(bfad, residue);
  48. scsi_set_resid(cmnd, residue);
  49. if (!sns_len && (scsi_status == SAM_STAT_GOOD) &&
  50. (scsi_bufflen(cmnd) - residue) <
  51. cmnd->underflow) {
  52. bfa_trc(bfad, 0);
  53. host_status = DID_ERROR;
  54. }
  55. }
  56. cmnd->result = host_status << 16 | scsi_status;
  57. break;
  58. case BFI_IOIM_STS_TIMEDOUT:
  59. cmnd->result = DID_TIME_OUT << 16;
  60. break;
  61. case BFI_IOIM_STS_PATHTOV:
  62. cmnd->result = DID_TRANSPORT_DISRUPTED << 16;
  63. break;
  64. default:
  65. cmnd->result = DID_ERROR << 16;
  66. }
  67. /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
  68. if (cmnd->device->host != NULL)
  69. scsi_dma_unmap(cmnd);
  70. cmnd->host_scribble = NULL;
  71. bfa_trc(bfad, cmnd->result);
  72. itnim_data = cmnd->device->hostdata;
  73. if (itnim_data) {
  74. itnim = itnim_data->itnim;
  75. if (!cmnd->result && itnim &&
  76. (bfa_lun_queue_depth > cmnd->device->queue_depth)) {
  77. /* Queue depth adjustment for good status completion */
  78. bfad_ramp_up_qdepth(itnim, cmnd->device);
  79. } else if (cmnd->result == SAM_STAT_TASK_SET_FULL && itnim) {
  80. /* qfull handling */
  81. bfad_handle_qfull(itnim, cmnd->device);
  82. }
  83. }
  84. scsi_done(cmnd);
  85. }
  86. void
  87. bfa_cb_ioim_good_comp(void *drv, struct bfad_ioim_s *dio)
  88. {
  89. struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
  90. struct bfad_itnim_data_s *itnim_data;
  91. struct bfad_itnim_s *itnim;
  92. cmnd->result = DID_OK << 16 | SAM_STAT_GOOD;
  93. /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
  94. if (cmnd->device->host != NULL)
  95. scsi_dma_unmap(cmnd);
  96. cmnd->host_scribble = NULL;
  97. /* Queue depth adjustment */
  98. if (bfa_lun_queue_depth > cmnd->device->queue_depth) {
  99. itnim_data = cmnd->device->hostdata;
  100. if (itnim_data) {
  101. itnim = itnim_data->itnim;
  102. if (itnim)
  103. bfad_ramp_up_qdepth(itnim, cmnd->device);
  104. }
  105. }
  106. scsi_done(cmnd);
  107. }
  108. void
  109. bfa_cb_ioim_abort(void *drv, struct bfad_ioim_s *dio)
  110. {
  111. struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
  112. struct bfad_s *bfad = drv;
  113. cmnd->result = DID_ERROR << 16;
  114. /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
  115. if (cmnd->device->host != NULL)
  116. scsi_dma_unmap(cmnd);
  117. bfa_trc(bfad, cmnd->result);
  118. cmnd->host_scribble = NULL;
  119. }
  120. void
  121. bfa_cb_tskim_done(void *bfad, struct bfad_tskim_s *dtsk,
  122. enum bfi_tskim_status tsk_status)
  123. {
  124. struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dtsk;
  125. wait_queue_head_t *wq;
  126. bfad_priv(cmnd)->status |= tsk_status << 1;
  127. set_bit(IO_DONE_BIT, &bfad_priv(cmnd)->status);
  128. wq = bfad_priv(cmnd)->wq;
  129. bfad_priv(cmnd)->wq = NULL;
  130. if (wq)
  131. wake_up(wq);
  132. }
  133. /*
  134. * Scsi_Host_template SCSI host template
  135. */
  136. /*
  137. * Scsi_Host template entry, returns BFAD PCI info.
  138. */
  139. static const char *
  140. bfad_im_info(struct Scsi_Host *shost)
  141. {
  142. static char bfa_buf[256];
  143. struct bfad_im_port_s *im_port =
  144. (struct bfad_im_port_s *) shost->hostdata[0];
  145. struct bfad_s *bfad = im_port->bfad;
  146. memset(bfa_buf, 0, sizeof(bfa_buf));
  147. snprintf(bfa_buf, sizeof(bfa_buf),
  148. "QLogic BR-series FC/FCOE Adapter, hwpath: %s driver: %s",
  149. bfad->pci_name, BFAD_DRIVER_VERSION);
  150. return bfa_buf;
  151. }
  152. /*
  153. * Scsi_Host template entry, aborts the specified SCSI command.
  154. *
  155. * Returns: SUCCESS or FAILED.
  156. */
  157. static int
  158. bfad_im_abort_handler(struct scsi_cmnd *cmnd)
  159. {
  160. struct Scsi_Host *shost = cmnd->device->host;
  161. struct bfad_im_port_s *im_port =
  162. (struct bfad_im_port_s *) shost->hostdata[0];
  163. struct bfad_s *bfad = im_port->bfad;
  164. struct bfa_ioim_s *hal_io;
  165. unsigned long flags;
  166. u32 timeout;
  167. int rc = FAILED;
  168. spin_lock_irqsave(&bfad->bfad_lock, flags);
  169. hal_io = (struct bfa_ioim_s *) cmnd->host_scribble;
  170. if (!hal_io) {
  171. /* IO has been completed, return success */
  172. rc = SUCCESS;
  173. goto out;
  174. }
  175. if (hal_io->dio != (struct bfad_ioim_s *) cmnd) {
  176. rc = FAILED;
  177. goto out;
  178. }
  179. bfa_trc(bfad, hal_io->iotag);
  180. BFA_LOG(KERN_INFO, bfad, bfa_log_level,
  181. "scsi%d: abort cmnd %p iotag %x\n",
  182. im_port->shost->host_no, cmnd, hal_io->iotag);
  183. (void) bfa_ioim_abort(hal_io);
  184. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  185. /* Need to wait until the command get aborted */
  186. timeout = 10;
  187. while ((struct bfa_ioim_s *) cmnd->host_scribble == hal_io) {
  188. set_current_state(TASK_UNINTERRUPTIBLE);
  189. schedule_timeout(timeout);
  190. if (timeout < 4 * HZ)
  191. timeout *= 2;
  192. }
  193. scsi_done(cmnd);
  194. bfa_trc(bfad, hal_io->iotag);
  195. BFA_LOG(KERN_INFO, bfad, bfa_log_level,
  196. "scsi%d: complete abort 0x%p iotag 0x%x\n",
  197. im_port->shost->host_no, cmnd, hal_io->iotag);
  198. return SUCCESS;
  199. out:
  200. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  201. return rc;
  202. }
  203. static bfa_status_t
  204. bfad_im_target_reset_send(struct bfad_s *bfad, struct scsi_cmnd *cmnd,
  205. struct bfad_itnim_s *itnim)
  206. {
  207. struct bfa_tskim_s *tskim;
  208. struct bfa_itnim_s *bfa_itnim;
  209. bfa_status_t rc = BFA_STATUS_OK;
  210. struct scsi_lun scsilun;
  211. tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
  212. if (!tskim) {
  213. BFA_LOG(KERN_ERR, bfad, bfa_log_level,
  214. "target reset, fail to allocate tskim\n");
  215. rc = BFA_STATUS_FAILED;
  216. goto out;
  217. }
  218. /*
  219. * Set host_scribble to NULL to avoid aborting a task command if
  220. * happens.
  221. */
  222. cmnd->host_scribble = NULL;
  223. bfad_priv(cmnd)->status = 0;
  224. bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
  225. /*
  226. * bfa_itnim can be NULL if the port gets disconnected and the bfa
  227. * and fcs layers have cleaned up their nexus with the targets and
  228. * the same has not been cleaned up by the shim
  229. */
  230. if (bfa_itnim == NULL) {
  231. bfa_tskim_free(tskim);
  232. BFA_LOG(KERN_ERR, bfad, bfa_log_level,
  233. "target reset, bfa_itnim is NULL\n");
  234. rc = BFA_STATUS_FAILED;
  235. goto out;
  236. }
  237. memset(&scsilun, 0, sizeof(scsilun));
  238. bfa_tskim_start(tskim, bfa_itnim, scsilun,
  239. FCP_TM_TARGET_RESET, BFAD_TARGET_RESET_TMO);
  240. out:
  241. return rc;
  242. }
  243. /*
  244. * Scsi_Host template entry, resets a LUN and abort its all commands.
  245. *
  246. * Returns: SUCCESS or FAILED.
  247. *
  248. */
  249. static int
  250. bfad_im_reset_lun_handler(struct scsi_cmnd *cmnd)
  251. {
  252. struct Scsi_Host *shost = cmnd->device->host;
  253. struct bfad_im_port_s *im_port =
  254. (struct bfad_im_port_s *) shost->hostdata[0];
  255. struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
  256. struct bfad_s *bfad = im_port->bfad;
  257. struct bfa_tskim_s *tskim;
  258. struct bfad_itnim_s *itnim;
  259. struct bfa_itnim_s *bfa_itnim;
  260. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  261. int rc = SUCCESS;
  262. unsigned long flags;
  263. enum bfi_tskim_status task_status;
  264. struct scsi_lun scsilun;
  265. spin_lock_irqsave(&bfad->bfad_lock, flags);
  266. itnim = itnim_data->itnim;
  267. if (!itnim) {
  268. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  269. rc = FAILED;
  270. goto out;
  271. }
  272. tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
  273. if (!tskim) {
  274. BFA_LOG(KERN_ERR, bfad, bfa_log_level,
  275. "LUN reset, fail to allocate tskim");
  276. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  277. rc = FAILED;
  278. goto out;
  279. }
  280. /*
  281. * Set host_scribble to NULL to avoid aborting a task command
  282. * if happens.
  283. */
  284. cmnd->host_scribble = NULL;
  285. bfad_priv(cmnd)->wq = &wq;
  286. bfad_priv(cmnd)->status = 0;
  287. bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
  288. /*
  289. * bfa_itnim can be NULL if the port gets disconnected and the bfa
  290. * and fcs layers have cleaned up their nexus with the targets and
  291. * the same has not been cleaned up by the shim
  292. */
  293. if (bfa_itnim == NULL) {
  294. bfa_tskim_free(tskim);
  295. BFA_LOG(KERN_ERR, bfad, bfa_log_level,
  296. "lun reset, bfa_itnim is NULL\n");
  297. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  298. rc = FAILED;
  299. goto out;
  300. }
  301. int_to_scsilun(cmnd->device->lun, &scsilun);
  302. bfa_tskim_start(tskim, bfa_itnim, scsilun,
  303. FCP_TM_LUN_RESET, BFAD_LUN_RESET_TMO);
  304. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  305. wait_event(wq, test_bit(IO_DONE_BIT, &bfad_priv(cmnd)->status));
  306. task_status = bfad_priv(cmnd)->status >> 1;
  307. if (task_status != BFI_TSKIM_STS_OK) {
  308. BFA_LOG(KERN_ERR, bfad, bfa_log_level,
  309. "LUN reset failure, status: %d\n", task_status);
  310. rc = FAILED;
  311. }
  312. out:
  313. return rc;
  314. }
  315. /*
  316. * Scsi_Host template entry, resets the target and abort all commands.
  317. */
  318. static int
  319. bfad_im_reset_target_handler(struct scsi_cmnd *cmnd)
  320. {
  321. struct Scsi_Host *shost = cmnd->device->host;
  322. struct scsi_target *starget = scsi_target(cmnd->device);
  323. struct bfad_im_port_s *im_port =
  324. (struct bfad_im_port_s *) shost->hostdata[0];
  325. struct bfad_s *bfad = im_port->bfad;
  326. struct bfad_itnim_s *itnim;
  327. unsigned long flags;
  328. u32 rc, rtn = FAILED;
  329. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  330. enum bfi_tskim_status task_status;
  331. spin_lock_irqsave(&bfad->bfad_lock, flags);
  332. itnim = bfad_get_itnim(im_port, starget->id);
  333. if (itnim) {
  334. bfad_priv(cmnd)->wq = &wq;
  335. rc = bfad_im_target_reset_send(bfad, cmnd, itnim);
  336. if (rc == BFA_STATUS_OK) {
  337. /* wait target reset to complete */
  338. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  339. wait_event(wq, test_bit(IO_DONE_BIT,
  340. &bfad_priv(cmnd)->status));
  341. spin_lock_irqsave(&bfad->bfad_lock, flags);
  342. task_status = bfad_priv(cmnd)->status >> 1;
  343. if (task_status != BFI_TSKIM_STS_OK)
  344. BFA_LOG(KERN_ERR, bfad, bfa_log_level,
  345. "target reset failure,"
  346. " status: %d\n", task_status);
  347. else
  348. rtn = SUCCESS;
  349. }
  350. }
  351. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  352. return rtn;
  353. }
  354. /*
  355. * Scsi_Host template entry slave_destroy.
  356. */
  357. static void
  358. bfad_im_slave_destroy(struct scsi_device *sdev)
  359. {
  360. sdev->hostdata = NULL;
  361. return;
  362. }
  363. /*
  364. * BFA FCS itnim callbacks
  365. */
  366. /*
  367. * BFA FCS itnim alloc callback, after successful PRLI
  368. * Context: Interrupt
  369. */
  370. int
  371. bfa_fcb_itnim_alloc(struct bfad_s *bfad, struct bfa_fcs_itnim_s **itnim,
  372. struct bfad_itnim_s **itnim_drv)
  373. {
  374. *itnim_drv = kzalloc(sizeof(struct bfad_itnim_s), GFP_ATOMIC);
  375. if (*itnim_drv == NULL)
  376. return -ENOMEM;
  377. (*itnim_drv)->im = bfad->im;
  378. *itnim = &(*itnim_drv)->fcs_itnim;
  379. (*itnim_drv)->state = ITNIM_STATE_NONE;
  380. /*
  381. * Initiaze the itnim_work
  382. */
  383. INIT_WORK(&(*itnim_drv)->itnim_work, bfad_im_itnim_work_handler);
  384. bfad->bfad_flags |= BFAD_RPORT_ONLINE;
  385. return 0;
  386. }
  387. /*
  388. * BFA FCS itnim free callback.
  389. * Context: Interrupt. bfad_lock is held
  390. */
  391. void
  392. bfa_fcb_itnim_free(struct bfad_s *bfad, struct bfad_itnim_s *itnim_drv)
  393. {
  394. struct bfad_port_s *port;
  395. wwn_t wwpn;
  396. u32 fcid;
  397. char wwpn_str[32], fcid_str[16];
  398. struct bfad_im_s *im = itnim_drv->im;
  399. /* online to free state transtion should not happen */
  400. WARN_ON(itnim_drv->state == ITNIM_STATE_ONLINE);
  401. itnim_drv->queue_work = 1;
  402. /* offline request is not yet done, use the same request to free */
  403. if (itnim_drv->state == ITNIM_STATE_OFFLINE_PENDING)
  404. itnim_drv->queue_work = 0;
  405. itnim_drv->state = ITNIM_STATE_FREE;
  406. port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
  407. itnim_drv->im_port = port->im_port;
  408. wwpn = bfa_fcs_itnim_get_pwwn(&itnim_drv->fcs_itnim);
  409. fcid = bfa_fcs_itnim_get_fcid(&itnim_drv->fcs_itnim);
  410. wwn2str(wwpn_str, wwpn);
  411. fcid2str(fcid_str, fcid);
  412. BFA_LOG(KERN_INFO, bfad, bfa_log_level,
  413. "ITNIM FREE scsi%d: FCID: %s WWPN: %s\n",
  414. port->im_port->shost->host_no,
  415. fcid_str, wwpn_str);
  416. /* ITNIM processing */
  417. if (itnim_drv->queue_work)
  418. queue_work(im->drv_workq, &itnim_drv->itnim_work);
  419. }
  420. /*
  421. * BFA FCS itnim online callback.
  422. * Context: Interrupt. bfad_lock is held
  423. */
  424. void
  425. bfa_fcb_itnim_online(struct bfad_itnim_s *itnim_drv)
  426. {
  427. struct bfad_port_s *port;
  428. struct bfad_im_s *im = itnim_drv->im;
  429. itnim_drv->bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim_drv->fcs_itnim);
  430. port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
  431. itnim_drv->state = ITNIM_STATE_ONLINE;
  432. itnim_drv->queue_work = 1;
  433. itnim_drv->im_port = port->im_port;
  434. /* ITNIM processing */
  435. if (itnim_drv->queue_work)
  436. queue_work(im->drv_workq, &itnim_drv->itnim_work);
  437. }
  438. /*
  439. * BFA FCS itnim offline callback.
  440. * Context: Interrupt. bfad_lock is held
  441. */
  442. void
  443. bfa_fcb_itnim_offline(struct bfad_itnim_s *itnim_drv)
  444. {
  445. struct bfad_port_s *port;
  446. struct bfad_s *bfad;
  447. struct bfad_im_s *im = itnim_drv->im;
  448. port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
  449. bfad = port->bfad;
  450. if ((bfad->pport.flags & BFAD_PORT_DELETE) ||
  451. (port->flags & BFAD_PORT_DELETE)) {
  452. itnim_drv->state = ITNIM_STATE_OFFLINE;
  453. return;
  454. }
  455. itnim_drv->im_port = port->im_port;
  456. itnim_drv->state = ITNIM_STATE_OFFLINE_PENDING;
  457. itnim_drv->queue_work = 1;
  458. /* ITNIM processing */
  459. if (itnim_drv->queue_work)
  460. queue_work(im->drv_workq, &itnim_drv->itnim_work);
  461. }
  462. /*
  463. * Allocate a Scsi_Host for a port.
  464. */
  465. int
  466. bfad_im_scsi_host_alloc(struct bfad_s *bfad, struct bfad_im_port_s *im_port,
  467. struct device *dev)
  468. {
  469. struct bfad_im_port_pointer *im_portp;
  470. int error;
  471. mutex_lock(&bfad_mutex);
  472. error = idr_alloc(&bfad_im_port_index, im_port, 0, 0, GFP_KERNEL);
  473. if (error < 0) {
  474. mutex_unlock(&bfad_mutex);
  475. printk(KERN_WARNING "idr_alloc failure\n");
  476. goto out;
  477. }
  478. im_port->idr_id = error;
  479. mutex_unlock(&bfad_mutex);
  480. im_port->shost = bfad_scsi_host_alloc(im_port, bfad);
  481. if (!im_port->shost) {
  482. error = 1;
  483. goto out_free_idr;
  484. }
  485. im_portp = shost_priv(im_port->shost);
  486. im_portp->p = im_port;
  487. im_port->shost->unique_id = im_port->idr_id;
  488. im_port->shost->this_id = -1;
  489. im_port->shost->max_id = MAX_FCP_TARGET;
  490. im_port->shost->max_lun = MAX_FCP_LUN;
  491. im_port->shost->max_cmd_len = 16;
  492. im_port->shost->can_queue = bfad->cfg_data.ioc_queue_depth;
  493. if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
  494. im_port->shost->transportt = bfad_im_scsi_transport_template;
  495. else
  496. im_port->shost->transportt =
  497. bfad_im_scsi_vport_transport_template;
  498. error = scsi_add_host_with_dma(im_port->shost, dev, &bfad->pcidev->dev);
  499. if (error) {
  500. printk(KERN_WARNING "scsi_add_host failure %d\n", error);
  501. goto out_fc_rel;
  502. }
  503. return 0;
  504. out_fc_rel:
  505. scsi_host_put(im_port->shost);
  506. im_port->shost = NULL;
  507. out_free_idr:
  508. mutex_lock(&bfad_mutex);
  509. idr_remove(&bfad_im_port_index, im_port->idr_id);
  510. mutex_unlock(&bfad_mutex);
  511. out:
  512. return error;
  513. }
  514. void
  515. bfad_im_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
  516. {
  517. bfa_trc(bfad, bfad->inst_no);
  518. BFA_LOG(KERN_INFO, bfad, bfa_log_level, "Free scsi%d\n",
  519. im_port->shost->host_no);
  520. fc_remove_host(im_port->shost);
  521. scsi_remove_host(im_port->shost);
  522. scsi_host_put(im_port->shost);
  523. mutex_lock(&bfad_mutex);
  524. idr_remove(&bfad_im_port_index, im_port->idr_id);
  525. mutex_unlock(&bfad_mutex);
  526. }
  527. static void
  528. bfad_im_port_delete_handler(struct work_struct *work)
  529. {
  530. struct bfad_im_port_s *im_port =
  531. container_of(work, struct bfad_im_port_s, port_delete_work);
  532. if (im_port->port->pvb_type != BFAD_PORT_PHYS_BASE) {
  533. im_port->flags |= BFAD_PORT_DELETE;
  534. fc_vport_terminate(im_port->fc_vport);
  535. }
  536. }
  537. bfa_status_t
  538. bfad_im_port_new(struct bfad_s *bfad, struct bfad_port_s *port)
  539. {
  540. int rc = BFA_STATUS_OK;
  541. struct bfad_im_port_s *im_port;
  542. im_port = kzalloc(sizeof(struct bfad_im_port_s), GFP_ATOMIC);
  543. if (im_port == NULL) {
  544. rc = BFA_STATUS_ENOMEM;
  545. goto ext;
  546. }
  547. port->im_port = im_port;
  548. im_port->port = port;
  549. im_port->bfad = bfad;
  550. INIT_WORK(&im_port->port_delete_work, bfad_im_port_delete_handler);
  551. INIT_LIST_HEAD(&im_port->itnim_mapped_list);
  552. INIT_LIST_HEAD(&im_port->binding_list);
  553. ext:
  554. return rc;
  555. }
  556. void
  557. bfad_im_port_delete(struct bfad_s *bfad, struct bfad_port_s *port)
  558. {
  559. struct bfad_im_port_s *im_port = port->im_port;
  560. queue_work(bfad->im->drv_workq,
  561. &im_port->port_delete_work);
  562. }
  563. void
  564. bfad_im_port_clean(struct bfad_im_port_s *im_port)
  565. {
  566. struct bfad_fcp_binding *bp, *bp_new;
  567. unsigned long flags;
  568. struct bfad_s *bfad = im_port->bfad;
  569. spin_lock_irqsave(&bfad->bfad_lock, flags);
  570. list_for_each_entry_safe(bp, bp_new, &im_port->binding_list,
  571. list_entry) {
  572. list_del(&bp->list_entry);
  573. kfree(bp);
  574. }
  575. /* the itnim_mapped_list must be empty at this time */
  576. WARN_ON(!list_empty(&im_port->itnim_mapped_list));
  577. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  578. }
  579. static void bfad_aen_im_notify_handler(struct work_struct *work)
  580. {
  581. struct bfad_im_s *im =
  582. container_of(work, struct bfad_im_s, aen_im_notify_work);
  583. struct bfa_aen_entry_s *aen_entry;
  584. struct bfad_s *bfad = im->bfad;
  585. struct Scsi_Host *shost = bfad->pport.im_port->shost;
  586. void *event_data;
  587. unsigned long flags;
  588. while (!list_empty(&bfad->active_aen_q)) {
  589. spin_lock_irqsave(&bfad->bfad_aen_spinlock, flags);
  590. bfa_q_deq(&bfad->active_aen_q, &aen_entry);
  591. spin_unlock_irqrestore(&bfad->bfad_aen_spinlock, flags);
  592. event_data = (char *)aen_entry + sizeof(struct list_head);
  593. fc_host_post_vendor_event(shost, fc_get_event_number(),
  594. sizeof(struct bfa_aen_entry_s) -
  595. sizeof(struct list_head),
  596. (char *)event_data, BFAD_NL_VENDOR_ID);
  597. spin_lock_irqsave(&bfad->bfad_aen_spinlock, flags);
  598. list_add_tail(&aen_entry->qe, &bfad->free_aen_q);
  599. spin_unlock_irqrestore(&bfad->bfad_aen_spinlock, flags);
  600. }
  601. }
  602. bfa_status_t
  603. bfad_im_probe(struct bfad_s *bfad)
  604. {
  605. struct bfad_im_s *im;
  606. im = kzalloc(sizeof(struct bfad_im_s), GFP_KERNEL);
  607. if (im == NULL)
  608. return BFA_STATUS_ENOMEM;
  609. bfad->im = im;
  610. im->bfad = bfad;
  611. if (bfad_thread_workq(bfad) != BFA_STATUS_OK) {
  612. kfree(im);
  613. return BFA_STATUS_FAILED;
  614. }
  615. INIT_WORK(&im->aen_im_notify_work, bfad_aen_im_notify_handler);
  616. return BFA_STATUS_OK;
  617. }
  618. void
  619. bfad_im_probe_undo(struct bfad_s *bfad)
  620. {
  621. if (bfad->im) {
  622. bfad_destroy_workq(bfad->im);
  623. kfree(bfad->im);
  624. bfad->im = NULL;
  625. }
  626. }
  627. struct Scsi_Host *
  628. bfad_scsi_host_alloc(struct bfad_im_port_s *im_port, struct bfad_s *bfad)
  629. {
  630. struct scsi_host_template *sht;
  631. if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
  632. sht = &bfad_im_scsi_host_template;
  633. else
  634. sht = &bfad_im_vport_template;
  635. if (max_xfer_size != BFAD_MAX_SECTORS >> 1)
  636. sht->max_sectors = max_xfer_size << 1;
  637. sht->sg_tablesize = bfad->cfg_data.io_max_sge;
  638. return scsi_host_alloc(sht, sizeof(struct bfad_im_port_pointer));
  639. }
  640. void
  641. bfad_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
  642. {
  643. if (!(im_port->flags & BFAD_PORT_DELETE))
  644. flush_workqueue(bfad->im->drv_workq);
  645. bfad_im_scsi_host_free(im_port->bfad, im_port);
  646. bfad_im_port_clean(im_port);
  647. kfree(im_port);
  648. }
  649. void
  650. bfad_destroy_workq(struct bfad_im_s *im)
  651. {
  652. if (im && im->drv_workq) {
  653. destroy_workqueue(im->drv_workq);
  654. im->drv_workq = NULL;
  655. }
  656. }
  657. bfa_status_t
  658. bfad_thread_workq(struct bfad_s *bfad)
  659. {
  660. struct bfad_im_s *im = bfad->im;
  661. bfa_trc(bfad, 0);
  662. snprintf(im->drv_workq_name, KOBJ_NAME_LEN, "bfad_wq_%d",
  663. bfad->inst_no);
  664. im->drv_workq = create_singlethread_workqueue(im->drv_workq_name);
  665. if (!im->drv_workq)
  666. return BFA_STATUS_FAILED;
  667. return BFA_STATUS_OK;
  668. }
  669. /*
  670. * Scsi_Host template entry.
  671. *
  672. * Description:
  673. * OS entry point to adjust the queue_depths on a per-device basis.
  674. * Called once per device during the bus scan.
  675. * Return non-zero if fails.
  676. */
  677. static int
  678. bfad_im_slave_configure(struct scsi_device *sdev)
  679. {
  680. scsi_change_queue_depth(sdev, bfa_lun_queue_depth);
  681. return 0;
  682. }
  683. struct scsi_host_template bfad_im_scsi_host_template = {
  684. .module = THIS_MODULE,
  685. .name = BFAD_DRIVER_NAME,
  686. .info = bfad_im_info,
  687. .queuecommand = bfad_im_queuecommand,
  688. .cmd_size = sizeof(struct bfad_cmd_priv),
  689. .eh_timed_out = fc_eh_timed_out,
  690. .eh_abort_handler = bfad_im_abort_handler,
  691. .eh_device_reset_handler = bfad_im_reset_lun_handler,
  692. .eh_target_reset_handler = bfad_im_reset_target_handler,
  693. .slave_alloc = bfad_im_slave_alloc,
  694. .slave_configure = bfad_im_slave_configure,
  695. .slave_destroy = bfad_im_slave_destroy,
  696. .this_id = -1,
  697. .sg_tablesize = BFAD_IO_MAX_SGE,
  698. .cmd_per_lun = 3,
  699. .shost_groups = bfad_im_host_groups,
  700. .max_sectors = BFAD_MAX_SECTORS,
  701. .vendor_id = BFA_PCI_VENDOR_ID_BROCADE,
  702. };
  703. struct scsi_host_template bfad_im_vport_template = {
  704. .module = THIS_MODULE,
  705. .name = BFAD_DRIVER_NAME,
  706. .info = bfad_im_info,
  707. .queuecommand = bfad_im_queuecommand,
  708. .cmd_size = sizeof(struct bfad_cmd_priv),
  709. .eh_timed_out = fc_eh_timed_out,
  710. .eh_abort_handler = bfad_im_abort_handler,
  711. .eh_device_reset_handler = bfad_im_reset_lun_handler,
  712. .eh_target_reset_handler = bfad_im_reset_target_handler,
  713. .slave_alloc = bfad_im_slave_alloc,
  714. .slave_configure = bfad_im_slave_configure,
  715. .slave_destroy = bfad_im_slave_destroy,
  716. .this_id = -1,
  717. .sg_tablesize = BFAD_IO_MAX_SGE,
  718. .cmd_per_lun = 3,
  719. .shost_groups = bfad_im_vport_groups,
  720. .max_sectors = BFAD_MAX_SECTORS,
  721. };
  722. bfa_status_t
  723. bfad_im_module_init(void)
  724. {
  725. bfad_im_scsi_transport_template =
  726. fc_attach_transport(&bfad_im_fc_function_template);
  727. if (!bfad_im_scsi_transport_template)
  728. return BFA_STATUS_ENOMEM;
  729. bfad_im_scsi_vport_transport_template =
  730. fc_attach_transport(&bfad_im_vport_fc_function_template);
  731. if (!bfad_im_scsi_vport_transport_template) {
  732. fc_release_transport(bfad_im_scsi_transport_template);
  733. return BFA_STATUS_ENOMEM;
  734. }
  735. return BFA_STATUS_OK;
  736. }
  737. void
  738. bfad_im_module_exit(void)
  739. {
  740. if (bfad_im_scsi_transport_template)
  741. fc_release_transport(bfad_im_scsi_transport_template);
  742. if (bfad_im_scsi_vport_transport_template)
  743. fc_release_transport(bfad_im_scsi_vport_transport_template);
  744. idr_destroy(&bfad_im_port_index);
  745. }
  746. void
  747. bfad_ramp_up_qdepth(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
  748. {
  749. struct scsi_device *tmp_sdev;
  750. if (((jiffies - itnim->last_ramp_up_time) >
  751. BFA_QUEUE_FULL_RAMP_UP_TIME * HZ) &&
  752. ((jiffies - itnim->last_queue_full_time) >
  753. BFA_QUEUE_FULL_RAMP_UP_TIME * HZ)) {
  754. shost_for_each_device(tmp_sdev, sdev->host) {
  755. if (bfa_lun_queue_depth > tmp_sdev->queue_depth) {
  756. if (tmp_sdev->id != sdev->id)
  757. continue;
  758. scsi_change_queue_depth(tmp_sdev,
  759. tmp_sdev->queue_depth + 1);
  760. itnim->last_ramp_up_time = jiffies;
  761. }
  762. }
  763. }
  764. }
  765. void
  766. bfad_handle_qfull(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
  767. {
  768. struct scsi_device *tmp_sdev;
  769. itnim->last_queue_full_time = jiffies;
  770. shost_for_each_device(tmp_sdev, sdev->host) {
  771. if (tmp_sdev->id != sdev->id)
  772. continue;
  773. scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
  774. }
  775. }
  776. struct bfad_itnim_s *
  777. bfad_get_itnim(struct bfad_im_port_s *im_port, int id)
  778. {
  779. struct bfad_itnim_s *itnim = NULL;
  780. /* Search the mapped list for this target ID */
  781. list_for_each_entry(itnim, &im_port->itnim_mapped_list, list_entry) {
  782. if (id == itnim->scsi_tgt_id)
  783. return itnim;
  784. }
  785. return NULL;
  786. }
  787. /*
  788. * Function is invoked from the SCSI Host Template slave_alloc() entry point.
  789. * Has the logic to query the LUN Mask database to check if this LUN needs to
  790. * be made visible to the SCSI mid-layer or not.
  791. *
  792. * Returns BFA_STATUS_OK if this LUN needs to be added to the OS stack.
  793. * Returns -ENXIO to notify SCSI mid-layer to not add this LUN to the OS stack.
  794. */
  795. static int
  796. bfad_im_check_if_make_lun_visible(struct scsi_device *sdev,
  797. struct fc_rport *rport)
  798. {
  799. struct bfad_itnim_data_s *itnim_data =
  800. (struct bfad_itnim_data_s *) rport->dd_data;
  801. struct bfa_s *bfa = itnim_data->itnim->bfa_itnim->bfa;
  802. struct bfa_rport_s *bfa_rport = itnim_data->itnim->bfa_itnim->rport;
  803. struct bfa_lun_mask_s *lun_list = bfa_get_lun_mask_list(bfa);
  804. int i = 0, ret = -ENXIO;
  805. for (i = 0; i < MAX_LUN_MASK_CFG; i++) {
  806. if (lun_list[i].state == BFA_IOIM_LUN_MASK_ACTIVE &&
  807. scsilun_to_int(&lun_list[i].lun) == sdev->lun &&
  808. lun_list[i].rp_tag == bfa_rport->rport_tag &&
  809. lun_list[i].lp_tag == (u8)bfa_rport->rport_info.lp_tag) {
  810. ret = BFA_STATUS_OK;
  811. break;
  812. }
  813. }
  814. return ret;
  815. }
  816. /*
  817. * Scsi_Host template entry slave_alloc
  818. */
  819. static int
  820. bfad_im_slave_alloc(struct scsi_device *sdev)
  821. {
  822. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  823. struct bfad_itnim_data_s *itnim_data;
  824. struct bfa_s *bfa;
  825. if (!rport || fc_remote_port_chkready(rport))
  826. return -ENXIO;
  827. itnim_data = (struct bfad_itnim_data_s *) rport->dd_data;
  828. bfa = itnim_data->itnim->bfa_itnim->bfa;
  829. if (bfa_get_lun_mask_status(bfa) == BFA_LUNMASK_ENABLED) {
  830. /*
  831. * We should not mask LUN 0 - since this will translate
  832. * to no LUN / TARGET for SCSI ml resulting no scan.
  833. */
  834. if (sdev->lun == 0) {
  835. sdev->sdev_bflags |= BLIST_NOREPORTLUN |
  836. BLIST_SPARSELUN;
  837. goto done;
  838. }
  839. /*
  840. * Query LUN Mask configuration - to expose this LUN
  841. * to the SCSI mid-layer or to mask it.
  842. */
  843. if (bfad_im_check_if_make_lun_visible(sdev, rport) !=
  844. BFA_STATUS_OK)
  845. return -ENXIO;
  846. }
  847. done:
  848. sdev->hostdata = rport->dd_data;
  849. return 0;
  850. }
  851. u32
  852. bfad_im_supported_speeds(struct bfa_s *bfa)
  853. {
  854. struct bfa_ioc_attr_s *ioc_attr;
  855. u32 supported_speed = 0;
  856. ioc_attr = kzalloc(sizeof(struct bfa_ioc_attr_s), GFP_KERNEL);
  857. if (!ioc_attr)
  858. return 0;
  859. bfa_ioc_get_attr(&bfa->ioc, ioc_attr);
  860. if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_16GBPS)
  861. supported_speed |= FC_PORTSPEED_16GBIT | FC_PORTSPEED_8GBIT |
  862. FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT;
  863. else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_8GBPS) {
  864. if (ioc_attr->adapter_attr.is_mezz) {
  865. supported_speed |= FC_PORTSPEED_8GBIT |
  866. FC_PORTSPEED_4GBIT |
  867. FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
  868. } else {
  869. supported_speed |= FC_PORTSPEED_8GBIT |
  870. FC_PORTSPEED_4GBIT |
  871. FC_PORTSPEED_2GBIT;
  872. }
  873. } else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_4GBPS) {
  874. supported_speed |= FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
  875. FC_PORTSPEED_1GBIT;
  876. } else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_10GBPS) {
  877. supported_speed |= FC_PORTSPEED_10GBIT;
  878. }
  879. kfree(ioc_attr);
  880. return supported_speed;
  881. }
  882. void
  883. bfad_fc_host_init(struct bfad_im_port_s *im_port)
  884. {
  885. struct Scsi_Host *host = im_port->shost;
  886. struct bfad_s *bfad = im_port->bfad;
  887. struct bfad_port_s *port = im_port->port;
  888. char symname[BFA_SYMNAME_MAXLEN];
  889. struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(&bfad->bfa);
  890. fc_host_node_name(host) =
  891. cpu_to_be64((bfa_fcs_lport_get_nwwn(port->fcs_port)));
  892. fc_host_port_name(host) =
  893. cpu_to_be64((bfa_fcs_lport_get_pwwn(port->fcs_port)));
  894. fc_host_max_npiv_vports(host) = bfa_lps_get_max_vport(&bfad->bfa);
  895. fc_host_supported_classes(host) = FC_COS_CLASS3;
  896. memset(fc_host_supported_fc4s(host), 0,
  897. sizeof(fc_host_supported_fc4s(host)));
  898. if (supported_fc4s & BFA_LPORT_ROLE_FCP_IM)
  899. /* For FCP type 0x08 */
  900. fc_host_supported_fc4s(host)[2] = 1;
  901. /* For fibre channel services type 0x20 */
  902. fc_host_supported_fc4s(host)[7] = 1;
  903. strlcpy(symname, bfad->bfa_fcs.fabric.bport.port_cfg.sym_name.symname,
  904. BFA_SYMNAME_MAXLEN);
  905. sprintf(fc_host_symbolic_name(host), "%s", symname);
  906. fc_host_supported_speeds(host) = bfad_im_supported_speeds(&bfad->bfa);
  907. fc_host_maxframe_size(host) = fcport->cfg.maxfrsize;
  908. }
  909. static void
  910. bfad_im_fc_rport_add(struct bfad_im_port_s *im_port, struct bfad_itnim_s *itnim)
  911. {
  912. struct fc_rport_identifiers rport_ids;
  913. struct fc_rport *fc_rport;
  914. struct bfad_itnim_data_s *itnim_data;
  915. rport_ids.node_name =
  916. cpu_to_be64(bfa_fcs_itnim_get_nwwn(&itnim->fcs_itnim));
  917. rport_ids.port_name =
  918. cpu_to_be64(bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
  919. rport_ids.port_id =
  920. bfa_hton3b(bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim));
  921. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  922. itnim->fc_rport = fc_rport =
  923. fc_remote_port_add(im_port->shost, 0, &rport_ids);
  924. if (!fc_rport)
  925. return;
  926. fc_rport->maxframe_size =
  927. bfa_fcs_itnim_get_maxfrsize(&itnim->fcs_itnim);
  928. fc_rport->supported_classes = bfa_fcs_itnim_get_cos(&itnim->fcs_itnim);
  929. itnim_data = fc_rport->dd_data;
  930. itnim_data->itnim = itnim;
  931. rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
  932. if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
  933. fc_remote_port_rolechg(fc_rport, rport_ids.roles);
  934. if ((fc_rport->scsi_target_id != -1)
  935. && (fc_rport->scsi_target_id < MAX_FCP_TARGET))
  936. itnim->scsi_tgt_id = fc_rport->scsi_target_id;
  937. itnim->channel = fc_rport->channel;
  938. return;
  939. }
  940. /*
  941. * Work queue handler using FC transport service
  942. * Context: kernel
  943. */
  944. static void
  945. bfad_im_itnim_work_handler(struct work_struct *work)
  946. {
  947. struct bfad_itnim_s *itnim = container_of(work, struct bfad_itnim_s,
  948. itnim_work);
  949. struct bfad_im_s *im = itnim->im;
  950. struct bfad_s *bfad = im->bfad;
  951. struct bfad_im_port_s *im_port;
  952. unsigned long flags;
  953. struct fc_rport *fc_rport;
  954. wwn_t wwpn;
  955. u32 fcid;
  956. char wwpn_str[32], fcid_str[16];
  957. spin_lock_irqsave(&bfad->bfad_lock, flags);
  958. im_port = itnim->im_port;
  959. bfa_trc(bfad, itnim->state);
  960. switch (itnim->state) {
  961. case ITNIM_STATE_ONLINE:
  962. if (!itnim->fc_rport) {
  963. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  964. bfad_im_fc_rport_add(im_port, itnim);
  965. spin_lock_irqsave(&bfad->bfad_lock, flags);
  966. wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
  967. fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
  968. wwn2str(wwpn_str, wwpn);
  969. fcid2str(fcid_str, fcid);
  970. list_add_tail(&itnim->list_entry,
  971. &im_port->itnim_mapped_list);
  972. BFA_LOG(KERN_INFO, bfad, bfa_log_level,
  973. "ITNIM ONLINE Target: %d:0:%d "
  974. "FCID: %s WWPN: %s\n",
  975. im_port->shost->host_no,
  976. itnim->scsi_tgt_id,
  977. fcid_str, wwpn_str);
  978. } else {
  979. printk(KERN_WARNING
  980. "%s: itnim %llx is already in online state\n",
  981. __func__,
  982. bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
  983. }
  984. break;
  985. case ITNIM_STATE_OFFLINE_PENDING:
  986. itnim->state = ITNIM_STATE_OFFLINE;
  987. if (itnim->fc_rport) {
  988. fc_rport = itnim->fc_rport;
  989. ((struct bfad_itnim_data_s *)
  990. fc_rport->dd_data)->itnim = NULL;
  991. itnim->fc_rport = NULL;
  992. if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
  993. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  994. fc_rport->dev_loss_tmo =
  995. bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
  996. fc_remote_port_delete(fc_rport);
  997. spin_lock_irqsave(&bfad->bfad_lock, flags);
  998. }
  999. wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
  1000. fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
  1001. wwn2str(wwpn_str, wwpn);
  1002. fcid2str(fcid_str, fcid);
  1003. list_del(&itnim->list_entry);
  1004. BFA_LOG(KERN_INFO, bfad, bfa_log_level,
  1005. "ITNIM OFFLINE Target: %d:0:%d "
  1006. "FCID: %s WWPN: %s\n",
  1007. im_port->shost->host_no,
  1008. itnim->scsi_tgt_id,
  1009. fcid_str, wwpn_str);
  1010. }
  1011. break;
  1012. case ITNIM_STATE_FREE:
  1013. if (itnim->fc_rport) {
  1014. fc_rport = itnim->fc_rport;
  1015. ((struct bfad_itnim_data_s *)
  1016. fc_rport->dd_data)->itnim = NULL;
  1017. itnim->fc_rport = NULL;
  1018. if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
  1019. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  1020. fc_rport->dev_loss_tmo =
  1021. bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
  1022. fc_remote_port_delete(fc_rport);
  1023. spin_lock_irqsave(&bfad->bfad_lock, flags);
  1024. }
  1025. list_del(&itnim->list_entry);
  1026. }
  1027. kfree(itnim);
  1028. break;
  1029. default:
  1030. WARN_ON(1);
  1031. break;
  1032. }
  1033. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  1034. }
  1035. /*
  1036. * Scsi_Host template entry, queue a SCSI command to the BFAD.
  1037. */
  1038. static int bfad_im_queuecommand_lck(struct scsi_cmnd *cmnd)
  1039. {
  1040. void (*done)(struct scsi_cmnd *) = scsi_done;
  1041. struct bfad_im_port_s *im_port =
  1042. (struct bfad_im_port_s *) cmnd->device->host->hostdata[0];
  1043. struct bfad_s *bfad = im_port->bfad;
  1044. struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
  1045. struct bfad_itnim_s *itnim;
  1046. struct bfa_ioim_s *hal_io;
  1047. unsigned long flags;
  1048. int rc;
  1049. int sg_cnt = 0;
  1050. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  1051. rc = fc_remote_port_chkready(rport);
  1052. if (rc) {
  1053. cmnd->result = rc;
  1054. done(cmnd);
  1055. return 0;
  1056. }
  1057. if (bfad->bfad_flags & BFAD_EEH_BUSY) {
  1058. if (bfad->bfad_flags & BFAD_EEH_PCI_CHANNEL_IO_PERM_FAILURE)
  1059. cmnd->result = DID_NO_CONNECT << 16;
  1060. else
  1061. cmnd->result = DID_REQUEUE << 16;
  1062. done(cmnd);
  1063. return 0;
  1064. }
  1065. sg_cnt = scsi_dma_map(cmnd);
  1066. if (sg_cnt < 0)
  1067. return SCSI_MLQUEUE_HOST_BUSY;
  1068. spin_lock_irqsave(&bfad->bfad_lock, flags);
  1069. if (!(bfad->bfad_flags & BFAD_HAL_START_DONE)) {
  1070. printk(KERN_WARNING
  1071. "bfad%d, queuecommand %p %x failed, BFA stopped\n",
  1072. bfad->inst_no, cmnd, cmnd->cmnd[0]);
  1073. cmnd->result = DID_NO_CONNECT << 16;
  1074. goto out_fail_cmd;
  1075. }
  1076. itnim = itnim_data->itnim;
  1077. if (!itnim) {
  1078. cmnd->result = DID_IMM_RETRY << 16;
  1079. goto out_fail_cmd;
  1080. }
  1081. hal_io = bfa_ioim_alloc(&bfad->bfa, (struct bfad_ioim_s *) cmnd,
  1082. itnim->bfa_itnim, sg_cnt);
  1083. if (!hal_io) {
  1084. printk(KERN_WARNING "hal_io failure\n");
  1085. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  1086. scsi_dma_unmap(cmnd);
  1087. return SCSI_MLQUEUE_HOST_BUSY;
  1088. }
  1089. cmnd->host_scribble = (char *)hal_io;
  1090. bfa_ioim_start(hal_io);
  1091. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  1092. return 0;
  1093. out_fail_cmd:
  1094. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  1095. scsi_dma_unmap(cmnd);
  1096. if (done)
  1097. done(cmnd);
  1098. return 0;
  1099. }
  1100. static DEF_SCSI_QCMD(bfad_im_queuecommand)
  1101. void
  1102. bfad_rport_online_wait(struct bfad_s *bfad)
  1103. {
  1104. int i;
  1105. int rport_delay = 10;
  1106. for (i = 0; !(bfad->bfad_flags & BFAD_PORT_ONLINE)
  1107. && i < bfa_linkup_delay; i++) {
  1108. set_current_state(TASK_UNINTERRUPTIBLE);
  1109. schedule_timeout(HZ);
  1110. }
  1111. if (bfad->bfad_flags & BFAD_PORT_ONLINE) {
  1112. rport_delay = rport_delay < bfa_linkup_delay ?
  1113. rport_delay : bfa_linkup_delay;
  1114. for (i = 0; !(bfad->bfad_flags & BFAD_RPORT_ONLINE)
  1115. && i < rport_delay; i++) {
  1116. set_current_state(TASK_UNINTERRUPTIBLE);
  1117. schedule_timeout(HZ);
  1118. }
  1119. if (rport_delay > 0 && (bfad->bfad_flags & BFAD_RPORT_ONLINE)) {
  1120. set_current_state(TASK_UNINTERRUPTIBLE);
  1121. schedule_timeout(rport_delay * HZ);
  1122. }
  1123. }
  1124. }
  1125. int
  1126. bfad_get_linkup_delay(struct bfad_s *bfad)
  1127. {
  1128. u8 nwwns = 0;
  1129. wwn_t wwns[BFA_PREBOOT_BOOTLUN_MAX];
  1130. int linkup_delay;
  1131. /*
  1132. * Querying for the boot target port wwns
  1133. * -- read from boot information in flash.
  1134. * If nwwns > 0 => boot over SAN and set linkup_delay = 30
  1135. * else => local boot machine set linkup_delay = 0
  1136. */
  1137. bfa_iocfc_get_bootwwns(&bfad->bfa, &nwwns, wwns);
  1138. if (nwwns > 0)
  1139. /* If Boot over SAN set linkup_delay = 30sec */
  1140. linkup_delay = 30;
  1141. else
  1142. /* If local boot; no linkup_delay */
  1143. linkup_delay = 0;
  1144. return linkup_delay;
  1145. }