bfad_im.c 35 KB

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