libiscsi.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  1. /*
  2. * iSCSI lib functions
  3. *
  4. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2004 - 2006 Mike Christie
  6. * Copyright (C) 2004 - 2005 Dmitry Yusupov
  7. * Copyright (C) 2004 - 2005 Alex Aizman
  8. * maintained by open-iscsi@googlegroups.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/mutex.h>
  26. #include <linux/kfifo.h>
  27. #include <linux/delay.h>
  28. #include <asm/unaligned.h>
  29. #include <net/tcp.h>
  30. #include <scsi/scsi_cmnd.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_eh.h>
  33. #include <scsi/scsi_tcq.h>
  34. #include <scsi/scsi_host.h>
  35. #include <scsi/scsi.h>
  36. #include <scsi/iscsi_proto.h>
  37. #include <scsi/scsi_transport.h>
  38. #include <scsi/scsi_transport_iscsi.h>
  39. #include <scsi/libiscsi.h>
  40. struct iscsi_session *
  41. class_to_transport_session(struct iscsi_cls_session *cls_session)
  42. {
  43. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  44. return iscsi_hostdata(shost->hostdata);
  45. }
  46. EXPORT_SYMBOL_GPL(class_to_transport_session);
  47. #define INVALID_SN_DELTA 0xffff
  48. int
  49. iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  50. {
  51. uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
  52. uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
  53. if (max_cmdsn < exp_cmdsn -1 &&
  54. max_cmdsn > exp_cmdsn - INVALID_SN_DELTA)
  55. return ISCSI_ERR_MAX_CMDSN;
  56. if (max_cmdsn > session->max_cmdsn ||
  57. max_cmdsn < session->max_cmdsn - INVALID_SN_DELTA)
  58. session->max_cmdsn = max_cmdsn;
  59. if (exp_cmdsn > session->exp_cmdsn ||
  60. exp_cmdsn < session->exp_cmdsn - INVALID_SN_DELTA)
  61. session->exp_cmdsn = exp_cmdsn;
  62. return 0;
  63. }
  64. EXPORT_SYMBOL_GPL(iscsi_check_assign_cmdsn);
  65. void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
  66. struct iscsi_data *hdr)
  67. {
  68. struct iscsi_conn *conn = ctask->conn;
  69. memset(hdr, 0, sizeof(struct iscsi_data));
  70. hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  71. hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
  72. ctask->unsol_datasn++;
  73. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  74. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  75. hdr->itt = ctask->hdr->itt;
  76. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  77. hdr->offset = cpu_to_be32(ctask->unsol_offset);
  78. if (ctask->unsol_count > conn->max_xmit_dlength) {
  79. hton24(hdr->dlength, conn->max_xmit_dlength);
  80. ctask->data_count = conn->max_xmit_dlength;
  81. ctask->unsol_offset += ctask->data_count;
  82. hdr->flags = 0;
  83. } else {
  84. hton24(hdr->dlength, ctask->unsol_count);
  85. ctask->data_count = ctask->unsol_count;
  86. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  87. }
  88. }
  89. EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
  90. /**
  91. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  92. * @ctask: iscsi cmd task
  93. *
  94. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  95. * fields like dlength or final based on how much data it sends
  96. */
  97. static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
  98. {
  99. struct iscsi_conn *conn = ctask->conn;
  100. struct iscsi_session *session = conn->session;
  101. struct iscsi_cmd *hdr = ctask->hdr;
  102. struct scsi_cmnd *sc = ctask->sc;
  103. hdr->opcode = ISCSI_OP_SCSI_CMD;
  104. hdr->flags = ISCSI_ATTR_SIMPLE;
  105. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  106. hdr->itt = build_itt(ctask->itt, conn->id, session->age);
  107. hdr->data_length = cpu_to_be32(sc->request_bufflen);
  108. hdr->cmdsn = cpu_to_be32(session->cmdsn);
  109. session->cmdsn++;
  110. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  111. memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
  112. memset(&hdr->cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len);
  113. ctask->data_count = 0;
  114. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  115. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  116. /*
  117. * Write counters:
  118. *
  119. * imm_count bytes to be sent right after
  120. * SCSI PDU Header
  121. *
  122. * unsol_count bytes(as Data-Out) to be sent
  123. * without R2T ack right after
  124. * immediate data
  125. *
  126. * r2t_data_count bytes to be sent via R2T ack's
  127. *
  128. * pad_count bytes to be sent as zero-padding
  129. */
  130. ctask->imm_count = 0;
  131. ctask->unsol_count = 0;
  132. ctask->unsol_offset = 0;
  133. ctask->unsol_datasn = 0;
  134. if (session->imm_data_en) {
  135. if (ctask->total_length >= session->first_burst)
  136. ctask->imm_count = min(session->first_burst,
  137. conn->max_xmit_dlength);
  138. else
  139. ctask->imm_count = min(ctask->total_length,
  140. conn->max_xmit_dlength);
  141. hton24(ctask->hdr->dlength, ctask->imm_count);
  142. } else
  143. zero_data(ctask->hdr->dlength);
  144. if (!session->initial_r2t_en) {
  145. ctask->unsol_count = min(session->first_burst,
  146. ctask->total_length) - ctask->imm_count;
  147. ctask->unsol_offset = ctask->imm_count;
  148. }
  149. if (!ctask->unsol_count)
  150. /* No unsolicit Data-Out's */
  151. ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  152. } else {
  153. ctask->datasn = 0;
  154. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  155. zero_data(hdr->dlength);
  156. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  157. hdr->flags |= ISCSI_FLAG_CMD_READ;
  158. }
  159. conn->scsicmd_pdus_cnt++;
  160. }
  161. EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu);
  162. /**
  163. * iscsi_complete_command - return command back to scsi-ml
  164. * @ctask: iscsi cmd task
  165. *
  166. * Must be called with session lock.
  167. * This function returns the scsi command to scsi-ml and returns
  168. * the cmd task to the pool of available cmd tasks.
  169. */
  170. static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
  171. {
  172. struct iscsi_session *session = ctask->conn->session;
  173. struct scsi_cmnd *sc = ctask->sc;
  174. ctask->state = ISCSI_TASK_COMPLETED;
  175. ctask->sc = NULL;
  176. /* SCSI eh reuses commands to verify us */
  177. sc->SCp.ptr = NULL;
  178. list_del_init(&ctask->running);
  179. __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  180. sc->scsi_done(sc);
  181. }
  182. static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
  183. {
  184. atomic_inc(&ctask->refcount);
  185. }
  186. static void iscsi_get_ctask(struct iscsi_cmd_task *ctask)
  187. {
  188. spin_lock_bh(&ctask->conn->session->lock);
  189. __iscsi_get_ctask(ctask);
  190. spin_unlock_bh(&ctask->conn->session->lock);
  191. }
  192. static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
  193. {
  194. if (atomic_dec_and_test(&ctask->refcount))
  195. iscsi_complete_command(ctask);
  196. }
  197. static void iscsi_put_ctask(struct iscsi_cmd_task *ctask)
  198. {
  199. spin_lock_bh(&ctask->conn->session->lock);
  200. __iscsi_put_ctask(ctask);
  201. spin_unlock_bh(&ctask->conn->session->lock);
  202. }
  203. /**
  204. * iscsi_cmd_rsp - SCSI Command Response processing
  205. * @conn: iscsi connection
  206. * @hdr: iscsi header
  207. * @ctask: scsi command task
  208. * @data: cmd data buffer
  209. * @datalen: len of buffer
  210. *
  211. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  212. * then completes the command and task.
  213. **/
  214. static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  215. struct iscsi_cmd_task *ctask, char *data,
  216. int datalen)
  217. {
  218. int rc;
  219. struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
  220. struct iscsi_session *session = conn->session;
  221. struct scsi_cmnd *sc = ctask->sc;
  222. rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
  223. if (rc) {
  224. sc->result = DID_ERROR << 16;
  225. goto out;
  226. }
  227. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  228. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  229. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  230. sc->result = DID_ERROR << 16;
  231. goto out;
  232. }
  233. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  234. uint16_t senselen;
  235. if (datalen < 2) {
  236. invalid_datalen:
  237. printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
  238. "invalid data buffer size of %d\n", datalen);
  239. sc->result = DID_BAD_TARGET << 16;
  240. goto out;
  241. }
  242. senselen = be16_to_cpu(get_unaligned((__be16 *) data));
  243. if (datalen < senselen)
  244. goto invalid_datalen;
  245. memcpy(sc->sense_buffer, data + 2,
  246. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  247. debug_scsi("copied %d bytes of sense\n",
  248. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  249. }
  250. if (sc->sc_data_direction == DMA_TO_DEVICE)
  251. goto out;
  252. if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
  253. int res_count = be32_to_cpu(rhdr->residual_count);
  254. if (res_count > 0 && res_count <= sc->request_bufflen)
  255. sc->resid = res_count;
  256. else
  257. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  258. } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
  259. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  260. else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
  261. sc->resid = be32_to_cpu(rhdr->residual_count);
  262. out:
  263. debug_scsi("done [sc %lx res %d itt 0x%x]\n",
  264. (long)sc, sc->result, ctask->itt);
  265. conn->scsirsp_pdus_cnt++;
  266. __iscsi_put_ctask(ctask);
  267. return rc;
  268. }
  269. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  270. {
  271. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  272. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  273. conn->tmfrsp_pdus_cnt++;
  274. if (conn->tmabort_state != TMABORT_INITIAL)
  275. return;
  276. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  277. conn->tmabort_state = TMABORT_SUCCESS;
  278. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  279. conn->tmabort_state = TMABORT_NOT_FOUND;
  280. else
  281. conn->tmabort_state = TMABORT_FAILED;
  282. wake_up(&conn->ehwait);
  283. }
  284. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  285. char *data, int datalen)
  286. {
  287. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  288. struct iscsi_hdr rejected_pdu;
  289. uint32_t itt;
  290. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  291. if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
  292. if (ntoh24(reject->dlength) > datalen)
  293. return ISCSI_ERR_PROTO;
  294. if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
  295. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  296. itt = get_itt(rejected_pdu.itt);
  297. printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
  298. "due to DataDigest error.\n", itt,
  299. rejected_pdu.opcode);
  300. }
  301. }
  302. return 0;
  303. }
  304. /**
  305. * __iscsi_complete_pdu - complete pdu
  306. * @conn: iscsi conn
  307. * @hdr: iscsi header
  308. * @data: data buffer
  309. * @datalen: len of data buffer
  310. *
  311. * Completes pdu processing by freeing any resources allocated at
  312. * queuecommand or send generic. session lock must be held and verify
  313. * itt must have been called.
  314. */
  315. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  316. char *data, int datalen)
  317. {
  318. struct iscsi_session *session = conn->session;
  319. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  320. struct iscsi_cmd_task *ctask;
  321. struct iscsi_mgmt_task *mtask;
  322. uint32_t itt;
  323. if (hdr->itt != RESERVED_ITT)
  324. itt = get_itt(hdr->itt);
  325. else
  326. itt = ~0U;
  327. if (itt < session->cmds_max) {
  328. ctask = session->cmds[itt];
  329. debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  330. opcode, conn->id, ctask->itt, datalen);
  331. switch(opcode) {
  332. case ISCSI_OP_SCSI_CMD_RSP:
  333. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  334. rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
  335. datalen);
  336. break;
  337. case ISCSI_OP_SCSI_DATA_IN:
  338. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  339. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  340. conn->scsirsp_pdus_cnt++;
  341. __iscsi_put_ctask(ctask);
  342. }
  343. break;
  344. case ISCSI_OP_R2T:
  345. /* LLD handles this for now */
  346. break;
  347. default:
  348. rc = ISCSI_ERR_BAD_OPCODE;
  349. break;
  350. }
  351. } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
  352. itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
  353. mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
  354. debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  355. opcode, conn->id, mtask->itt, datalen);
  356. rc = iscsi_check_assign_cmdsn(session,
  357. (struct iscsi_nopin*)hdr);
  358. if (rc)
  359. goto done;
  360. switch(opcode) {
  361. case ISCSI_OP_LOGOUT_RSP:
  362. if (datalen) {
  363. rc = ISCSI_ERR_PROTO;
  364. break;
  365. }
  366. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  367. /* fall through */
  368. case ISCSI_OP_LOGIN_RSP:
  369. case ISCSI_OP_TEXT_RSP:
  370. /*
  371. * login related PDU's exp_statsn is handled in
  372. * userspace
  373. */
  374. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  375. rc = ISCSI_ERR_CONN_FAILED;
  376. list_del(&mtask->running);
  377. if (conn->login_mtask != mtask)
  378. __kfifo_put(session->mgmtpool.queue,
  379. (void*)&mtask, sizeof(void*));
  380. break;
  381. case ISCSI_OP_SCSI_TMFUNC_RSP:
  382. if (datalen) {
  383. rc = ISCSI_ERR_PROTO;
  384. break;
  385. }
  386. iscsi_tmf_rsp(conn, hdr);
  387. break;
  388. case ISCSI_OP_NOOP_IN:
  389. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
  390. rc = ISCSI_ERR_PROTO;
  391. break;
  392. }
  393. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  394. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  395. rc = ISCSI_ERR_CONN_FAILED;
  396. list_del(&mtask->running);
  397. if (conn->login_mtask != mtask)
  398. __kfifo_put(session->mgmtpool.queue,
  399. (void*)&mtask, sizeof(void*));
  400. break;
  401. default:
  402. rc = ISCSI_ERR_BAD_OPCODE;
  403. break;
  404. }
  405. } else if (itt == ~0U) {
  406. rc = iscsi_check_assign_cmdsn(session,
  407. (struct iscsi_nopin*)hdr);
  408. if (rc)
  409. goto done;
  410. switch(opcode) {
  411. case ISCSI_OP_NOOP_IN:
  412. if (datalen) {
  413. rc = ISCSI_ERR_PROTO;
  414. break;
  415. }
  416. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  417. break;
  418. if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
  419. rc = ISCSI_ERR_CONN_FAILED;
  420. break;
  421. case ISCSI_OP_REJECT:
  422. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  423. break;
  424. case ISCSI_OP_ASYNC_EVENT:
  425. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  426. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  427. rc = ISCSI_ERR_CONN_FAILED;
  428. break;
  429. default:
  430. rc = ISCSI_ERR_BAD_OPCODE;
  431. break;
  432. }
  433. } else
  434. rc = ISCSI_ERR_BAD_ITT;
  435. done:
  436. return rc;
  437. }
  438. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  439. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  440. char *data, int datalen)
  441. {
  442. int rc;
  443. spin_lock(&conn->session->lock);
  444. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  445. spin_unlock(&conn->session->lock);
  446. return rc;
  447. }
  448. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  449. /* verify itt (itt encoding: age+cid+itt) */
  450. int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  451. uint32_t *ret_itt)
  452. {
  453. struct iscsi_session *session = conn->session;
  454. struct iscsi_cmd_task *ctask;
  455. uint32_t itt;
  456. if (hdr->itt != RESERVED_ITT) {
  457. if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
  458. (session->age << ISCSI_AGE_SHIFT)) {
  459. printk(KERN_ERR "iscsi: received itt %x expected "
  460. "session age (%x)\n", (__force u32)hdr->itt,
  461. session->age & ISCSI_AGE_MASK);
  462. return ISCSI_ERR_BAD_ITT;
  463. }
  464. if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
  465. (conn->id << ISCSI_CID_SHIFT)) {
  466. printk(KERN_ERR "iscsi: received itt %x, expected "
  467. "CID (%x)\n", (__force u32)hdr->itt, conn->id);
  468. return ISCSI_ERR_BAD_ITT;
  469. }
  470. itt = get_itt(hdr->itt);
  471. } else
  472. itt = ~0U;
  473. if (itt < session->cmds_max) {
  474. ctask = session->cmds[itt];
  475. if (!ctask->sc) {
  476. printk(KERN_INFO "iscsi: dropping ctask with "
  477. "itt 0x%x\n", ctask->itt);
  478. /* force drop */
  479. return ISCSI_ERR_NO_SCSI_CMD;
  480. }
  481. if (ctask->sc->SCp.phase != session->age) {
  482. printk(KERN_ERR "iscsi: ctask's session age %d, "
  483. "expected %d\n", ctask->sc->SCp.phase,
  484. session->age);
  485. return ISCSI_ERR_SESSION_FAILED;
  486. }
  487. }
  488. *ret_itt = itt;
  489. return 0;
  490. }
  491. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  492. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  493. {
  494. struct iscsi_session *session = conn->session;
  495. unsigned long flags;
  496. spin_lock_irqsave(&session->lock, flags);
  497. if (session->state == ISCSI_STATE_FAILED) {
  498. spin_unlock_irqrestore(&session->lock, flags);
  499. return;
  500. }
  501. if (conn->stop_stage == 0)
  502. session->state = ISCSI_STATE_FAILED;
  503. spin_unlock_irqrestore(&session->lock, flags);
  504. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  505. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  506. iscsi_conn_error(conn->cls_conn, err);
  507. }
  508. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  509. static int iscsi_xmit_mtask(struct iscsi_conn *conn)
  510. {
  511. struct iscsi_hdr *hdr = conn->mtask->hdr;
  512. int rc, was_logout = 0;
  513. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) {
  514. conn->session->state = ISCSI_STATE_IN_RECOVERY;
  515. iscsi_block_session(session_to_cls(conn->session));
  516. was_logout = 1;
  517. }
  518. rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
  519. if (rc)
  520. return rc;
  521. /* done with this in-progress mtask */
  522. conn->mtask = NULL;
  523. if (was_logout) {
  524. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  525. return -ENODATA;
  526. }
  527. return 0;
  528. }
  529. /**
  530. * iscsi_data_xmit - xmit any command into the scheduled connection
  531. * @conn: iscsi connection
  532. *
  533. * Notes:
  534. * The function can return -EAGAIN in which case the caller must
  535. * re-schedule it again later or recover. '0' return code means
  536. * successful xmit.
  537. **/
  538. static int iscsi_data_xmit(struct iscsi_conn *conn)
  539. {
  540. struct iscsi_transport *tt;
  541. int rc = 0;
  542. if (unlikely(conn->suspend_tx)) {
  543. debug_scsi("conn %d Tx suspended!\n", conn->id);
  544. return -ENODATA;
  545. }
  546. tt = conn->session->tt;
  547. /*
  548. * Transmit in the following order:
  549. *
  550. * 1) un-finished xmit (ctask or mtask)
  551. * 2) immediate control PDUs
  552. * 3) write data
  553. * 4) SCSI commands
  554. * 5) non-immediate control PDUs
  555. *
  556. * No need to lock around __kfifo_get as long as
  557. * there's one producer and one consumer.
  558. */
  559. BUG_ON(conn->ctask && conn->mtask);
  560. if (conn->ctask) {
  561. iscsi_get_ctask(conn->ctask);
  562. rc = tt->xmit_cmd_task(conn, conn->ctask);
  563. iscsi_put_ctask(conn->ctask);
  564. if (rc)
  565. goto again;
  566. /* done with this in-progress ctask */
  567. conn->ctask = NULL;
  568. }
  569. if (conn->mtask) {
  570. rc = iscsi_xmit_mtask(conn);
  571. if (rc)
  572. goto again;
  573. }
  574. /* process immediate first */
  575. if (unlikely(__kfifo_len(conn->immqueue))) {
  576. while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
  577. sizeof(void*))) {
  578. spin_lock_bh(&conn->session->lock);
  579. list_add_tail(&conn->mtask->running,
  580. &conn->mgmt_run_list);
  581. spin_unlock_bh(&conn->session->lock);
  582. rc = iscsi_xmit_mtask(conn);
  583. if (rc)
  584. goto again;
  585. }
  586. }
  587. /* process command queue */
  588. spin_lock_bh(&conn->session->lock);
  589. while (!list_empty(&conn->xmitqueue)) {
  590. /*
  591. * iscsi tcp may readd the task to the xmitqueue to send
  592. * write data
  593. */
  594. conn->ctask = list_entry(conn->xmitqueue.next,
  595. struct iscsi_cmd_task, running);
  596. conn->ctask->state = ISCSI_TASK_RUNNING;
  597. list_move_tail(conn->xmitqueue.next, &conn->run_list);
  598. __iscsi_get_ctask(conn->ctask);
  599. spin_unlock_bh(&conn->session->lock);
  600. rc = tt->xmit_cmd_task(conn, conn->ctask);
  601. spin_lock_bh(&conn->session->lock);
  602. __iscsi_put_ctask(conn->ctask);
  603. if (rc) {
  604. spin_unlock_bh(&conn->session->lock);
  605. goto again;
  606. }
  607. }
  608. spin_unlock_bh(&conn->session->lock);
  609. /* done with this ctask */
  610. conn->ctask = NULL;
  611. /* process the rest control plane PDUs, if any */
  612. if (unlikely(__kfifo_len(conn->mgmtqueue))) {
  613. while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
  614. sizeof(void*))) {
  615. spin_lock_bh(&conn->session->lock);
  616. list_add_tail(&conn->mtask->running,
  617. &conn->mgmt_run_list);
  618. spin_unlock_bh(&conn->session->lock);
  619. rc = iscsi_xmit_mtask(conn);
  620. if (rc)
  621. goto again;
  622. }
  623. }
  624. return -ENODATA;
  625. again:
  626. if (unlikely(conn->suspend_tx))
  627. return -ENODATA;
  628. return rc;
  629. }
  630. static void iscsi_xmitworker(struct work_struct *work)
  631. {
  632. struct iscsi_conn *conn =
  633. container_of(work, struct iscsi_conn, xmitwork);
  634. int rc;
  635. /*
  636. * serialize Xmit worker on a per-connection basis.
  637. */
  638. mutex_lock(&conn->xmitmutex);
  639. do {
  640. rc = iscsi_data_xmit(conn);
  641. } while (rc >= 0 || rc == -EAGAIN);
  642. mutex_unlock(&conn->xmitmutex);
  643. }
  644. enum {
  645. FAILURE_BAD_HOST = 1,
  646. FAILURE_SESSION_FAILED,
  647. FAILURE_SESSION_FREED,
  648. FAILURE_WINDOW_CLOSED,
  649. FAILURE_OOM,
  650. FAILURE_SESSION_TERMINATE,
  651. FAILURE_SESSION_IN_RECOVERY,
  652. FAILURE_SESSION_RECOVERY_TIMEOUT,
  653. };
  654. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  655. {
  656. struct Scsi_Host *host;
  657. int reason = 0;
  658. struct iscsi_session *session;
  659. struct iscsi_conn *conn;
  660. struct iscsi_cmd_task *ctask = NULL;
  661. sc->scsi_done = done;
  662. sc->result = 0;
  663. sc->SCp.ptr = NULL;
  664. host = sc->device->host;
  665. session = iscsi_hostdata(host->hostdata);
  666. spin_lock(&session->lock);
  667. /*
  668. * ISCSI_STATE_FAILED is a temp. state. The recovery
  669. * code will decide what is best to do with command queued
  670. * during this time
  671. */
  672. if (session->state != ISCSI_STATE_LOGGED_IN &&
  673. session->state != ISCSI_STATE_FAILED) {
  674. /*
  675. * to handle the race between when we set the recovery state
  676. * and block the session we requeue here (commands could
  677. * be entering our queuecommand while a block is starting
  678. * up because the block code is not locked)
  679. */
  680. if (session->state == ISCSI_STATE_IN_RECOVERY) {
  681. reason = FAILURE_SESSION_IN_RECOVERY;
  682. goto reject;
  683. }
  684. if (session->state == ISCSI_STATE_RECOVERY_FAILED)
  685. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  686. else if (session->state == ISCSI_STATE_TERMINATE)
  687. reason = FAILURE_SESSION_TERMINATE;
  688. else
  689. reason = FAILURE_SESSION_FREED;
  690. goto fault;
  691. }
  692. /*
  693. * Check for iSCSI window and take care of CmdSN wrap-around
  694. */
  695. if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
  696. reason = FAILURE_WINDOW_CLOSED;
  697. goto reject;
  698. }
  699. conn = session->leadconn;
  700. if (!conn) {
  701. reason = FAILURE_SESSION_FREED;
  702. goto fault;
  703. }
  704. if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
  705. sizeof(void*))) {
  706. reason = FAILURE_OOM;
  707. goto reject;
  708. }
  709. sc->SCp.phase = session->age;
  710. sc->SCp.ptr = (char *)ctask;
  711. atomic_set(&ctask->refcount, 1);
  712. ctask->state = ISCSI_TASK_PENDING;
  713. ctask->mtask = NULL;
  714. ctask->conn = conn;
  715. ctask->sc = sc;
  716. INIT_LIST_HEAD(&ctask->running);
  717. ctask->total_length = sc->request_bufflen;
  718. iscsi_prep_scsi_cmd_pdu(ctask);
  719. session->tt->init_cmd_task(ctask);
  720. list_add_tail(&ctask->running, &conn->xmitqueue);
  721. debug_scsi(
  722. "ctask enq [%s cid %d sc %p cdb 0x%x itt 0x%x len %d cmdsn %d "
  723. "win %d]\n",
  724. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  725. conn->id, sc, sc->cmnd[0], ctask->itt, sc->request_bufflen,
  726. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  727. spin_unlock(&session->lock);
  728. scsi_queue_work(host, &conn->xmitwork);
  729. return 0;
  730. reject:
  731. spin_unlock(&session->lock);
  732. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  733. return SCSI_MLQUEUE_HOST_BUSY;
  734. fault:
  735. spin_unlock(&session->lock);
  736. printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
  737. sc->cmnd[0], reason);
  738. sc->result = (DID_NO_CONNECT << 16);
  739. sc->resid = sc->request_bufflen;
  740. sc->scsi_done(sc);
  741. return 0;
  742. }
  743. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  744. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  745. {
  746. if (depth > ISCSI_MAX_CMD_PER_LUN)
  747. depth = ISCSI_MAX_CMD_PER_LUN;
  748. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  749. return sdev->queue_depth;
  750. }
  751. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  752. static int
  753. iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  754. char *data, uint32_t data_size)
  755. {
  756. struct iscsi_session *session = conn->session;
  757. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  758. struct iscsi_mgmt_task *mtask;
  759. spin_lock_bh(&session->lock);
  760. if (session->state == ISCSI_STATE_TERMINATE) {
  761. spin_unlock_bh(&session->lock);
  762. return -EPERM;
  763. }
  764. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  765. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  766. /*
  767. * Login and Text are sent serially, in
  768. * request-followed-by-response sequence.
  769. * Same mtask can be used. Same ITT must be used.
  770. * Note that login_mtask is preallocated at conn_create().
  771. */
  772. mtask = conn->login_mtask;
  773. else {
  774. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  775. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  776. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  777. if (!__kfifo_get(session->mgmtpool.queue,
  778. (void*)&mtask, sizeof(void*))) {
  779. spin_unlock_bh(&session->lock);
  780. return -ENOSPC;
  781. }
  782. }
  783. /*
  784. * pre-format CmdSN for outgoing PDU.
  785. */
  786. if (hdr->itt != RESERVED_ITT) {
  787. hdr->itt = build_itt(mtask->itt, conn->id, session->age);
  788. nop->cmdsn = cpu_to_be32(session->cmdsn);
  789. if (conn->c_stage == ISCSI_CONN_STARTED &&
  790. !(hdr->opcode & ISCSI_OP_IMMEDIATE))
  791. session->cmdsn++;
  792. } else
  793. /* do not advance CmdSN */
  794. nop->cmdsn = cpu_to_be32(session->cmdsn);
  795. if (data_size) {
  796. memcpy(mtask->data, data, data_size);
  797. mtask->data_count = data_size;
  798. } else
  799. mtask->data_count = 0;
  800. INIT_LIST_HEAD(&mtask->running);
  801. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  802. if (session->tt->init_mgmt_task)
  803. session->tt->init_mgmt_task(conn, mtask, data, data_size);
  804. spin_unlock_bh(&session->lock);
  805. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  806. hdr->opcode, hdr->itt, data_size);
  807. /*
  808. * since send_pdu() could be called at least from two contexts,
  809. * we need to serialize __kfifo_put, so we don't have to take
  810. * additional lock on fast data-path
  811. */
  812. if (hdr->opcode & ISCSI_OP_IMMEDIATE)
  813. __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
  814. else
  815. __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
  816. scsi_queue_work(session->host, &conn->xmitwork);
  817. return 0;
  818. }
  819. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  820. char *data, uint32_t data_size)
  821. {
  822. struct iscsi_conn *conn = cls_conn->dd_data;
  823. int rc;
  824. mutex_lock(&conn->xmitmutex);
  825. rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
  826. mutex_unlock(&conn->xmitmutex);
  827. return rc;
  828. }
  829. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  830. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  831. {
  832. struct iscsi_session *session = class_to_transport_session(cls_session);
  833. struct iscsi_conn *conn = session->leadconn;
  834. spin_lock_bh(&session->lock);
  835. if (session->state != ISCSI_STATE_LOGGED_IN) {
  836. session->state = ISCSI_STATE_RECOVERY_FAILED;
  837. if (conn)
  838. wake_up(&conn->ehwait);
  839. }
  840. spin_unlock_bh(&session->lock);
  841. }
  842. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  843. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  844. {
  845. struct Scsi_Host *host = sc->device->host;
  846. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  847. struct iscsi_conn *conn = session->leadconn;
  848. int fail_session = 0;
  849. spin_lock_bh(&session->lock);
  850. if (session->state == ISCSI_STATE_TERMINATE) {
  851. failed:
  852. debug_scsi("failing host reset: session terminated "
  853. "[CID %d age %d]\n", conn->id, session->age);
  854. spin_unlock_bh(&session->lock);
  855. return FAILED;
  856. }
  857. if (sc->SCp.phase == session->age) {
  858. debug_scsi("failing connection CID %d due to SCSI host reset\n",
  859. conn->id);
  860. fail_session = 1;
  861. }
  862. spin_unlock_bh(&session->lock);
  863. /*
  864. * we drop the lock here but the leadconn cannot be destoyed while
  865. * we are in the scsi eh
  866. */
  867. if (fail_session)
  868. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  869. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  870. wait_event_interruptible(conn->ehwait,
  871. session->state == ISCSI_STATE_TERMINATE ||
  872. session->state == ISCSI_STATE_LOGGED_IN ||
  873. session->state == ISCSI_STATE_RECOVERY_FAILED);
  874. if (signal_pending(current))
  875. flush_signals(current);
  876. spin_lock_bh(&session->lock);
  877. if (session->state == ISCSI_STATE_LOGGED_IN)
  878. printk(KERN_INFO "iscsi: host reset succeeded\n");
  879. else
  880. goto failed;
  881. spin_unlock_bh(&session->lock);
  882. return SUCCESS;
  883. }
  884. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  885. static void iscsi_tmabort_timedout(unsigned long data)
  886. {
  887. struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
  888. struct iscsi_conn *conn = ctask->conn;
  889. struct iscsi_session *session = conn->session;
  890. spin_lock(&session->lock);
  891. if (conn->tmabort_state == TMABORT_INITIAL) {
  892. conn->tmabort_state = TMABORT_TIMEDOUT;
  893. debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
  894. ctask->sc, ctask->itt);
  895. /* unblock eh_abort() */
  896. wake_up(&conn->ehwait);
  897. }
  898. spin_unlock(&session->lock);
  899. }
  900. /* must be called with the mutex lock */
  901. static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
  902. struct iscsi_cmd_task *ctask)
  903. {
  904. struct iscsi_conn *conn = ctask->conn;
  905. struct iscsi_session *session = conn->session;
  906. struct iscsi_tm *hdr = &conn->tmhdr;
  907. int rc;
  908. /*
  909. * ctask timed out but session is OK requests must be serialized.
  910. */
  911. memset(hdr, 0, sizeof(struct iscsi_tm));
  912. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  913. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
  914. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  915. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  916. hdr->rtt = ctask->hdr->itt;
  917. hdr->refcmdsn = ctask->hdr->cmdsn;
  918. rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
  919. NULL, 0);
  920. if (rc) {
  921. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  922. debug_scsi("abort sent failure [itt 0x%x] %d\n", ctask->itt,
  923. rc);
  924. return rc;
  925. }
  926. debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
  927. spin_lock_bh(&session->lock);
  928. ctask->mtask = (struct iscsi_mgmt_task *)
  929. session->mgmt_cmds[get_itt(hdr->itt) -
  930. ISCSI_MGMT_ITT_OFFSET];
  931. if (conn->tmabort_state == TMABORT_INITIAL) {
  932. conn->tmfcmd_pdus_cnt++;
  933. conn->tmabort_timer.expires = 10*HZ + jiffies;
  934. conn->tmabort_timer.function = iscsi_tmabort_timedout;
  935. conn->tmabort_timer.data = (unsigned long)ctask;
  936. add_timer(&conn->tmabort_timer);
  937. debug_scsi("abort set timeout [itt 0x%x]\n", ctask->itt);
  938. }
  939. spin_unlock_bh(&session->lock);
  940. mutex_unlock(&conn->xmitmutex);
  941. /*
  942. * block eh thread until:
  943. *
  944. * 1) abort response
  945. * 2) abort timeout
  946. * 3) session is terminated or restarted or userspace has
  947. * given up on recovery
  948. */
  949. wait_event_interruptible(conn->ehwait,
  950. sc->SCp.phase != session->age ||
  951. session->state != ISCSI_STATE_LOGGED_IN ||
  952. conn->tmabort_state != TMABORT_INITIAL);
  953. if (signal_pending(current))
  954. flush_signals(current);
  955. del_timer_sync(&conn->tmabort_timer);
  956. mutex_lock(&conn->xmitmutex);
  957. return 0;
  958. }
  959. /*
  960. * xmit mutex and session lock must be held
  961. */
  962. static struct iscsi_mgmt_task *
  963. iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
  964. {
  965. int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
  966. struct iscsi_mgmt_task *task;
  967. debug_scsi("searching %d tasks\n", nr_tasks);
  968. for (i = 0; i < nr_tasks; i++) {
  969. __kfifo_get(fifo, (void*)&task, sizeof(void*));
  970. debug_scsi("check task %u\n", task->itt);
  971. if (task->itt == itt) {
  972. debug_scsi("matched task\n");
  973. return task;
  974. }
  975. __kfifo_put(fifo, (void*)&task, sizeof(void*));
  976. }
  977. return NULL;
  978. }
  979. static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
  980. {
  981. struct iscsi_conn *conn = ctask->conn;
  982. struct iscsi_session *session = conn->session;
  983. if (!ctask->mtask)
  984. return -EINVAL;
  985. if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
  986. list_del(&ctask->mtask->running);
  987. __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
  988. sizeof(void*));
  989. ctask->mtask = NULL;
  990. return 0;
  991. }
  992. /*
  993. * session lock and xmitmutex must be held
  994. */
  995. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  996. int err)
  997. {
  998. struct scsi_cmnd *sc;
  999. sc = ctask->sc;
  1000. if (!sc)
  1001. return;
  1002. conn->session->tt->cleanup_cmd_task(conn, ctask);
  1003. iscsi_ctask_mtask_cleanup(ctask);
  1004. sc->result = err;
  1005. sc->resid = sc->request_bufflen;
  1006. /* release ref from queuecommand */
  1007. __iscsi_put_ctask(ctask);
  1008. }
  1009. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1010. {
  1011. struct iscsi_cmd_task *ctask;
  1012. struct iscsi_conn *conn;
  1013. struct iscsi_session *session;
  1014. int rc;
  1015. /*
  1016. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1017. * got the command.
  1018. */
  1019. if (!sc->SCp.ptr) {
  1020. debug_scsi("sc never reached iscsi layer or it completed.\n");
  1021. return SUCCESS;
  1022. }
  1023. ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  1024. conn = ctask->conn;
  1025. session = conn->session;
  1026. conn->eh_abort_cnt++;
  1027. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  1028. mutex_lock(&conn->xmitmutex);
  1029. spin_lock_bh(&session->lock);
  1030. /*
  1031. * If we are not logged in or we have started a new session
  1032. * then let the host reset code handle this
  1033. */
  1034. if (session->state != ISCSI_STATE_LOGGED_IN ||
  1035. sc->SCp.phase != session->age)
  1036. goto failed;
  1037. /* ctask completed before time out */
  1038. if (!ctask->sc) {
  1039. spin_unlock_bh(&session->lock);
  1040. debug_scsi("sc completed while abort in progress\n");
  1041. goto success_rel_mutex;
  1042. }
  1043. /* what should we do here ? */
  1044. if (conn->ctask == ctask) {
  1045. printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
  1046. "Failing abort\n", sc, ctask->itt);
  1047. goto failed;
  1048. }
  1049. if (ctask->state == ISCSI_TASK_PENDING)
  1050. goto success_cleanup;
  1051. conn->tmabort_state = TMABORT_INITIAL;
  1052. spin_unlock_bh(&session->lock);
  1053. rc = iscsi_exec_abort_task(sc, ctask);
  1054. spin_lock_bh(&session->lock);
  1055. if (rc || sc->SCp.phase != session->age ||
  1056. session->state != ISCSI_STATE_LOGGED_IN)
  1057. goto failed;
  1058. iscsi_ctask_mtask_cleanup(ctask);
  1059. switch (conn->tmabort_state) {
  1060. case TMABORT_SUCCESS:
  1061. goto success_cleanup;
  1062. case TMABORT_NOT_FOUND:
  1063. if (!ctask->sc) {
  1064. /* ctask completed before tmf abort response */
  1065. spin_unlock_bh(&session->lock);
  1066. debug_scsi("sc completed while abort in progress\n");
  1067. goto success_rel_mutex;
  1068. }
  1069. /* fall through */
  1070. default:
  1071. /* timedout or failed */
  1072. spin_unlock_bh(&session->lock);
  1073. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1074. spin_lock_bh(&session->lock);
  1075. goto failed;
  1076. }
  1077. success_cleanup:
  1078. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1079. spin_unlock_bh(&session->lock);
  1080. /*
  1081. * clean up task if aborted. we have the xmitmutex so grab
  1082. * the recv lock as a writer
  1083. */
  1084. write_lock_bh(conn->recv_lock);
  1085. spin_lock(&session->lock);
  1086. fail_command(conn, ctask, DID_ABORT << 16);
  1087. spin_unlock(&session->lock);
  1088. write_unlock_bh(conn->recv_lock);
  1089. success_rel_mutex:
  1090. mutex_unlock(&conn->xmitmutex);
  1091. return SUCCESS;
  1092. failed:
  1093. spin_unlock_bh(&session->lock);
  1094. mutex_unlock(&conn->xmitmutex);
  1095. debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1096. return FAILED;
  1097. }
  1098. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1099. int
  1100. iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
  1101. {
  1102. int i;
  1103. *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1104. if (*items == NULL)
  1105. return -ENOMEM;
  1106. q->max = max;
  1107. q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1108. if (q->pool == NULL) {
  1109. kfree(*items);
  1110. return -ENOMEM;
  1111. }
  1112. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1113. GFP_KERNEL, NULL);
  1114. if (q->queue == ERR_PTR(-ENOMEM)) {
  1115. kfree(q->pool);
  1116. kfree(*items);
  1117. return -ENOMEM;
  1118. }
  1119. for (i = 0; i < max; i++) {
  1120. q->pool[i] = kmalloc(item_size, GFP_KERNEL);
  1121. if (q->pool[i] == NULL) {
  1122. int j;
  1123. for (j = 0; j < i; j++)
  1124. kfree(q->pool[j]);
  1125. kfifo_free(q->queue);
  1126. kfree(q->pool);
  1127. kfree(*items);
  1128. return -ENOMEM;
  1129. }
  1130. memset(q->pool[i], 0, item_size);
  1131. (*items)[i] = q->pool[i];
  1132. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1133. }
  1134. return 0;
  1135. }
  1136. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1137. void iscsi_pool_free(struct iscsi_queue *q, void **items)
  1138. {
  1139. int i;
  1140. for (i = 0; i < q->max; i++)
  1141. kfree(items[i]);
  1142. kfree(q->pool);
  1143. kfree(items);
  1144. }
  1145. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1146. /*
  1147. * iSCSI Session's hostdata organization:
  1148. *
  1149. * *------------------* <== hostdata_session(host->hostdata)
  1150. * | ptr to class sess|
  1151. * |------------------| <== iscsi_hostdata(host->hostdata)
  1152. * | iscsi_session |
  1153. * *------------------*
  1154. */
  1155. #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
  1156. _sz % sizeof(unsigned long))
  1157. #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
  1158. /**
  1159. * iscsi_session_setup - create iscsi cls session and host and session
  1160. * @scsit: scsi transport template
  1161. * @iscsit: iscsi transport template
  1162. * @initial_cmdsn: initial CmdSN
  1163. * @hostno: host no allocated
  1164. *
  1165. * This can be used by software iscsi_transports that allocate
  1166. * a session per scsi host.
  1167. **/
  1168. struct iscsi_cls_session *
  1169. iscsi_session_setup(struct iscsi_transport *iscsit,
  1170. struct scsi_transport_template *scsit,
  1171. int cmd_task_size, int mgmt_task_size,
  1172. uint32_t initial_cmdsn, uint32_t *hostno)
  1173. {
  1174. struct Scsi_Host *shost;
  1175. struct iscsi_session *session;
  1176. struct iscsi_cls_session *cls_session;
  1177. int cmd_i;
  1178. shost = scsi_host_alloc(iscsit->host_template,
  1179. hostdata_privsize(sizeof(*session)));
  1180. if (!shost)
  1181. return NULL;
  1182. shost->max_id = 1;
  1183. shost->max_channel = 0;
  1184. shost->max_lun = iscsit->max_lun;
  1185. shost->max_cmd_len = iscsit->max_cmd_len;
  1186. shost->transportt = scsit;
  1187. shost->transportt->create_work_queue = 1;
  1188. *hostno = shost->host_no;
  1189. session = iscsi_hostdata(shost->hostdata);
  1190. memset(session, 0, sizeof(struct iscsi_session));
  1191. session->host = shost;
  1192. session->state = ISCSI_STATE_FREE;
  1193. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1194. session->cmds_max = ISCSI_XMIT_CMDS_MAX;
  1195. session->cmdsn = initial_cmdsn;
  1196. session->exp_cmdsn = initial_cmdsn + 1;
  1197. session->max_cmdsn = initial_cmdsn + 1;
  1198. session->max_r2t = 1;
  1199. session->tt = iscsit;
  1200. /* initialize SCSI PDU commands pool */
  1201. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1202. (void***)&session->cmds,
  1203. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1204. goto cmdpool_alloc_fail;
  1205. /* pre-format cmds pool with ITT */
  1206. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1207. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1208. if (cmd_task_size)
  1209. ctask->dd_data = &ctask[1];
  1210. ctask->itt = cmd_i;
  1211. INIT_LIST_HEAD(&ctask->running);
  1212. }
  1213. spin_lock_init(&session->lock);
  1214. /* initialize immediate command pool */
  1215. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1216. (void***)&session->mgmt_cmds,
  1217. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1218. goto mgmtpool_alloc_fail;
  1219. /* pre-format immediate cmds pool with ITT */
  1220. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1221. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1222. if (mgmt_task_size)
  1223. mtask->dd_data = &mtask[1];
  1224. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1225. INIT_LIST_HEAD(&mtask->running);
  1226. }
  1227. if (scsi_add_host(shost, NULL))
  1228. goto add_host_fail;
  1229. if (!try_module_get(iscsit->owner))
  1230. goto cls_session_fail;
  1231. cls_session = iscsi_create_session(shost, iscsit, 0);
  1232. if (!cls_session)
  1233. goto module_put;
  1234. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1235. return cls_session;
  1236. module_put:
  1237. module_put(iscsit->owner);
  1238. cls_session_fail:
  1239. scsi_remove_host(shost);
  1240. add_host_fail:
  1241. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1242. mgmtpool_alloc_fail:
  1243. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1244. cmdpool_alloc_fail:
  1245. scsi_host_put(shost);
  1246. return NULL;
  1247. }
  1248. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1249. /**
  1250. * iscsi_session_teardown - destroy session, host, and cls_session
  1251. * shost: scsi host
  1252. *
  1253. * This can be used by software iscsi_transports that allocate
  1254. * a session per scsi host.
  1255. **/
  1256. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1257. {
  1258. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1259. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1260. struct module *owner = cls_session->transport->owner;
  1261. scsi_remove_host(shost);
  1262. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1263. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1264. kfree(session->targetname);
  1265. iscsi_destroy_session(cls_session);
  1266. scsi_host_put(shost);
  1267. module_put(owner);
  1268. }
  1269. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1270. /**
  1271. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1272. * @cls_session: iscsi_cls_session
  1273. * @conn_idx: cid
  1274. **/
  1275. struct iscsi_cls_conn *
  1276. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1277. {
  1278. struct iscsi_session *session = class_to_transport_session(cls_session);
  1279. struct iscsi_conn *conn;
  1280. struct iscsi_cls_conn *cls_conn;
  1281. char *data;
  1282. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1283. if (!cls_conn)
  1284. return NULL;
  1285. conn = cls_conn->dd_data;
  1286. memset(conn, 0, sizeof(*conn));
  1287. conn->session = session;
  1288. conn->cls_conn = cls_conn;
  1289. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1290. conn->id = conn_idx;
  1291. conn->exp_statsn = 0;
  1292. conn->tmabort_state = TMABORT_INITIAL;
  1293. INIT_LIST_HEAD(&conn->run_list);
  1294. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1295. INIT_LIST_HEAD(&conn->xmitqueue);
  1296. /* initialize general immediate & non-immediate PDU commands queue */
  1297. conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1298. GFP_KERNEL, NULL);
  1299. if (conn->immqueue == ERR_PTR(-ENOMEM))
  1300. goto immqueue_alloc_fail;
  1301. conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1302. GFP_KERNEL, NULL);
  1303. if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
  1304. goto mgmtqueue_alloc_fail;
  1305. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  1306. /* allocate login_mtask used for the login/text sequences */
  1307. spin_lock_bh(&session->lock);
  1308. if (!__kfifo_get(session->mgmtpool.queue,
  1309. (void*)&conn->login_mtask,
  1310. sizeof(void*))) {
  1311. spin_unlock_bh(&session->lock);
  1312. goto login_mtask_alloc_fail;
  1313. }
  1314. spin_unlock_bh(&session->lock);
  1315. data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
  1316. if (!data)
  1317. goto login_mtask_data_alloc_fail;
  1318. conn->login_mtask->data = conn->data = data;
  1319. init_timer(&conn->tmabort_timer);
  1320. mutex_init(&conn->xmitmutex);
  1321. init_waitqueue_head(&conn->ehwait);
  1322. return cls_conn;
  1323. login_mtask_data_alloc_fail:
  1324. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1325. sizeof(void*));
  1326. login_mtask_alloc_fail:
  1327. kfifo_free(conn->mgmtqueue);
  1328. mgmtqueue_alloc_fail:
  1329. kfifo_free(conn->immqueue);
  1330. immqueue_alloc_fail:
  1331. iscsi_destroy_conn(cls_conn);
  1332. return NULL;
  1333. }
  1334. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1335. /**
  1336. * iscsi_conn_teardown - teardown iscsi connection
  1337. * cls_conn: iscsi class connection
  1338. *
  1339. * TODO: we may need to make this into a two step process
  1340. * like scsi-mls remove + put host
  1341. */
  1342. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1343. {
  1344. struct iscsi_conn *conn = cls_conn->dd_data;
  1345. struct iscsi_session *session = conn->session;
  1346. unsigned long flags;
  1347. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1348. mutex_lock(&conn->xmitmutex);
  1349. spin_lock_bh(&session->lock);
  1350. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1351. if (session->leadconn == conn) {
  1352. /*
  1353. * leading connection? then give up on recovery.
  1354. */
  1355. session->state = ISCSI_STATE_TERMINATE;
  1356. wake_up(&conn->ehwait);
  1357. }
  1358. spin_unlock_bh(&session->lock);
  1359. mutex_unlock(&conn->xmitmutex);
  1360. /*
  1361. * Block until all in-progress commands for this connection
  1362. * time out or fail.
  1363. */
  1364. for (;;) {
  1365. spin_lock_irqsave(session->host->host_lock, flags);
  1366. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1367. spin_unlock_irqrestore(session->host->host_lock, flags);
  1368. break;
  1369. }
  1370. spin_unlock_irqrestore(session->host->host_lock, flags);
  1371. msleep_interruptible(500);
  1372. printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
  1373. "host_failed %d\n", session->host->host_busy,
  1374. session->host->host_failed);
  1375. /*
  1376. * force eh_abort() to unblock
  1377. */
  1378. wake_up(&conn->ehwait);
  1379. }
  1380. /* flush queued up work because we free the connection below */
  1381. scsi_flush_work(session->host);
  1382. spin_lock_bh(&session->lock);
  1383. kfree(conn->data);
  1384. kfree(conn->persistent_address);
  1385. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1386. sizeof(void*));
  1387. if (session->leadconn == conn) {
  1388. session->leadconn = NULL;
  1389. /* no connections exits.. reset sequencing */
  1390. session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
  1391. }
  1392. spin_unlock_bh(&session->lock);
  1393. kfifo_free(conn->immqueue);
  1394. kfifo_free(conn->mgmtqueue);
  1395. iscsi_destroy_conn(cls_conn);
  1396. }
  1397. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1398. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1399. {
  1400. struct iscsi_conn *conn = cls_conn->dd_data;
  1401. struct iscsi_session *session = conn->session;
  1402. if (!session) {
  1403. printk(KERN_ERR "iscsi: can't start unbound connection\n");
  1404. return -EPERM;
  1405. }
  1406. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1407. session->first_burst > session->max_burst) {
  1408. printk("iscsi: invalid burst lengths: "
  1409. "first_burst %d max_burst %d\n",
  1410. session->first_burst, session->max_burst);
  1411. return -EINVAL;
  1412. }
  1413. spin_lock_bh(&session->lock);
  1414. conn->c_stage = ISCSI_CONN_STARTED;
  1415. session->state = ISCSI_STATE_LOGGED_IN;
  1416. switch(conn->stop_stage) {
  1417. case STOP_CONN_RECOVER:
  1418. /*
  1419. * unblock eh_abort() if it is blocked. re-try all
  1420. * commands after successful recovery
  1421. */
  1422. conn->stop_stage = 0;
  1423. conn->tmabort_state = TMABORT_INITIAL;
  1424. session->age++;
  1425. spin_unlock_bh(&session->lock);
  1426. iscsi_unblock_session(session_to_cls(session));
  1427. wake_up(&conn->ehwait);
  1428. return 0;
  1429. case STOP_CONN_TERM:
  1430. conn->stop_stage = 0;
  1431. break;
  1432. default:
  1433. break;
  1434. }
  1435. spin_unlock_bh(&session->lock);
  1436. return 0;
  1437. }
  1438. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1439. static void
  1440. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1441. {
  1442. struct iscsi_mgmt_task *mtask, *tmp;
  1443. /* handle pending */
  1444. while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
  1445. __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
  1446. if (mtask == conn->login_mtask)
  1447. continue;
  1448. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1449. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1450. sizeof(void*));
  1451. }
  1452. /* handle running */
  1453. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1454. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1455. list_del(&mtask->running);
  1456. if (mtask == conn->login_mtask)
  1457. continue;
  1458. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1459. sizeof(void*));
  1460. }
  1461. conn->mtask = NULL;
  1462. }
  1463. /* Fail commands. Mutex and session lock held and recv side suspended */
  1464. static void fail_all_commands(struct iscsi_conn *conn)
  1465. {
  1466. struct iscsi_cmd_task *ctask, *tmp;
  1467. /* flush pending */
  1468. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1469. debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
  1470. ctask->itt);
  1471. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1472. }
  1473. /* fail all other running */
  1474. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1475. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1476. ctask->sc, ctask->itt);
  1477. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1478. }
  1479. conn->ctask = NULL;
  1480. }
  1481. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1482. struct iscsi_conn *conn, int flag)
  1483. {
  1484. int old_stop_stage;
  1485. spin_lock_bh(&session->lock);
  1486. if (conn->stop_stage == STOP_CONN_TERM) {
  1487. spin_unlock_bh(&session->lock);
  1488. return;
  1489. }
  1490. /*
  1491. * When this is called for the in_login state, we only want to clean
  1492. * up the login task and connection. We do not need to block and set
  1493. * the recovery state again
  1494. */
  1495. if (flag == STOP_CONN_TERM)
  1496. session->state = ISCSI_STATE_TERMINATE;
  1497. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1498. session->state = ISCSI_STATE_IN_RECOVERY;
  1499. old_stop_stage = conn->stop_stage;
  1500. conn->stop_stage = flag;
  1501. conn->c_stage = ISCSI_CONN_STOPPED;
  1502. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1503. spin_unlock_bh(&session->lock);
  1504. write_lock_bh(conn->recv_lock);
  1505. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1506. write_unlock_bh(conn->recv_lock);
  1507. mutex_lock(&conn->xmitmutex);
  1508. /*
  1509. * for connection level recovery we should not calculate
  1510. * header digest. conn->hdr_size used for optimization
  1511. * in hdr_extract() and will be re-negotiated at
  1512. * set_param() time.
  1513. */
  1514. if (flag == STOP_CONN_RECOVER) {
  1515. conn->hdrdgst_en = 0;
  1516. conn->datadgst_en = 0;
  1517. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1518. old_stop_stage != STOP_CONN_RECOVER) {
  1519. debug_scsi("blocking session\n");
  1520. iscsi_block_session(session_to_cls(session));
  1521. }
  1522. }
  1523. /*
  1524. * flush queues.
  1525. */
  1526. spin_lock_bh(&session->lock);
  1527. fail_all_commands(conn);
  1528. flush_control_queues(session, conn);
  1529. spin_unlock_bh(&session->lock);
  1530. mutex_unlock(&conn->xmitmutex);
  1531. }
  1532. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1533. {
  1534. struct iscsi_conn *conn = cls_conn->dd_data;
  1535. struct iscsi_session *session = conn->session;
  1536. switch (flag) {
  1537. case STOP_CONN_RECOVER:
  1538. case STOP_CONN_TERM:
  1539. iscsi_start_session_recovery(session, conn, flag);
  1540. break;
  1541. default:
  1542. printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
  1543. }
  1544. }
  1545. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1546. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1547. struct iscsi_cls_conn *cls_conn, int is_leading)
  1548. {
  1549. struct iscsi_session *session = class_to_transport_session(cls_session);
  1550. struct iscsi_conn *conn = cls_conn->dd_data;
  1551. spin_lock_bh(&session->lock);
  1552. if (is_leading)
  1553. session->leadconn = conn;
  1554. spin_unlock_bh(&session->lock);
  1555. /*
  1556. * Unblock xmitworker(), Login Phase will pass through.
  1557. */
  1558. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1559. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1560. return 0;
  1561. }
  1562. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  1563. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  1564. enum iscsi_param param, char *buf, int buflen)
  1565. {
  1566. struct iscsi_conn *conn = cls_conn->dd_data;
  1567. struct iscsi_session *session = conn->session;
  1568. uint32_t value;
  1569. switch(param) {
  1570. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1571. sscanf(buf, "%d", &conn->max_recv_dlength);
  1572. break;
  1573. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1574. sscanf(buf, "%d", &conn->max_xmit_dlength);
  1575. break;
  1576. case ISCSI_PARAM_HDRDGST_EN:
  1577. sscanf(buf, "%d", &conn->hdrdgst_en);
  1578. break;
  1579. case ISCSI_PARAM_DATADGST_EN:
  1580. sscanf(buf, "%d", &conn->datadgst_en);
  1581. break;
  1582. case ISCSI_PARAM_INITIAL_R2T_EN:
  1583. sscanf(buf, "%d", &session->initial_r2t_en);
  1584. break;
  1585. case ISCSI_PARAM_MAX_R2T:
  1586. sscanf(buf, "%d", &session->max_r2t);
  1587. break;
  1588. case ISCSI_PARAM_IMM_DATA_EN:
  1589. sscanf(buf, "%d", &session->imm_data_en);
  1590. break;
  1591. case ISCSI_PARAM_FIRST_BURST:
  1592. sscanf(buf, "%d", &session->first_burst);
  1593. break;
  1594. case ISCSI_PARAM_MAX_BURST:
  1595. sscanf(buf, "%d", &session->max_burst);
  1596. break;
  1597. case ISCSI_PARAM_PDU_INORDER_EN:
  1598. sscanf(buf, "%d", &session->pdu_inorder_en);
  1599. break;
  1600. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1601. sscanf(buf, "%d", &session->dataseq_inorder_en);
  1602. break;
  1603. case ISCSI_PARAM_ERL:
  1604. sscanf(buf, "%d", &session->erl);
  1605. break;
  1606. case ISCSI_PARAM_IFMARKER_EN:
  1607. sscanf(buf, "%d", &value);
  1608. BUG_ON(value);
  1609. break;
  1610. case ISCSI_PARAM_OFMARKER_EN:
  1611. sscanf(buf, "%d", &value);
  1612. BUG_ON(value);
  1613. break;
  1614. case ISCSI_PARAM_EXP_STATSN:
  1615. sscanf(buf, "%u", &conn->exp_statsn);
  1616. break;
  1617. case ISCSI_PARAM_TARGET_NAME:
  1618. /* this should not change between logins */
  1619. if (session->targetname)
  1620. break;
  1621. session->targetname = kstrdup(buf, GFP_KERNEL);
  1622. if (!session->targetname)
  1623. return -ENOMEM;
  1624. break;
  1625. case ISCSI_PARAM_TPGT:
  1626. sscanf(buf, "%d", &session->tpgt);
  1627. break;
  1628. case ISCSI_PARAM_PERSISTENT_PORT:
  1629. sscanf(buf, "%d", &conn->persistent_port);
  1630. break;
  1631. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1632. /*
  1633. * this is the address returned in discovery so it should
  1634. * not change between logins.
  1635. */
  1636. if (conn->persistent_address)
  1637. break;
  1638. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  1639. if (!conn->persistent_address)
  1640. return -ENOMEM;
  1641. break;
  1642. default:
  1643. return -ENOSYS;
  1644. }
  1645. return 0;
  1646. }
  1647. EXPORT_SYMBOL_GPL(iscsi_set_param);
  1648. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  1649. enum iscsi_param param, char *buf)
  1650. {
  1651. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1652. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1653. int len;
  1654. switch(param) {
  1655. case ISCSI_PARAM_INITIAL_R2T_EN:
  1656. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  1657. break;
  1658. case ISCSI_PARAM_MAX_R2T:
  1659. len = sprintf(buf, "%hu\n", session->max_r2t);
  1660. break;
  1661. case ISCSI_PARAM_IMM_DATA_EN:
  1662. len = sprintf(buf, "%d\n", session->imm_data_en);
  1663. break;
  1664. case ISCSI_PARAM_FIRST_BURST:
  1665. len = sprintf(buf, "%u\n", session->first_burst);
  1666. break;
  1667. case ISCSI_PARAM_MAX_BURST:
  1668. len = sprintf(buf, "%u\n", session->max_burst);
  1669. break;
  1670. case ISCSI_PARAM_PDU_INORDER_EN:
  1671. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  1672. break;
  1673. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1674. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  1675. break;
  1676. case ISCSI_PARAM_ERL:
  1677. len = sprintf(buf, "%d\n", session->erl);
  1678. break;
  1679. case ISCSI_PARAM_TARGET_NAME:
  1680. len = sprintf(buf, "%s\n", session->targetname);
  1681. break;
  1682. case ISCSI_PARAM_TPGT:
  1683. len = sprintf(buf, "%d\n", session->tpgt);
  1684. break;
  1685. default:
  1686. return -ENOSYS;
  1687. }
  1688. return len;
  1689. }
  1690. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  1691. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1692. enum iscsi_param param, char *buf)
  1693. {
  1694. struct iscsi_conn *conn = cls_conn->dd_data;
  1695. int len;
  1696. switch(param) {
  1697. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1698. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  1699. break;
  1700. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1701. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  1702. break;
  1703. case ISCSI_PARAM_HDRDGST_EN:
  1704. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  1705. break;
  1706. case ISCSI_PARAM_DATADGST_EN:
  1707. len = sprintf(buf, "%d\n", conn->datadgst_en);
  1708. break;
  1709. case ISCSI_PARAM_IFMARKER_EN:
  1710. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  1711. break;
  1712. case ISCSI_PARAM_OFMARKER_EN:
  1713. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  1714. break;
  1715. case ISCSI_PARAM_EXP_STATSN:
  1716. len = sprintf(buf, "%u\n", conn->exp_statsn);
  1717. break;
  1718. case ISCSI_PARAM_PERSISTENT_PORT:
  1719. len = sprintf(buf, "%d\n", conn->persistent_port);
  1720. break;
  1721. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1722. len = sprintf(buf, "%s\n", conn->persistent_address);
  1723. break;
  1724. default:
  1725. return -ENOSYS;
  1726. }
  1727. return len;
  1728. }
  1729. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  1730. MODULE_AUTHOR("Mike Christie");
  1731. MODULE_DESCRIPTION("iSCSI library functions");
  1732. MODULE_LICENSE("GPL");