bfad_im.c 31 KB

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