libiscsi.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475
  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/kfifo.h>
  26. #include <linux/delay.h>
  27. #include <linux/log2.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. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  48. #define SNA32_CHECK 2147483648UL
  49. static int iscsi_sna_lt(u32 n1, u32 n2)
  50. {
  51. return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  52. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  53. }
  54. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  55. static int iscsi_sna_lte(u32 n1, u32 n2)
  56. {
  57. return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  58. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  59. }
  60. void
  61. iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  62. {
  63. uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
  64. uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
  65. /*
  66. * standard specifies this check for when to update expected and
  67. * max sequence numbers
  68. */
  69. if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
  70. return;
  71. if (exp_cmdsn != session->exp_cmdsn &&
  72. !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
  73. session->exp_cmdsn = exp_cmdsn;
  74. if (max_cmdsn != session->max_cmdsn &&
  75. !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
  76. session->max_cmdsn = max_cmdsn;
  77. /*
  78. * if the window closed with IO queued, then kick the
  79. * xmit thread
  80. */
  81. if (!list_empty(&session->leadconn->xmitqueue) ||
  82. !list_empty(&session->leadconn->mgmtqueue))
  83. scsi_queue_work(session->host,
  84. &session->leadconn->xmitwork);
  85. }
  86. }
  87. EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
  88. void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
  89. struct iscsi_data *hdr)
  90. {
  91. struct iscsi_conn *conn = ctask->conn;
  92. memset(hdr, 0, sizeof(struct iscsi_data));
  93. hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  94. hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
  95. ctask->unsol_datasn++;
  96. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  97. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  98. hdr->itt = ctask->hdr->itt;
  99. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  100. hdr->offset = cpu_to_be32(ctask->unsol_offset);
  101. if (ctask->unsol_count > conn->max_xmit_dlength) {
  102. hton24(hdr->dlength, conn->max_xmit_dlength);
  103. ctask->data_count = conn->max_xmit_dlength;
  104. ctask->unsol_offset += ctask->data_count;
  105. hdr->flags = 0;
  106. } else {
  107. hton24(hdr->dlength, ctask->unsol_count);
  108. ctask->data_count = ctask->unsol_count;
  109. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  110. }
  111. }
  112. EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
  113. static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
  114. {
  115. unsigned exp_len = ctask->hdr_len + len;
  116. if (exp_len > ctask->hdr_max) {
  117. WARN_ON(1);
  118. return -EINVAL;
  119. }
  120. WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
  121. ctask->hdr_len = exp_len;
  122. return 0;
  123. }
  124. /**
  125. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  126. * @ctask: iscsi cmd task
  127. *
  128. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  129. * fields like dlength or final based on how much data it sends
  130. */
  131. static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
  132. {
  133. struct iscsi_conn *conn = ctask->conn;
  134. struct iscsi_session *session = conn->session;
  135. struct iscsi_cmd *hdr = ctask->hdr;
  136. struct scsi_cmnd *sc = ctask->sc;
  137. unsigned hdrlength;
  138. int rc;
  139. ctask->hdr_len = 0;
  140. rc = iscsi_add_hdr(ctask, sizeof(*hdr));
  141. if (rc)
  142. return rc;
  143. hdr->opcode = ISCSI_OP_SCSI_CMD;
  144. hdr->flags = ISCSI_ATTR_SIMPLE;
  145. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  146. hdr->itt = build_itt(ctask->itt, conn->id, session->age);
  147. hdr->data_length = cpu_to_be32(scsi_bufflen(sc));
  148. hdr->cmdsn = cpu_to_be32(session->cmdsn);
  149. session->cmdsn++;
  150. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  151. memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
  152. if (sc->cmd_len < MAX_COMMAND_SIZE)
  153. memset(&hdr->cdb[sc->cmd_len], 0,
  154. MAX_COMMAND_SIZE - sc->cmd_len);
  155. ctask->imm_count = 0;
  156. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  157. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  158. /*
  159. * Write counters:
  160. *
  161. * imm_count bytes to be sent right after
  162. * SCSI PDU Header
  163. *
  164. * unsol_count bytes(as Data-Out) to be sent
  165. * without R2T ack right after
  166. * immediate data
  167. *
  168. * r2t_data_count bytes to be sent via R2T ack's
  169. *
  170. * pad_count bytes to be sent as zero-padding
  171. */
  172. ctask->unsol_count = 0;
  173. ctask->unsol_offset = 0;
  174. ctask->unsol_datasn = 0;
  175. if (session->imm_data_en) {
  176. if (scsi_bufflen(sc) >= session->first_burst)
  177. ctask->imm_count = min(session->first_burst,
  178. conn->max_xmit_dlength);
  179. else
  180. ctask->imm_count = min(scsi_bufflen(sc),
  181. conn->max_xmit_dlength);
  182. hton24(hdr->dlength, ctask->imm_count);
  183. } else
  184. zero_data(hdr->dlength);
  185. if (!session->initial_r2t_en) {
  186. ctask->unsol_count = min((session->first_burst),
  187. (scsi_bufflen(sc))) - ctask->imm_count;
  188. ctask->unsol_offset = ctask->imm_count;
  189. }
  190. if (!ctask->unsol_count)
  191. /* No unsolicit Data-Out's */
  192. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  193. } else {
  194. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  195. zero_data(hdr->dlength);
  196. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  197. hdr->flags |= ISCSI_FLAG_CMD_READ;
  198. }
  199. /* calculate size of additional header segments (AHSs) */
  200. hdrlength = ctask->hdr_len - sizeof(*hdr);
  201. WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
  202. hdrlength /= ISCSI_PAD_LEN;
  203. WARN_ON(hdrlength >= 256);
  204. hdr->hlength = hdrlength & 0xFF;
  205. if (conn->session->tt->init_cmd_task(conn->ctask))
  206. return EIO;
  207. conn->scsicmd_pdus_cnt++;
  208. debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
  209. "cmdsn %d win %d]\n",
  210. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  211. conn->id, sc, sc->cmnd[0], ctask->itt, scsi_bufflen(sc),
  212. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  213. return 0;
  214. }
  215. /**
  216. * iscsi_complete_command - return command back to scsi-ml
  217. * @ctask: iscsi cmd task
  218. *
  219. * Must be called with session lock.
  220. * This function returns the scsi command to scsi-ml and returns
  221. * the cmd task to the pool of available cmd tasks.
  222. */
  223. static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
  224. {
  225. struct iscsi_conn *conn = ctask->conn;
  226. struct iscsi_session *session = conn->session;
  227. struct scsi_cmnd *sc = ctask->sc;
  228. ctask->state = ISCSI_TASK_COMPLETED;
  229. ctask->sc = NULL;
  230. /* SCSI eh reuses commands to verify us */
  231. sc->SCp.ptr = NULL;
  232. if (conn->ctask == ctask)
  233. conn->ctask = NULL;
  234. list_del_init(&ctask->running);
  235. __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  236. sc->scsi_done(sc);
  237. }
  238. static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
  239. {
  240. atomic_inc(&ctask->refcount);
  241. }
  242. static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
  243. {
  244. if (atomic_dec_and_test(&ctask->refcount))
  245. iscsi_complete_command(ctask);
  246. }
  247. /*
  248. * session lock must be held
  249. */
  250. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  251. int err)
  252. {
  253. struct scsi_cmnd *sc;
  254. sc = ctask->sc;
  255. if (!sc)
  256. return;
  257. if (ctask->state == ISCSI_TASK_PENDING)
  258. /*
  259. * cmd never made it to the xmit thread, so we should not count
  260. * the cmd in the sequencing
  261. */
  262. conn->session->queued_cmdsn--;
  263. else
  264. conn->session->tt->cleanup_cmd_task(conn, ctask);
  265. sc->result = err;
  266. scsi_set_resid(sc, scsi_bufflen(sc));
  267. if (conn->ctask == ctask)
  268. conn->ctask = NULL;
  269. /* release ref from queuecommand */
  270. __iscsi_put_ctask(ctask);
  271. }
  272. /**
  273. * iscsi_free_mgmt_task - return mgmt task back to pool
  274. * @conn: iscsi connection
  275. * @mtask: mtask
  276. *
  277. * Must be called with session lock.
  278. */
  279. void iscsi_free_mgmt_task(struct iscsi_conn *conn,
  280. struct iscsi_mgmt_task *mtask)
  281. {
  282. list_del_init(&mtask->running);
  283. if (conn->login_mtask == mtask)
  284. return;
  285. if (conn->ping_mtask == mtask)
  286. conn->ping_mtask = NULL;
  287. __kfifo_put(conn->session->mgmtpool.queue,
  288. (void*)&mtask, sizeof(void*));
  289. }
  290. EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task);
  291. static struct iscsi_mgmt_task *
  292. __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  293. char *data, uint32_t data_size)
  294. {
  295. struct iscsi_session *session = conn->session;
  296. struct iscsi_mgmt_task *mtask;
  297. if (session->state == ISCSI_STATE_TERMINATE)
  298. return NULL;
  299. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  300. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  301. /*
  302. * Login and Text are sent serially, in
  303. * request-followed-by-response sequence.
  304. * Same mtask can be used. Same ITT must be used.
  305. * Note that login_mtask is preallocated at conn_create().
  306. */
  307. mtask = conn->login_mtask;
  308. else {
  309. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  310. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  311. if (!__kfifo_get(session->mgmtpool.queue,
  312. (void*)&mtask, sizeof(void*)))
  313. return NULL;
  314. }
  315. if (data_size) {
  316. memcpy(mtask->data, data, data_size);
  317. mtask->data_count = data_size;
  318. } else
  319. mtask->data_count = 0;
  320. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  321. INIT_LIST_HEAD(&mtask->running);
  322. list_add_tail(&mtask->running, &conn->mgmtqueue);
  323. return mtask;
  324. }
  325. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  326. char *data, uint32_t data_size)
  327. {
  328. struct iscsi_conn *conn = cls_conn->dd_data;
  329. struct iscsi_session *session = conn->session;
  330. int err = 0;
  331. spin_lock_bh(&session->lock);
  332. if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
  333. err = -EPERM;
  334. spin_unlock_bh(&session->lock);
  335. scsi_queue_work(session->host, &conn->xmitwork);
  336. return err;
  337. }
  338. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  339. /**
  340. * iscsi_cmd_rsp - SCSI Command Response processing
  341. * @conn: iscsi connection
  342. * @hdr: iscsi header
  343. * @ctask: scsi command task
  344. * @data: cmd data buffer
  345. * @datalen: len of buffer
  346. *
  347. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  348. * then completes the command and task.
  349. **/
  350. static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  351. struct iscsi_cmd_task *ctask, char *data,
  352. int datalen)
  353. {
  354. struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
  355. struct iscsi_session *session = conn->session;
  356. struct scsi_cmnd *sc = ctask->sc;
  357. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  358. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  359. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  360. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  361. sc->result = DID_ERROR << 16;
  362. goto out;
  363. }
  364. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  365. uint16_t senselen;
  366. if (datalen < 2) {
  367. invalid_datalen:
  368. printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
  369. "invalid data buffer size of %d\n", datalen);
  370. sc->result = DID_BAD_TARGET << 16;
  371. goto out;
  372. }
  373. senselen = be16_to_cpu(get_unaligned((__be16 *) data));
  374. if (datalen < senselen)
  375. goto invalid_datalen;
  376. memcpy(sc->sense_buffer, data + 2,
  377. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  378. debug_scsi("copied %d bytes of sense\n",
  379. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  380. }
  381. if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
  382. ISCSI_FLAG_CMD_OVERFLOW)) {
  383. int res_count = be32_to_cpu(rhdr->residual_count);
  384. if (res_count > 0 &&
  385. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  386. res_count <= scsi_bufflen(sc)))
  387. scsi_set_resid(sc, res_count);
  388. else
  389. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  390. } else if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
  391. ISCSI_FLAG_CMD_BIDI_OVERFLOW))
  392. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  393. out:
  394. debug_scsi("done [sc %lx res %d itt 0x%x]\n",
  395. (long)sc, sc->result, ctask->itt);
  396. conn->scsirsp_pdus_cnt++;
  397. __iscsi_put_ctask(ctask);
  398. }
  399. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  400. {
  401. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  402. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  403. conn->tmfrsp_pdus_cnt++;
  404. if (conn->tmf_state != TMF_QUEUED)
  405. return;
  406. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  407. conn->tmf_state = TMF_SUCCESS;
  408. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  409. conn->tmf_state = TMF_NOT_FOUND;
  410. else
  411. conn->tmf_state = TMF_FAILED;
  412. wake_up(&conn->ehwait);
  413. }
  414. static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
  415. {
  416. struct iscsi_nopout hdr;
  417. struct iscsi_mgmt_task *mtask;
  418. if (!rhdr && conn->ping_mtask)
  419. return;
  420. memset(&hdr, 0, sizeof(struct iscsi_nopout));
  421. hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
  422. hdr.flags = ISCSI_FLAG_CMD_FINAL;
  423. if (rhdr) {
  424. memcpy(hdr.lun, rhdr->lun, 8);
  425. hdr.ttt = rhdr->ttt;
  426. hdr.itt = RESERVED_ITT;
  427. } else
  428. hdr.ttt = RESERVED_ITT;
  429. mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
  430. if (!mtask) {
  431. printk(KERN_ERR "Could not send nopout\n");
  432. return;
  433. }
  434. /* only track our nops */
  435. if (!rhdr) {
  436. conn->ping_mtask = mtask;
  437. conn->last_ping = jiffies;
  438. }
  439. scsi_queue_work(conn->session->host, &conn->xmitwork);
  440. }
  441. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  442. char *data, int datalen)
  443. {
  444. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  445. struct iscsi_hdr rejected_pdu;
  446. uint32_t itt;
  447. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  448. if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
  449. if (ntoh24(reject->dlength) > datalen)
  450. return ISCSI_ERR_PROTO;
  451. if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
  452. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  453. itt = get_itt(rejected_pdu.itt);
  454. printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
  455. "due to DataDigest error.\n", itt,
  456. rejected_pdu.opcode);
  457. }
  458. }
  459. return 0;
  460. }
  461. /**
  462. * __iscsi_complete_pdu - complete pdu
  463. * @conn: iscsi conn
  464. * @hdr: iscsi header
  465. * @data: data buffer
  466. * @datalen: len of data buffer
  467. *
  468. * Completes pdu processing by freeing any resources allocated at
  469. * queuecommand or send generic. session lock must be held and verify
  470. * itt must have been called.
  471. */
  472. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  473. char *data, int datalen)
  474. {
  475. struct iscsi_session *session = conn->session;
  476. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  477. struct iscsi_cmd_task *ctask;
  478. struct iscsi_mgmt_task *mtask;
  479. uint32_t itt;
  480. conn->last_recv = jiffies;
  481. if (hdr->itt != RESERVED_ITT)
  482. itt = get_itt(hdr->itt);
  483. else
  484. itt = ~0U;
  485. if (itt < session->cmds_max) {
  486. ctask = session->cmds[itt];
  487. debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  488. opcode, conn->id, ctask->itt, datalen);
  489. switch(opcode) {
  490. case ISCSI_OP_SCSI_CMD_RSP:
  491. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  492. iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
  493. datalen);
  494. break;
  495. case ISCSI_OP_SCSI_DATA_IN:
  496. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  497. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  498. conn->scsirsp_pdus_cnt++;
  499. __iscsi_put_ctask(ctask);
  500. }
  501. break;
  502. case ISCSI_OP_R2T:
  503. /* LLD handles this for now */
  504. break;
  505. default:
  506. rc = ISCSI_ERR_BAD_OPCODE;
  507. break;
  508. }
  509. } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
  510. itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
  511. mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
  512. debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  513. opcode, conn->id, mtask->itt, datalen);
  514. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  515. switch(opcode) {
  516. case ISCSI_OP_LOGOUT_RSP:
  517. if (datalen) {
  518. rc = ISCSI_ERR_PROTO;
  519. break;
  520. }
  521. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  522. /* fall through */
  523. case ISCSI_OP_LOGIN_RSP:
  524. case ISCSI_OP_TEXT_RSP:
  525. /*
  526. * login related PDU's exp_statsn is handled in
  527. * userspace
  528. */
  529. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  530. rc = ISCSI_ERR_CONN_FAILED;
  531. iscsi_free_mgmt_task(conn, mtask);
  532. break;
  533. case ISCSI_OP_SCSI_TMFUNC_RSP:
  534. if (datalen) {
  535. rc = ISCSI_ERR_PROTO;
  536. break;
  537. }
  538. iscsi_tmf_rsp(conn, hdr);
  539. iscsi_free_mgmt_task(conn, mtask);
  540. break;
  541. case ISCSI_OP_NOOP_IN:
  542. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) ||
  543. datalen) {
  544. rc = ISCSI_ERR_PROTO;
  545. break;
  546. }
  547. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  548. if (conn->ping_mtask != mtask) {
  549. /*
  550. * If this is not in response to one of our
  551. * nops then it must be from userspace.
  552. */
  553. if (iscsi_recv_pdu(conn->cls_conn, hdr, data,
  554. datalen))
  555. rc = ISCSI_ERR_CONN_FAILED;
  556. }
  557. iscsi_free_mgmt_task(conn, mtask);
  558. break;
  559. default:
  560. rc = ISCSI_ERR_BAD_OPCODE;
  561. break;
  562. }
  563. } else if (itt == ~0U) {
  564. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  565. switch(opcode) {
  566. case ISCSI_OP_NOOP_IN:
  567. if (datalen) {
  568. rc = ISCSI_ERR_PROTO;
  569. break;
  570. }
  571. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  572. break;
  573. iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
  574. break;
  575. case ISCSI_OP_REJECT:
  576. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  577. break;
  578. case ISCSI_OP_ASYNC_EVENT:
  579. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  580. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  581. rc = ISCSI_ERR_CONN_FAILED;
  582. break;
  583. default:
  584. rc = ISCSI_ERR_BAD_OPCODE;
  585. break;
  586. }
  587. } else
  588. rc = ISCSI_ERR_BAD_ITT;
  589. return rc;
  590. }
  591. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  592. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  593. char *data, int datalen)
  594. {
  595. int rc;
  596. spin_lock(&conn->session->lock);
  597. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  598. spin_unlock(&conn->session->lock);
  599. return rc;
  600. }
  601. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  602. /* verify itt (itt encoding: age+cid+itt) */
  603. int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  604. uint32_t *ret_itt)
  605. {
  606. struct iscsi_session *session = conn->session;
  607. struct iscsi_cmd_task *ctask;
  608. uint32_t itt;
  609. if (hdr->itt != RESERVED_ITT) {
  610. if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
  611. (session->age << ISCSI_AGE_SHIFT)) {
  612. printk(KERN_ERR "iscsi: received itt %x expected "
  613. "session age (%x)\n", (__force u32)hdr->itt,
  614. session->age & ISCSI_AGE_MASK);
  615. return ISCSI_ERR_BAD_ITT;
  616. }
  617. if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
  618. (conn->id << ISCSI_CID_SHIFT)) {
  619. printk(KERN_ERR "iscsi: received itt %x, expected "
  620. "CID (%x)\n", (__force u32)hdr->itt, conn->id);
  621. return ISCSI_ERR_BAD_ITT;
  622. }
  623. itt = get_itt(hdr->itt);
  624. } else
  625. itt = ~0U;
  626. if (itt < session->cmds_max) {
  627. ctask = session->cmds[itt];
  628. if (!ctask->sc) {
  629. printk(KERN_INFO "iscsi: dropping ctask with "
  630. "itt 0x%x\n", ctask->itt);
  631. /* force drop */
  632. return ISCSI_ERR_NO_SCSI_CMD;
  633. }
  634. if (ctask->sc->SCp.phase != session->age) {
  635. printk(KERN_ERR "iscsi: ctask's session age %d, "
  636. "expected %d\n", ctask->sc->SCp.phase,
  637. session->age);
  638. return ISCSI_ERR_SESSION_FAILED;
  639. }
  640. }
  641. *ret_itt = itt;
  642. return 0;
  643. }
  644. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  645. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  646. {
  647. struct iscsi_session *session = conn->session;
  648. unsigned long flags;
  649. spin_lock_irqsave(&session->lock, flags);
  650. if (session->state == ISCSI_STATE_FAILED) {
  651. spin_unlock_irqrestore(&session->lock, flags);
  652. return;
  653. }
  654. if (conn->stop_stage == 0)
  655. session->state = ISCSI_STATE_FAILED;
  656. spin_unlock_irqrestore(&session->lock, flags);
  657. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  658. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  659. iscsi_conn_error(conn->cls_conn, err);
  660. }
  661. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  662. static void iscsi_prep_mtask(struct iscsi_conn *conn,
  663. struct iscsi_mgmt_task *mtask)
  664. {
  665. struct iscsi_session *session = conn->session;
  666. struct iscsi_hdr *hdr = mtask->hdr;
  667. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  668. if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
  669. hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  670. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  671. /*
  672. * pre-format CmdSN for outgoing PDU.
  673. */
  674. nop->cmdsn = cpu_to_be32(session->cmdsn);
  675. if (hdr->itt != RESERVED_ITT) {
  676. hdr->itt = build_itt(mtask->itt, conn->id, session->age);
  677. /*
  678. * TODO: We always use immediate, so we never hit this.
  679. * If we start to send tmfs or nops as non-immediate then
  680. * we should start checking the cmdsn numbers for mgmt tasks.
  681. */
  682. if (conn->c_stage == ISCSI_CONN_STARTED &&
  683. !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
  684. session->queued_cmdsn++;
  685. session->cmdsn++;
  686. }
  687. }
  688. if (session->tt->init_mgmt_task)
  689. session->tt->init_mgmt_task(conn, mtask);
  690. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  691. hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
  692. mtask->data_count);
  693. }
  694. static int iscsi_xmit_mtask(struct iscsi_conn *conn)
  695. {
  696. struct iscsi_hdr *hdr = conn->mtask->hdr;
  697. int rc;
  698. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  699. conn->session->state = ISCSI_STATE_LOGGING_OUT;
  700. spin_unlock_bh(&conn->session->lock);
  701. rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
  702. spin_lock_bh(&conn->session->lock);
  703. if (rc)
  704. return rc;
  705. /* done with this in-progress mtask */
  706. conn->mtask = NULL;
  707. return 0;
  708. }
  709. static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
  710. {
  711. struct iscsi_session *session = conn->session;
  712. /*
  713. * Check for iSCSI window and take care of CmdSN wrap-around
  714. */
  715. if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
  716. debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
  717. "CmdSN %u/%u\n", session->exp_cmdsn,
  718. session->max_cmdsn, session->cmdsn,
  719. session->queued_cmdsn);
  720. return -ENOSPC;
  721. }
  722. return 0;
  723. }
  724. static int iscsi_xmit_ctask(struct iscsi_conn *conn)
  725. {
  726. struct iscsi_cmd_task *ctask = conn->ctask;
  727. int rc;
  728. __iscsi_get_ctask(ctask);
  729. spin_unlock_bh(&conn->session->lock);
  730. rc = conn->session->tt->xmit_cmd_task(conn, ctask);
  731. spin_lock_bh(&conn->session->lock);
  732. __iscsi_put_ctask(ctask);
  733. if (!rc)
  734. /* done with this ctask */
  735. conn->ctask = NULL;
  736. return rc;
  737. }
  738. /**
  739. * iscsi_requeue_ctask - requeue ctask to run from session workqueue
  740. * @ctask: ctask to requeue
  741. *
  742. * LLDs that need to run a ctask from the session workqueue should call
  743. * this. The session lock must be held.
  744. */
  745. void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
  746. {
  747. struct iscsi_conn *conn = ctask->conn;
  748. list_move_tail(&ctask->running, &conn->requeue);
  749. scsi_queue_work(conn->session->host, &conn->xmitwork);
  750. }
  751. EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
  752. /**
  753. * iscsi_data_xmit - xmit any command into the scheduled connection
  754. * @conn: iscsi connection
  755. *
  756. * Notes:
  757. * The function can return -EAGAIN in which case the caller must
  758. * re-schedule it again later or recover. '0' return code means
  759. * successful xmit.
  760. **/
  761. static int iscsi_data_xmit(struct iscsi_conn *conn)
  762. {
  763. int rc = 0;
  764. spin_lock_bh(&conn->session->lock);
  765. if (unlikely(conn->suspend_tx)) {
  766. debug_scsi("conn %d Tx suspended!\n", conn->id);
  767. spin_unlock_bh(&conn->session->lock);
  768. return -ENODATA;
  769. }
  770. if (conn->ctask) {
  771. rc = iscsi_xmit_ctask(conn);
  772. if (rc)
  773. goto again;
  774. }
  775. if (conn->mtask) {
  776. rc = iscsi_xmit_mtask(conn);
  777. if (rc)
  778. goto again;
  779. }
  780. /*
  781. * process mgmt pdus like nops before commands since we should
  782. * only have one nop-out as a ping from us and targets should not
  783. * overflow us with nop-ins
  784. */
  785. check_mgmt:
  786. while (!list_empty(&conn->mgmtqueue)) {
  787. conn->mtask = list_entry(conn->mgmtqueue.next,
  788. struct iscsi_mgmt_task, running);
  789. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  790. iscsi_free_mgmt_task(conn, conn->mtask);
  791. conn->mtask = NULL;
  792. continue;
  793. }
  794. iscsi_prep_mtask(conn, conn->mtask);
  795. list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
  796. rc = iscsi_xmit_mtask(conn);
  797. if (rc)
  798. goto again;
  799. }
  800. /* process pending command queue */
  801. while (!list_empty(&conn->xmitqueue)) {
  802. if (conn->tmf_state == TMF_QUEUED)
  803. break;
  804. conn->ctask = list_entry(conn->xmitqueue.next,
  805. struct iscsi_cmd_task, running);
  806. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  807. fail_command(conn, conn->ctask, DID_IMM_RETRY << 16);
  808. continue;
  809. }
  810. if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
  811. fail_command(conn, conn->ctask, DID_ABORT << 16);
  812. continue;
  813. }
  814. conn->ctask->state = ISCSI_TASK_RUNNING;
  815. list_move_tail(conn->xmitqueue.next, &conn->run_list);
  816. rc = iscsi_xmit_ctask(conn);
  817. if (rc)
  818. goto again;
  819. /*
  820. * we could continuously get new ctask requests so
  821. * we need to check the mgmt queue for nops that need to
  822. * be sent to aviod starvation
  823. */
  824. if (!list_empty(&conn->mgmtqueue))
  825. goto check_mgmt;
  826. }
  827. while (!list_empty(&conn->requeue)) {
  828. if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
  829. break;
  830. /*
  831. * we always do fastlogout - conn stop code will clean up.
  832. */
  833. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  834. break;
  835. conn->ctask = list_entry(conn->requeue.next,
  836. struct iscsi_cmd_task, running);
  837. conn->ctask->state = ISCSI_TASK_RUNNING;
  838. list_move_tail(conn->requeue.next, &conn->run_list);
  839. rc = iscsi_xmit_ctask(conn);
  840. if (rc)
  841. goto again;
  842. if (!list_empty(&conn->mgmtqueue))
  843. goto check_mgmt;
  844. }
  845. spin_unlock_bh(&conn->session->lock);
  846. return -ENODATA;
  847. again:
  848. if (unlikely(conn->suspend_tx))
  849. rc = -ENODATA;
  850. spin_unlock_bh(&conn->session->lock);
  851. return rc;
  852. }
  853. static void iscsi_xmitworker(struct work_struct *work)
  854. {
  855. struct iscsi_conn *conn =
  856. container_of(work, struct iscsi_conn, xmitwork);
  857. int rc;
  858. /*
  859. * serialize Xmit worker on a per-connection basis.
  860. */
  861. do {
  862. rc = iscsi_data_xmit(conn);
  863. } while (rc >= 0 || rc == -EAGAIN);
  864. }
  865. enum {
  866. FAILURE_BAD_HOST = 1,
  867. FAILURE_SESSION_FAILED,
  868. FAILURE_SESSION_FREED,
  869. FAILURE_WINDOW_CLOSED,
  870. FAILURE_OOM,
  871. FAILURE_SESSION_TERMINATE,
  872. FAILURE_SESSION_IN_RECOVERY,
  873. FAILURE_SESSION_RECOVERY_TIMEOUT,
  874. FAILURE_SESSION_LOGGING_OUT,
  875. };
  876. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  877. {
  878. struct Scsi_Host *host;
  879. int reason = 0;
  880. struct iscsi_session *session;
  881. struct iscsi_conn *conn;
  882. struct iscsi_cmd_task *ctask = NULL;
  883. sc->scsi_done = done;
  884. sc->result = 0;
  885. sc->SCp.ptr = NULL;
  886. host = sc->device->host;
  887. spin_unlock(host->host_lock);
  888. session = iscsi_hostdata(host->hostdata);
  889. spin_lock(&session->lock);
  890. /*
  891. * ISCSI_STATE_FAILED is a temp. state. The recovery
  892. * code will decide what is best to do with command queued
  893. * during this time
  894. */
  895. if (session->state != ISCSI_STATE_LOGGED_IN &&
  896. session->state != ISCSI_STATE_FAILED) {
  897. /*
  898. * to handle the race between when we set the recovery state
  899. * and block the session we requeue here (commands could
  900. * be entering our queuecommand while a block is starting
  901. * up because the block code is not locked)
  902. */
  903. switch (session->state) {
  904. case ISCSI_STATE_IN_RECOVERY:
  905. reason = FAILURE_SESSION_IN_RECOVERY;
  906. goto reject;
  907. case ISCSI_STATE_LOGGING_OUT:
  908. reason = FAILURE_SESSION_LOGGING_OUT;
  909. goto reject;
  910. case ISCSI_STATE_RECOVERY_FAILED:
  911. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  912. break;
  913. case ISCSI_STATE_TERMINATE:
  914. reason = FAILURE_SESSION_TERMINATE;
  915. break;
  916. default:
  917. reason = FAILURE_SESSION_FREED;
  918. }
  919. goto fault;
  920. }
  921. conn = session->leadconn;
  922. if (!conn) {
  923. reason = FAILURE_SESSION_FREED;
  924. goto fault;
  925. }
  926. if (iscsi_check_cmdsn_window_closed(conn)) {
  927. reason = FAILURE_WINDOW_CLOSED;
  928. goto reject;
  929. }
  930. if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
  931. sizeof(void*))) {
  932. reason = FAILURE_OOM;
  933. goto reject;
  934. }
  935. session->queued_cmdsn++;
  936. sc->SCp.phase = session->age;
  937. sc->SCp.ptr = (char *)ctask;
  938. atomic_set(&ctask->refcount, 1);
  939. ctask->state = ISCSI_TASK_PENDING;
  940. ctask->conn = conn;
  941. ctask->sc = sc;
  942. INIT_LIST_HEAD(&ctask->running);
  943. list_add_tail(&ctask->running, &conn->xmitqueue);
  944. spin_unlock(&session->lock);
  945. scsi_queue_work(host, &conn->xmitwork);
  946. spin_lock(host->host_lock);
  947. return 0;
  948. reject:
  949. spin_unlock(&session->lock);
  950. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  951. spin_lock(host->host_lock);
  952. return SCSI_MLQUEUE_HOST_BUSY;
  953. fault:
  954. spin_unlock(&session->lock);
  955. printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
  956. sc->cmnd[0], reason);
  957. sc->result = (DID_NO_CONNECT << 16);
  958. scsi_set_resid(sc, scsi_bufflen(sc));
  959. sc->scsi_done(sc);
  960. spin_lock(host->host_lock);
  961. return 0;
  962. }
  963. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  964. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  965. {
  966. if (depth > ISCSI_MAX_CMD_PER_LUN)
  967. depth = ISCSI_MAX_CMD_PER_LUN;
  968. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  969. return sdev->queue_depth;
  970. }
  971. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  972. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  973. {
  974. struct iscsi_session *session = class_to_transport_session(cls_session);
  975. spin_lock_bh(&session->lock);
  976. if (session->state != ISCSI_STATE_LOGGED_IN) {
  977. session->state = ISCSI_STATE_RECOVERY_FAILED;
  978. if (session->leadconn)
  979. wake_up(&session->leadconn->ehwait);
  980. }
  981. spin_unlock_bh(&session->lock);
  982. }
  983. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  984. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  985. {
  986. struct Scsi_Host *host = sc->device->host;
  987. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  988. struct iscsi_conn *conn = session->leadconn;
  989. mutex_lock(&session->eh_mutex);
  990. spin_lock_bh(&session->lock);
  991. if (session->state == ISCSI_STATE_TERMINATE) {
  992. failed:
  993. debug_scsi("failing host reset: session terminated "
  994. "[CID %d age %d]\n", conn->id, session->age);
  995. spin_unlock_bh(&session->lock);
  996. mutex_unlock(&session->eh_mutex);
  997. return FAILED;
  998. }
  999. spin_unlock_bh(&session->lock);
  1000. mutex_unlock(&session->eh_mutex);
  1001. /*
  1002. * we drop the lock here but the leadconn cannot be destoyed while
  1003. * we are in the scsi eh
  1004. */
  1005. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1006. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  1007. wait_event_interruptible(conn->ehwait,
  1008. session->state == ISCSI_STATE_TERMINATE ||
  1009. session->state == ISCSI_STATE_LOGGED_IN ||
  1010. session->state == ISCSI_STATE_RECOVERY_FAILED);
  1011. if (signal_pending(current))
  1012. flush_signals(current);
  1013. mutex_lock(&session->eh_mutex);
  1014. spin_lock_bh(&session->lock);
  1015. if (session->state == ISCSI_STATE_LOGGED_IN)
  1016. printk(KERN_INFO "iscsi: host reset succeeded\n");
  1017. else
  1018. goto failed;
  1019. spin_unlock_bh(&session->lock);
  1020. mutex_unlock(&session->eh_mutex);
  1021. return SUCCESS;
  1022. }
  1023. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  1024. static void iscsi_tmf_timedout(unsigned long data)
  1025. {
  1026. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1027. struct iscsi_session *session = conn->session;
  1028. spin_lock(&session->lock);
  1029. if (conn->tmf_state == TMF_QUEUED) {
  1030. conn->tmf_state = TMF_TIMEDOUT;
  1031. debug_scsi("tmf timedout\n");
  1032. /* unblock eh_abort() */
  1033. wake_up(&conn->ehwait);
  1034. }
  1035. spin_unlock(&session->lock);
  1036. }
  1037. static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
  1038. struct iscsi_tm *hdr, int age,
  1039. int timeout)
  1040. {
  1041. struct iscsi_session *session = conn->session;
  1042. struct iscsi_mgmt_task *mtask;
  1043. mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
  1044. NULL, 0);
  1045. if (!mtask) {
  1046. spin_unlock_bh(&session->lock);
  1047. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1048. spin_lock_bh(&session->lock);
  1049. debug_scsi("tmf exec failure\n");
  1050. return -EPERM;
  1051. }
  1052. conn->tmfcmd_pdus_cnt++;
  1053. conn->tmf_timer.expires = timeout * HZ + jiffies;
  1054. conn->tmf_timer.function = iscsi_tmf_timedout;
  1055. conn->tmf_timer.data = (unsigned long)conn;
  1056. add_timer(&conn->tmf_timer);
  1057. debug_scsi("tmf set timeout\n");
  1058. spin_unlock_bh(&session->lock);
  1059. mutex_unlock(&session->eh_mutex);
  1060. scsi_queue_work(session->host, &conn->xmitwork);
  1061. /*
  1062. * block eh thread until:
  1063. *
  1064. * 1) tmf response
  1065. * 2) tmf timeout
  1066. * 3) session is terminated or restarted or userspace has
  1067. * given up on recovery
  1068. */
  1069. wait_event_interruptible(conn->ehwait, age != session->age ||
  1070. session->state != ISCSI_STATE_LOGGED_IN ||
  1071. conn->tmf_state != TMF_QUEUED);
  1072. if (signal_pending(current))
  1073. flush_signals(current);
  1074. del_timer_sync(&conn->tmf_timer);
  1075. mutex_lock(&session->eh_mutex);
  1076. spin_lock_bh(&session->lock);
  1077. /* if the session drops it will clean up the mtask */
  1078. if (age != session->age ||
  1079. session->state != ISCSI_STATE_LOGGED_IN)
  1080. return -ENOTCONN;
  1081. return 0;
  1082. }
  1083. /*
  1084. * Fail commands. session lock held and recv side suspended and xmit
  1085. * thread flushed
  1086. */
  1087. static void fail_all_commands(struct iscsi_conn *conn, unsigned lun)
  1088. {
  1089. struct iscsi_cmd_task *ctask, *tmp;
  1090. if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
  1091. conn->ctask = NULL;
  1092. /* flush pending */
  1093. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1094. if (lun == ctask->sc->device->lun || lun == -1) {
  1095. debug_scsi("failing pending sc %p itt 0x%x\n",
  1096. ctask->sc, ctask->itt);
  1097. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1098. }
  1099. }
  1100. list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
  1101. if (lun == ctask->sc->device->lun || lun == -1) {
  1102. debug_scsi("failing requeued sc %p itt 0x%x\n",
  1103. ctask->sc, ctask->itt);
  1104. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1105. }
  1106. }
  1107. /* fail all other running */
  1108. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1109. if (lun == ctask->sc->device->lun || lun == -1) {
  1110. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1111. ctask->sc, ctask->itt);
  1112. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1113. }
  1114. }
  1115. }
  1116. static void iscsi_suspend_tx(struct iscsi_conn *conn)
  1117. {
  1118. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1119. scsi_flush_work(conn->session->host);
  1120. }
  1121. static void iscsi_start_tx(struct iscsi_conn *conn)
  1122. {
  1123. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1124. scsi_queue_work(conn->session->host, &conn->xmitwork);
  1125. }
  1126. static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
  1127. {
  1128. struct iscsi_cls_session *cls_session;
  1129. struct iscsi_session *session;
  1130. struct iscsi_conn *conn;
  1131. enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
  1132. cls_session = starget_to_session(scsi_target(scmd->device));
  1133. session = class_to_transport_session(cls_session);
  1134. debug_scsi("scsi cmd %p timedout\n", scmd);
  1135. spin_lock(&session->lock);
  1136. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1137. /*
  1138. * We are probably in the middle of iscsi recovery so let
  1139. * that complete and handle the error.
  1140. */
  1141. rc = EH_RESET_TIMER;
  1142. goto done;
  1143. }
  1144. conn = session->leadconn;
  1145. if (!conn) {
  1146. /* In the middle of shuting down */
  1147. rc = EH_RESET_TIMER;
  1148. goto done;
  1149. }
  1150. if (!conn->recv_timeout && !conn->ping_timeout)
  1151. goto done;
  1152. /*
  1153. * if the ping timedout then we are in the middle of cleaning up
  1154. * and can let the iscsi eh handle it
  1155. */
  1156. if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
  1157. (conn->ping_timeout * HZ), jiffies))
  1158. rc = EH_RESET_TIMER;
  1159. /*
  1160. * if we are about to check the transport then give the command
  1161. * more time
  1162. */
  1163. if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
  1164. jiffies))
  1165. rc = EH_RESET_TIMER;
  1166. /* if in the middle of checking the transport then give us more time */
  1167. if (conn->ping_mtask)
  1168. rc = EH_RESET_TIMER;
  1169. done:
  1170. spin_unlock(&session->lock);
  1171. debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
  1172. return rc;
  1173. }
  1174. static void iscsi_check_transport_timeouts(unsigned long data)
  1175. {
  1176. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1177. struct iscsi_session *session = conn->session;
  1178. unsigned long timeout, next_timeout = 0, last_recv;
  1179. spin_lock(&session->lock);
  1180. if (session->state != ISCSI_STATE_LOGGED_IN)
  1181. goto done;
  1182. timeout = conn->recv_timeout;
  1183. if (!timeout)
  1184. goto done;
  1185. timeout *= HZ;
  1186. last_recv = conn->last_recv;
  1187. if (time_before_eq(last_recv + timeout + (conn->ping_timeout * HZ),
  1188. jiffies)) {
  1189. printk(KERN_ERR "ping timeout of %d secs expired, "
  1190. "last rx %lu, last ping %lu, now %lu\n",
  1191. conn->ping_timeout, last_recv,
  1192. conn->last_ping, jiffies);
  1193. spin_unlock(&session->lock);
  1194. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1195. return;
  1196. }
  1197. if (time_before_eq(last_recv + timeout, jiffies)) {
  1198. if (time_before_eq(conn->last_ping, last_recv)) {
  1199. /* send a ping to try to provoke some traffic */
  1200. debug_scsi("Sending nopout as ping on conn %p\n", conn);
  1201. iscsi_send_nopout(conn, NULL);
  1202. }
  1203. next_timeout = last_recv + timeout + (conn->ping_timeout * HZ);
  1204. } else {
  1205. next_timeout = last_recv + timeout;
  1206. }
  1207. if (next_timeout) {
  1208. debug_scsi("Setting next tmo %lu\n", next_timeout);
  1209. mod_timer(&conn->transport_timer, next_timeout);
  1210. }
  1211. done:
  1212. spin_unlock(&session->lock);
  1213. }
  1214. static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
  1215. struct iscsi_tm *hdr)
  1216. {
  1217. memset(hdr, 0, sizeof(*hdr));
  1218. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1219. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
  1220. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1221. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  1222. hdr->rtt = ctask->hdr->itt;
  1223. hdr->refcmdsn = ctask->hdr->cmdsn;
  1224. }
  1225. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1226. {
  1227. struct Scsi_Host *host = sc->device->host;
  1228. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  1229. struct iscsi_conn *conn;
  1230. struct iscsi_cmd_task *ctask;
  1231. struct iscsi_tm *hdr;
  1232. int rc, age;
  1233. mutex_lock(&session->eh_mutex);
  1234. spin_lock_bh(&session->lock);
  1235. /*
  1236. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1237. * got the command.
  1238. */
  1239. if (!sc->SCp.ptr) {
  1240. debug_scsi("sc never reached iscsi layer or it completed.\n");
  1241. spin_unlock_bh(&session->lock);
  1242. mutex_unlock(&session->eh_mutex);
  1243. return SUCCESS;
  1244. }
  1245. /*
  1246. * If we are not logged in or we have started a new session
  1247. * then let the host reset code handle this
  1248. */
  1249. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
  1250. sc->SCp.phase != session->age) {
  1251. spin_unlock_bh(&session->lock);
  1252. mutex_unlock(&session->eh_mutex);
  1253. return FAILED;
  1254. }
  1255. conn = session->leadconn;
  1256. conn->eh_abort_cnt++;
  1257. age = session->age;
  1258. ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  1259. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  1260. /* ctask completed before time out */
  1261. if (!ctask->sc) {
  1262. debug_scsi("sc completed while abort in progress\n");
  1263. goto success;
  1264. }
  1265. if (ctask->state == ISCSI_TASK_PENDING) {
  1266. fail_command(conn, ctask, DID_ABORT << 16);
  1267. goto success;
  1268. }
  1269. /* only have one tmf outstanding at a time */
  1270. if (conn->tmf_state != TMF_INITIAL)
  1271. goto failed;
  1272. conn->tmf_state = TMF_QUEUED;
  1273. hdr = &conn->tmhdr;
  1274. iscsi_prep_abort_task_pdu(ctask, hdr);
  1275. if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
  1276. rc = FAILED;
  1277. goto failed;
  1278. }
  1279. switch (conn->tmf_state) {
  1280. case TMF_SUCCESS:
  1281. spin_unlock_bh(&session->lock);
  1282. iscsi_suspend_tx(conn);
  1283. /*
  1284. * clean up task if aborted. grab the recv lock as a writer
  1285. */
  1286. write_lock_bh(conn->recv_lock);
  1287. spin_lock(&session->lock);
  1288. fail_command(conn, ctask, DID_ABORT << 16);
  1289. conn->tmf_state = TMF_INITIAL;
  1290. spin_unlock(&session->lock);
  1291. write_unlock_bh(conn->recv_lock);
  1292. iscsi_start_tx(conn);
  1293. goto success_unlocked;
  1294. case TMF_TIMEDOUT:
  1295. spin_unlock_bh(&session->lock);
  1296. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1297. goto failed_unlocked;
  1298. case TMF_NOT_FOUND:
  1299. if (!sc->SCp.ptr) {
  1300. conn->tmf_state = TMF_INITIAL;
  1301. /* ctask completed before tmf abort response */
  1302. debug_scsi("sc completed while abort in progress\n");
  1303. goto success;
  1304. }
  1305. /* fall through */
  1306. default:
  1307. conn->tmf_state = TMF_INITIAL;
  1308. goto failed;
  1309. }
  1310. success:
  1311. spin_unlock_bh(&session->lock);
  1312. success_unlocked:
  1313. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1314. mutex_unlock(&session->eh_mutex);
  1315. return SUCCESS;
  1316. failed:
  1317. spin_unlock_bh(&session->lock);
  1318. failed_unlocked:
  1319. debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
  1320. ctask ? ctask->itt : 0);
  1321. mutex_unlock(&session->eh_mutex);
  1322. return FAILED;
  1323. }
  1324. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1325. static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  1326. {
  1327. memset(hdr, 0, sizeof(*hdr));
  1328. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1329. hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  1330. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1331. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  1332. hdr->rtt = RESERVED_ITT;
  1333. }
  1334. int iscsi_eh_device_reset(struct scsi_cmnd *sc)
  1335. {
  1336. struct Scsi_Host *host = sc->device->host;
  1337. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  1338. struct iscsi_conn *conn;
  1339. struct iscsi_tm *hdr;
  1340. int rc = FAILED;
  1341. debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
  1342. mutex_lock(&session->eh_mutex);
  1343. spin_lock_bh(&session->lock);
  1344. /*
  1345. * Just check if we are not logged in. We cannot check for
  1346. * the phase because the reset could come from a ioctl.
  1347. */
  1348. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  1349. goto unlock;
  1350. conn = session->leadconn;
  1351. /* only have one tmf outstanding at a time */
  1352. if (conn->tmf_state != TMF_INITIAL)
  1353. goto unlock;
  1354. conn->tmf_state = TMF_QUEUED;
  1355. hdr = &conn->tmhdr;
  1356. iscsi_prep_lun_reset_pdu(sc, hdr);
  1357. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  1358. session->lu_reset_timeout)) {
  1359. rc = FAILED;
  1360. goto unlock;
  1361. }
  1362. switch (conn->tmf_state) {
  1363. case TMF_SUCCESS:
  1364. break;
  1365. case TMF_TIMEDOUT:
  1366. spin_unlock_bh(&session->lock);
  1367. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1368. goto done;
  1369. default:
  1370. conn->tmf_state = TMF_INITIAL;
  1371. goto unlock;
  1372. }
  1373. rc = SUCCESS;
  1374. spin_unlock_bh(&session->lock);
  1375. iscsi_suspend_tx(conn);
  1376. /* need to grab the recv lock then session lock */
  1377. write_lock_bh(conn->recv_lock);
  1378. spin_lock(&session->lock);
  1379. fail_all_commands(conn, sc->device->lun);
  1380. conn->tmf_state = TMF_INITIAL;
  1381. spin_unlock(&session->lock);
  1382. write_unlock_bh(conn->recv_lock);
  1383. iscsi_start_tx(conn);
  1384. goto done;
  1385. unlock:
  1386. spin_unlock_bh(&session->lock);
  1387. done:
  1388. debug_scsi("iscsi_eh_device_reset %s\n",
  1389. rc == SUCCESS ? "SUCCESS" : "FAILED");
  1390. mutex_unlock(&session->eh_mutex);
  1391. return rc;
  1392. }
  1393. EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
  1394. /*
  1395. * Pre-allocate a pool of @max items of @item_size. By default, the pool
  1396. * should be accessed via kfifo_{get,put} on q->queue.
  1397. * Optionally, the caller can obtain the array of object pointers
  1398. * by passing in a non-NULL @items pointer
  1399. */
  1400. int
  1401. iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
  1402. {
  1403. int i, num_arrays = 1;
  1404. memset(q, 0, sizeof(*q));
  1405. q->max = max;
  1406. /* If the user passed an items pointer, he wants a copy of
  1407. * the array. */
  1408. if (items)
  1409. num_arrays++;
  1410. q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
  1411. if (q->pool == NULL)
  1412. goto enomem;
  1413. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1414. GFP_KERNEL, NULL);
  1415. if (q->queue == ERR_PTR(-ENOMEM))
  1416. goto enomem;
  1417. for (i = 0; i < max; i++) {
  1418. q->pool[i] = kzalloc(item_size, GFP_KERNEL);
  1419. if (q->pool[i] == NULL) {
  1420. q->max = i;
  1421. goto enomem;
  1422. }
  1423. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1424. }
  1425. if (items) {
  1426. *items = q->pool + max;
  1427. memcpy(*items, q->pool, max * sizeof(void *));
  1428. }
  1429. return 0;
  1430. enomem:
  1431. iscsi_pool_free(q);
  1432. return -ENOMEM;
  1433. }
  1434. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1435. void iscsi_pool_free(struct iscsi_pool *q)
  1436. {
  1437. int i;
  1438. for (i = 0; i < q->max; i++)
  1439. kfree(q->pool[i]);
  1440. if (q->pool)
  1441. kfree(q->pool);
  1442. }
  1443. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1444. /*
  1445. * iSCSI Session's hostdata organization:
  1446. *
  1447. * *------------------* <== hostdata_session(host->hostdata)
  1448. * | ptr to class sess|
  1449. * |------------------| <== iscsi_hostdata(host->hostdata)
  1450. * | iscsi_session |
  1451. * *------------------*
  1452. */
  1453. #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
  1454. _sz % sizeof(unsigned long))
  1455. #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
  1456. /**
  1457. * iscsi_session_setup - create iscsi cls session and host and session
  1458. * @scsit: scsi transport template
  1459. * @iscsit: iscsi transport template
  1460. * @cmds_max: scsi host can queue
  1461. * @qdepth: scsi host cmds per lun
  1462. * @cmd_task_size: LLD ctask private data size
  1463. * @mgmt_task_size: LLD mtask private data size
  1464. * @initial_cmdsn: initial CmdSN
  1465. * @hostno: host no allocated
  1466. *
  1467. * This can be used by software iscsi_transports that allocate
  1468. * a session per scsi host.
  1469. **/
  1470. struct iscsi_cls_session *
  1471. iscsi_session_setup(struct iscsi_transport *iscsit,
  1472. struct scsi_transport_template *scsit,
  1473. uint16_t cmds_max, uint16_t qdepth,
  1474. int cmd_task_size, int mgmt_task_size,
  1475. uint32_t initial_cmdsn, uint32_t *hostno)
  1476. {
  1477. struct Scsi_Host *shost;
  1478. struct iscsi_session *session;
  1479. struct iscsi_cls_session *cls_session;
  1480. int cmd_i;
  1481. if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
  1482. if (qdepth != 0)
  1483. printk(KERN_ERR "iscsi: invalid queue depth of %d. "
  1484. "Queue depth must be between 1 and %d.\n",
  1485. qdepth, ISCSI_MAX_CMD_PER_LUN);
  1486. qdepth = ISCSI_DEF_CMD_PER_LUN;
  1487. }
  1488. if (!is_power_of_2(cmds_max) ||
  1489. cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
  1490. if (cmds_max != 0)
  1491. printk(KERN_ERR "iscsi: invalid can_queue of %d. "
  1492. "can_queue must be a power of 2 and between "
  1493. "2 and %d - setting to %d.\n", cmds_max,
  1494. ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
  1495. cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
  1496. }
  1497. shost = scsi_host_alloc(iscsit->host_template,
  1498. hostdata_privsize(sizeof(*session)));
  1499. if (!shost)
  1500. return NULL;
  1501. /* the iscsi layer takes one task for reserve */
  1502. shost->can_queue = cmds_max - 1;
  1503. shost->cmd_per_lun = qdepth;
  1504. shost->max_id = 1;
  1505. shost->max_channel = 0;
  1506. shost->max_lun = iscsit->max_lun;
  1507. shost->max_cmd_len = iscsit->max_cmd_len;
  1508. shost->transportt = scsit;
  1509. shost->transportt->create_work_queue = 1;
  1510. shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
  1511. *hostno = shost->host_no;
  1512. session = iscsi_hostdata(shost->hostdata);
  1513. memset(session, 0, sizeof(struct iscsi_session));
  1514. session->host = shost;
  1515. session->state = ISCSI_STATE_FREE;
  1516. session->fast_abort = 1;
  1517. session->lu_reset_timeout = 15;
  1518. session->abort_timeout = 10;
  1519. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1520. session->cmds_max = cmds_max;
  1521. session->queued_cmdsn = session->cmdsn = initial_cmdsn;
  1522. session->exp_cmdsn = initial_cmdsn + 1;
  1523. session->max_cmdsn = initial_cmdsn + 1;
  1524. session->max_r2t = 1;
  1525. session->tt = iscsit;
  1526. mutex_init(&session->eh_mutex);
  1527. /* initialize SCSI PDU commands pool */
  1528. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1529. (void***)&session->cmds,
  1530. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1531. goto cmdpool_alloc_fail;
  1532. /* pre-format cmds pool with ITT */
  1533. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1534. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1535. if (cmd_task_size)
  1536. ctask->dd_data = &ctask[1];
  1537. ctask->itt = cmd_i;
  1538. INIT_LIST_HEAD(&ctask->running);
  1539. }
  1540. spin_lock_init(&session->lock);
  1541. /* initialize immediate command pool */
  1542. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1543. (void***)&session->mgmt_cmds,
  1544. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1545. goto mgmtpool_alloc_fail;
  1546. /* pre-format immediate cmds pool with ITT */
  1547. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1548. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1549. if (mgmt_task_size)
  1550. mtask->dd_data = &mtask[1];
  1551. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1552. INIT_LIST_HEAD(&mtask->running);
  1553. }
  1554. if (scsi_add_host(shost, NULL))
  1555. goto add_host_fail;
  1556. if (!try_module_get(iscsit->owner))
  1557. goto cls_session_fail;
  1558. cls_session = iscsi_create_session(shost, iscsit, 0);
  1559. if (!cls_session)
  1560. goto module_put;
  1561. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1562. return cls_session;
  1563. module_put:
  1564. module_put(iscsit->owner);
  1565. cls_session_fail:
  1566. scsi_remove_host(shost);
  1567. add_host_fail:
  1568. iscsi_pool_free(&session->mgmtpool);
  1569. mgmtpool_alloc_fail:
  1570. iscsi_pool_free(&session->cmdpool);
  1571. cmdpool_alloc_fail:
  1572. scsi_host_put(shost);
  1573. return NULL;
  1574. }
  1575. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1576. /**
  1577. * iscsi_session_teardown - destroy session, host, and cls_session
  1578. * shost: scsi host
  1579. *
  1580. * This can be used by software iscsi_transports that allocate
  1581. * a session per scsi host.
  1582. **/
  1583. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1584. {
  1585. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1586. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1587. struct module *owner = cls_session->transport->owner;
  1588. iscsi_remove_session(cls_session);
  1589. scsi_remove_host(shost);
  1590. iscsi_pool_free(&session->mgmtpool);
  1591. iscsi_pool_free(&session->cmdpool);
  1592. kfree(session->password);
  1593. kfree(session->password_in);
  1594. kfree(session->username);
  1595. kfree(session->username_in);
  1596. kfree(session->targetname);
  1597. kfree(session->netdev);
  1598. kfree(session->hwaddress);
  1599. kfree(session->initiatorname);
  1600. iscsi_free_session(cls_session);
  1601. scsi_host_put(shost);
  1602. module_put(owner);
  1603. }
  1604. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1605. /**
  1606. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1607. * @cls_session: iscsi_cls_session
  1608. * @conn_idx: cid
  1609. **/
  1610. struct iscsi_cls_conn *
  1611. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1612. {
  1613. struct iscsi_session *session = class_to_transport_session(cls_session);
  1614. struct iscsi_conn *conn;
  1615. struct iscsi_cls_conn *cls_conn;
  1616. char *data;
  1617. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1618. if (!cls_conn)
  1619. return NULL;
  1620. conn = cls_conn->dd_data;
  1621. memset(conn, 0, sizeof(*conn));
  1622. conn->session = session;
  1623. conn->cls_conn = cls_conn;
  1624. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1625. conn->id = conn_idx;
  1626. conn->exp_statsn = 0;
  1627. conn->tmf_state = TMF_INITIAL;
  1628. init_timer(&conn->transport_timer);
  1629. conn->transport_timer.data = (unsigned long)conn;
  1630. conn->transport_timer.function = iscsi_check_transport_timeouts;
  1631. INIT_LIST_HEAD(&conn->run_list);
  1632. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1633. INIT_LIST_HEAD(&conn->mgmtqueue);
  1634. INIT_LIST_HEAD(&conn->xmitqueue);
  1635. INIT_LIST_HEAD(&conn->requeue);
  1636. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  1637. /* allocate login_mtask used for the login/text sequences */
  1638. spin_lock_bh(&session->lock);
  1639. if (!__kfifo_get(session->mgmtpool.queue,
  1640. (void*)&conn->login_mtask,
  1641. sizeof(void*))) {
  1642. spin_unlock_bh(&session->lock);
  1643. goto login_mtask_alloc_fail;
  1644. }
  1645. spin_unlock_bh(&session->lock);
  1646. data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
  1647. if (!data)
  1648. goto login_mtask_data_alloc_fail;
  1649. conn->login_mtask->data = conn->data = data;
  1650. init_timer(&conn->tmf_timer);
  1651. init_waitqueue_head(&conn->ehwait);
  1652. return cls_conn;
  1653. login_mtask_data_alloc_fail:
  1654. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1655. sizeof(void*));
  1656. login_mtask_alloc_fail:
  1657. iscsi_destroy_conn(cls_conn);
  1658. return NULL;
  1659. }
  1660. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1661. /**
  1662. * iscsi_conn_teardown - teardown iscsi connection
  1663. * cls_conn: iscsi class connection
  1664. *
  1665. * TODO: we may need to make this into a two step process
  1666. * like scsi-mls remove + put host
  1667. */
  1668. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1669. {
  1670. struct iscsi_conn *conn = cls_conn->dd_data;
  1671. struct iscsi_session *session = conn->session;
  1672. unsigned long flags;
  1673. del_timer_sync(&conn->transport_timer);
  1674. spin_lock_bh(&session->lock);
  1675. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1676. if (session->leadconn == conn) {
  1677. /*
  1678. * leading connection? then give up on recovery.
  1679. */
  1680. session->state = ISCSI_STATE_TERMINATE;
  1681. wake_up(&conn->ehwait);
  1682. }
  1683. spin_unlock_bh(&session->lock);
  1684. /*
  1685. * Block until all in-progress commands for this connection
  1686. * time out or fail.
  1687. */
  1688. for (;;) {
  1689. spin_lock_irqsave(session->host->host_lock, flags);
  1690. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1691. spin_unlock_irqrestore(session->host->host_lock, flags);
  1692. break;
  1693. }
  1694. spin_unlock_irqrestore(session->host->host_lock, flags);
  1695. msleep_interruptible(500);
  1696. printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
  1697. "host_failed %d\n", session->host->host_busy,
  1698. session->host->host_failed);
  1699. /*
  1700. * force eh_abort() to unblock
  1701. */
  1702. wake_up(&conn->ehwait);
  1703. }
  1704. /* flush queued up work because we free the connection below */
  1705. iscsi_suspend_tx(conn);
  1706. spin_lock_bh(&session->lock);
  1707. kfree(conn->data);
  1708. kfree(conn->persistent_address);
  1709. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1710. sizeof(void*));
  1711. if (session->leadconn == conn)
  1712. session->leadconn = NULL;
  1713. spin_unlock_bh(&session->lock);
  1714. iscsi_destroy_conn(cls_conn);
  1715. }
  1716. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1717. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1718. {
  1719. struct iscsi_conn *conn = cls_conn->dd_data;
  1720. struct iscsi_session *session = conn->session;
  1721. if (!session) {
  1722. printk(KERN_ERR "iscsi: can't start unbound connection\n");
  1723. return -EPERM;
  1724. }
  1725. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1726. session->first_burst > session->max_burst) {
  1727. printk("iscsi: invalid burst lengths: "
  1728. "first_burst %d max_burst %d\n",
  1729. session->first_burst, session->max_burst);
  1730. return -EINVAL;
  1731. }
  1732. if (conn->ping_timeout && !conn->recv_timeout) {
  1733. printk(KERN_ERR "iscsi: invalid recv timeout of zero "
  1734. "Using 5 seconds\n.");
  1735. conn->recv_timeout = 5;
  1736. }
  1737. if (conn->recv_timeout && !conn->ping_timeout) {
  1738. printk(KERN_ERR "iscsi: invalid ping timeout of zero "
  1739. "Using 5 seconds.\n");
  1740. conn->ping_timeout = 5;
  1741. }
  1742. spin_lock_bh(&session->lock);
  1743. conn->c_stage = ISCSI_CONN_STARTED;
  1744. session->state = ISCSI_STATE_LOGGED_IN;
  1745. session->queued_cmdsn = session->cmdsn;
  1746. conn->last_recv = jiffies;
  1747. conn->last_ping = jiffies;
  1748. if (conn->recv_timeout && conn->ping_timeout)
  1749. mod_timer(&conn->transport_timer,
  1750. jiffies + (conn->recv_timeout * HZ));
  1751. switch(conn->stop_stage) {
  1752. case STOP_CONN_RECOVER:
  1753. /*
  1754. * unblock eh_abort() if it is blocked. re-try all
  1755. * commands after successful recovery
  1756. */
  1757. conn->stop_stage = 0;
  1758. conn->tmf_state = TMF_INITIAL;
  1759. session->age++;
  1760. spin_unlock_bh(&session->lock);
  1761. iscsi_unblock_session(session_to_cls(session));
  1762. wake_up(&conn->ehwait);
  1763. return 0;
  1764. case STOP_CONN_TERM:
  1765. conn->stop_stage = 0;
  1766. break;
  1767. default:
  1768. break;
  1769. }
  1770. spin_unlock_bh(&session->lock);
  1771. return 0;
  1772. }
  1773. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1774. static void
  1775. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1776. {
  1777. struct iscsi_mgmt_task *mtask, *tmp;
  1778. /* handle pending */
  1779. list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
  1780. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1781. iscsi_free_mgmt_task(conn, mtask);
  1782. }
  1783. /* handle running */
  1784. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1785. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1786. iscsi_free_mgmt_task(conn, mtask);
  1787. }
  1788. conn->mtask = NULL;
  1789. }
  1790. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1791. struct iscsi_conn *conn, int flag)
  1792. {
  1793. int old_stop_stage;
  1794. del_timer_sync(&conn->transport_timer);
  1795. mutex_lock(&session->eh_mutex);
  1796. spin_lock_bh(&session->lock);
  1797. if (conn->stop_stage == STOP_CONN_TERM) {
  1798. spin_unlock_bh(&session->lock);
  1799. mutex_unlock(&session->eh_mutex);
  1800. return;
  1801. }
  1802. /*
  1803. * The LLD either freed/unset the lock on us, or userspace called
  1804. * stop but did not create a proper connection (connection was never
  1805. * bound or it was unbound then stop was called).
  1806. */
  1807. if (!conn->recv_lock) {
  1808. spin_unlock_bh(&session->lock);
  1809. mutex_unlock(&session->eh_mutex);
  1810. return;
  1811. }
  1812. /*
  1813. * When this is called for the in_login state, we only want to clean
  1814. * up the login task and connection. We do not need to block and set
  1815. * the recovery state again
  1816. */
  1817. if (flag == STOP_CONN_TERM)
  1818. session->state = ISCSI_STATE_TERMINATE;
  1819. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1820. session->state = ISCSI_STATE_IN_RECOVERY;
  1821. old_stop_stage = conn->stop_stage;
  1822. conn->stop_stage = flag;
  1823. conn->c_stage = ISCSI_CONN_STOPPED;
  1824. spin_unlock_bh(&session->lock);
  1825. iscsi_suspend_tx(conn);
  1826. write_lock_bh(conn->recv_lock);
  1827. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1828. write_unlock_bh(conn->recv_lock);
  1829. /*
  1830. * for connection level recovery we should not calculate
  1831. * header digest. conn->hdr_size used for optimization
  1832. * in hdr_extract() and will be re-negotiated at
  1833. * set_param() time.
  1834. */
  1835. if (flag == STOP_CONN_RECOVER) {
  1836. conn->hdrdgst_en = 0;
  1837. conn->datadgst_en = 0;
  1838. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1839. old_stop_stage != STOP_CONN_RECOVER) {
  1840. debug_scsi("blocking session\n");
  1841. iscsi_block_session(session_to_cls(session));
  1842. }
  1843. }
  1844. /*
  1845. * flush queues.
  1846. */
  1847. spin_lock_bh(&session->lock);
  1848. fail_all_commands(conn, -1);
  1849. flush_control_queues(session, conn);
  1850. spin_unlock_bh(&session->lock);
  1851. mutex_unlock(&session->eh_mutex);
  1852. }
  1853. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1854. {
  1855. struct iscsi_conn *conn = cls_conn->dd_data;
  1856. struct iscsi_session *session = conn->session;
  1857. switch (flag) {
  1858. case STOP_CONN_RECOVER:
  1859. case STOP_CONN_TERM:
  1860. iscsi_start_session_recovery(session, conn, flag);
  1861. break;
  1862. default:
  1863. printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
  1864. }
  1865. }
  1866. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1867. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1868. struct iscsi_cls_conn *cls_conn, int is_leading)
  1869. {
  1870. struct iscsi_session *session = class_to_transport_session(cls_session);
  1871. struct iscsi_conn *conn = cls_conn->dd_data;
  1872. spin_lock_bh(&session->lock);
  1873. if (is_leading)
  1874. session->leadconn = conn;
  1875. spin_unlock_bh(&session->lock);
  1876. /*
  1877. * Unblock xmitworker(), Login Phase will pass through.
  1878. */
  1879. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1880. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1881. return 0;
  1882. }
  1883. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  1884. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  1885. enum iscsi_param param, char *buf, int buflen)
  1886. {
  1887. struct iscsi_conn *conn = cls_conn->dd_data;
  1888. struct iscsi_session *session = conn->session;
  1889. uint32_t value;
  1890. switch(param) {
  1891. case ISCSI_PARAM_FAST_ABORT:
  1892. sscanf(buf, "%d", &session->fast_abort);
  1893. break;
  1894. case ISCSI_PARAM_ABORT_TMO:
  1895. sscanf(buf, "%d", &session->abort_timeout);
  1896. break;
  1897. case ISCSI_PARAM_LU_RESET_TMO:
  1898. sscanf(buf, "%d", &session->lu_reset_timeout);
  1899. break;
  1900. case ISCSI_PARAM_PING_TMO:
  1901. sscanf(buf, "%d", &conn->ping_timeout);
  1902. break;
  1903. case ISCSI_PARAM_RECV_TMO:
  1904. sscanf(buf, "%d", &conn->recv_timeout);
  1905. break;
  1906. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1907. sscanf(buf, "%d", &conn->max_recv_dlength);
  1908. break;
  1909. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1910. sscanf(buf, "%d", &conn->max_xmit_dlength);
  1911. break;
  1912. case ISCSI_PARAM_HDRDGST_EN:
  1913. sscanf(buf, "%d", &conn->hdrdgst_en);
  1914. break;
  1915. case ISCSI_PARAM_DATADGST_EN:
  1916. sscanf(buf, "%d", &conn->datadgst_en);
  1917. break;
  1918. case ISCSI_PARAM_INITIAL_R2T_EN:
  1919. sscanf(buf, "%d", &session->initial_r2t_en);
  1920. break;
  1921. case ISCSI_PARAM_MAX_R2T:
  1922. sscanf(buf, "%d", &session->max_r2t);
  1923. break;
  1924. case ISCSI_PARAM_IMM_DATA_EN:
  1925. sscanf(buf, "%d", &session->imm_data_en);
  1926. break;
  1927. case ISCSI_PARAM_FIRST_BURST:
  1928. sscanf(buf, "%d", &session->first_burst);
  1929. break;
  1930. case ISCSI_PARAM_MAX_BURST:
  1931. sscanf(buf, "%d", &session->max_burst);
  1932. break;
  1933. case ISCSI_PARAM_PDU_INORDER_EN:
  1934. sscanf(buf, "%d", &session->pdu_inorder_en);
  1935. break;
  1936. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1937. sscanf(buf, "%d", &session->dataseq_inorder_en);
  1938. break;
  1939. case ISCSI_PARAM_ERL:
  1940. sscanf(buf, "%d", &session->erl);
  1941. break;
  1942. case ISCSI_PARAM_IFMARKER_EN:
  1943. sscanf(buf, "%d", &value);
  1944. BUG_ON(value);
  1945. break;
  1946. case ISCSI_PARAM_OFMARKER_EN:
  1947. sscanf(buf, "%d", &value);
  1948. BUG_ON(value);
  1949. break;
  1950. case ISCSI_PARAM_EXP_STATSN:
  1951. sscanf(buf, "%u", &conn->exp_statsn);
  1952. break;
  1953. case ISCSI_PARAM_USERNAME:
  1954. kfree(session->username);
  1955. session->username = kstrdup(buf, GFP_KERNEL);
  1956. if (!session->username)
  1957. return -ENOMEM;
  1958. break;
  1959. case ISCSI_PARAM_USERNAME_IN:
  1960. kfree(session->username_in);
  1961. session->username_in = kstrdup(buf, GFP_KERNEL);
  1962. if (!session->username_in)
  1963. return -ENOMEM;
  1964. break;
  1965. case ISCSI_PARAM_PASSWORD:
  1966. kfree(session->password);
  1967. session->password = kstrdup(buf, GFP_KERNEL);
  1968. if (!session->password)
  1969. return -ENOMEM;
  1970. break;
  1971. case ISCSI_PARAM_PASSWORD_IN:
  1972. kfree(session->password_in);
  1973. session->password_in = kstrdup(buf, GFP_KERNEL);
  1974. if (!session->password_in)
  1975. return -ENOMEM;
  1976. break;
  1977. case ISCSI_PARAM_TARGET_NAME:
  1978. /* this should not change between logins */
  1979. if (session->targetname)
  1980. break;
  1981. session->targetname = kstrdup(buf, GFP_KERNEL);
  1982. if (!session->targetname)
  1983. return -ENOMEM;
  1984. break;
  1985. case ISCSI_PARAM_TPGT:
  1986. sscanf(buf, "%d", &session->tpgt);
  1987. break;
  1988. case ISCSI_PARAM_PERSISTENT_PORT:
  1989. sscanf(buf, "%d", &conn->persistent_port);
  1990. break;
  1991. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1992. /*
  1993. * this is the address returned in discovery so it should
  1994. * not change between logins.
  1995. */
  1996. if (conn->persistent_address)
  1997. break;
  1998. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  1999. if (!conn->persistent_address)
  2000. return -ENOMEM;
  2001. break;
  2002. default:
  2003. return -ENOSYS;
  2004. }
  2005. return 0;
  2006. }
  2007. EXPORT_SYMBOL_GPL(iscsi_set_param);
  2008. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  2009. enum iscsi_param param, char *buf)
  2010. {
  2011. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  2012. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  2013. int len;
  2014. switch(param) {
  2015. case ISCSI_PARAM_FAST_ABORT:
  2016. len = sprintf(buf, "%d\n", session->fast_abort);
  2017. break;
  2018. case ISCSI_PARAM_ABORT_TMO:
  2019. len = sprintf(buf, "%d\n", session->abort_timeout);
  2020. break;
  2021. case ISCSI_PARAM_LU_RESET_TMO:
  2022. len = sprintf(buf, "%d\n", session->lu_reset_timeout);
  2023. break;
  2024. case ISCSI_PARAM_INITIAL_R2T_EN:
  2025. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  2026. break;
  2027. case ISCSI_PARAM_MAX_R2T:
  2028. len = sprintf(buf, "%hu\n", session->max_r2t);
  2029. break;
  2030. case ISCSI_PARAM_IMM_DATA_EN:
  2031. len = sprintf(buf, "%d\n", session->imm_data_en);
  2032. break;
  2033. case ISCSI_PARAM_FIRST_BURST:
  2034. len = sprintf(buf, "%u\n", session->first_burst);
  2035. break;
  2036. case ISCSI_PARAM_MAX_BURST:
  2037. len = sprintf(buf, "%u\n", session->max_burst);
  2038. break;
  2039. case ISCSI_PARAM_PDU_INORDER_EN:
  2040. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  2041. break;
  2042. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2043. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  2044. break;
  2045. case ISCSI_PARAM_ERL:
  2046. len = sprintf(buf, "%d\n", session->erl);
  2047. break;
  2048. case ISCSI_PARAM_TARGET_NAME:
  2049. len = sprintf(buf, "%s\n", session->targetname);
  2050. break;
  2051. case ISCSI_PARAM_TPGT:
  2052. len = sprintf(buf, "%d\n", session->tpgt);
  2053. break;
  2054. case ISCSI_PARAM_USERNAME:
  2055. len = sprintf(buf, "%s\n", session->username);
  2056. break;
  2057. case ISCSI_PARAM_USERNAME_IN:
  2058. len = sprintf(buf, "%s\n", session->username_in);
  2059. break;
  2060. case ISCSI_PARAM_PASSWORD:
  2061. len = sprintf(buf, "%s\n", session->password);
  2062. break;
  2063. case ISCSI_PARAM_PASSWORD_IN:
  2064. len = sprintf(buf, "%s\n", session->password_in);
  2065. break;
  2066. default:
  2067. return -ENOSYS;
  2068. }
  2069. return len;
  2070. }
  2071. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  2072. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  2073. enum iscsi_param param, char *buf)
  2074. {
  2075. struct iscsi_conn *conn = cls_conn->dd_data;
  2076. int len;
  2077. switch(param) {
  2078. case ISCSI_PARAM_PING_TMO:
  2079. len = sprintf(buf, "%u\n", conn->ping_timeout);
  2080. break;
  2081. case ISCSI_PARAM_RECV_TMO:
  2082. len = sprintf(buf, "%u\n", conn->recv_timeout);
  2083. break;
  2084. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2085. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  2086. break;
  2087. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2088. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  2089. break;
  2090. case ISCSI_PARAM_HDRDGST_EN:
  2091. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  2092. break;
  2093. case ISCSI_PARAM_DATADGST_EN:
  2094. len = sprintf(buf, "%d\n", conn->datadgst_en);
  2095. break;
  2096. case ISCSI_PARAM_IFMARKER_EN:
  2097. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  2098. break;
  2099. case ISCSI_PARAM_OFMARKER_EN:
  2100. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  2101. break;
  2102. case ISCSI_PARAM_EXP_STATSN:
  2103. len = sprintf(buf, "%u\n", conn->exp_statsn);
  2104. break;
  2105. case ISCSI_PARAM_PERSISTENT_PORT:
  2106. len = sprintf(buf, "%d\n", conn->persistent_port);
  2107. break;
  2108. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2109. len = sprintf(buf, "%s\n", conn->persistent_address);
  2110. break;
  2111. default:
  2112. return -ENOSYS;
  2113. }
  2114. return len;
  2115. }
  2116. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  2117. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2118. char *buf)
  2119. {
  2120. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  2121. int len;
  2122. switch (param) {
  2123. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2124. if (!session->netdev)
  2125. len = sprintf(buf, "%s\n", "default");
  2126. else
  2127. len = sprintf(buf, "%s\n", session->netdev);
  2128. break;
  2129. case ISCSI_HOST_PARAM_HWADDRESS:
  2130. if (!session->hwaddress)
  2131. len = sprintf(buf, "%s\n", "default");
  2132. else
  2133. len = sprintf(buf, "%s\n", session->hwaddress);
  2134. break;
  2135. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  2136. if (!session->initiatorname)
  2137. len = sprintf(buf, "%s\n", "unknown");
  2138. else
  2139. len = sprintf(buf, "%s\n", session->initiatorname);
  2140. break;
  2141. default:
  2142. return -ENOSYS;
  2143. }
  2144. return len;
  2145. }
  2146. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  2147. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2148. char *buf, int buflen)
  2149. {
  2150. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  2151. switch (param) {
  2152. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2153. if (!session->netdev)
  2154. session->netdev = kstrdup(buf, GFP_KERNEL);
  2155. break;
  2156. case ISCSI_HOST_PARAM_HWADDRESS:
  2157. if (!session->hwaddress)
  2158. session->hwaddress = kstrdup(buf, GFP_KERNEL);
  2159. break;
  2160. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  2161. if (!session->initiatorname)
  2162. session->initiatorname = kstrdup(buf, GFP_KERNEL);
  2163. break;
  2164. default:
  2165. return -ENOSYS;
  2166. }
  2167. return 0;
  2168. }
  2169. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  2170. MODULE_AUTHOR("Mike Christie");
  2171. MODULE_DESCRIPTION("iSCSI library functions");
  2172. MODULE_LICENSE("GPL");