fc_fcp.c 54 KB

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