bfad_im.c 34 KB

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