bfad_im.c 31 KB

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