bfad_im.c 32 KB

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