bfad_im.c 34 KB

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