libiscsi.c 65 KB

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