bfad_im.c 31 KB

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