bfad_im.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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 "bfad_drv.h"
  21. #include "bfad_im.h"
  22. #include "bfa_cb_ioim.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_os_ramp_up_qdepth(itnim, cmnd->device);
  84. } else if (cmnd->result == SAM_STAT_TASK_SET_FULL && itnim) {
  85. /* qfull handling */
  86. bfad_os_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_os_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. struct bfa_s *bfa = &bfad->bfa;
  152. struct bfa_ioc_s *ioc = &bfa->ioc;
  153. char model[BFA_ADAPTER_MODEL_NAME_LEN];
  154. bfa_get_adapter_model(bfa, model);
  155. memset(bfa_buf, 0, sizeof(bfa_buf));
  156. if (ioc->ctdev)
  157. snprintf(bfa_buf, sizeof(bfa_buf),
  158. "Brocade FCOE Adapter, " "model: %s hwpath: %s driver: %s",
  159. model, bfad->pci_name, BFAD_DRIVER_VERSION);
  160. else
  161. snprintf(bfa_buf, sizeof(bfa_buf),
  162. "Brocade FC Adapter, " "model: %s hwpath: %s driver: %s",
  163. model, bfad->pci_name, BFAD_DRIVER_VERSION);
  164. return bfa_buf;
  165. }
  166. /*
  167. * Scsi_Host template entry, aborts the specified SCSI command.
  168. *
  169. * Returns: SUCCESS or FAILED.
  170. */
  171. static int
  172. bfad_im_abort_handler(struct scsi_cmnd *cmnd)
  173. {
  174. struct Scsi_Host *shost = cmnd->device->host;
  175. struct bfad_im_port_s *im_port =
  176. (struct bfad_im_port_s *) shost->hostdata[0];
  177. struct bfad_s *bfad = im_port->bfad;
  178. struct bfa_ioim_s *hal_io;
  179. unsigned long flags;
  180. u32 timeout;
  181. int rc = FAILED;
  182. spin_lock_irqsave(&bfad->bfad_lock, flags);
  183. hal_io = (struct bfa_ioim_s *) cmnd->host_scribble;
  184. if (!hal_io) {
  185. /* IO has been completed, retrun success */
  186. rc = SUCCESS;
  187. goto out;
  188. }
  189. if (hal_io->dio != (struct bfad_ioim_s *) cmnd) {
  190. rc = FAILED;
  191. goto out;
  192. }
  193. bfa_trc(bfad, hal_io->iotag);
  194. BFA_LOG(KERN_INFO, bfad, log_level, "scsi%d: abort cmnd %p iotag %x\n",
  195. im_port->shost->host_no, cmnd, hal_io->iotag);
  196. (void) bfa_ioim_abort(hal_io);
  197. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  198. /* Need to wait until the command get aborted */
  199. timeout = 10;
  200. while ((struct bfa_ioim_s *) cmnd->host_scribble == hal_io) {
  201. set_current_state(TASK_UNINTERRUPTIBLE);
  202. schedule_timeout(timeout);
  203. if (timeout < 4 * HZ)
  204. timeout *= 2;
  205. }
  206. cmnd->scsi_done(cmnd);
  207. bfa_trc(bfad, hal_io->iotag);
  208. BFA_LOG(KERN_INFO, bfad, log_level,
  209. "scsi%d: complete abort 0x%p iotag 0x%x\n",
  210. im_port->shost->host_no, cmnd, hal_io->iotag);
  211. return SUCCESS;
  212. out:
  213. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  214. return rc;
  215. }
  216. static bfa_status_t
  217. bfad_im_target_reset_send(struct bfad_s *bfad, struct scsi_cmnd *cmnd,
  218. struct bfad_itnim_s *itnim)
  219. {
  220. struct bfa_tskim_s *tskim;
  221. struct bfa_itnim_s *bfa_itnim;
  222. bfa_status_t rc = BFA_STATUS_OK;
  223. tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
  224. if (!tskim) {
  225. BFA_LOG(KERN_ERR, bfad, log_level,
  226. "target reset, fail to allocate tskim\n");
  227. rc = BFA_STATUS_FAILED;
  228. goto out;
  229. }
  230. /*
  231. * Set host_scribble to NULL to avoid aborting a task command if
  232. * happens.
  233. */
  234. cmnd->host_scribble = NULL;
  235. cmnd->SCp.Status = 0;
  236. bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
  237. bfa_tskim_start(tskim, bfa_itnim, (lun_t)0,
  238. FCP_TM_TARGET_RESET, BFAD_TARGET_RESET_TMO);
  239. out:
  240. return rc;
  241. }
  242. /*
  243. * Scsi_Host template entry, resets a LUN and abort its all commands.
  244. *
  245. * Returns: SUCCESS or FAILED.
  246. *
  247. */
  248. static int
  249. bfad_im_reset_lun_handler(struct scsi_cmnd *cmnd)
  250. {
  251. struct Scsi_Host *shost = cmnd->device->host;
  252. struct bfad_im_port_s *im_port =
  253. (struct bfad_im_port_s *) shost->hostdata[0];
  254. struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
  255. struct bfad_s *bfad = im_port->bfad;
  256. struct bfa_tskim_s *tskim;
  257. struct bfad_itnim_s *itnim;
  258. struct bfa_itnim_s *bfa_itnim;
  259. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  260. int rc = SUCCESS;
  261. unsigned long flags;
  262. enum bfi_tskim_status task_status;
  263. spin_lock_irqsave(&bfad->bfad_lock, flags);
  264. itnim = itnim_data->itnim;
  265. if (!itnim) {
  266. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  267. rc = FAILED;
  268. goto out;
  269. }
  270. tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
  271. if (!tskim) {
  272. BFA_LOG(KERN_ERR, bfad, log_level,
  273. "LUN reset, fail to allocate tskim");
  274. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  275. rc = FAILED;
  276. goto out;
  277. }
  278. /*
  279. * Set host_scribble to NULL to avoid aborting a task command
  280. * if happens.
  281. */
  282. cmnd->host_scribble = NULL;
  283. cmnd->SCp.ptr = (char *)&wq;
  284. cmnd->SCp.Status = 0;
  285. bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
  286. bfa_tskim_start(tskim, bfa_itnim,
  287. bfad_int_to_lun(cmnd->device->lun),
  288. FCP_TM_LUN_RESET, BFAD_LUN_RESET_TMO);
  289. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  290. wait_event(wq, test_bit(IO_DONE_BIT,
  291. (unsigned long *)&cmnd->SCp.Status));
  292. task_status = cmnd->SCp.Status >> 1;
  293. if (task_status != BFI_TSKIM_STS_OK) {
  294. BFA_LOG(KERN_ERR, bfad, log_level,
  295. "LUN reset failure, status: %d\n", task_status);
  296. rc = FAILED;
  297. }
  298. out:
  299. return rc;
  300. }
  301. /*
  302. * Scsi_Host template entry, resets the bus and abort all commands.
  303. */
  304. static int
  305. bfad_im_reset_bus_handler(struct scsi_cmnd *cmnd)
  306. {
  307. struct Scsi_Host *shost = cmnd->device->host;
  308. struct bfad_im_port_s *im_port =
  309. (struct bfad_im_port_s *) shost->hostdata[0];
  310. struct bfad_s *bfad = im_port->bfad;
  311. struct bfad_itnim_s *itnim;
  312. unsigned long flags;
  313. u32 i, rc, err_cnt = 0;
  314. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  315. enum bfi_tskim_status task_status;
  316. spin_lock_irqsave(&bfad->bfad_lock, flags);
  317. for (i = 0; i < MAX_FCP_TARGET; i++) {
  318. itnim = bfad_os_get_itnim(im_port, i);
  319. if (itnim) {
  320. cmnd->SCp.ptr = (char *)&wq;
  321. rc = bfad_im_target_reset_send(bfad, cmnd, itnim);
  322. if (rc != BFA_STATUS_OK) {
  323. err_cnt++;
  324. continue;
  325. }
  326. /* wait target reset to complete */
  327. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  328. wait_event(wq, test_bit(IO_DONE_BIT,
  329. (unsigned long *)&cmnd->SCp.Status));
  330. spin_lock_irqsave(&bfad->bfad_lock, flags);
  331. task_status = cmnd->SCp.Status >> 1;
  332. if (task_status != BFI_TSKIM_STS_OK) {
  333. BFA_LOG(KERN_ERR, bfad, log_level,
  334. "target reset failure,"
  335. " status: %d\n", task_status);
  336. err_cnt++;
  337. }
  338. }
  339. }
  340. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  341. if (err_cnt)
  342. return FAILED;
  343. return SUCCESS;
  344. }
  345. /*
  346. * Scsi_Host template entry slave_destroy.
  347. */
  348. static void
  349. bfad_im_slave_destroy(struct scsi_device *sdev)
  350. {
  351. sdev->hostdata = NULL;
  352. return;
  353. }
  354. /*
  355. * BFA FCS itnim callbacks
  356. */
  357. /*
  358. * BFA FCS itnim alloc callback, after successful PRLI
  359. * Context: Interrupt
  360. */
  361. void
  362. bfa_fcb_itnim_alloc(struct bfad_s *bfad, struct bfa_fcs_itnim_s **itnim,
  363. struct bfad_itnim_s **itnim_drv)
  364. {
  365. *itnim_drv = kzalloc(sizeof(struct bfad_itnim_s), GFP_ATOMIC);
  366. if (*itnim_drv == NULL)
  367. return;
  368. (*itnim_drv)->im = bfad->im;
  369. *itnim = &(*itnim_drv)->fcs_itnim;
  370. (*itnim_drv)->state = ITNIM_STATE_NONE;
  371. /*
  372. * Initiaze the itnim_work
  373. */
  374. INIT_WORK(&(*itnim_drv)->itnim_work, bfad_im_itnim_work_handler);
  375. bfad->bfad_flags |= BFAD_RPORT_ONLINE;
  376. }
  377. /*
  378. * BFA FCS itnim free callback.
  379. * Context: Interrupt. bfad_lock is held
  380. */
  381. void
  382. bfa_fcb_itnim_free(struct bfad_s *bfad, struct bfad_itnim_s *itnim_drv)
  383. {
  384. struct bfad_port_s *port;
  385. wwn_t wwpn;
  386. u32 fcid;
  387. char wwpn_str[32], fcid_str[16];
  388. struct bfad_im_s *im = itnim_drv->im;
  389. /* online to free state transtion should not happen */
  390. bfa_assert(itnim_drv->state != ITNIM_STATE_ONLINE);
  391. itnim_drv->queue_work = 1;
  392. /* offline request is not yet done, use the same request to free */
  393. if (itnim_drv->state == ITNIM_STATE_OFFLINE_PENDING)
  394. itnim_drv->queue_work = 0;
  395. itnim_drv->state = ITNIM_STATE_FREE;
  396. port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
  397. itnim_drv->im_port = port->im_port;
  398. wwpn = bfa_fcs_itnim_get_pwwn(&itnim_drv->fcs_itnim);
  399. fcid = bfa_fcs_itnim_get_fcid(&itnim_drv->fcs_itnim);
  400. wwn2str(wwpn_str, wwpn);
  401. fcid2str(fcid_str, fcid);
  402. BFA_LOG(KERN_INFO, bfad, log_level,
  403. "ITNIM FREE scsi%d: FCID: %s WWPN: %s\n",
  404. port->im_port->shost->host_no,
  405. fcid_str, wwpn_str);
  406. /* ITNIM processing */
  407. if (itnim_drv->queue_work)
  408. queue_work(im->drv_workq, &itnim_drv->itnim_work);
  409. }
  410. /*
  411. * BFA FCS itnim online callback.
  412. * Context: Interrupt. bfad_lock is held
  413. */
  414. void
  415. bfa_fcb_itnim_online(struct bfad_itnim_s *itnim_drv)
  416. {
  417. struct bfad_port_s *port;
  418. struct bfad_im_s *im = itnim_drv->im;
  419. itnim_drv->bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim_drv->fcs_itnim);
  420. port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
  421. itnim_drv->state = ITNIM_STATE_ONLINE;
  422. itnim_drv->queue_work = 1;
  423. itnim_drv->im_port = port->im_port;
  424. /* ITNIM processing */
  425. if (itnim_drv->queue_work)
  426. queue_work(im->drv_workq, &itnim_drv->itnim_work);
  427. }
  428. /*
  429. * BFA FCS itnim offline callback.
  430. * Context: Interrupt. bfad_lock is held
  431. */
  432. void
  433. bfa_fcb_itnim_offline(struct bfad_itnim_s *itnim_drv)
  434. {
  435. struct bfad_port_s *port;
  436. struct bfad_s *bfad;
  437. struct bfad_im_s *im = itnim_drv->im;
  438. port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
  439. bfad = port->bfad;
  440. if ((bfad->pport.flags & BFAD_PORT_DELETE) ||
  441. (port->flags & BFAD_PORT_DELETE)) {
  442. itnim_drv->state = ITNIM_STATE_OFFLINE;
  443. return;
  444. }
  445. itnim_drv->im_port = port->im_port;
  446. itnim_drv->state = ITNIM_STATE_OFFLINE_PENDING;
  447. itnim_drv->queue_work = 1;
  448. /* ITNIM processing */
  449. if (itnim_drv->queue_work)
  450. queue_work(im->drv_workq, &itnim_drv->itnim_work);
  451. }
  452. /*
  453. * Allocate a Scsi_Host for a port.
  454. */
  455. int
  456. bfad_im_scsi_host_alloc(struct bfad_s *bfad, struct bfad_im_port_s *im_port,
  457. struct device *dev)
  458. {
  459. int error = 1;
  460. mutex_lock(&bfad_mutex);
  461. if (!idr_pre_get(&bfad_im_port_index, GFP_KERNEL)) {
  462. mutex_unlock(&bfad_mutex);
  463. printk(KERN_WARNING "idr_pre_get failure\n");
  464. goto out;
  465. }
  466. error = idr_get_new(&bfad_im_port_index, im_port,
  467. &im_port->idr_id);
  468. if (error) {
  469. mutex_unlock(&bfad_mutex);
  470. printk(KERN_WARNING "idr_get_new failure\n");
  471. goto out;
  472. }
  473. mutex_unlock(&bfad_mutex);
  474. im_port->shost = bfad_os_scsi_host_alloc(im_port, bfad);
  475. if (!im_port->shost) {
  476. error = 1;
  477. goto out_free_idr;
  478. }
  479. im_port->shost->hostdata[0] = (unsigned long)im_port;
  480. im_port->shost->unique_id = im_port->idr_id;
  481. im_port->shost->this_id = -1;
  482. im_port->shost->max_id = MAX_FCP_TARGET;
  483. im_port->shost->max_lun = MAX_FCP_LUN;
  484. im_port->shost->max_cmd_len = 16;
  485. im_port->shost->can_queue = bfad->cfg_data.ioc_queue_depth;
  486. if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
  487. im_port->shost->transportt = bfad_im_scsi_transport_template;
  488. else
  489. im_port->shost->transportt =
  490. bfad_im_scsi_vport_transport_template;
  491. error = scsi_add_host_with_dma(im_port->shost, dev, &bfad->pcidev->dev);
  492. if (error) {
  493. printk(KERN_WARNING "scsi_add_host failure %d\n", error);
  494. goto out_fc_rel;
  495. }
  496. /* setup host fixed attribute if the lk supports */
  497. bfad_os_fc_host_init(im_port);
  498. return 0;
  499. out_fc_rel:
  500. scsi_host_put(im_port->shost);
  501. im_port->shost = NULL;
  502. out_free_idr:
  503. mutex_lock(&bfad_mutex);
  504. idr_remove(&bfad_im_port_index, im_port->idr_id);
  505. mutex_unlock(&bfad_mutex);
  506. out:
  507. return error;
  508. }
  509. void
  510. bfad_im_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
  511. {
  512. bfa_trc(bfad, bfad->inst_no);
  513. BFA_LOG(KERN_INFO, bfad, log_level, "Free scsi%d\n",
  514. im_port->shost->host_no);
  515. fc_remove_host(im_port->shost);
  516. scsi_remove_host(im_port->shost);
  517. scsi_host_put(im_port->shost);
  518. mutex_lock(&bfad_mutex);
  519. idr_remove(&bfad_im_port_index, im_port->idr_id);
  520. mutex_unlock(&bfad_mutex);
  521. }
  522. static void
  523. bfad_im_port_delete_handler(struct work_struct *work)
  524. {
  525. struct bfad_im_port_s *im_port =
  526. container_of(work, struct bfad_im_port_s, port_delete_work);
  527. if (im_port->port->pvb_type != BFAD_PORT_PHYS_BASE) {
  528. im_port->flags |= BFAD_PORT_DELETE;
  529. fc_vport_terminate(im_port->fc_vport);
  530. }
  531. }
  532. bfa_status_t
  533. bfad_im_port_new(struct bfad_s *bfad, struct bfad_port_s *port)
  534. {
  535. int rc = BFA_STATUS_OK;
  536. struct bfad_im_port_s *im_port;
  537. im_port = kzalloc(sizeof(struct bfad_im_port_s), GFP_ATOMIC);
  538. if (im_port == NULL) {
  539. rc = BFA_STATUS_ENOMEM;
  540. goto ext;
  541. }
  542. port->im_port = im_port;
  543. im_port->port = port;
  544. im_port->bfad = bfad;
  545. INIT_WORK(&im_port->port_delete_work, bfad_im_port_delete_handler);
  546. INIT_LIST_HEAD(&im_port->itnim_mapped_list);
  547. INIT_LIST_HEAD(&im_port->binding_list);
  548. ext:
  549. return rc;
  550. }
  551. void
  552. bfad_im_port_delete(struct bfad_s *bfad, struct bfad_port_s *port)
  553. {
  554. struct bfad_im_port_s *im_port = port->im_port;
  555. queue_work(bfad->im->drv_workq,
  556. &im_port->port_delete_work);
  557. }
  558. void
  559. bfad_im_port_clean(struct bfad_im_port_s *im_port)
  560. {
  561. struct bfad_fcp_binding *bp, *bp_new;
  562. unsigned long flags;
  563. struct bfad_s *bfad = im_port->bfad;
  564. spin_lock_irqsave(&bfad->bfad_lock, flags);
  565. list_for_each_entry_safe(bp, bp_new, &im_port->binding_list,
  566. list_entry) {
  567. list_del(&bp->list_entry);
  568. kfree(bp);
  569. }
  570. /* the itnim_mapped_list must be empty at this time */
  571. bfa_assert(list_empty(&im_port->itnim_mapped_list));
  572. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  573. }
  574. bfa_status_t
  575. bfad_im_probe(struct bfad_s *bfad)
  576. {
  577. struct bfad_im_s *im;
  578. bfa_status_t rc = BFA_STATUS_OK;
  579. im = kzalloc(sizeof(struct bfad_im_s), GFP_KERNEL);
  580. if (im == NULL) {
  581. rc = BFA_STATUS_ENOMEM;
  582. goto ext;
  583. }
  584. bfad->im = im;
  585. im->bfad = bfad;
  586. if (bfad_os_thread_workq(bfad) != BFA_STATUS_OK) {
  587. kfree(im);
  588. rc = BFA_STATUS_FAILED;
  589. }
  590. ext:
  591. return rc;
  592. }
  593. void
  594. bfad_im_probe_undo(struct bfad_s *bfad)
  595. {
  596. if (bfad->im) {
  597. bfad_os_destroy_workq(bfad->im);
  598. kfree(bfad->im);
  599. bfad->im = NULL;
  600. }
  601. }
  602. struct Scsi_Host *
  603. bfad_os_scsi_host_alloc(struct bfad_im_port_s *im_port, struct bfad_s *bfad)
  604. {
  605. struct scsi_host_template *sht;
  606. if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
  607. sht = &bfad_im_scsi_host_template;
  608. else
  609. sht = &bfad_im_vport_template;
  610. sht->sg_tablesize = bfad->cfg_data.io_max_sge;
  611. return scsi_host_alloc(sht, sizeof(unsigned long));
  612. }
  613. void
  614. bfad_os_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
  615. {
  616. if (!(im_port->flags & BFAD_PORT_DELETE))
  617. flush_workqueue(bfad->im->drv_workq);
  618. bfad_im_scsi_host_free(im_port->bfad, im_port);
  619. bfad_im_port_clean(im_port);
  620. kfree(im_port);
  621. }
  622. void
  623. bfad_os_destroy_workq(struct bfad_im_s *im)
  624. {
  625. if (im && im->drv_workq) {
  626. flush_workqueue(im->drv_workq);
  627. destroy_workqueue(im->drv_workq);
  628. im->drv_workq = NULL;
  629. }
  630. }
  631. bfa_status_t
  632. bfad_os_thread_workq(struct bfad_s *bfad)
  633. {
  634. struct bfad_im_s *im = bfad->im;
  635. bfa_trc(bfad, 0);
  636. snprintf(im->drv_workq_name, KOBJ_NAME_LEN, "bfad_wq_%d",
  637. bfad->inst_no);
  638. im->drv_workq = create_singlethread_workqueue(im->drv_workq_name);
  639. if (!im->drv_workq)
  640. return BFA_STATUS_FAILED;
  641. return BFA_STATUS_OK;
  642. }
  643. /*
  644. * Scsi_Host template entry.
  645. *
  646. * Description:
  647. * OS entry point to adjust the queue_depths on a per-device basis.
  648. * Called once per device during the bus scan.
  649. * Return non-zero if fails.
  650. */
  651. static int
  652. bfad_im_slave_configure(struct scsi_device *sdev)
  653. {
  654. if (sdev->tagged_supported)
  655. scsi_activate_tcq(sdev, bfa_lun_queue_depth);
  656. else
  657. scsi_deactivate_tcq(sdev, bfa_lun_queue_depth);
  658. return 0;
  659. }
  660. struct scsi_host_template bfad_im_scsi_host_template = {
  661. .module = THIS_MODULE,
  662. .name = BFAD_DRIVER_NAME,
  663. .info = bfad_im_info,
  664. .queuecommand = bfad_im_queuecommand,
  665. .eh_abort_handler = bfad_im_abort_handler,
  666. .eh_device_reset_handler = bfad_im_reset_lun_handler,
  667. .eh_bus_reset_handler = bfad_im_reset_bus_handler,
  668. .slave_alloc = bfad_im_slave_alloc,
  669. .slave_configure = bfad_im_slave_configure,
  670. .slave_destroy = bfad_im_slave_destroy,
  671. .this_id = -1,
  672. .sg_tablesize = BFAD_IO_MAX_SGE,
  673. .cmd_per_lun = 3,
  674. .use_clustering = ENABLE_CLUSTERING,
  675. .shost_attrs = bfad_im_host_attrs,
  676. .max_sectors = 0xFFFF,
  677. };
  678. struct scsi_host_template bfad_im_vport_template = {
  679. .module = THIS_MODULE,
  680. .name = BFAD_DRIVER_NAME,
  681. .info = bfad_im_info,
  682. .queuecommand = bfad_im_queuecommand,
  683. .eh_abort_handler = bfad_im_abort_handler,
  684. .eh_device_reset_handler = bfad_im_reset_lun_handler,
  685. .eh_bus_reset_handler = bfad_im_reset_bus_handler,
  686. .slave_alloc = bfad_im_slave_alloc,
  687. .slave_configure = bfad_im_slave_configure,
  688. .slave_destroy = bfad_im_slave_destroy,
  689. .this_id = -1,
  690. .sg_tablesize = BFAD_IO_MAX_SGE,
  691. .cmd_per_lun = 3,
  692. .use_clustering = ENABLE_CLUSTERING,
  693. .shost_attrs = bfad_im_vport_attrs,
  694. .max_sectors = 0xFFFF,
  695. };
  696. bfa_status_t
  697. bfad_im_module_init(void)
  698. {
  699. bfad_im_scsi_transport_template =
  700. fc_attach_transport(&bfad_im_fc_function_template);
  701. if (!bfad_im_scsi_transport_template)
  702. return BFA_STATUS_ENOMEM;
  703. bfad_im_scsi_vport_transport_template =
  704. fc_attach_transport(&bfad_im_vport_fc_function_template);
  705. if (!bfad_im_scsi_vport_transport_template) {
  706. fc_release_transport(bfad_im_scsi_transport_template);
  707. return BFA_STATUS_ENOMEM;
  708. }
  709. return BFA_STATUS_OK;
  710. }
  711. void
  712. bfad_im_module_exit(void)
  713. {
  714. if (bfad_im_scsi_transport_template)
  715. fc_release_transport(bfad_im_scsi_transport_template);
  716. if (bfad_im_scsi_vport_transport_template)
  717. fc_release_transport(bfad_im_scsi_vport_transport_template);
  718. }
  719. void
  720. bfad_os_ramp_up_qdepth(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
  721. {
  722. struct scsi_device *tmp_sdev;
  723. if (((jiffies - itnim->last_ramp_up_time) >
  724. BFA_QUEUE_FULL_RAMP_UP_TIME * HZ) &&
  725. ((jiffies - itnim->last_queue_full_time) >
  726. BFA_QUEUE_FULL_RAMP_UP_TIME * HZ)) {
  727. shost_for_each_device(tmp_sdev, sdev->host) {
  728. if (bfa_lun_queue_depth > tmp_sdev->queue_depth) {
  729. if (tmp_sdev->id != sdev->id)
  730. continue;
  731. if (tmp_sdev->ordered_tags)
  732. scsi_adjust_queue_depth(tmp_sdev,
  733. MSG_ORDERED_TAG,
  734. tmp_sdev->queue_depth + 1);
  735. else
  736. scsi_adjust_queue_depth(tmp_sdev,
  737. MSG_SIMPLE_TAG,
  738. tmp_sdev->queue_depth + 1);
  739. itnim->last_ramp_up_time = jiffies;
  740. }
  741. }
  742. }
  743. }
  744. void
  745. bfad_os_handle_qfull(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
  746. {
  747. struct scsi_device *tmp_sdev;
  748. itnim->last_queue_full_time = jiffies;
  749. shost_for_each_device(tmp_sdev, sdev->host) {
  750. if (tmp_sdev->id != sdev->id)
  751. continue;
  752. scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
  753. }
  754. }
  755. struct bfad_itnim_s *
  756. bfad_os_get_itnim(struct bfad_im_port_s *im_port, int id)
  757. {
  758. struct bfad_itnim_s *itnim = NULL;
  759. /* Search the mapped list for this target ID */
  760. list_for_each_entry(itnim, &im_port->itnim_mapped_list, list_entry) {
  761. if (id == itnim->scsi_tgt_id)
  762. return itnim;
  763. }
  764. return NULL;
  765. }
  766. /*
  767. * Scsi_Host template entry slave_alloc
  768. */
  769. static int
  770. bfad_im_slave_alloc(struct scsi_device *sdev)
  771. {
  772. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  773. if (!rport || fc_remote_port_chkready(rport))
  774. return -ENXIO;
  775. sdev->hostdata = rport->dd_data;
  776. return 0;
  777. }
  778. static u32
  779. bfad_im_supported_speeds(struct bfa_s *bfa)
  780. {
  781. struct bfa_ioc_attr_s *ioc_attr;
  782. u32 supported_speed = 0;
  783. ioc_attr = kzalloc(sizeof(struct bfa_ioc_attr_s), GFP_KERNEL);
  784. if (!ioc_attr)
  785. return 0;
  786. bfa_get_attr(bfa, ioc_attr);
  787. if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_8GBPS) {
  788. if (ioc_attr->adapter_attr.is_mezz) {
  789. supported_speed |= FC_PORTSPEED_8GBIT |
  790. FC_PORTSPEED_4GBIT |
  791. FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
  792. } else {
  793. supported_speed |= FC_PORTSPEED_8GBIT |
  794. FC_PORTSPEED_4GBIT |
  795. FC_PORTSPEED_2GBIT;
  796. }
  797. } else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_4GBPS) {
  798. supported_speed |= FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
  799. FC_PORTSPEED_1GBIT;
  800. } else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_10GBPS) {
  801. supported_speed |= FC_PORTSPEED_10GBIT;
  802. }
  803. kfree(ioc_attr);
  804. return supported_speed;
  805. }
  806. void
  807. bfad_os_fc_host_init(struct bfad_im_port_s *im_port)
  808. {
  809. struct Scsi_Host *host = im_port->shost;
  810. struct bfad_s *bfad = im_port->bfad;
  811. struct bfad_port_s *port = im_port->port;
  812. char symname[BFA_SYMNAME_MAXLEN];
  813. struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(&bfad->bfa);
  814. fc_host_node_name(host) =
  815. cpu_to_be64((bfa_fcs_lport_get_nwwn(port->fcs_port)));
  816. fc_host_port_name(host) =
  817. cpu_to_be64((bfa_fcs_lport_get_pwwn(port->fcs_port)));
  818. fc_host_max_npiv_vports(host) = bfa_lps_get_max_vport(&bfad->bfa);
  819. fc_host_supported_classes(host) = FC_COS_CLASS3;
  820. memset(fc_host_supported_fc4s(host), 0,
  821. sizeof(fc_host_supported_fc4s(host)));
  822. if (supported_fc4s & BFA_LPORT_ROLE_FCP_IM)
  823. /* For FCP type 0x08 */
  824. fc_host_supported_fc4s(host)[2] = 1;
  825. /* For fibre channel services type 0x20 */
  826. fc_host_supported_fc4s(host)[7] = 1;
  827. strncpy(symname, bfad->bfa_fcs.fabric.bport.port_cfg.sym_name.symname,
  828. BFA_SYMNAME_MAXLEN);
  829. sprintf(fc_host_symbolic_name(host), "%s", symname);
  830. fc_host_supported_speeds(host) = bfad_im_supported_speeds(&bfad->bfa);
  831. fc_host_maxframe_size(host) = fcport->cfg.maxfrsize;
  832. }
  833. static void
  834. bfad_im_fc_rport_add(struct bfad_im_port_s *im_port, struct bfad_itnim_s *itnim)
  835. {
  836. struct fc_rport_identifiers rport_ids;
  837. struct fc_rport *fc_rport;
  838. struct bfad_itnim_data_s *itnim_data;
  839. rport_ids.node_name =
  840. cpu_to_be64(bfa_fcs_itnim_get_nwwn(&itnim->fcs_itnim));
  841. rport_ids.port_name =
  842. cpu_to_be64(bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
  843. rport_ids.port_id =
  844. bfa_os_hton3b(bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim));
  845. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  846. itnim->fc_rport = fc_rport =
  847. fc_remote_port_add(im_port->shost, 0, &rport_ids);
  848. if (!fc_rport)
  849. return;
  850. fc_rport->maxframe_size =
  851. bfa_fcs_itnim_get_maxfrsize(&itnim->fcs_itnim);
  852. fc_rport->supported_classes = bfa_fcs_itnim_get_cos(&itnim->fcs_itnim);
  853. itnim_data = fc_rport->dd_data;
  854. itnim_data->itnim = itnim;
  855. rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
  856. if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
  857. fc_remote_port_rolechg(fc_rport, rport_ids.roles);
  858. if ((fc_rport->scsi_target_id != -1)
  859. && (fc_rport->scsi_target_id < MAX_FCP_TARGET))
  860. itnim->scsi_tgt_id = fc_rport->scsi_target_id;
  861. return;
  862. }
  863. /*
  864. * Work queue handler using FC transport service
  865. * Context: kernel
  866. */
  867. static void
  868. bfad_im_itnim_work_handler(struct work_struct *work)
  869. {
  870. struct bfad_itnim_s *itnim = container_of(work, struct bfad_itnim_s,
  871. itnim_work);
  872. struct bfad_im_s *im = itnim->im;
  873. struct bfad_s *bfad = im->bfad;
  874. struct bfad_im_port_s *im_port;
  875. unsigned long flags;
  876. struct fc_rport *fc_rport;
  877. wwn_t wwpn;
  878. u32 fcid;
  879. char wwpn_str[32], fcid_str[16];
  880. spin_lock_irqsave(&bfad->bfad_lock, flags);
  881. im_port = itnim->im_port;
  882. bfa_trc(bfad, itnim->state);
  883. switch (itnim->state) {
  884. case ITNIM_STATE_ONLINE:
  885. if (!itnim->fc_rport) {
  886. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  887. bfad_im_fc_rport_add(im_port, itnim);
  888. spin_lock_irqsave(&bfad->bfad_lock, flags);
  889. wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
  890. fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
  891. wwn2str(wwpn_str, wwpn);
  892. fcid2str(fcid_str, fcid);
  893. list_add_tail(&itnim->list_entry,
  894. &im_port->itnim_mapped_list);
  895. BFA_LOG(KERN_INFO, bfad, log_level,
  896. "ITNIM ONLINE Target: %d:0:%d "
  897. "FCID: %s WWPN: %s\n",
  898. im_port->shost->host_no,
  899. itnim->scsi_tgt_id,
  900. fcid_str, wwpn_str);
  901. } else {
  902. printk(KERN_WARNING
  903. "%s: itnim %llx is already in online state\n",
  904. __func__,
  905. bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
  906. }
  907. break;
  908. case ITNIM_STATE_OFFLINE_PENDING:
  909. itnim->state = ITNIM_STATE_OFFLINE;
  910. if (itnim->fc_rport) {
  911. fc_rport = itnim->fc_rport;
  912. ((struct bfad_itnim_data_s *)
  913. fc_rport->dd_data)->itnim = NULL;
  914. itnim->fc_rport = NULL;
  915. if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
  916. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  917. fc_rport->dev_loss_tmo =
  918. bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
  919. fc_remote_port_delete(fc_rport);
  920. spin_lock_irqsave(&bfad->bfad_lock, flags);
  921. }
  922. wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
  923. fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
  924. wwn2str(wwpn_str, wwpn);
  925. fcid2str(fcid_str, fcid);
  926. list_del(&itnim->list_entry);
  927. BFA_LOG(KERN_INFO, bfad, log_level,
  928. "ITNIM OFFLINE Target: %d:0:%d "
  929. "FCID: %s WWPN: %s\n",
  930. im_port->shost->host_no,
  931. itnim->scsi_tgt_id,
  932. fcid_str, wwpn_str);
  933. }
  934. break;
  935. case ITNIM_STATE_FREE:
  936. if (itnim->fc_rport) {
  937. fc_rport = itnim->fc_rport;
  938. ((struct bfad_itnim_data_s *)
  939. fc_rport->dd_data)->itnim = NULL;
  940. itnim->fc_rport = NULL;
  941. if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
  942. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  943. fc_rport->dev_loss_tmo =
  944. bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
  945. fc_remote_port_delete(fc_rport);
  946. spin_lock_irqsave(&bfad->bfad_lock, flags);
  947. }
  948. list_del(&itnim->list_entry);
  949. }
  950. kfree(itnim);
  951. break;
  952. default:
  953. bfa_assert(0);
  954. break;
  955. }
  956. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  957. }
  958. /*
  959. * Scsi_Host template entry, queue a SCSI command to the BFAD.
  960. */
  961. static int
  962. bfad_im_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
  963. {
  964. struct bfad_im_port_s *im_port =
  965. (struct bfad_im_port_s *) cmnd->device->host->hostdata[0];
  966. struct bfad_s *bfad = im_port->bfad;
  967. struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
  968. struct bfad_itnim_s *itnim;
  969. struct bfa_ioim_s *hal_io;
  970. unsigned long flags;
  971. int rc;
  972. int sg_cnt = 0;
  973. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  974. rc = fc_remote_port_chkready(rport);
  975. if (rc) {
  976. cmnd->result = rc;
  977. done(cmnd);
  978. return 0;
  979. }
  980. sg_cnt = scsi_dma_map(cmnd);
  981. if (sg_cnt < 0)
  982. return SCSI_MLQUEUE_HOST_BUSY;
  983. cmnd->scsi_done = done;
  984. spin_lock_irqsave(&bfad->bfad_lock, flags);
  985. if (!(bfad->bfad_flags & BFAD_HAL_START_DONE)) {
  986. printk(KERN_WARNING
  987. "bfad%d, queuecommand %p %x failed, BFA stopped\n",
  988. bfad->inst_no, cmnd, cmnd->cmnd[0]);
  989. cmnd->result = ScsiResult(DID_NO_CONNECT, 0);
  990. goto out_fail_cmd;
  991. }
  992. itnim = itnim_data->itnim;
  993. if (!itnim) {
  994. cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
  995. goto out_fail_cmd;
  996. }
  997. hal_io = bfa_ioim_alloc(&bfad->bfa, (struct bfad_ioim_s *) cmnd,
  998. itnim->bfa_itnim, sg_cnt);
  999. if (!hal_io) {
  1000. printk(KERN_WARNING "hal_io failure\n");
  1001. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  1002. scsi_dma_unmap(cmnd);
  1003. return SCSI_MLQUEUE_HOST_BUSY;
  1004. }
  1005. cmnd->host_scribble = (char *)hal_io;
  1006. bfa_trc_fp(bfad, hal_io->iotag);
  1007. bfa_ioim_start(hal_io);
  1008. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  1009. return 0;
  1010. out_fail_cmd:
  1011. spin_unlock_irqrestore(&bfad->bfad_lock, flags);
  1012. scsi_dma_unmap(cmnd);
  1013. if (done)
  1014. done(cmnd);
  1015. return 0;
  1016. }
  1017. static DEF_SCSI_QCMD(bfad_im_queuecommand)
  1018. void
  1019. bfad_os_rport_online_wait(struct bfad_s *bfad)
  1020. {
  1021. int i;
  1022. int rport_delay = 10;
  1023. for (i = 0; !(bfad->bfad_flags & BFAD_PORT_ONLINE)
  1024. && i < bfa_linkup_delay; i++) {
  1025. set_current_state(TASK_UNINTERRUPTIBLE);
  1026. schedule_timeout(HZ);
  1027. }
  1028. if (bfad->bfad_flags & BFAD_PORT_ONLINE) {
  1029. rport_delay = rport_delay < bfa_linkup_delay ?
  1030. rport_delay : bfa_linkup_delay;
  1031. for (i = 0; !(bfad->bfad_flags & BFAD_RPORT_ONLINE)
  1032. && i < rport_delay; i++) {
  1033. set_current_state(TASK_UNINTERRUPTIBLE);
  1034. schedule_timeout(HZ);
  1035. }
  1036. if (rport_delay > 0 && (bfad->bfad_flags & BFAD_RPORT_ONLINE)) {
  1037. set_current_state(TASK_UNINTERRUPTIBLE);
  1038. schedule_timeout(rport_delay * HZ);
  1039. }
  1040. }
  1041. }
  1042. int
  1043. bfad_os_get_linkup_delay(struct bfad_s *bfad)
  1044. {
  1045. u8 nwwns = 0;
  1046. wwn_t wwns[BFA_PREBOOT_BOOTLUN_MAX];
  1047. int linkup_delay;
  1048. /*
  1049. * Querying for the boot target port wwns
  1050. * -- read from boot information in flash.
  1051. * If nwwns > 0 => boot over SAN and set linkup_delay = 30
  1052. * else => local boot machine set linkup_delay = 0
  1053. */
  1054. bfa_iocfc_get_bootwwns(&bfad->bfa, &nwwns, wwns);
  1055. if (nwwns > 0)
  1056. /* If Boot over SAN set linkup_delay = 30sec */
  1057. linkup_delay = 30;
  1058. else
  1059. /* If local boot; no linkup_delay */
  1060. linkup_delay = 0;
  1061. return linkup_delay;
  1062. }