libiscsi.c 65 KB

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