fc_fcp.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198
  1. /*
  2. * Copyright(c) 2007 Intel Corporation. All rights reserved.
  3. * Copyright(c) 2008 Red Hat, Inc. All rights reserved.
  4. * Copyright(c) 2008 Mike Christie
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Maintained at www.Open-FCoE.org
  20. */
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/kernel.h>
  24. #include <linux/types.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/err.h>
  28. #include <linux/crc32.h>
  29. #include <scsi/scsi_tcq.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_host.h>
  32. #include <scsi/scsi_device.h>
  33. #include <scsi/scsi_cmnd.h>
  34. #include <scsi/fc/fc_fc2.h>
  35. #include <scsi/libfc.h>
  36. #include <scsi/fc_encode.h>
  37. MODULE_AUTHOR("Open-FCoE.org");
  38. MODULE_DESCRIPTION("libfc");
  39. MODULE_LICENSE("GPL v2");
  40. static int fc_fcp_debug;
  41. #define FC_DEBUG_FCP(fmt...) \
  42. do { \
  43. if (fc_fcp_debug) \
  44. FC_DBG(fmt); \
  45. } while (0)
  46. static struct kmem_cache *scsi_pkt_cachep;
  47. /* SRB state definitions */
  48. #define FC_SRB_FREE 0 /* cmd is free */
  49. #define FC_SRB_CMD_SENT (1 << 0) /* cmd has been sent */
  50. #define FC_SRB_RCV_STATUS (1 << 1) /* response has arrived */
  51. #define FC_SRB_ABORT_PENDING (1 << 2) /* cmd abort sent to device */
  52. #define FC_SRB_ABORTED (1 << 3) /* abort acknowleged */
  53. #define FC_SRB_DISCONTIG (1 << 4) /* non-sequential data recvd */
  54. #define FC_SRB_COMPL (1 << 5) /* fc_io_compl has been run */
  55. #define FC_SRB_FCP_PROCESSING_TMO (1 << 6) /* timer function processing */
  56. #define FC_SRB_NOMEM (1 << 7) /* dropped to out of mem */
  57. #define FC_SRB_READ (1 << 1)
  58. #define FC_SRB_WRITE (1 << 0)
  59. /*
  60. * The SCp.ptr should be tested and set under the host lock. NULL indicates
  61. * that the command has been retruned to the scsi layer.
  62. */
  63. #define CMD_SP(Cmnd) ((struct fc_fcp_pkt *)(Cmnd)->SCp.ptr)
  64. #define CMD_ENTRY_STATUS(Cmnd) ((Cmnd)->SCp.have_data_in)
  65. #define CMD_COMPL_STATUS(Cmnd) ((Cmnd)->SCp.this_residual)
  66. #define CMD_SCSI_STATUS(Cmnd) ((Cmnd)->SCp.Status)
  67. #define CMD_RESID_LEN(Cmnd) ((Cmnd)->SCp.buffers_residual)
  68. struct fc_fcp_internal {
  69. mempool_t *scsi_pkt_pool;
  70. struct list_head scsi_pkt_queue;
  71. u8 throttled;
  72. };
  73. #define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv)
  74. /*
  75. * function prototypes
  76. * FC scsi I/O related functions
  77. */
  78. static void fc_fcp_recv_data(struct fc_fcp_pkt *, struct fc_frame *);
  79. static void fc_fcp_recv(struct fc_seq *, struct fc_frame *, void *);
  80. static void fc_fcp_resp(struct fc_fcp_pkt *, struct fc_frame *);
  81. static void fc_fcp_complete_locked(struct fc_fcp_pkt *);
  82. static void fc_tm_done(struct fc_seq *, struct fc_frame *, void *);
  83. static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp);
  84. static void fc_timeout_error(struct fc_fcp_pkt *);
  85. static void fc_fcp_timeout(unsigned long data);
  86. static void fc_fcp_rec(struct fc_fcp_pkt *);
  87. static void fc_fcp_rec_error(struct fc_fcp_pkt *, struct fc_frame *);
  88. static void fc_fcp_rec_resp(struct fc_seq *, struct fc_frame *, void *);
  89. static void fc_io_compl(struct fc_fcp_pkt *);
  90. static void fc_fcp_srr(struct fc_fcp_pkt *, enum fc_rctl, u32);
  91. static void fc_fcp_srr_resp(struct fc_seq *, struct fc_frame *, void *);
  92. static void fc_fcp_srr_error(struct fc_fcp_pkt *, struct fc_frame *);
  93. /*
  94. * command status codes
  95. */
  96. #define FC_COMPLETE 0
  97. #define FC_CMD_ABORTED 1
  98. #define FC_CMD_RESET 2
  99. #define FC_CMD_PLOGO 3
  100. #define FC_SNS_RCV 4
  101. #define FC_TRANS_ERR 5
  102. #define FC_DATA_OVRRUN 6
  103. #define FC_DATA_UNDRUN 7
  104. #define FC_ERROR 8
  105. #define FC_HRD_ERROR 9
  106. #define FC_CMD_TIME_OUT 10
  107. /*
  108. * Error recovery timeout values.
  109. */
  110. #define FC_SCSI_ER_TIMEOUT (10 * HZ)
  111. #define FC_SCSI_TM_TOV (10 * HZ)
  112. #define FC_SCSI_REC_TOV (2 * HZ)
  113. #define FC_HOST_RESET_TIMEOUT (30 * HZ)
  114. #define FC_MAX_ERROR_CNT 5
  115. #define FC_MAX_RECOV_RETRY 3
  116. #define FC_FCP_DFLT_QUEUE_DEPTH 32
  117. /**
  118. * fc_fcp_pkt_alloc - allocation routine for scsi_pkt packet
  119. * @lp: fc lport struct
  120. * @gfp: gfp flags for allocation
  121. *
  122. * This is used by upper layer scsi driver.
  123. * Return Value : scsi_pkt structure or null on allocation failure.
  124. * Context : call from process context. no locking required.
  125. */
  126. static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lp, gfp_t gfp)
  127. {
  128. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  129. struct fc_fcp_pkt *fsp;
  130. fsp = mempool_alloc(si->scsi_pkt_pool, gfp);
  131. if (fsp) {
  132. memset(fsp, 0, sizeof(*fsp));
  133. fsp->lp = lp;
  134. atomic_set(&fsp->ref_cnt, 1);
  135. init_timer(&fsp->timer);
  136. INIT_LIST_HEAD(&fsp->list);
  137. spin_lock_init(&fsp->scsi_pkt_lock);
  138. }
  139. return fsp;
  140. }
  141. /**
  142. * fc_fcp_pkt_release() - release hold on scsi_pkt packet
  143. * @fsp: fcp packet struct
  144. *
  145. * This is used by upper layer scsi driver.
  146. * Context : call from process and interrupt context.
  147. * no locking required
  148. */
  149. static void fc_fcp_pkt_release(struct fc_fcp_pkt *fsp)
  150. {
  151. if (atomic_dec_and_test(&fsp->ref_cnt)) {
  152. struct fc_fcp_internal *si = fc_get_scsi_internal(fsp->lp);
  153. mempool_free(fsp, si->scsi_pkt_pool);
  154. }
  155. }
  156. static void fc_fcp_pkt_hold(struct fc_fcp_pkt *fsp)
  157. {
  158. atomic_inc(&fsp->ref_cnt);
  159. }
  160. /**
  161. * fc_fcp_pkt_destory() - release hold on scsi_pkt packet
  162. * @seq: exchange sequence
  163. * @fsp: fcp packet struct
  164. *
  165. * Release hold on scsi_pkt packet set to keep scsi_pkt
  166. * till EM layer exch resource is not freed.
  167. * Context : called from from EM layer.
  168. * no locking required
  169. */
  170. static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp)
  171. {
  172. fc_fcp_pkt_release(fsp);
  173. }
  174. /**
  175. * fc_fcp_lock_pkt() - lock a packet and get a ref to it.
  176. * @fsp: fcp packet
  177. *
  178. * We should only return error if we return a command to scsi-ml before
  179. * getting a response. This can happen in cases where we send a abort, but
  180. * do not wait for the response and the abort and command can be passing
  181. * each other on the wire/network-layer.
  182. *
  183. * Note: this function locks the packet and gets a reference to allow
  184. * callers to call the completion function while the lock is held and
  185. * not have to worry about the packets refcount.
  186. *
  187. * TODO: Maybe we should just have callers grab/release the lock and
  188. * have a function that they call to verify the fsp and grab a ref if
  189. * needed.
  190. */
  191. static inline int fc_fcp_lock_pkt(struct fc_fcp_pkt *fsp)
  192. {
  193. spin_lock_bh(&fsp->scsi_pkt_lock);
  194. if (fsp->state & FC_SRB_COMPL) {
  195. spin_unlock_bh(&fsp->scsi_pkt_lock);
  196. return -EPERM;
  197. }
  198. fc_fcp_pkt_hold(fsp);
  199. return 0;
  200. }
  201. static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt *fsp)
  202. {
  203. spin_unlock_bh(&fsp->scsi_pkt_lock);
  204. fc_fcp_pkt_release(fsp);
  205. }
  206. static void fc_fcp_timer_set(struct fc_fcp_pkt *fsp, unsigned long delay)
  207. {
  208. if (!(fsp->state & FC_SRB_COMPL))
  209. mod_timer(&fsp->timer, jiffies + delay);
  210. }
  211. static int fc_fcp_send_abort(struct fc_fcp_pkt *fsp)
  212. {
  213. if (!fsp->seq_ptr)
  214. return -EINVAL;
  215. fsp->state |= FC_SRB_ABORT_PENDING;
  216. return fsp->lp->tt.seq_exch_abort(fsp->seq_ptr, 0);
  217. }
  218. /*
  219. * Retry command.
  220. * An abort isn't needed.
  221. */
  222. static void fc_fcp_retry_cmd(struct fc_fcp_pkt *fsp)
  223. {
  224. if (fsp->seq_ptr) {
  225. fsp->lp->tt.exch_done(fsp->seq_ptr);
  226. fsp->seq_ptr = NULL;
  227. }
  228. fsp->state &= ~FC_SRB_ABORT_PENDING;
  229. fsp->io_status = 0;
  230. fsp->status_code = FC_ERROR;
  231. fc_fcp_complete_locked(fsp);
  232. }
  233. /*
  234. * fc_fcp_ddp_setup - calls to LLD's ddp_setup to set up DDP
  235. * transfer for a read I/O indicated by the fc_fcp_pkt.
  236. * @fsp: ptr to the fc_fcp_pkt
  237. *
  238. * This is called in exch_seq_send() when we have a newly allocated
  239. * exchange with a valid exchange id to setup ddp.
  240. *
  241. * returns: none
  242. */
  243. void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid)
  244. {
  245. struct fc_lport *lp;
  246. if (!fsp)
  247. return;
  248. lp = fsp->lp;
  249. if ((fsp->req_flags & FC_SRB_READ) &&
  250. (lp->lro_enabled) && (lp->tt.ddp_setup)) {
  251. if (lp->tt.ddp_setup(lp, xid, scsi_sglist(fsp->cmd),
  252. scsi_sg_count(fsp->cmd)))
  253. fsp->xfer_ddp = xid;
  254. }
  255. }
  256. EXPORT_SYMBOL(fc_fcp_ddp_setup);
  257. /*
  258. * fc_fcp_ddp_done - calls to LLD's ddp_done to release any
  259. * DDP related resources for this I/O if it is initialized
  260. * as a ddp transfer
  261. * @fsp: ptr to the fc_fcp_pkt
  262. *
  263. * returns: none
  264. */
  265. static void fc_fcp_ddp_done(struct fc_fcp_pkt *fsp)
  266. {
  267. struct fc_lport *lp;
  268. if (!fsp)
  269. return;
  270. lp = fsp->lp;
  271. if (fsp->xfer_ddp && lp->tt.ddp_done) {
  272. fsp->xfer_len = lp->tt.ddp_done(lp, fsp->xfer_ddp);
  273. fsp->xfer_ddp = 0;
  274. }
  275. }
  276. /*
  277. * Receive SCSI data from target.
  278. * Called after receiving solicited data.
  279. */
  280. static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  281. {
  282. struct scsi_cmnd *sc = fsp->cmd;
  283. struct fc_lport *lp = fsp->lp;
  284. struct fcoe_dev_stats *stats;
  285. struct fc_frame_header *fh;
  286. size_t start_offset;
  287. size_t offset;
  288. u32 crc;
  289. u32 copy_len = 0;
  290. size_t len;
  291. void *buf;
  292. struct scatterlist *sg;
  293. size_t remaining;
  294. fh = fc_frame_header_get(fp);
  295. offset = ntohl(fh->fh_parm_offset);
  296. start_offset = offset;
  297. len = fr_len(fp) - sizeof(*fh);
  298. buf = fc_frame_payload_get(fp, 0);
  299. /* if this I/O is ddped, update xfer len */
  300. fc_fcp_ddp_done(fsp);
  301. if (offset + len > fsp->data_len) {
  302. /* this should never happen */
  303. if ((fr_flags(fp) & FCPHF_CRC_UNCHECKED) &&
  304. fc_frame_crc_check(fp))
  305. goto crc_err;
  306. FC_DEBUG_FCP("data received past end. len %zx offset %zx "
  307. "data_len %x\n", len, offset, fsp->data_len);
  308. fc_fcp_retry_cmd(fsp);
  309. return;
  310. }
  311. if (offset != fsp->xfer_len)
  312. fsp->state |= FC_SRB_DISCONTIG;
  313. crc = 0;
  314. if (fr_flags(fp) & FCPHF_CRC_UNCHECKED)
  315. crc = crc32(~0, (u8 *) fh, sizeof(*fh));
  316. sg = scsi_sglist(sc);
  317. remaining = len;
  318. while (remaining > 0 && sg) {
  319. size_t off;
  320. void *page_addr;
  321. size_t sg_bytes;
  322. if (offset >= sg->length) {
  323. offset -= sg->length;
  324. sg = sg_next(sg);
  325. continue;
  326. }
  327. sg_bytes = min(remaining, sg->length - offset);
  328. /*
  329. * The scatterlist item may be bigger than PAGE_SIZE,
  330. * but we are limited to mapping PAGE_SIZE at a time.
  331. */
  332. off = offset + sg->offset;
  333. sg_bytes = min(sg_bytes, (size_t)
  334. (PAGE_SIZE - (off & ~PAGE_MASK)));
  335. page_addr = kmap_atomic(sg_page(sg) + (off >> PAGE_SHIFT),
  336. KM_SOFTIRQ0);
  337. if (!page_addr)
  338. break; /* XXX panic? */
  339. if (fr_flags(fp) & FCPHF_CRC_UNCHECKED)
  340. crc = crc32(crc, buf, sg_bytes);
  341. memcpy((char *)page_addr + (off & ~PAGE_MASK), buf,
  342. sg_bytes);
  343. kunmap_atomic(page_addr, KM_SOFTIRQ0);
  344. buf += sg_bytes;
  345. offset += sg_bytes;
  346. remaining -= sg_bytes;
  347. copy_len += sg_bytes;
  348. }
  349. if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
  350. buf = fc_frame_payload_get(fp, 0);
  351. if (len % 4) {
  352. crc = crc32(crc, buf + len, 4 - (len % 4));
  353. len += 4 - (len % 4);
  354. }
  355. if (~crc != le32_to_cpu(fr_crc(fp))) {
  356. crc_err:
  357. stats = fc_lport_get_stats(lp);
  358. stats->ErrorFrames++;
  359. /* FIXME - per cpu count, not total count! */
  360. if (stats->InvalidCRCCount++ < 5)
  361. printk(KERN_WARNING "CRC error on data frame for port (%6x)\n",
  362. fc_host_port_id(lp->host));
  363. /*
  364. * Assume the frame is total garbage.
  365. * We may have copied it over the good part
  366. * of the buffer.
  367. * If so, we need to retry the entire operation.
  368. * Otherwise, ignore it.
  369. */
  370. if (fsp->state & FC_SRB_DISCONTIG)
  371. fc_fcp_retry_cmd(fsp);
  372. return;
  373. }
  374. }
  375. if (fsp->xfer_contig_end == start_offset)
  376. fsp->xfer_contig_end += copy_len;
  377. fsp->xfer_len += copy_len;
  378. /*
  379. * In the very rare event that this data arrived after the response
  380. * and completes the transfer, call the completion handler.
  381. */
  382. if (unlikely(fsp->state & FC_SRB_RCV_STATUS) &&
  383. fsp->xfer_len == fsp->data_len - fsp->scsi_resid)
  384. fc_fcp_complete_locked(fsp);
  385. }
  386. /**
  387. * fc_fcp_send_data() - Send SCSI data to target.
  388. * @fsp: ptr to fc_fcp_pkt
  389. * @sp: ptr to this sequence
  390. * @offset: starting offset for this data request
  391. * @seq_blen: the burst length for this data request
  392. *
  393. * Called after receiving a Transfer Ready data descriptor.
  394. * if LLD is capable of seq offload then send down seq_blen
  395. * size of data in single frame, otherwise send multiple FC
  396. * frames of max FC frame payload supported by target port.
  397. *
  398. * Returns : 0 for success.
  399. */
  400. static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
  401. size_t offset, size_t seq_blen)
  402. {
  403. struct fc_exch *ep;
  404. struct scsi_cmnd *sc;
  405. struct scatterlist *sg;
  406. struct fc_frame *fp = NULL;
  407. struct fc_lport *lp = fsp->lp;
  408. size_t remaining;
  409. size_t t_blen;
  410. size_t tlen;
  411. size_t sg_bytes;
  412. size_t frame_offset, fh_parm_offset;
  413. int error;
  414. void *data = NULL;
  415. void *page_addr;
  416. int using_sg = lp->sg_supp;
  417. u32 f_ctl;
  418. WARN_ON(seq_blen <= 0);
  419. if (unlikely(offset + seq_blen > fsp->data_len)) {
  420. /* this should never happen */
  421. FC_DEBUG_FCP("xfer-ready past end. seq_blen %zx offset %zx\n",
  422. seq_blen, offset);
  423. fc_fcp_send_abort(fsp);
  424. return 0;
  425. } else if (offset != fsp->xfer_len) {
  426. /* Out of Order Data Request - no problem, but unexpected. */
  427. FC_DEBUG_FCP("xfer-ready non-contiguous. "
  428. "seq_blen %zx offset %zx\n", seq_blen, offset);
  429. }
  430. /*
  431. * if LLD is capable of seq_offload then set transport
  432. * burst length (t_blen) to seq_blen, otherwise set t_blen
  433. * to max FC frame payload previously set in fsp->max_payload.
  434. */
  435. t_blen = fsp->max_payload;
  436. if (lp->seq_offload) {
  437. t_blen = min(seq_blen, (size_t)lp->lso_max);
  438. FC_DEBUG_FCP("fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n",
  439. fsp, seq_blen, lp->lso_max, t_blen);
  440. }
  441. WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD);
  442. if (t_blen > 512)
  443. t_blen &= ~(512 - 1); /* round down to block size */
  444. WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD); /* won't go below 256 */
  445. sc = fsp->cmd;
  446. remaining = seq_blen;
  447. fh_parm_offset = frame_offset = offset;
  448. tlen = 0;
  449. seq = lp->tt.seq_start_next(seq);
  450. f_ctl = FC_FC_REL_OFF;
  451. WARN_ON(!seq);
  452. /*
  453. * If a get_page()/put_page() will fail, don't use sg lists
  454. * in the fc_frame structure.
  455. *
  456. * The put_page() may be long after the I/O has completed
  457. * in the case of FCoE, since the network driver does it
  458. * via free_skb(). See the test in free_pages_check().
  459. *
  460. * Test this case with 'dd </dev/zero >/dev/st0 bs=64k'.
  461. */
  462. if (using_sg) {
  463. for (sg = scsi_sglist(sc); sg; sg = sg_next(sg)) {
  464. if (page_count(sg_page(sg)) == 0 ||
  465. (sg_page(sg)->flags & (1 << PG_lru |
  466. 1 << PG_private |
  467. 1 << PG_locked |
  468. 1 << PG_active |
  469. 1 << PG_slab |
  470. 1 << PG_swapcache |
  471. 1 << PG_writeback |
  472. 1 << PG_reserved |
  473. 1 << PG_buddy))) {
  474. using_sg = 0;
  475. break;
  476. }
  477. }
  478. }
  479. sg = scsi_sglist(sc);
  480. while (remaining > 0 && sg) {
  481. if (offset >= sg->length) {
  482. offset -= sg->length;
  483. sg = sg_next(sg);
  484. continue;
  485. }
  486. if (!fp) {
  487. tlen = min(t_blen, remaining);
  488. /*
  489. * TODO. Temporary workaround. fc_seq_send() can't
  490. * handle odd lengths in non-linear skbs.
  491. * This will be the final fragment only.
  492. */
  493. if (tlen % 4)
  494. using_sg = 0;
  495. if (using_sg) {
  496. fp = _fc_frame_alloc(lp, 0);
  497. if (!fp)
  498. return -ENOMEM;
  499. } else {
  500. fp = fc_frame_alloc(lp, tlen);
  501. if (!fp)
  502. return -ENOMEM;
  503. data = (void *)(fr_hdr(fp)) +
  504. sizeof(struct fc_frame_header);
  505. }
  506. fh_parm_offset = frame_offset;
  507. fr_max_payload(fp) = fsp->max_payload;
  508. }
  509. sg_bytes = min(tlen, sg->length - offset);
  510. if (using_sg) {
  511. WARN_ON(skb_shinfo(fp_skb(fp))->nr_frags >
  512. FC_FRAME_SG_LEN);
  513. get_page(sg_page(sg));
  514. skb_fill_page_desc(fp_skb(fp),
  515. skb_shinfo(fp_skb(fp))->nr_frags,
  516. sg_page(sg), sg->offset + offset,
  517. sg_bytes);
  518. fp_skb(fp)->data_len += sg_bytes;
  519. fr_len(fp) += sg_bytes;
  520. fp_skb(fp)->truesize += PAGE_SIZE;
  521. } else {
  522. size_t off = offset + sg->offset;
  523. /*
  524. * The scatterlist item may be bigger than PAGE_SIZE,
  525. * but we must not cross pages inside the kmap.
  526. */
  527. sg_bytes = min(sg_bytes, (size_t) (PAGE_SIZE -
  528. (off & ~PAGE_MASK)));
  529. page_addr = kmap_atomic(sg_page(sg) +
  530. (off >> PAGE_SHIFT),
  531. KM_SOFTIRQ0);
  532. memcpy(data, (char *)page_addr + (off & ~PAGE_MASK),
  533. sg_bytes);
  534. kunmap_atomic(page_addr, KM_SOFTIRQ0);
  535. data += sg_bytes;
  536. }
  537. offset += sg_bytes;
  538. frame_offset += sg_bytes;
  539. tlen -= sg_bytes;
  540. remaining -= sg_bytes;
  541. if (tlen)
  542. continue;
  543. /*
  544. * Send sequence with transfer sequence initiative in case
  545. * this is last FCP frame of the sequence.
  546. */
  547. if (remaining == 0)
  548. f_ctl |= FC_FC_SEQ_INIT | FC_FC_END_SEQ;
  549. ep = fc_seq_exch(seq);
  550. fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
  551. FC_TYPE_FCP, f_ctl, fh_parm_offset);
  552. /*
  553. * send fragment using for a sequence.
  554. */
  555. error = lp->tt.seq_send(lp, seq, fp);
  556. if (error) {
  557. WARN_ON(1); /* send error should be rare */
  558. fc_fcp_retry_cmd(fsp);
  559. return 0;
  560. }
  561. fp = NULL;
  562. }
  563. fsp->xfer_len += seq_blen; /* premature count? */
  564. return 0;
  565. }
  566. static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  567. {
  568. int ba_done = 1;
  569. struct fc_ba_rjt *brp;
  570. struct fc_frame_header *fh;
  571. fh = fc_frame_header_get(fp);
  572. switch (fh->fh_r_ctl) {
  573. case FC_RCTL_BA_ACC:
  574. break;
  575. case FC_RCTL_BA_RJT:
  576. brp = fc_frame_payload_get(fp, sizeof(*brp));
  577. if (brp && brp->br_reason == FC_BA_RJT_LOG_ERR)
  578. break;
  579. /* fall thru */
  580. default:
  581. /*
  582. * we will let the command timeout
  583. * and scsi-ml recover in this case,
  584. * therefore cleared the ba_done flag.
  585. */
  586. ba_done = 0;
  587. }
  588. if (ba_done) {
  589. fsp->state |= FC_SRB_ABORTED;
  590. fsp->state &= ~FC_SRB_ABORT_PENDING;
  591. if (fsp->wait_for_comp)
  592. complete(&fsp->tm_done);
  593. else
  594. fc_fcp_complete_locked(fsp);
  595. }
  596. }
  597. /**
  598. * fc_fcp_reduce_can_queue() - drop can_queue
  599. * @lp: lport to drop queueing for
  600. *
  601. * If we are getting memory allocation failures, then we may
  602. * be trying to execute too many commands. We let the running
  603. * commands complete or timeout, then try again with a reduced
  604. * can_queue. Eventually we will hit the point where we run
  605. * on all reserved structs.
  606. */
  607. static void fc_fcp_reduce_can_queue(struct fc_lport *lp)
  608. {
  609. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  610. unsigned long flags;
  611. int can_queue;
  612. spin_lock_irqsave(lp->host->host_lock, flags);
  613. if (si->throttled)
  614. goto done;
  615. si->throttled = 1;
  616. can_queue = lp->host->can_queue;
  617. can_queue >>= 1;
  618. if (!can_queue)
  619. can_queue = 1;
  620. lp->host->can_queue = can_queue;
  621. shost_printk(KERN_ERR, lp->host, "Could not allocate frame.\n"
  622. "Reducing can_queue to %d.\n", can_queue);
  623. done:
  624. spin_unlock_irqrestore(lp->host->host_lock, flags);
  625. }
  626. /**
  627. * fc_fcp_recv() - Reveive FCP frames
  628. * @seq: The sequence the frame is on
  629. * @fp: The FC frame
  630. * @arg: The related FCP packet
  631. *
  632. * Return : None
  633. * Context : called from Soft IRQ context
  634. * can not called holding list lock
  635. */
  636. static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  637. {
  638. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  639. struct fc_lport *lp;
  640. struct fc_frame_header *fh;
  641. struct fcp_txrdy *dd;
  642. u8 r_ctl;
  643. int rc = 0;
  644. if (IS_ERR(fp))
  645. goto errout;
  646. fh = fc_frame_header_get(fp);
  647. r_ctl = fh->fh_r_ctl;
  648. lp = fsp->lp;
  649. if (!(lp->state & LPORT_ST_READY))
  650. goto out;
  651. if (fc_fcp_lock_pkt(fsp))
  652. goto out;
  653. fsp->last_pkt_time = jiffies;
  654. if (fh->fh_type == FC_TYPE_BLS) {
  655. fc_fcp_abts_resp(fsp, fp);
  656. goto unlock;
  657. }
  658. if (fsp->state & (FC_SRB_ABORTED | FC_SRB_ABORT_PENDING))
  659. goto unlock;
  660. if (r_ctl == FC_RCTL_DD_DATA_DESC) {
  661. /*
  662. * received XFER RDY from the target
  663. * need to send data to the target
  664. */
  665. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  666. dd = fc_frame_payload_get(fp, sizeof(*dd));
  667. WARN_ON(!dd);
  668. rc = fc_fcp_send_data(fsp, seq,
  669. (size_t) ntohl(dd->ft_data_ro),
  670. (size_t) ntohl(dd->ft_burst_len));
  671. if (!rc)
  672. seq->rec_data = fsp->xfer_len;
  673. else if (rc == -ENOMEM)
  674. fsp->state |= FC_SRB_NOMEM;
  675. } else if (r_ctl == FC_RCTL_DD_SOL_DATA) {
  676. /*
  677. * received a DATA frame
  678. * next we will copy the data to the system buffer
  679. */
  680. WARN_ON(fr_len(fp) < sizeof(*fh)); /* len may be 0 */
  681. fc_fcp_recv_data(fsp, fp);
  682. seq->rec_data = fsp->xfer_contig_end;
  683. } else if (r_ctl == FC_RCTL_DD_CMD_STATUS) {
  684. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  685. fc_fcp_resp(fsp, fp);
  686. } else {
  687. FC_DBG("unexpected frame. r_ctl %x\n", r_ctl);
  688. }
  689. unlock:
  690. fc_fcp_unlock_pkt(fsp);
  691. out:
  692. fc_frame_free(fp);
  693. errout:
  694. if (IS_ERR(fp))
  695. fc_fcp_error(fsp, fp);
  696. else if (rc == -ENOMEM)
  697. fc_fcp_reduce_can_queue(lp);
  698. }
  699. static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  700. {
  701. struct fc_frame_header *fh;
  702. struct fcp_resp *fc_rp;
  703. struct fcp_resp_ext *rp_ex;
  704. struct fcp_resp_rsp_info *fc_rp_info;
  705. u32 plen;
  706. u32 expected_len;
  707. u32 respl = 0;
  708. u32 snsl = 0;
  709. u8 flags = 0;
  710. plen = fr_len(fp);
  711. fh = (struct fc_frame_header *)fr_hdr(fp);
  712. if (unlikely(plen < sizeof(*fh) + sizeof(*fc_rp)))
  713. goto len_err;
  714. plen -= sizeof(*fh);
  715. fc_rp = (struct fcp_resp *)(fh + 1);
  716. fsp->cdb_status = fc_rp->fr_status;
  717. flags = fc_rp->fr_flags;
  718. fsp->scsi_comp_flags = flags;
  719. expected_len = fsp->data_len;
  720. /* if ddp, update xfer len */
  721. fc_fcp_ddp_done(fsp);
  722. if (unlikely((flags & ~FCP_CONF_REQ) || fc_rp->fr_status)) {
  723. rp_ex = (void *)(fc_rp + 1);
  724. if (flags & (FCP_RSP_LEN_VAL | FCP_SNS_LEN_VAL)) {
  725. if (plen < sizeof(*fc_rp) + sizeof(*rp_ex))
  726. goto len_err;
  727. fc_rp_info = (struct fcp_resp_rsp_info *)(rp_ex + 1);
  728. if (flags & FCP_RSP_LEN_VAL) {
  729. respl = ntohl(rp_ex->fr_rsp_len);
  730. if (respl != sizeof(*fc_rp_info))
  731. goto len_err;
  732. if (fsp->wait_for_comp) {
  733. /* Abuse cdb_status for rsp code */
  734. fsp->cdb_status = fc_rp_info->rsp_code;
  735. complete(&fsp->tm_done);
  736. /*
  737. * tmfs will not have any scsi cmd so
  738. * exit here
  739. */
  740. return;
  741. } else
  742. goto err;
  743. }
  744. if (flags & FCP_SNS_LEN_VAL) {
  745. snsl = ntohl(rp_ex->fr_sns_len);
  746. if (snsl > SCSI_SENSE_BUFFERSIZE)
  747. snsl = SCSI_SENSE_BUFFERSIZE;
  748. memcpy(fsp->cmd->sense_buffer,
  749. (char *)fc_rp_info + respl, snsl);
  750. }
  751. }
  752. if (flags & (FCP_RESID_UNDER | FCP_RESID_OVER)) {
  753. if (plen < sizeof(*fc_rp) + sizeof(rp_ex->fr_resid))
  754. goto len_err;
  755. if (flags & FCP_RESID_UNDER) {
  756. fsp->scsi_resid = ntohl(rp_ex->fr_resid);
  757. /*
  758. * The cmnd->underflow is the minimum number of
  759. * bytes that must be transfered for this
  760. * command. Provided a sense condition is not
  761. * present, make sure the actual amount
  762. * transferred is at least the underflow value
  763. * or fail.
  764. */
  765. if (!(flags & FCP_SNS_LEN_VAL) &&
  766. (fc_rp->fr_status == 0) &&
  767. (scsi_bufflen(fsp->cmd) -
  768. fsp->scsi_resid) < fsp->cmd->underflow)
  769. goto err;
  770. expected_len -= fsp->scsi_resid;
  771. } else {
  772. fsp->status_code = FC_ERROR;
  773. }
  774. }
  775. }
  776. fsp->state |= FC_SRB_RCV_STATUS;
  777. /*
  778. * Check for missing or extra data frames.
  779. */
  780. if (unlikely(fsp->xfer_len != expected_len)) {
  781. if (fsp->xfer_len < expected_len) {
  782. /*
  783. * Some data may be queued locally,
  784. * Wait a at least one jiffy to see if it is delivered.
  785. * If this expires without data, we may do SRR.
  786. */
  787. fc_fcp_timer_set(fsp, 2);
  788. return;
  789. }
  790. fsp->status_code = FC_DATA_OVRRUN;
  791. FC_DBG("tgt %6x xfer len %zx greater than expected len %x. "
  792. "data len %x\n",
  793. fsp->rport->port_id,
  794. fsp->xfer_len, expected_len, fsp->data_len);
  795. }
  796. fc_fcp_complete_locked(fsp);
  797. return;
  798. len_err:
  799. FC_DBG("short FCP response. flags 0x%x len %u respl %u snsl %u\n",
  800. flags, fr_len(fp), respl, snsl);
  801. err:
  802. fsp->status_code = FC_ERROR;
  803. fc_fcp_complete_locked(fsp);
  804. }
  805. /**
  806. * fc_fcp_complete_locked() - complete processing of a fcp packet
  807. * @fsp: fcp packet
  808. *
  809. * This function may sleep if a timer is pending. The packet lock must be
  810. * held, and the host lock must not be held.
  811. */
  812. static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
  813. {
  814. struct fc_lport *lp = fsp->lp;
  815. struct fc_seq *seq;
  816. struct fc_exch *ep;
  817. u32 f_ctl;
  818. if (fsp->state & FC_SRB_ABORT_PENDING)
  819. return;
  820. if (fsp->state & FC_SRB_ABORTED) {
  821. if (!fsp->status_code)
  822. fsp->status_code = FC_CMD_ABORTED;
  823. } else {
  824. /*
  825. * Test for transport underrun, independent of response
  826. * underrun status.
  827. */
  828. if (fsp->xfer_len < fsp->data_len && !fsp->io_status &&
  829. (!(fsp->scsi_comp_flags & FCP_RESID_UNDER) ||
  830. fsp->xfer_len < fsp->data_len - fsp->scsi_resid)) {
  831. fsp->status_code = FC_DATA_UNDRUN;
  832. fsp->io_status = 0;
  833. }
  834. }
  835. seq = fsp->seq_ptr;
  836. if (seq) {
  837. fsp->seq_ptr = NULL;
  838. if (unlikely(fsp->scsi_comp_flags & FCP_CONF_REQ)) {
  839. struct fc_frame *conf_frame;
  840. struct fc_seq *csp;
  841. csp = lp->tt.seq_start_next(seq);
  842. conf_frame = fc_frame_alloc(fsp->lp, 0);
  843. if (conf_frame) {
  844. f_ctl = FC_FC_SEQ_INIT;
  845. f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  846. ep = fc_seq_exch(seq);
  847. fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL,
  848. ep->did, ep->sid,
  849. FC_TYPE_FCP, f_ctl, 0);
  850. lp->tt.seq_send(lp, csp, conf_frame);
  851. }
  852. }
  853. lp->tt.exch_done(seq);
  854. }
  855. fc_io_compl(fsp);
  856. }
  857. static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error)
  858. {
  859. struct fc_lport *lp = fsp->lp;
  860. if (fsp->seq_ptr) {
  861. lp->tt.exch_done(fsp->seq_ptr);
  862. fsp->seq_ptr = NULL;
  863. }
  864. fsp->status_code = error;
  865. }
  866. /**
  867. * fc_fcp_cleanup_each_cmd() - Cleanup active commads
  868. * @lp: logical port
  869. * @id: target id
  870. * @lun: lun
  871. * @error: fsp status code
  872. *
  873. * If lun or id is -1, they are ignored.
  874. */
  875. static void fc_fcp_cleanup_each_cmd(struct fc_lport *lp, unsigned int id,
  876. unsigned int lun, int error)
  877. {
  878. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  879. struct fc_fcp_pkt *fsp;
  880. struct scsi_cmnd *sc_cmd;
  881. unsigned long flags;
  882. spin_lock_irqsave(lp->host->host_lock, flags);
  883. restart:
  884. list_for_each_entry(fsp, &si->scsi_pkt_queue, list) {
  885. sc_cmd = fsp->cmd;
  886. if (id != -1 && scmd_id(sc_cmd) != id)
  887. continue;
  888. if (lun != -1 && sc_cmd->device->lun != lun)
  889. continue;
  890. fc_fcp_pkt_hold(fsp);
  891. spin_unlock_irqrestore(lp->host->host_lock, flags);
  892. if (!fc_fcp_lock_pkt(fsp)) {
  893. fc_fcp_cleanup_cmd(fsp, error);
  894. fc_io_compl(fsp);
  895. fc_fcp_unlock_pkt(fsp);
  896. }
  897. fc_fcp_pkt_release(fsp);
  898. spin_lock_irqsave(lp->host->host_lock, flags);
  899. /*
  900. * while we dropped the lock multiple pkts could
  901. * have been released, so we have to start over.
  902. */
  903. goto restart;
  904. }
  905. spin_unlock_irqrestore(lp->host->host_lock, flags);
  906. }
  907. static void fc_fcp_abort_io(struct fc_lport *lp)
  908. {
  909. fc_fcp_cleanup_each_cmd(lp, -1, -1, FC_HRD_ERROR);
  910. }
  911. /**
  912. * fc_fcp_pkt_send() - send a fcp packet to the lower level.
  913. * @lp: fc lport
  914. * @fsp: fc packet.
  915. *
  916. * This is called by upper layer protocol.
  917. * Return : zero for success and -1 for failure
  918. * Context : called from queuecommand which can be called from process
  919. * or scsi soft irq.
  920. * Locks : called with the host lock and irqs disabled.
  921. */
  922. static int fc_fcp_pkt_send(struct fc_lport *lp, struct fc_fcp_pkt *fsp)
  923. {
  924. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  925. int rc;
  926. fsp->cmd->SCp.ptr = (char *)fsp;
  927. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  928. fsp->cdb_cmd.fc_flags = fsp->req_flags & ~FCP_CFL_LEN_MASK;
  929. int_to_scsilun(fsp->cmd->device->lun,
  930. (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
  931. memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len);
  932. list_add_tail(&fsp->list, &si->scsi_pkt_queue);
  933. spin_unlock_irq(lp->host->host_lock);
  934. rc = lp->tt.fcp_cmd_send(lp, fsp, fc_fcp_recv);
  935. spin_lock_irq(lp->host->host_lock);
  936. if (rc)
  937. list_del(&fsp->list);
  938. return rc;
  939. }
  940. static int fc_fcp_cmd_send(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
  941. void (*resp)(struct fc_seq *,
  942. struct fc_frame *fp,
  943. void *arg))
  944. {
  945. struct fc_frame *fp;
  946. struct fc_seq *seq;
  947. struct fc_rport *rport;
  948. struct fc_rport_libfc_priv *rp;
  949. const size_t len = sizeof(fsp->cdb_cmd);
  950. int rc = 0;
  951. if (fc_fcp_lock_pkt(fsp))
  952. return 0;
  953. fp = fc_frame_alloc(lp, sizeof(fsp->cdb_cmd));
  954. if (!fp) {
  955. rc = -1;
  956. goto unlock;
  957. }
  958. memcpy(fc_frame_payload_get(fp, len), &fsp->cdb_cmd, len);
  959. fr_fsp(fp) = fsp;
  960. rport = fsp->rport;
  961. fsp->max_payload = rport->maxframe_size;
  962. rp = rport->dd_data;
  963. fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id,
  964. fc_host_port_id(rp->local_port->host), FC_TYPE_FCP,
  965. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  966. seq = lp->tt.exch_seq_send(lp, fp, resp, fc_fcp_pkt_destroy, fsp, 0);
  967. if (!seq) {
  968. fc_frame_free(fp);
  969. rc = -1;
  970. goto unlock;
  971. }
  972. fsp->last_pkt_time = jiffies;
  973. fsp->seq_ptr = seq;
  974. fc_fcp_pkt_hold(fsp); /* hold for fc_fcp_pkt_destroy */
  975. setup_timer(&fsp->timer, fc_fcp_timeout, (unsigned long)fsp);
  976. fc_fcp_timer_set(fsp,
  977. (fsp->tgt_flags & FC_RP_FLAGS_REC_SUPPORTED) ?
  978. FC_SCSI_REC_TOV : FC_SCSI_ER_TIMEOUT);
  979. unlock:
  980. fc_fcp_unlock_pkt(fsp);
  981. return rc;
  982. }
  983. /*
  984. * transport error handler
  985. */
  986. static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  987. {
  988. int error = PTR_ERR(fp);
  989. if (fc_fcp_lock_pkt(fsp))
  990. return;
  991. switch (error) {
  992. case -FC_EX_CLOSED:
  993. fc_fcp_retry_cmd(fsp);
  994. goto unlock;
  995. default:
  996. FC_DBG("unknown error %ld\n", PTR_ERR(fp));
  997. }
  998. /*
  999. * clear abort pending, because the lower layer
  1000. * decided to force completion.
  1001. */
  1002. fsp->state &= ~FC_SRB_ABORT_PENDING;
  1003. fsp->status_code = FC_CMD_PLOGO;
  1004. fc_fcp_complete_locked(fsp);
  1005. unlock:
  1006. fc_fcp_unlock_pkt(fsp);
  1007. }
  1008. /*
  1009. * Scsi abort handler- calls to send an abort
  1010. * and then wait for abort completion
  1011. */
  1012. static int fc_fcp_pkt_abort(struct fc_lport *lp, struct fc_fcp_pkt *fsp)
  1013. {
  1014. int rc = FAILED;
  1015. if (fc_fcp_send_abort(fsp))
  1016. return FAILED;
  1017. init_completion(&fsp->tm_done);
  1018. fsp->wait_for_comp = 1;
  1019. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1020. rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
  1021. spin_lock_bh(&fsp->scsi_pkt_lock);
  1022. fsp->wait_for_comp = 0;
  1023. if (!rc) {
  1024. FC_DBG("target abort cmd failed\n");
  1025. rc = FAILED;
  1026. } else if (fsp->state & FC_SRB_ABORTED) {
  1027. FC_DBG("target abort cmd passed\n");
  1028. rc = SUCCESS;
  1029. fc_fcp_complete_locked(fsp);
  1030. }
  1031. return rc;
  1032. }
  1033. /*
  1034. * Retry LUN reset after resource allocation failed.
  1035. */
  1036. static void fc_lun_reset_send(unsigned long data)
  1037. {
  1038. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  1039. struct fc_lport *lp = fsp->lp;
  1040. if (lp->tt.fcp_cmd_send(lp, fsp, fc_tm_done)) {
  1041. if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY)
  1042. return;
  1043. if (fc_fcp_lock_pkt(fsp))
  1044. return;
  1045. setup_timer(&fsp->timer, fc_lun_reset_send, (unsigned long)fsp);
  1046. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1047. fc_fcp_unlock_pkt(fsp);
  1048. }
  1049. }
  1050. /*
  1051. * Scsi device reset handler- send a LUN RESET to the device
  1052. * and wait for reset reply
  1053. */
  1054. static int fc_lun_reset(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
  1055. unsigned int id, unsigned int lun)
  1056. {
  1057. int rc;
  1058. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  1059. fsp->cdb_cmd.fc_tm_flags = FCP_TMF_LUN_RESET;
  1060. int_to_scsilun(lun, (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
  1061. fsp->wait_for_comp = 1;
  1062. init_completion(&fsp->tm_done);
  1063. fc_lun_reset_send((unsigned long)fsp);
  1064. /*
  1065. * wait for completion of reset
  1066. * after that make sure all commands are terminated
  1067. */
  1068. rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
  1069. spin_lock_bh(&fsp->scsi_pkt_lock);
  1070. fsp->state |= FC_SRB_COMPL;
  1071. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1072. del_timer_sync(&fsp->timer);
  1073. spin_lock_bh(&fsp->scsi_pkt_lock);
  1074. if (fsp->seq_ptr) {
  1075. lp->tt.exch_done(fsp->seq_ptr);
  1076. fsp->seq_ptr = NULL;
  1077. }
  1078. fsp->wait_for_comp = 0;
  1079. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1080. if (!rc) {
  1081. FC_DBG("lun reset failed\n");
  1082. return FAILED;
  1083. }
  1084. /* cdb_status holds the tmf's rsp code */
  1085. if (fsp->cdb_status != FCP_TMF_CMPL)
  1086. return FAILED;
  1087. FC_DBG("lun reset to lun %u completed\n", lun);
  1088. fc_fcp_cleanup_each_cmd(lp, id, lun, FC_CMD_ABORTED);
  1089. return SUCCESS;
  1090. }
  1091. /*
  1092. * Task Managment response handler
  1093. */
  1094. static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1095. {
  1096. struct fc_fcp_pkt *fsp = arg;
  1097. struct fc_frame_header *fh;
  1098. if (IS_ERR(fp)) {
  1099. /*
  1100. * If there is an error just let it timeout or wait
  1101. * for TMF to be aborted if it timedout.
  1102. *
  1103. * scsi-eh will escalate for when either happens.
  1104. */
  1105. return;
  1106. }
  1107. if (fc_fcp_lock_pkt(fsp))
  1108. return;
  1109. /*
  1110. * raced with eh timeout handler.
  1111. */
  1112. if (!fsp->seq_ptr || !fsp->wait_for_comp) {
  1113. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1114. return;
  1115. }
  1116. fh = fc_frame_header_get(fp);
  1117. if (fh->fh_type != FC_TYPE_BLS)
  1118. fc_fcp_resp(fsp, fp);
  1119. fsp->seq_ptr = NULL;
  1120. fsp->lp->tt.exch_done(seq);
  1121. fc_frame_free(fp);
  1122. fc_fcp_unlock_pkt(fsp);
  1123. }
  1124. static void fc_fcp_cleanup(struct fc_lport *lp)
  1125. {
  1126. fc_fcp_cleanup_each_cmd(lp, -1, -1, FC_ERROR);
  1127. }
  1128. /*
  1129. * fc_fcp_timeout: called by OS timer function.
  1130. *
  1131. * The timer has been inactivated and must be reactivated if desired
  1132. * using fc_fcp_timer_set().
  1133. *
  1134. * Algorithm:
  1135. *
  1136. * If REC is supported, just issue it, and return. The REC exchange will
  1137. * complete or time out, and recovery can continue at that point.
  1138. *
  1139. * Otherwise, if the response has been received without all the data,
  1140. * it has been ER_TIMEOUT since the response was received.
  1141. *
  1142. * If the response has not been received,
  1143. * we see if data was received recently. If it has been, we continue waiting,
  1144. * otherwise, we abort the command.
  1145. */
  1146. static void fc_fcp_timeout(unsigned long data)
  1147. {
  1148. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  1149. struct fc_rport *rport = fsp->rport;
  1150. struct fc_rport_libfc_priv *rp = rport->dd_data;
  1151. if (fc_fcp_lock_pkt(fsp))
  1152. return;
  1153. if (fsp->cdb_cmd.fc_tm_flags)
  1154. goto unlock;
  1155. fsp->state |= FC_SRB_FCP_PROCESSING_TMO;
  1156. if (rp->flags & FC_RP_FLAGS_REC_SUPPORTED)
  1157. fc_fcp_rec(fsp);
  1158. else if (time_after_eq(fsp->last_pkt_time + (FC_SCSI_ER_TIMEOUT / 2),
  1159. jiffies))
  1160. fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
  1161. else if (fsp->state & FC_SRB_RCV_STATUS)
  1162. fc_fcp_complete_locked(fsp);
  1163. else
  1164. fc_timeout_error(fsp);
  1165. fsp->state &= ~FC_SRB_FCP_PROCESSING_TMO;
  1166. unlock:
  1167. fc_fcp_unlock_pkt(fsp);
  1168. }
  1169. /*
  1170. * Send a REC ELS request
  1171. */
  1172. static void fc_fcp_rec(struct fc_fcp_pkt *fsp)
  1173. {
  1174. struct fc_lport *lp;
  1175. struct fc_frame *fp;
  1176. struct fc_rport *rport;
  1177. struct fc_rport_libfc_priv *rp;
  1178. lp = fsp->lp;
  1179. rport = fsp->rport;
  1180. rp = rport->dd_data;
  1181. if (!fsp->seq_ptr || rp->rp_state != RPORT_ST_READY) {
  1182. fsp->status_code = FC_HRD_ERROR;
  1183. fsp->io_status = 0;
  1184. fc_fcp_complete_locked(fsp);
  1185. return;
  1186. }
  1187. fp = fc_frame_alloc(lp, sizeof(struct fc_els_rec));
  1188. if (!fp)
  1189. goto retry;
  1190. fr_seq(fp) = fsp->seq_ptr;
  1191. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id,
  1192. fc_host_port_id(rp->local_port->host), FC_TYPE_ELS,
  1193. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1194. if (lp->tt.elsct_send(lp, rport, fp, ELS_REC, fc_fcp_rec_resp,
  1195. fsp, jiffies_to_msecs(FC_SCSI_REC_TOV))) {
  1196. fc_fcp_pkt_hold(fsp); /* hold while REC outstanding */
  1197. return;
  1198. }
  1199. fc_frame_free(fp);
  1200. retry:
  1201. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1202. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1203. else
  1204. fc_timeout_error(fsp);
  1205. }
  1206. /*
  1207. * Receive handler for REC ELS frame
  1208. * if it is a reject then let the scsi layer to handle
  1209. * the timeout. if it is a LS_ACC then if the io was not completed
  1210. * then set the timeout and return otherwise complete the exchange
  1211. * and tell the scsi layer to restart the I/O.
  1212. */
  1213. static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1214. {
  1215. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  1216. struct fc_els_rec_acc *recp;
  1217. struct fc_els_ls_rjt *rjt;
  1218. u32 e_stat;
  1219. u8 opcode;
  1220. u32 offset;
  1221. enum dma_data_direction data_dir;
  1222. enum fc_rctl r_ctl;
  1223. struct fc_rport_libfc_priv *rp;
  1224. if (IS_ERR(fp)) {
  1225. fc_fcp_rec_error(fsp, fp);
  1226. return;
  1227. }
  1228. if (fc_fcp_lock_pkt(fsp))
  1229. goto out;
  1230. fsp->recov_retry = 0;
  1231. opcode = fc_frame_payload_op(fp);
  1232. if (opcode == ELS_LS_RJT) {
  1233. rjt = fc_frame_payload_get(fp, sizeof(*rjt));
  1234. switch (rjt->er_reason) {
  1235. default:
  1236. FC_DEBUG_FCP("device %x unexpected REC reject "
  1237. "reason %d expl %d\n",
  1238. fsp->rport->port_id, rjt->er_reason,
  1239. rjt->er_explan);
  1240. /* fall through */
  1241. case ELS_RJT_UNSUP:
  1242. FC_DEBUG_FCP("device does not support REC\n");
  1243. rp = fsp->rport->dd_data;
  1244. /*
  1245. * if we do not spport RECs or got some bogus
  1246. * reason then resetup timer so we check for
  1247. * making progress.
  1248. */
  1249. rp->flags &= ~FC_RP_FLAGS_REC_SUPPORTED;
  1250. fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
  1251. break;
  1252. case ELS_RJT_LOGIC:
  1253. case ELS_RJT_UNAB:
  1254. /*
  1255. * If no data transfer, the command frame got dropped
  1256. * so we just retry. If data was transferred, we
  1257. * lost the response but the target has no record,
  1258. * so we abort and retry.
  1259. */
  1260. if (rjt->er_explan == ELS_EXPL_OXID_RXID &&
  1261. fsp->xfer_len == 0) {
  1262. fc_fcp_retry_cmd(fsp);
  1263. break;
  1264. }
  1265. fc_timeout_error(fsp);
  1266. break;
  1267. }
  1268. } else if (opcode == ELS_LS_ACC) {
  1269. if (fsp->state & FC_SRB_ABORTED)
  1270. goto unlock_out;
  1271. data_dir = fsp->cmd->sc_data_direction;
  1272. recp = fc_frame_payload_get(fp, sizeof(*recp));
  1273. offset = ntohl(recp->reca_fc4value);
  1274. e_stat = ntohl(recp->reca_e_stat);
  1275. if (e_stat & ESB_ST_COMPLETE) {
  1276. /*
  1277. * The exchange is complete.
  1278. *
  1279. * For output, we must've lost the response.
  1280. * For input, all data must've been sent.
  1281. * We lost may have lost the response
  1282. * (and a confirmation was requested) and maybe
  1283. * some data.
  1284. *
  1285. * If all data received, send SRR
  1286. * asking for response. If partial data received,
  1287. * or gaps, SRR requests data at start of gap.
  1288. * Recovery via SRR relies on in-order-delivery.
  1289. */
  1290. if (data_dir == DMA_TO_DEVICE) {
  1291. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1292. } else if (fsp->xfer_contig_end == offset) {
  1293. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1294. } else {
  1295. offset = fsp->xfer_contig_end;
  1296. r_ctl = FC_RCTL_DD_SOL_DATA;
  1297. }
  1298. fc_fcp_srr(fsp, r_ctl, offset);
  1299. } else if (e_stat & ESB_ST_SEQ_INIT) {
  1300. /*
  1301. * The remote port has the initiative, so just
  1302. * keep waiting for it to complete.
  1303. */
  1304. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1305. } else {
  1306. /*
  1307. * The exchange is incomplete, we have seq. initiative.
  1308. * Lost response with requested confirmation,
  1309. * lost confirmation, lost transfer ready or
  1310. * lost write data.
  1311. *
  1312. * For output, if not all data was received, ask
  1313. * for transfer ready to be repeated.
  1314. *
  1315. * If we received or sent all the data, send SRR to
  1316. * request response.
  1317. *
  1318. * If we lost a response, we may have lost some read
  1319. * data as well.
  1320. */
  1321. r_ctl = FC_RCTL_DD_SOL_DATA;
  1322. if (data_dir == DMA_TO_DEVICE) {
  1323. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1324. if (offset < fsp->data_len)
  1325. r_ctl = FC_RCTL_DD_DATA_DESC;
  1326. } else if (offset == fsp->xfer_contig_end) {
  1327. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1328. } else if (fsp->xfer_contig_end < offset) {
  1329. offset = fsp->xfer_contig_end;
  1330. }
  1331. fc_fcp_srr(fsp, r_ctl, offset);
  1332. }
  1333. }
  1334. unlock_out:
  1335. fc_fcp_unlock_pkt(fsp);
  1336. out:
  1337. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1338. fc_frame_free(fp);
  1339. }
  1340. /*
  1341. * Handle error response or timeout for REC exchange.
  1342. */
  1343. static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1344. {
  1345. int error = PTR_ERR(fp);
  1346. if (fc_fcp_lock_pkt(fsp))
  1347. goto out;
  1348. switch (error) {
  1349. case -FC_EX_CLOSED:
  1350. fc_fcp_retry_cmd(fsp);
  1351. break;
  1352. default:
  1353. FC_DBG("REC %p fid %x error unexpected error %d\n",
  1354. fsp, fsp->rport->port_id, error);
  1355. fsp->status_code = FC_CMD_PLOGO;
  1356. /* fall through */
  1357. case -FC_EX_TIMEOUT:
  1358. /*
  1359. * Assume REC or LS_ACC was lost.
  1360. * The exchange manager will have aborted REC, so retry.
  1361. */
  1362. FC_DBG("REC fid %x error error %d retry %d/%d\n",
  1363. fsp->rport->port_id, error, fsp->recov_retry,
  1364. FC_MAX_RECOV_RETRY);
  1365. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1366. fc_fcp_rec(fsp);
  1367. else
  1368. fc_timeout_error(fsp);
  1369. break;
  1370. }
  1371. fc_fcp_unlock_pkt(fsp);
  1372. out:
  1373. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1374. }
  1375. /*
  1376. * Time out error routine:
  1377. * abort's the I/O close the exchange and
  1378. * send completion notification to scsi layer
  1379. */
  1380. static void fc_timeout_error(struct fc_fcp_pkt *fsp)
  1381. {
  1382. fsp->status_code = FC_CMD_TIME_OUT;
  1383. fsp->cdb_status = 0;
  1384. fsp->io_status = 0;
  1385. /*
  1386. * if this fails then we let the scsi command timer fire and
  1387. * scsi-ml escalate.
  1388. */
  1389. fc_fcp_send_abort(fsp);
  1390. }
  1391. /*
  1392. * Sequence retransmission request.
  1393. * This is called after receiving status but insufficient data, or
  1394. * when expecting status but the request has timed out.
  1395. */
  1396. static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
  1397. {
  1398. struct fc_lport *lp = fsp->lp;
  1399. struct fc_rport *rport;
  1400. struct fc_rport_libfc_priv *rp;
  1401. struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
  1402. struct fc_seq *seq;
  1403. struct fcp_srr *srr;
  1404. struct fc_frame *fp;
  1405. u8 cdb_op;
  1406. rport = fsp->rport;
  1407. rp = rport->dd_data;
  1408. cdb_op = fsp->cdb_cmd.fc_cdb[0];
  1409. if (!(rp->flags & FC_RP_FLAGS_RETRY) || rp->rp_state != RPORT_ST_READY)
  1410. goto retry; /* shouldn't happen */
  1411. fp = fc_frame_alloc(lp, sizeof(*srr));
  1412. if (!fp)
  1413. goto retry;
  1414. srr = fc_frame_payload_get(fp, sizeof(*srr));
  1415. memset(srr, 0, sizeof(*srr));
  1416. srr->srr_op = ELS_SRR;
  1417. srr->srr_ox_id = htons(ep->oxid);
  1418. srr->srr_rx_id = htons(ep->rxid);
  1419. srr->srr_r_ctl = r_ctl;
  1420. srr->srr_rel_off = htonl(offset);
  1421. fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id,
  1422. fc_host_port_id(rp->local_port->host), FC_TYPE_FCP,
  1423. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1424. seq = lp->tt.exch_seq_send(lp, fp, fc_fcp_srr_resp, NULL,
  1425. fsp, jiffies_to_msecs(FC_SCSI_REC_TOV));
  1426. if (!seq) {
  1427. fc_frame_free(fp);
  1428. goto retry;
  1429. }
  1430. fsp->recov_seq = seq;
  1431. fsp->xfer_len = offset;
  1432. fsp->xfer_contig_end = offset;
  1433. fsp->state &= ~FC_SRB_RCV_STATUS;
  1434. fc_fcp_pkt_hold(fsp); /* hold for outstanding SRR */
  1435. return;
  1436. retry:
  1437. fc_fcp_retry_cmd(fsp);
  1438. }
  1439. /*
  1440. * Handle response from SRR.
  1441. */
  1442. static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1443. {
  1444. struct fc_fcp_pkt *fsp = arg;
  1445. struct fc_frame_header *fh;
  1446. if (IS_ERR(fp)) {
  1447. fc_fcp_srr_error(fsp, fp);
  1448. return;
  1449. }
  1450. if (fc_fcp_lock_pkt(fsp))
  1451. goto out;
  1452. fh = fc_frame_header_get(fp);
  1453. /*
  1454. * BUG? fc_fcp_srr_error calls exch_done which would release
  1455. * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
  1456. * then fc_exch_timeout would be sending an abort. The exch_done
  1457. * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
  1458. * an abort response though.
  1459. */
  1460. if (fh->fh_type == FC_TYPE_BLS) {
  1461. fc_fcp_unlock_pkt(fsp);
  1462. return;
  1463. }
  1464. fsp->recov_seq = NULL;
  1465. switch (fc_frame_payload_op(fp)) {
  1466. case ELS_LS_ACC:
  1467. fsp->recov_retry = 0;
  1468. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1469. break;
  1470. case ELS_LS_RJT:
  1471. default:
  1472. fc_timeout_error(fsp);
  1473. break;
  1474. }
  1475. fc_fcp_unlock_pkt(fsp);
  1476. fsp->lp->tt.exch_done(seq);
  1477. out:
  1478. fc_frame_free(fp);
  1479. fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
  1480. }
  1481. static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1482. {
  1483. if (fc_fcp_lock_pkt(fsp))
  1484. goto out;
  1485. fsp->lp->tt.exch_done(fsp->recov_seq);
  1486. fsp->recov_seq = NULL;
  1487. switch (PTR_ERR(fp)) {
  1488. case -FC_EX_TIMEOUT:
  1489. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1490. fc_fcp_rec(fsp);
  1491. else
  1492. fc_timeout_error(fsp);
  1493. break;
  1494. case -FC_EX_CLOSED: /* e.g., link failure */
  1495. /* fall through */
  1496. default:
  1497. fc_fcp_retry_cmd(fsp);
  1498. break;
  1499. }
  1500. fc_fcp_unlock_pkt(fsp);
  1501. out:
  1502. fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
  1503. }
  1504. static inline int fc_fcp_lport_queue_ready(struct fc_lport *lp)
  1505. {
  1506. /* lock ? */
  1507. return (lp->state == LPORT_ST_READY) && lp->link_up && !lp->qfull;
  1508. }
  1509. /**
  1510. * fc_queuecommand - The queuecommand function of the scsi template
  1511. * @cmd: struct scsi_cmnd to be executed
  1512. * @done: Callback function to be called when cmd is completed
  1513. *
  1514. * this is the i/o strategy routine, called by the scsi layer
  1515. * this routine is called with holding the host_lock.
  1516. */
  1517. int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
  1518. {
  1519. struct fc_lport *lp;
  1520. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1521. struct fc_fcp_pkt *fsp;
  1522. struct fc_rport_libfc_priv *rp;
  1523. int rval;
  1524. int rc = 0;
  1525. struct fcoe_dev_stats *stats;
  1526. lp = shost_priv(sc_cmd->device->host);
  1527. rval = fc_remote_port_chkready(rport);
  1528. if (rval) {
  1529. sc_cmd->result = rval;
  1530. done(sc_cmd);
  1531. goto out;
  1532. }
  1533. if (!*(struct fc_remote_port **)rport->dd_data) {
  1534. /*
  1535. * rport is transitioning from blocked/deleted to
  1536. * online
  1537. */
  1538. sc_cmd->result = DID_IMM_RETRY << 16;
  1539. done(sc_cmd);
  1540. goto out;
  1541. }
  1542. rp = rport->dd_data;
  1543. if (!fc_fcp_lport_queue_ready(lp)) {
  1544. rc = SCSI_MLQUEUE_HOST_BUSY;
  1545. goto out;
  1546. }
  1547. fsp = fc_fcp_pkt_alloc(lp, GFP_ATOMIC);
  1548. if (fsp == NULL) {
  1549. rc = SCSI_MLQUEUE_HOST_BUSY;
  1550. goto out;
  1551. }
  1552. /*
  1553. * build the libfc request pkt
  1554. */
  1555. fsp->cmd = sc_cmd; /* save the cmd */
  1556. fsp->lp = lp; /* save the softc ptr */
  1557. fsp->rport = rport; /* set the remote port ptr */
  1558. sc_cmd->scsi_done = done;
  1559. /*
  1560. * set up the transfer length
  1561. */
  1562. fsp->data_len = scsi_bufflen(sc_cmd);
  1563. fsp->xfer_len = 0;
  1564. /*
  1565. * setup the data direction
  1566. */
  1567. stats = fc_lport_get_stats(lp);
  1568. if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
  1569. fsp->req_flags = FC_SRB_READ;
  1570. stats->InputRequests++;
  1571. stats->InputMegabytes = fsp->data_len;
  1572. } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
  1573. fsp->req_flags = FC_SRB_WRITE;
  1574. stats->OutputRequests++;
  1575. stats->OutputMegabytes = fsp->data_len;
  1576. } else {
  1577. fsp->req_flags = 0;
  1578. stats->ControlRequests++;
  1579. }
  1580. fsp->tgt_flags = rp->flags;
  1581. init_timer(&fsp->timer);
  1582. fsp->timer.data = (unsigned long)fsp;
  1583. /*
  1584. * send it to the lower layer
  1585. * if we get -1 return then put the request in the pending
  1586. * queue.
  1587. */
  1588. rval = fc_fcp_pkt_send(lp, fsp);
  1589. if (rval != 0) {
  1590. fsp->state = FC_SRB_FREE;
  1591. fc_fcp_pkt_release(fsp);
  1592. rc = SCSI_MLQUEUE_HOST_BUSY;
  1593. }
  1594. out:
  1595. return rc;
  1596. }
  1597. EXPORT_SYMBOL(fc_queuecommand);
  1598. /**
  1599. * fc_io_compl() - Handle responses for completed commands
  1600. * @fsp: scsi packet
  1601. *
  1602. * Translates a error to a Linux SCSI error.
  1603. *
  1604. * The fcp packet lock must be held when calling.
  1605. */
  1606. static void fc_io_compl(struct fc_fcp_pkt *fsp)
  1607. {
  1608. struct fc_fcp_internal *si;
  1609. struct scsi_cmnd *sc_cmd;
  1610. struct fc_lport *lp;
  1611. unsigned long flags;
  1612. /* release outstanding ddp context */
  1613. fc_fcp_ddp_done(fsp);
  1614. fsp->state |= FC_SRB_COMPL;
  1615. if (!(fsp->state & FC_SRB_FCP_PROCESSING_TMO)) {
  1616. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1617. del_timer_sync(&fsp->timer);
  1618. spin_lock_bh(&fsp->scsi_pkt_lock);
  1619. }
  1620. lp = fsp->lp;
  1621. si = fc_get_scsi_internal(lp);
  1622. spin_lock_irqsave(lp->host->host_lock, flags);
  1623. if (!fsp->cmd) {
  1624. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1625. return;
  1626. }
  1627. /*
  1628. * if a command timed out while we had to try and throttle IO
  1629. * and it is now getting cleaned up, then we are about to
  1630. * try again so clear the throttled flag incase we get more
  1631. * time outs.
  1632. */
  1633. if (si->throttled && fsp->state & FC_SRB_NOMEM)
  1634. si->throttled = 0;
  1635. sc_cmd = fsp->cmd;
  1636. fsp->cmd = NULL;
  1637. if (!sc_cmd->SCp.ptr) {
  1638. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1639. return;
  1640. }
  1641. CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status;
  1642. switch (fsp->status_code) {
  1643. case FC_COMPLETE:
  1644. if (fsp->cdb_status == 0) {
  1645. /*
  1646. * good I/O status
  1647. */
  1648. sc_cmd->result = DID_OK << 16;
  1649. if (fsp->scsi_resid)
  1650. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1651. } else if (fsp->cdb_status == QUEUE_FULL) {
  1652. struct scsi_device *tmp_sdev;
  1653. struct scsi_device *sdev = sc_cmd->device;
  1654. shost_for_each_device(tmp_sdev, sdev->host) {
  1655. if (tmp_sdev->id != sdev->id)
  1656. continue;
  1657. if (tmp_sdev->queue_depth > 1) {
  1658. scsi_track_queue_full(tmp_sdev,
  1659. tmp_sdev->
  1660. queue_depth - 1);
  1661. }
  1662. }
  1663. sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
  1664. } else {
  1665. /*
  1666. * transport level I/O was ok but scsi
  1667. * has non zero status
  1668. */
  1669. sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
  1670. }
  1671. break;
  1672. case FC_ERROR:
  1673. sc_cmd->result = DID_ERROR << 16;
  1674. break;
  1675. case FC_DATA_UNDRUN:
  1676. if ((fsp->cdb_status == 0) && !(fsp->req_flags & FC_SRB_READ)) {
  1677. /*
  1678. * scsi status is good but transport level
  1679. * underrun.
  1680. */
  1681. sc_cmd->result = DID_OK << 16;
  1682. } else {
  1683. /*
  1684. * scsi got underrun, this is an error
  1685. */
  1686. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1687. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1688. }
  1689. break;
  1690. case FC_DATA_OVRRUN:
  1691. /*
  1692. * overrun is an error
  1693. */
  1694. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1695. break;
  1696. case FC_CMD_ABORTED:
  1697. sc_cmd->result = (DID_ABORT << 16) | fsp->io_status;
  1698. break;
  1699. case FC_CMD_TIME_OUT:
  1700. sc_cmd->result = (DID_BUS_BUSY << 16) | fsp->io_status;
  1701. break;
  1702. case FC_CMD_RESET:
  1703. sc_cmd->result = (DID_RESET << 16);
  1704. break;
  1705. case FC_HRD_ERROR:
  1706. sc_cmd->result = (DID_NO_CONNECT << 16);
  1707. break;
  1708. default:
  1709. sc_cmd->result = (DID_ERROR << 16);
  1710. break;
  1711. }
  1712. list_del(&fsp->list);
  1713. sc_cmd->SCp.ptr = NULL;
  1714. sc_cmd->scsi_done(sc_cmd);
  1715. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1716. /* release ref from initial allocation in queue command */
  1717. fc_fcp_pkt_release(fsp);
  1718. }
  1719. /**
  1720. * fc_fcp_complete() - complete processing of a fcp packet
  1721. * @fsp: fcp packet
  1722. *
  1723. * This function may sleep if a fsp timer is pending.
  1724. * The host lock must not be held by caller.
  1725. */
  1726. void fc_fcp_complete(struct fc_fcp_pkt *fsp)
  1727. {
  1728. if (fc_fcp_lock_pkt(fsp))
  1729. return;
  1730. fc_fcp_complete_locked(fsp);
  1731. fc_fcp_unlock_pkt(fsp);
  1732. }
  1733. EXPORT_SYMBOL(fc_fcp_complete);
  1734. /**
  1735. * fc_eh_abort() - Abort a command
  1736. * @sc_cmd: scsi command to abort
  1737. *
  1738. * From scsi host template.
  1739. * send ABTS to the target device and wait for the response
  1740. * sc_cmd is the pointer to the command to be aborted.
  1741. */
  1742. int fc_eh_abort(struct scsi_cmnd *sc_cmd)
  1743. {
  1744. struct fc_fcp_pkt *fsp;
  1745. struct fc_lport *lp;
  1746. int rc = FAILED;
  1747. unsigned long flags;
  1748. lp = shost_priv(sc_cmd->device->host);
  1749. if (lp->state != LPORT_ST_READY)
  1750. return rc;
  1751. else if (!lp->link_up)
  1752. return rc;
  1753. spin_lock_irqsave(lp->host->host_lock, flags);
  1754. fsp = CMD_SP(sc_cmd);
  1755. if (!fsp) {
  1756. /* command completed while scsi eh was setting up */
  1757. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1758. return SUCCESS;
  1759. }
  1760. /* grab a ref so the fsp and sc_cmd cannot be relased from under us */
  1761. fc_fcp_pkt_hold(fsp);
  1762. spin_unlock_irqrestore(lp->host->host_lock, flags);
  1763. if (fc_fcp_lock_pkt(fsp)) {
  1764. /* completed while we were waiting for timer to be deleted */
  1765. rc = SUCCESS;
  1766. goto release_pkt;
  1767. }
  1768. rc = fc_fcp_pkt_abort(lp, fsp);
  1769. fc_fcp_unlock_pkt(fsp);
  1770. release_pkt:
  1771. fc_fcp_pkt_release(fsp);
  1772. return rc;
  1773. }
  1774. EXPORT_SYMBOL(fc_eh_abort);
  1775. /**
  1776. * fc_eh_device_reset() Reset a single LUN
  1777. * @sc_cmd: scsi command
  1778. *
  1779. * Set from scsi host template to send tm cmd to the target and wait for the
  1780. * response.
  1781. */
  1782. int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
  1783. {
  1784. struct fc_lport *lp;
  1785. struct fc_fcp_pkt *fsp;
  1786. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1787. int rc = FAILED;
  1788. struct fc_rport_libfc_priv *rp;
  1789. int rval;
  1790. rval = fc_remote_port_chkready(rport);
  1791. if (rval)
  1792. goto out;
  1793. rp = rport->dd_data;
  1794. lp = shost_priv(sc_cmd->device->host);
  1795. if (lp->state != LPORT_ST_READY)
  1796. return rc;
  1797. fsp = fc_fcp_pkt_alloc(lp, GFP_NOIO);
  1798. if (fsp == NULL) {
  1799. FC_DBG("could not allocate scsi_pkt\n");
  1800. sc_cmd->result = DID_NO_CONNECT << 16;
  1801. goto out;
  1802. }
  1803. /*
  1804. * Build the libfc request pkt. Do not set the scsi cmnd, because
  1805. * the sc passed in is not setup for execution like when sent
  1806. * through the queuecommand callout.
  1807. */
  1808. fsp->lp = lp; /* save the softc ptr */
  1809. fsp->rport = rport; /* set the remote port ptr */
  1810. /*
  1811. * flush outstanding commands
  1812. */
  1813. rc = fc_lun_reset(lp, fsp, scmd_id(sc_cmd), sc_cmd->device->lun);
  1814. fsp->state = FC_SRB_FREE;
  1815. fc_fcp_pkt_release(fsp);
  1816. out:
  1817. return rc;
  1818. }
  1819. EXPORT_SYMBOL(fc_eh_device_reset);
  1820. /**
  1821. * fc_eh_host_reset() - The reset function will reset the ports on the host.
  1822. * @sc_cmd: scsi command
  1823. */
  1824. int fc_eh_host_reset(struct scsi_cmnd *sc_cmd)
  1825. {
  1826. struct Scsi_Host *shost = sc_cmd->device->host;
  1827. struct fc_lport *lp = shost_priv(shost);
  1828. unsigned long wait_tmo;
  1829. lp->tt.lport_reset(lp);
  1830. wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT;
  1831. while (!fc_fcp_lport_queue_ready(lp) && time_before(jiffies, wait_tmo))
  1832. msleep(1000);
  1833. if (fc_fcp_lport_queue_ready(lp)) {
  1834. shost_printk(KERN_INFO, shost, "Host reset succeeded.\n");
  1835. return SUCCESS;
  1836. } else {
  1837. shost_printk(KERN_INFO, shost, "Host reset failed. "
  1838. "lport not ready.\n");
  1839. return FAILED;
  1840. }
  1841. }
  1842. EXPORT_SYMBOL(fc_eh_host_reset);
  1843. /**
  1844. * fc_slave_alloc() - configure queue depth
  1845. * @sdev: scsi device
  1846. *
  1847. * Configures queue depth based on host's cmd_per_len. If not set
  1848. * then we use the libfc default.
  1849. */
  1850. int fc_slave_alloc(struct scsi_device *sdev)
  1851. {
  1852. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1853. int queue_depth;
  1854. if (!rport || fc_remote_port_chkready(rport))
  1855. return -ENXIO;
  1856. if (sdev->tagged_supported) {
  1857. if (sdev->host->hostt->cmd_per_lun)
  1858. queue_depth = sdev->host->hostt->cmd_per_lun;
  1859. else
  1860. queue_depth = FC_FCP_DFLT_QUEUE_DEPTH;
  1861. scsi_activate_tcq(sdev, queue_depth);
  1862. }
  1863. return 0;
  1864. }
  1865. EXPORT_SYMBOL(fc_slave_alloc);
  1866. int fc_change_queue_depth(struct scsi_device *sdev, int qdepth)
  1867. {
  1868. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  1869. return sdev->queue_depth;
  1870. }
  1871. EXPORT_SYMBOL(fc_change_queue_depth);
  1872. int fc_change_queue_type(struct scsi_device *sdev, int tag_type)
  1873. {
  1874. if (sdev->tagged_supported) {
  1875. scsi_set_tag_type(sdev, tag_type);
  1876. if (tag_type)
  1877. scsi_activate_tcq(sdev, sdev->queue_depth);
  1878. else
  1879. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  1880. } else
  1881. tag_type = 0;
  1882. return tag_type;
  1883. }
  1884. EXPORT_SYMBOL(fc_change_queue_type);
  1885. void fc_fcp_destroy(struct fc_lport *lp)
  1886. {
  1887. struct fc_fcp_internal *si = fc_get_scsi_internal(lp);
  1888. if (!list_empty(&si->scsi_pkt_queue))
  1889. printk(KERN_ERR "Leaked scsi packets.\n");
  1890. mempool_destroy(si->scsi_pkt_pool);
  1891. kfree(si);
  1892. lp->scsi_priv = NULL;
  1893. }
  1894. EXPORT_SYMBOL(fc_fcp_destroy);
  1895. int fc_fcp_init(struct fc_lport *lp)
  1896. {
  1897. int rc;
  1898. struct fc_fcp_internal *si;
  1899. if (!lp->tt.fcp_cmd_send)
  1900. lp->tt.fcp_cmd_send = fc_fcp_cmd_send;
  1901. if (!lp->tt.fcp_cleanup)
  1902. lp->tt.fcp_cleanup = fc_fcp_cleanup;
  1903. if (!lp->tt.fcp_abort_io)
  1904. lp->tt.fcp_abort_io = fc_fcp_abort_io;
  1905. si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL);
  1906. if (!si)
  1907. return -ENOMEM;
  1908. lp->scsi_priv = si;
  1909. INIT_LIST_HEAD(&si->scsi_pkt_queue);
  1910. si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep);
  1911. if (!si->scsi_pkt_pool) {
  1912. rc = -ENOMEM;
  1913. goto free_internal;
  1914. }
  1915. return 0;
  1916. free_internal:
  1917. kfree(si);
  1918. return rc;
  1919. }
  1920. EXPORT_SYMBOL(fc_fcp_init);
  1921. static int __init libfc_init(void)
  1922. {
  1923. int rc;
  1924. scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt",
  1925. sizeof(struct fc_fcp_pkt),
  1926. 0, SLAB_HWCACHE_ALIGN, NULL);
  1927. if (scsi_pkt_cachep == NULL) {
  1928. FC_DBG("Unable to allocate SRB cache...module load failed!");
  1929. return -ENOMEM;
  1930. }
  1931. rc = fc_setup_exch_mgr();
  1932. if (rc)
  1933. goto destroy_pkt_cache;
  1934. rc = fc_setup_rport();
  1935. if (rc)
  1936. goto destroy_em;
  1937. return rc;
  1938. destroy_em:
  1939. fc_destroy_exch_mgr();
  1940. destroy_pkt_cache:
  1941. kmem_cache_destroy(scsi_pkt_cachep);
  1942. return rc;
  1943. }
  1944. static void __exit libfc_exit(void)
  1945. {
  1946. kmem_cache_destroy(scsi_pkt_cachep);
  1947. fc_destroy_exch_mgr();
  1948. fc_destroy_rport();
  1949. }
  1950. module_init(libfc_init);
  1951. module_exit(libfc_exit);