bfad_im.c 34 KB

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