zfcp_scsi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /*
  2. * This file is part of the zfcp device driver for
  3. * FCP adapters for IBM System z9 and zSeries.
  4. *
  5. * (C) Copyright IBM Corp. 2002, 2006
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_SCSI
  22. #include "zfcp_ext.h"
  23. static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
  24. static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
  25. static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
  26. static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
  27. void (*done) (struct scsi_cmnd *));
  28. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
  29. static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
  30. static int zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *);
  31. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
  32. static int zfcp_task_management_function(struct zfcp_unit *, u8,
  33. struct scsi_cmnd *);
  34. static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int,
  35. unsigned int, unsigned int);
  36. static struct device_attribute *zfcp_sysfs_sdev_attrs[];
  37. struct scsi_transport_template *zfcp_transport_template;
  38. struct zfcp_data zfcp_data = {
  39. .scsi_host_template = {
  40. .name = ZFCP_NAME,
  41. .proc_name = "zfcp",
  42. .proc_info = NULL,
  43. .detect = NULL,
  44. .slave_alloc = zfcp_scsi_slave_alloc,
  45. .slave_configure = zfcp_scsi_slave_configure,
  46. .slave_destroy = zfcp_scsi_slave_destroy,
  47. .queuecommand = zfcp_scsi_queuecommand,
  48. .eh_abort_handler = zfcp_scsi_eh_abort_handler,
  49. .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
  50. .eh_bus_reset_handler = zfcp_scsi_eh_bus_reset_handler,
  51. .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
  52. .can_queue = 4096,
  53. .this_id = -1,
  54. /*
  55. * FIXME:
  56. * one less? can zfcp_create_sbale cope with it?
  57. */
  58. .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
  59. .cmd_per_lun = 1,
  60. .unchecked_isa_dma = 0,
  61. .use_clustering = 1,
  62. .sdev_attrs = zfcp_sysfs_sdev_attrs,
  63. },
  64. .driver_version = ZFCP_VERSION,
  65. /* rest initialised with zeros */
  66. };
  67. /* Find start of Response Information in FCP response unit*/
  68. char *
  69. zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  70. {
  71. char *fcp_rsp_info_ptr;
  72. fcp_rsp_info_ptr =
  73. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  74. return fcp_rsp_info_ptr;
  75. }
  76. /* Find start of Sense Information in FCP response unit*/
  77. char *
  78. zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  79. {
  80. char *fcp_sns_info_ptr;
  81. fcp_sns_info_ptr =
  82. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  83. if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
  84. fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
  85. fcp_rsp_iu->fcp_rsp_len;
  86. return fcp_sns_info_ptr;
  87. }
  88. fcp_dl_t *
  89. zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
  90. {
  91. int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
  92. fcp_dl_t *fcp_dl_addr;
  93. fcp_dl_addr = (fcp_dl_t *)
  94. ((unsigned char *) fcp_cmd +
  95. sizeof (struct fcp_cmnd_iu) + additional_length);
  96. /*
  97. * fcp_dl_addr = start address of fcp_cmnd structure +
  98. * size of fixed part + size of dynamically sized add_dcp_cdb field
  99. * SEE FCP-2 documentation
  100. */
  101. return fcp_dl_addr;
  102. }
  103. fcp_dl_t
  104. zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
  105. {
  106. return *zfcp_get_fcp_dl_ptr(fcp_cmd);
  107. }
  108. void
  109. zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
  110. {
  111. *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
  112. }
  113. /*
  114. * note: it's a bit-or operation not an assignment
  115. * regarding the specified byte
  116. */
  117. static inline void
  118. set_byte(u32 * result, char status, char pos)
  119. {
  120. *result |= status << (pos * 8);
  121. }
  122. void
  123. set_host_byte(u32 * result, char status)
  124. {
  125. set_byte(result, status, 2);
  126. }
  127. void
  128. set_driver_byte(u32 * result, char status)
  129. {
  130. set_byte(result, status, 3);
  131. }
  132. static int
  133. zfcp_scsi_slave_alloc(struct scsi_device *sdp)
  134. {
  135. struct zfcp_adapter *adapter;
  136. struct zfcp_unit *unit;
  137. unsigned long flags;
  138. int retval = -ENXIO;
  139. adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
  140. if (!adapter)
  141. goto out;
  142. read_lock_irqsave(&zfcp_data.config_lock, flags);
  143. unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
  144. if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
  145. &unit->status)) {
  146. sdp->hostdata = unit;
  147. unit->device = sdp;
  148. zfcp_unit_get(unit);
  149. retval = 0;
  150. }
  151. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  152. out:
  153. return retval;
  154. }
  155. static void
  156. zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
  157. {
  158. struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
  159. if (unit) {
  160. atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
  161. sdpnt->hostdata = NULL;
  162. unit->device = NULL;
  163. zfcp_unit_put(unit);
  164. } else {
  165. ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
  166. "address %p\n", sdpnt);
  167. }
  168. }
  169. /*
  170. * called from scsi midlayer to allow finetuning of a device.
  171. */
  172. static int
  173. zfcp_scsi_slave_configure(struct scsi_device *sdp)
  174. {
  175. if (sdp->tagged_supported)
  176. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
  177. else
  178. scsi_adjust_queue_depth(sdp, 0, 1);
  179. return 0;
  180. }
  181. /**
  182. * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
  183. * @scpnt: pointer to struct scsi_cmnd where result is set
  184. * @result: result to be set in scpnt (e.g. DID_ERROR)
  185. */
  186. static void
  187. zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
  188. {
  189. set_host_byte(&scpnt->result, result);
  190. if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
  191. zfcp_scsi_dbf_event_result("fail", 4,
  192. (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
  193. scpnt, NULL);
  194. /* return directly */
  195. scpnt->scsi_done(scpnt);
  196. }
  197. /**
  198. * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
  199. * zfcp_scsi_command_sync
  200. * @adapter: adapter where scsi command is issued
  201. * @unit: unit to which scsi command is sent
  202. * @scpnt: scsi command to be sent
  203. * @timer: timer to be started if request is successfully initiated
  204. *
  205. * Note: In scsi_done function must be set in scpnt.
  206. */
  207. int
  208. zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
  209. struct scsi_cmnd *scpnt, struct timer_list *timer)
  210. {
  211. int tmp;
  212. int retval;
  213. retval = 0;
  214. BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
  215. BUG_ON(scpnt->scsi_done == NULL);
  216. if (unlikely(NULL == unit)) {
  217. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  218. goto out;
  219. }
  220. if (unlikely(
  221. atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
  222. !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
  223. ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
  224. "0x%016Lx on adapter %s\n",
  225. unit->fcp_lun, unit->port->wwpn,
  226. zfcp_get_busid_by_adapter(adapter));
  227. zfcp_scsi_command_fail(scpnt, DID_ERROR);
  228. goto out;
  229. }
  230. if (unlikely(
  231. !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
  232. ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
  233. "on port 0x%016Lx in recovery\n",
  234. zfcp_get_busid_by_unit(unit),
  235. unit->fcp_lun, unit->port->wwpn);
  236. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  237. goto out;
  238. }
  239. tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, timer,
  240. ZFCP_REQ_AUTO_CLEANUP);
  241. if (unlikely(tmp < 0)) {
  242. ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
  243. retval = SCSI_MLQUEUE_HOST_BUSY;
  244. }
  245. out:
  246. return retval;
  247. }
  248. void
  249. zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
  250. {
  251. struct completion *wait = (struct completion *) scpnt->SCp.ptr;
  252. complete(wait);
  253. }
  254. /**
  255. * zfcp_scsi_command_sync - send a SCSI command and wait for completion
  256. * @unit: unit where command is sent to
  257. * @scpnt: scsi command to be sent
  258. * @timer: timer to be started if request is successfully initiated
  259. * Return: 0
  260. *
  261. * Errors are indicated in scpnt->result
  262. */
  263. int
  264. zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
  265. struct timer_list *timer)
  266. {
  267. int ret;
  268. DECLARE_COMPLETION(wait);
  269. scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
  270. scpnt->scsi_done = zfcp_scsi_command_sync_handler;
  271. ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt, timer);
  272. if (ret == 0)
  273. wait_for_completion(&wait);
  274. scpnt->SCp.ptr = NULL;
  275. return 0;
  276. }
  277. /*
  278. * function: zfcp_scsi_queuecommand
  279. *
  280. * purpose: enqueues a SCSI command to the specified target device
  281. *
  282. * returns: 0 - success, SCSI command enqueued
  283. * !0 - failure
  284. */
  285. int
  286. zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
  287. void (*done) (struct scsi_cmnd *))
  288. {
  289. struct zfcp_unit *unit;
  290. struct zfcp_adapter *adapter;
  291. /* reset the status for this request */
  292. scpnt->result = 0;
  293. scpnt->host_scribble = NULL;
  294. scpnt->scsi_done = done;
  295. /*
  296. * figure out adapter and target device
  297. * (stored there by zfcp_scsi_slave_alloc)
  298. */
  299. adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
  300. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  301. return zfcp_scsi_command_async(adapter, unit, scpnt, NULL);
  302. }
  303. static struct zfcp_unit *
  304. zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
  305. unsigned int lun)
  306. {
  307. struct zfcp_port *port;
  308. struct zfcp_unit *unit, *retval = NULL;
  309. list_for_each_entry(port, &adapter->port_list_head, list) {
  310. if (!port->rport || (id != port->rport->scsi_target_id))
  311. continue;
  312. list_for_each_entry(unit, &port->unit_list_head, list) {
  313. if (lun == unit->scsi_lun) {
  314. retval = unit;
  315. goto out;
  316. }
  317. }
  318. }
  319. out:
  320. return retval;
  321. }
  322. /**
  323. * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
  324. * @scpnt: pointer to scsi_cmnd to be aborted
  325. * Return: SUCCESS - command has been aborted and cleaned up in internal
  326. * bookkeeping, SCSI stack won't be called for aborted command
  327. * FAILED - otherwise
  328. *
  329. * We do not need to care for a SCSI command which completes normally
  330. * but late during this abort routine runs. We are allowed to return
  331. * late commands to the SCSI stack. It tracks the state of commands and
  332. * will handle late commands. (Usually, the normal completion of late
  333. * commands is ignored with respect to the running abort operation.)
  334. */
  335. int
  336. zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
  337. {
  338. struct Scsi_Host *scsi_host;
  339. struct zfcp_adapter *adapter;
  340. struct zfcp_unit *unit;
  341. int retval = SUCCESS;
  342. struct zfcp_fsf_req *new_fsf_req = NULL;
  343. struct zfcp_fsf_req *old_fsf_req;
  344. unsigned long flags;
  345. scsi_host = scpnt->device->host;
  346. adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
  347. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  348. ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
  349. scpnt, zfcp_get_busid_by_adapter(adapter));
  350. /* avoid race condition between late normal completion and abort */
  351. write_lock_irqsave(&adapter->abort_lock, flags);
  352. /*
  353. * Check whether command has just completed and can not be aborted.
  354. * Even if the command has just been completed late, we can access
  355. * scpnt since the SCSI stack does not release it at least until
  356. * this routine returns. (scpnt is parameter passed to this routine
  357. * and must not disappear during abort even on late completion.)
  358. */
  359. old_fsf_req = (struct zfcp_fsf_req *) scpnt->host_scribble;
  360. if (!old_fsf_req) {
  361. write_unlock_irqrestore(&adapter->abort_lock, flags);
  362. zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, NULL);
  363. retval = SUCCESS;
  364. goto out;
  365. }
  366. old_fsf_req->data = 0;
  367. old_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
  368. /* don't access old_fsf_req after releasing the abort_lock */
  369. write_unlock_irqrestore(&adapter->abort_lock, flags);
  370. /* call FSF routine which does the abort */
  371. new_fsf_req = zfcp_fsf_abort_fcp_command((unsigned long) old_fsf_req,
  372. adapter, unit, 0);
  373. if (!new_fsf_req) {
  374. ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
  375. zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
  376. old_fsf_req);
  377. retval = FAILED;
  378. goto out;
  379. }
  380. /* wait for completion of abort */
  381. __wait_event(new_fsf_req->completion_wq,
  382. new_fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  383. /* status should be valid since signals were not permitted */
  384. if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
  385. zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, new_fsf_req,
  386. NULL);
  387. retval = SUCCESS;
  388. } else if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
  389. zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, new_fsf_req,
  390. NULL);
  391. retval = SUCCESS;
  392. } else {
  393. zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, new_fsf_req,
  394. NULL);
  395. retval = FAILED;
  396. }
  397. zfcp_fsf_req_free(new_fsf_req);
  398. out:
  399. return retval;
  400. }
  401. int
  402. zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
  403. {
  404. int retval;
  405. struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
  406. if (!unit) {
  407. ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
  408. retval = SUCCESS;
  409. goto out;
  410. }
  411. ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
  412. /*
  413. * If we do not know whether the unit supports 'logical unit reset'
  414. * then try 'logical unit reset' and proceed with 'target reset'
  415. * if 'logical unit reset' fails.
  416. * If the unit is known not to support 'logical unit reset' then
  417. * skip 'logical unit reset' and try 'target reset' immediately.
  418. */
  419. if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
  420. &unit->status)) {
  421. retval = zfcp_task_management_function(unit,
  422. FCP_LOGICAL_UNIT_RESET,
  423. scpnt);
  424. if (retval) {
  425. ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
  426. if (retval == -ENOTSUPP)
  427. atomic_set_mask
  428. (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
  429. &unit->status);
  430. /* fall through and try 'target reset' next */
  431. } else {
  432. ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
  433. unit);
  434. /* avoid 'target reset' */
  435. retval = SUCCESS;
  436. goto out;
  437. }
  438. }
  439. retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
  440. if (retval) {
  441. ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
  442. retval = FAILED;
  443. } else {
  444. ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
  445. retval = SUCCESS;
  446. }
  447. out:
  448. return retval;
  449. }
  450. static int
  451. zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
  452. struct scsi_cmnd *scpnt)
  453. {
  454. struct zfcp_adapter *adapter = unit->port->adapter;
  455. struct zfcp_fsf_req *fsf_req;
  456. int retval = 0;
  457. /* issue task management function */
  458. fsf_req = zfcp_fsf_send_fcp_command_task_management
  459. (adapter, unit, tm_flags, 0);
  460. if (!fsf_req) {
  461. ZFCP_LOG_INFO("error: creation of task management request "
  462. "failed for unit 0x%016Lx on port 0x%016Lx on "
  463. "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
  464. zfcp_get_busid_by_adapter(adapter));
  465. zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
  466. retval = -ENOMEM;
  467. goto out;
  468. }
  469. __wait_event(fsf_req->completion_wq,
  470. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  471. /*
  472. * check completion status of task management function
  473. */
  474. if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
  475. zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
  476. retval = -EIO;
  477. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
  478. zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
  479. retval = -ENOTSUPP;
  480. } else
  481. zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
  482. zfcp_fsf_req_free(fsf_req);
  483. out:
  484. return retval;
  485. }
  486. /**
  487. * zfcp_scsi_eh_bus_reset_handler - reset bus (reopen adapter)
  488. */
  489. int
  490. zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *scpnt)
  491. {
  492. struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
  493. struct zfcp_adapter *adapter = unit->port->adapter;
  494. ZFCP_LOG_NORMAL("bus reset because of problems with "
  495. "unit 0x%016Lx\n", unit->fcp_lun);
  496. zfcp_erp_adapter_reopen(adapter, 0);
  497. zfcp_erp_wait(adapter);
  498. return SUCCESS;
  499. }
  500. /**
  501. * zfcp_scsi_eh_host_reset_handler - reset host (reopen adapter)
  502. */
  503. int
  504. zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
  505. {
  506. struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
  507. struct zfcp_adapter *adapter = unit->port->adapter;
  508. ZFCP_LOG_NORMAL("host reset because of problems with "
  509. "unit 0x%016Lx\n", unit->fcp_lun);
  510. zfcp_erp_adapter_reopen(adapter, 0);
  511. zfcp_erp_wait(adapter);
  512. return SUCCESS;
  513. }
  514. int
  515. zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
  516. {
  517. int retval = 0;
  518. static unsigned int unique_id = 0;
  519. /* register adapter as SCSI host with mid layer of SCSI stack */
  520. adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
  521. sizeof (struct zfcp_adapter *));
  522. if (!adapter->scsi_host) {
  523. ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
  524. "for adapter %s ",
  525. zfcp_get_busid_by_adapter(adapter));
  526. retval = -EIO;
  527. goto out;
  528. }
  529. ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
  530. /* tell the SCSI stack some characteristics of this adapter */
  531. adapter->scsi_host->max_id = 1;
  532. adapter->scsi_host->max_lun = 1;
  533. adapter->scsi_host->max_channel = 0;
  534. adapter->scsi_host->unique_id = unique_id++; /* FIXME */
  535. adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
  536. adapter->scsi_host->transportt = zfcp_transport_template;
  537. /*
  538. * save a pointer to our own adapter data structure within
  539. * hostdata field of SCSI host data structure
  540. */
  541. adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
  542. if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
  543. scsi_host_put(adapter->scsi_host);
  544. retval = -EIO;
  545. goto out;
  546. }
  547. atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  548. out:
  549. return retval;
  550. }
  551. void
  552. zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
  553. {
  554. struct Scsi_Host *shost;
  555. struct zfcp_port *port;
  556. shost = adapter->scsi_host;
  557. if (!shost)
  558. return;
  559. read_lock_irq(&zfcp_data.config_lock);
  560. list_for_each_entry(port, &adapter->port_list_head, list)
  561. if (port->rport)
  562. port->rport = NULL;
  563. read_unlock_irq(&zfcp_data.config_lock);
  564. fc_remove_host(shost);
  565. scsi_remove_host(shost);
  566. scsi_host_put(shost);
  567. adapter->scsi_host = NULL;
  568. atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  569. return;
  570. }
  571. void
  572. zfcp_fsf_start_scsi_er_timer(struct zfcp_adapter *adapter)
  573. {
  574. adapter->scsi_er_timer.function = zfcp_fsf_scsi_er_timeout_handler;
  575. adapter->scsi_er_timer.data = (unsigned long) adapter;
  576. adapter->scsi_er_timer.expires = jiffies + ZFCP_SCSI_ER_TIMEOUT;
  577. add_timer(&adapter->scsi_er_timer);
  578. }
  579. /*
  580. * Support functions for FC transport class
  581. */
  582. static struct fc_host_statistics*
  583. zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
  584. {
  585. struct fc_host_statistics *fc_stats;
  586. if (!adapter->fc_stats) {
  587. fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
  588. if (!fc_stats)
  589. return NULL;
  590. adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
  591. }
  592. memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
  593. return adapter->fc_stats;
  594. }
  595. static void
  596. zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
  597. struct fsf_qtcb_bottom_port *data,
  598. struct fsf_qtcb_bottom_port *old)
  599. {
  600. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
  601. old->seconds_since_last_reset;
  602. fc_stats->tx_frames = data->tx_frames - old->tx_frames;
  603. fc_stats->tx_words = data->tx_words - old->tx_words;
  604. fc_stats->rx_frames = data->rx_frames - old->rx_frames;
  605. fc_stats->rx_words = data->rx_words - old->rx_words;
  606. fc_stats->lip_count = data->lip - old->lip;
  607. fc_stats->nos_count = data->nos - old->nos;
  608. fc_stats->error_frames = data->error_frames - old->error_frames;
  609. fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
  610. fc_stats->link_failure_count = data->link_failure - old->link_failure;
  611. fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
  612. fc_stats->loss_of_signal_count = data->loss_of_signal -
  613. old->loss_of_signal;
  614. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
  615. old->psp_error_counts;
  616. fc_stats->invalid_tx_word_count = data->invalid_tx_words -
  617. old->invalid_tx_words;
  618. fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
  619. fc_stats->fcp_input_requests = data->input_requests -
  620. old->input_requests;
  621. fc_stats->fcp_output_requests = data->output_requests -
  622. old->output_requests;
  623. fc_stats->fcp_control_requests = data->control_requests -
  624. old->control_requests;
  625. fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
  626. fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
  627. }
  628. static void
  629. zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
  630. struct fsf_qtcb_bottom_port *data)
  631. {
  632. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
  633. fc_stats->tx_frames = data->tx_frames;
  634. fc_stats->tx_words = data->tx_words;
  635. fc_stats->rx_frames = data->rx_frames;
  636. fc_stats->rx_words = data->rx_words;
  637. fc_stats->lip_count = data->lip;
  638. fc_stats->nos_count = data->nos;
  639. fc_stats->error_frames = data->error_frames;
  640. fc_stats->dumped_frames = data->dumped_frames;
  641. fc_stats->link_failure_count = data->link_failure;
  642. fc_stats->loss_of_sync_count = data->loss_of_sync;
  643. fc_stats->loss_of_signal_count = data->loss_of_signal;
  644. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
  645. fc_stats->invalid_tx_word_count = data->invalid_tx_words;
  646. fc_stats->invalid_crc_count = data->invalid_crcs;
  647. fc_stats->fcp_input_requests = data->input_requests;
  648. fc_stats->fcp_output_requests = data->output_requests;
  649. fc_stats->fcp_control_requests = data->control_requests;
  650. fc_stats->fcp_input_megabytes = data->input_mb;
  651. fc_stats->fcp_output_megabytes = data->output_mb;
  652. }
  653. /**
  654. * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
  655. *
  656. * assumption: scsi_transport_fc synchronizes calls of
  657. * get_fc_host_stats and reset_fc_host_stats
  658. * (XXX to be checked otherwise introduce locking)
  659. */
  660. static struct fc_host_statistics *
  661. zfcp_get_fc_host_stats(struct Scsi_Host *shost)
  662. {
  663. struct zfcp_adapter *adapter;
  664. struct fc_host_statistics *fc_stats;
  665. struct fsf_qtcb_bottom_port *data;
  666. int ret;
  667. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  668. fc_stats = zfcp_init_fc_host_stats(adapter);
  669. if (!fc_stats)
  670. return NULL;
  671. data = kzalloc(sizeof(*data), GFP_KERNEL);
  672. if (!data)
  673. return NULL;
  674. ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
  675. if (ret) {
  676. kfree(data);
  677. return NULL; /* XXX return zeroed fc_stats? */
  678. }
  679. if (adapter->stats_reset &&
  680. ((jiffies/HZ - adapter->stats_reset) <
  681. data->seconds_since_last_reset)) {
  682. zfcp_adjust_fc_host_stats(fc_stats, data,
  683. adapter->stats_reset_data);
  684. } else
  685. zfcp_set_fc_host_stats(fc_stats, data);
  686. kfree(data);
  687. return fc_stats;
  688. }
  689. static void
  690. zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
  691. {
  692. struct zfcp_adapter *adapter;
  693. struct fsf_qtcb_bottom_port *data, *old_data;
  694. int ret;
  695. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  696. data = kzalloc(sizeof(*data), GFP_KERNEL);
  697. if (!data)
  698. return;
  699. ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
  700. if (ret == 0) {
  701. adapter->stats_reset = jiffies/HZ;
  702. old_data = adapter->stats_reset_data;
  703. adapter->stats_reset_data = data; /* finally freed in
  704. adater_dequeue */
  705. kfree(old_data);
  706. }
  707. }
  708. static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  709. {
  710. rport->dev_loss_tmo = timeout;
  711. }
  712. struct fc_function_template zfcp_transport_functions = {
  713. .show_starget_port_id = 1,
  714. .show_starget_port_name = 1,
  715. .show_starget_node_name = 1,
  716. .show_rport_supported_classes = 1,
  717. .show_rport_maxframe_size = 1,
  718. .show_rport_dev_loss_tmo = 1,
  719. .show_host_node_name = 1,
  720. .show_host_port_name = 1,
  721. .show_host_permanent_port_name = 1,
  722. .show_host_supported_classes = 1,
  723. .show_host_supported_speeds = 1,
  724. .show_host_maxframe_size = 1,
  725. .show_host_serial_number = 1,
  726. .get_fc_host_stats = zfcp_get_fc_host_stats,
  727. .reset_fc_host_stats = zfcp_reset_fc_host_stats,
  728. .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
  729. /* no functions registered for following dynamic attributes but
  730. directly set by LLDD */
  731. .show_host_port_type = 1,
  732. .show_host_speed = 1,
  733. .show_host_port_id = 1,
  734. };
  735. /**
  736. * ZFCP_DEFINE_SCSI_ATTR
  737. * @_name: name of show attribute
  738. * @_format: format string
  739. * @_value: value to print
  740. *
  741. * Generates attribute for a unit.
  742. */
  743. #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
  744. static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
  745. char *buf) \
  746. { \
  747. struct scsi_device *sdev; \
  748. struct zfcp_unit *unit; \
  749. \
  750. sdev = to_scsi_device(dev); \
  751. unit = sdev->hostdata; \
  752. return sprintf(buf, _format, _value); \
  753. } \
  754. \
  755. static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
  756. ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
  757. ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
  758. ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
  759. static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
  760. &dev_attr_fcp_lun,
  761. &dev_attr_wwpn,
  762. &dev_attr_hba_id,
  763. NULL
  764. };
  765. #undef ZFCP_LOG_AREA