zfcp_scsi.c 25 KB

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